2024年4月9日发(作者:)
实验一
(1)三角形输入三边求面积:顺序结构编写
输入三角形的三边x,y,z(能构成三角形),求三角形的周长c和面积s,得到的结果保留
2位小数。
#include
#include
main()
{ float x,y,z,c,s,t;
scanf("%f,%f,%f",&x,&y,&z);
c=x+y+z;
t=c/2;
s=sqrt(t*(t-x)*(t-y)*(t-z));
printf("c=%.2f s=%.2fn",c,s);
}
(2)利用循环结构编写程序,打印图案,输出的三角形如下(循环语句)
(3)功能是:输入一行字符,统计其中的字母和非字母的个数。例如,输入B5a3d,# 则输
出4,5。(循环,分支结构)
#include
main()
{ char ch;
int a,b;
a=b=0;
ch=getchar();
while(ch!='n')
{ if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))a++;
else
b++;
ch=getchar();
}
printf("%d,%dn",a,b);
实验二
1、 输入三角形的三边,求三角形的面积。
A.以前:
#include
#include
main()
{
float a,b,c,s;
double area;
printf("Please input three sides' length:n");
scanf("%f,%f,%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("The area of triangle is %5.2f",area);
}
B. 函数的方法来实现
#include
#include
double area(float x,float y,float z)
{
double s,p;
p=(x+y+z)/2;
s=sqrt(p*(p-x)*(p-y)*(p-z));
return s;
}
main()
{ float a,b,c;
printf("Please input three sides' length:n");
scanf("%f,%f,%f",&a,&b,&c);
}
printf("The area of triangle is %5.2f",area(a,b,c));
发布者:admin,转转请注明出处:http://www.yc00.com/news/1712627257a2092449.html
评论列表(0条)