順序結構程序設計舉例(初學者)


例:輸入三角形的三邊長,求三角形面積。

已知三角形的三邊長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=    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM