c - C 编程语言问题中的函数:获取区域的程序
问题描述
我是一名学生,尝试使用 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()
作为函数定义的一部分放在前面。其余的好像还不错
int main(void)
{
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;
}
推荐阅读
- jquery - 如果父列表项未隐藏,jquery检查项目
- python - 对 REVIT RPW flexform 按钮使用 on_click 选项
- reactjs - 如何使用 Reactjs 正确更新用户数
- ios - 增加 UINavigationBarLargeTitleView 的高度
- assembly - 这个“mov”命令是否从我的教科书中正确实施?
- java - Java/C - 将本地语言代码编译成共享库 DLL
- php - 如何从感谢页面中删除帐单地址?
- python - 强制 python 子进程以特定顺序运行
- react-native - 如何获取当前关注的 FlatList 项
- python - 奇怪的输出正则表达式 r'[-.\:alnum:](.*)'