【问题标题】:Functions in C Programming Language Problem: Program to get the Area [closed]C编程语言问题中的函数:获取区域的程序[关闭]
【发布时间】: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 位)近似值。您应该用换行符结束输出(提示除外),而不是用换行符开始输出。

标签: c area


【解决方案1】:

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

【讨论】:

  • 虽然这是正确的,但这不是问题的答案。
【解决方案2】:

你只需要在 main 函数中添加一个类型。例如:int main(void) { … }

【讨论】:

  • 虽然这是正确的,但这不是问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
相关资源
最近更新 更多