顺序结构程序设计举例(初学者)

例:输入三角形的三边长,求三角形面积。

已知三角形的三边长a,b,c则该三角形的面积公式为:area=√s(s-a)(s-b)(s-c)其中s=a+b+c/2

程序

#include <math.h>

void main()
{
    double a,b,c,s,area;
    scanf("%lf,%lf,%lf",&a,&b,&c);
    s=1.0/2*(a+b+c);
    area=sqrt(s*(s-a)*(s-b)*(s-c));
    printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n",a,b,c,s);
    printf("area=%7.2f\n",area);
}

报错:1>c:\users\hp\desktop\test\test\test.cpp(6): error C3861: “scanf”: 找不到标识符
1>c:\users\hp\desktop\test\test\test.cpp(9): error C3861: “printf”: 找不到标识符
1>c:\users\hp\desktop\test\test\test.cpp(10): error C3861: “printf”: 找不到标识符

解决方法:添加#include<stdio.h>

注:输入时记得加上“,”

例:求ax2+bx+c=0的方程的根,a,b,c由键盘输入,设b2-4ac>0,求根公式为:设x=-b=√b2-4ac/2a,p=-b/2a,令q=√b2-4ac/2a,则x1=p+q,x2=p-q

程序:

#include <math.h>
#include<stdio.h>

void main()
{
    double a,b,c,disc,x1,x2,p,q;
    scanf("a=%lf,b=%lf,c=%lf",&a,&b,&c);
    disc=b*b-4*a*c;
    p=-b/(2*a);
    q=(sqrt(disc))/(2*a);
    x1=p+q;
    x2=p-q;
    printf("\nx1=%5.2lf\nx2=%5.2lf\n",x1,x2);
}

 注:输入时要加上a=    ,b=      ,c=    

猜你喜欢

转载自www.cnblogs.com/lvfengkun/p/10193294.html