用 C++ 输入计算圆的面积和周长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3827532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:50:25  来源:igfitidea点击:

Calculate Area and Circumference of a Circle with input in C++

c++

提问by James Hayek

I am writing a code for class and I thought I hat it all down pat. However, can not get any feedback from the input when I compile this on XCode.

我正在为班级编写代码,我想我已经把这一切都放下了。但是,当我在 XCode 上编译它时,无法从输入中获得任何反馈。

My code is as follows:

我的代码如下:

/*
 James George Hayek
 PCCC Assignment 2
 Prof Siegel

 This program will calculate the area and circumference of a circle.
 */

#include <iostream>
#include <cmath>
using namespace std;

int main () 
{

    float radius;
    float circumference;
    float area;

    cout << "Please enter the radius of a circle: ";
    cin >> radius;
    cout << "\n";

    circumference = 2 * 3.1416 * radius;
    area = 3.1416 * radius * radius;

    cout << "************************************" << "\n"
         << "*Area and Circumference of A Circle*" << "\n"
         << "************************************" << "\n"
         << "\tRadius= " << radius << "\n"
         << "\tArea= " << area << "\n"
         << "\tCircumference= " << circumference << "\n";

    cin.get();


    return 0;

} //end main

Okay, I just trouble shot this and it seems as though this works in the terminal but I can not get it to respond in the console in XCode. Should I not worry about this?

好的,我只是遇到了麻烦,似乎这在终端中有效,但我无法让它在 XCode 的控制台中响应。我不应该担心这个吗?

回答by Ruel

You got your tab back wards on your output: /tshould be \t. I don't see anything else wrong with the code. (tested)

你在你的输出上得到了你的标签:/t应该是\t。我没有看到代码有任何其他问题。(已测试)

cout << "************************************" << "\n"
      << "*Area and Circumference of A Circle*" << "\n"
      << "************************************" << "\n"
      << "\tRadius=" << radius << "\n"
      << "\tArea=" << area << "\n"
      << "\tCircumference=" << circumference << "\n";

回答by codaddict

Your program compiles and runs fine but its not pretty printing the result because you need \tto get a tab printed and you've used /t.

您的程序编译并运行良好,但打印结果并不漂亮,因为您需要\t打印一个选项卡并且您已经使用了/t.

\tis a single tab char where as /tis two separate char /and a t

\t是单个制表符,其中/t两个单独的字符/和一个t

Here is the list of escape sequencesavailable.

这是可用的转义序列列表。

回答by Gustavo Puma

It's '\t' not '/t'.

它是 '\t' 不是 '/t'。

it should be:

它应该是:

 << "\tRadius=" << radius << "\n"
 << "\tArea=" << area << "\n"
 << "\tCircumference=" << circumference << "\n";

回答by Chubsdad

Other posts have pointed out the reason/fix.

其他帖子指出了原因/修复。

Just noticed:

刚注意到:

Your code silently loses precision in the statements as there is a implicit double to float standard conversion that applies behind your back:

您的代码在语句中默默地失去了精度,因为有一个隐含的 double 到 float 标准转换适用于您的背后:

circumference = 2 * 3.1416 * radius;  (since 3.1416 is of type double, 
                                       the type of rhs is double).
area = 3.1416 * radius * radius;      (same here)

So, you lose precison. Don't do it unless it is intentional. Compile with the strictest warning level though that is not guaranteed to bring out all lurking issues.

所以,你失去了精度。除非是故意的,否则不要这样做。使用最严格的警告级别进行编译,但这并不能保证解决所有潜在问题。

$5/10 - "Otherwise, if either operand is double, the other shall be converted to double."

$5/10 - “否则,如果任一操作数为 double,则另一个应转换为 double。”

回答by Makiboy23

Area of Circle = (Radius * Radius) * PI

圆的面积 = (半径 * 半径) * PI

function area_of_circle($radius){

功能 area_of_circle($radius){

$area = pow($radius, 2) * pi();

$area = pow($radius, 2) * pi();

return $area; }

返回 $area; }

// given value $radius = 2;

// 给定值 $radius = 2;

$area = area_of_circle($radius);

$area = area_of_circle($radius);

echo "Radius of Circle : ".$radius." "; echo "Area of Circle : ".$area;

echo "圆的半径:".$radius." "; echo "圆的面积:".$area;

download here

在这里下载

回答by Makiboy23

#include<stdio.h>
#include<conio.h>
void main()
{

float radius,area;
clrscr();  // Clear Screen

printf("nEnter the radius of Circle : ");
scanf("%d",&radius);

area = 3.14 * radius * radius;

printf("nArea of Circle : %f",area);
getch();

}

i already translated the code from PHP to C++

我已经将代码从 PHP 翻译成 C++