大一C语言程序设计题
顺序接构程序设计第一部分
1.*******************
I am a student
*******************
#include
int main(void)
{
printf("**********************\nI am a student\n*********************\n");
}
/***********************
I am a student
**********************/
2.
【实验内容】求分段函数。【实验目的】运用if语句实现分支结构程序设计。【实验题目】编写程序,输入x,计算并输出下列分段函数f(x)的值(保留两位小数),请调用sqrt()函数求平方根,调用pow()函数求幂,该函数请参看素材。作品素材列表:素材1#include
#include
int main(void)
{
double x,y;
printf("Enterx:\n");
scanf("%lf",&x);
if(x<0){
y=pow(x,5)+2*x+1/x;
}
else{
y=sqrt(x);
}
printf("y=f(%f)=%.2f\n",x,y);
return 0;
}
Enterx:
10
y=f(10.000000)=3.16
Press any key to continue
3. 【实验内容】运用for循环处理简单数值计算问题。【实验目的】模仿第2章例题,掌握for循环的使用。【实验题目】编写程序,输入一个正整数n,计算数列的前n项和,结果保留5位小数。?1????2????3????4????5????6???????n?????????????????—?-?—?+?—?-?—?+?—?-?—?+?…?—??????????1????3????5????7????9????11?????2*n-1??????????????若n=50,参考答案为:?0.39020
#include
#include
int main(void)
{
int denominator,flag,i,n;
double item,sum;
printf("Enter n:");
scanf("%d",&n);
flag=1;
denominator=1;
sum=0;
for(i=1;i<=n;i++){
item=flag*pow(-1,flag+1)/denominator;
sum=sum+item;
flag=flag+1;
denominator=denominator+2;
}
printf("sum=%.5f\n",sum);
return 0;
}
/*Enter n:50
sum=0.39020
Press any key to continue*/
实验四 分支结构程序设计
1 30 输入任意的三角形的三条边求其面积,如果输入的三条边不能构成三角形,给出提示信息并退出,结果保留两位小数。提示:借助于海伦公式
#include
#include
main()
{
double a,b,c,half,s;
printf("输入三角形的3条边,各参数用,隔开:\n");
scanf("%lf,%lf,%lf",&a,&b,&c);
if(a+b<=c||a+c<=b||b+c<=a||a<0||b<0||c<0)
printf("不能构成三角形,请重新运行程序!\n");
else
{
half=(a+b+c)/2;
s=sqrt(half*(half-a)*(half-b)*(half-c));
printf("三角形面积为:%.2lf\n",s);
}
}
2 30 循环输入12个字符,将其中的大写字母转换为小写字母输出,小写字母转换为大写字母输出,其他字符不变,输出结果如下所示。原始字符序列:AbC123fG8JeB输出字符序列:aBc123Fg8jEb
#include
#include
main()
{
int i;
char ch;
printf("输入12个字符,可以多于12,不可少于12:\n");
for(i=1;i<=12;i++)
{
ch=getchar();
if(ch>='a'&&ch<='z')
ch=ch-32;
else
if(ch