首页 > 解决方案 > 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 平台。

我无法附上照片,因为我没有足够的声誉,但这就是编译器中出现的内容:

编译结果...

标签: carea

解决方案


您只需要将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;
}

推荐阅读