【发布时间】:2021-06-25 03:47:04
【问题描述】:
我是一名学生,尝试使用 C 语言编写一个简单的程序来获取三角形、正方形、矩形和圆形的面积。不幸的是,即使我仔细检查了语法并进行了调试,我的程序也不会运行。下面列出的是我的代码供参考:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float triangle(float b, float h)
{
float triangle;
triangle=0.5*b*h;
return triangle;
}
float square(float s)
{
float square;
square=s*s;
return square;
}
float rectangle(float l, float w)
{
float rectangle;
rectangle=l*w;
return rectangle;
}
float circle(float r)
{
float circle;
circle=3.14*r*r;
return circle;
}
main()
{
float base, height, side, length, width, radius;
printf("Triangle Area:\nEnter Base and Height:");
scanf("%f%f", &base, &height);
printf("Area of the Triangle= %f", triangle(base, height));
printf("\nSquare Area:\nEnter Side:");
scanf("%f", &side);
printf("Area of the Square= %f", square(side));
printf("\nRectangle Area:\nEnter Length and Width:");
scanf("%f%f", &length, &width);
printf("Area of the Rectangle= %f", rectangle(length, width));
printf("\nCircle Area:\nEnter Radius:");
scanf("%f", &radius);
printf("Area of the Circle= %f", circle(radius));
return 0;
}
编辑:很抱歉没有及时回复你们。我遵循了您的一项建议,将 int main(void){} 而不仅仅是 main(){}。
我相信我的编译器有问题,因为当我尝试运行程序时,它只会显示编译结果,并且我的语法没有任何问题。我决定不输入 355.0 / 113.0 来代替 pi 的 3.14。我正在按照要求使用 WindowsOS 平台。
我无法附上照片,因为我没有足够的声誉,但这就是编译器中出现的内容:
编译结果...
- 错误:0
- 警告:0
- 输出文件名:(个人信息)
- 输出大小:157.9794921875 KiB
- 编译时间:0.73s
我应该更改代码中的某些内容还是尝试其他内容?谢谢!
【问题讨论】:
-
您的代码看起来不错。请显示输入和预期与实际输出的示例。
-
“不会运行”到底是什么意思?在其他新闻中,您必须写
int main;如果你的书没有显示这种语法,那就太旧了。 -
你的平台是什么?你如何编译?是否有任何编译器错误?你如何运行程序?当您尝试运行该程序时,您是否收到任何错误消息?请edit 澄清。
-
对我来说运行良好。就像 Jabberwocky 说的那样,您必须向我们展示您尝试过的输入对我们没有帮助。仅供参考,如果您输入任何
scanf无法解析为浮点数的内容(例如“hello”),您将遇到问题。您应该检查scanf的返回值,看看它是否获得了有效输入,并在没有输入时进行处理。 -
使用
355.0 / 113.0作为 π 的更好(7 位)近似值。您应该用换行符结束输出(提示除外),而不是用换行符开始输出。