1. 程式人生 > >順序結構程式設計舉例(初學者)

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

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

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