武汉理工大学《C语言程序设计》实验报告答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

注:在Visual C++ 6.0编译环境中亲自调试通过,但不保证在Turbo C 中通过。

实验二 选择结构的程序设计

(题目当初没抄下来,这是根据程序整理的)

1.

编程实现分段函数⎪⎩⎪⎨⎧<≤-+-<≤-+<≤--=2112381011030184242x x x x x x x x x y

#include main() { float x,y; printf("Please input x:"); scanf("%f",&x); if (x<-1||x>=2) printf("Data Error\n"); else { if (x>=-1&&x<0) y=4*x-8; if (x>=0&&x<1) y=3*x*x+10*x-1; if (x>=1&&x<2) y=8*x*x*x*x-3*x*x+2*x-1; printf("\ny=%f\n",y); } } 2. 苹果有三个等级,一级5.50元/斤,二级3.00元/斤,三级2.50元/斤。

编写程序,输入苹果的等级,购买的数量和所付的钱数,

输出等级,购买的数量、应付钱数和找零。

#include main() { int level; double weight,pay,money,charge; printf("Please input the level:"); scanf("%d",&level); printf("Please input the weight you want:"); scanf("%lf",&weight); printf("Please input the money you pay:"); scanf("%lf",&pay); if (level<1||level>4||weight<0) printf("Data Error\n"); else{ if (level==1) money=weight*5.50; else if (level==2) money=weight*4.20; else if (level==3) money=weight*3.00; else if (level==4) money=weight*2.50; charge=pay-money; printf("\nLevel:%d\nweight:%g\n",level,weight); printf("money:%g\ncharge:%g\n\n",money,charge); } }

实验三循环结构

1.编程计算1!+2!+3!+4!+5!

●分别用单层循环和双层循环实现

●考虑

∑∑

=

=

20

1

10

1i

i

i

i!

#include

main()

{

int i,x;

double j,k,sum;

printf("Please input a number:");

scanf("%d",&x);

//用单层循环

for(sum=0,j=1,i=1;i<=x;i++)

{

j*=i;

sum+=j;

}

printf("The result is %lf\n",sum);

//用双层循环

for(sum=0,i=1;i<=x;i++)

{

for(k=1,j=1;j<=i;j++) k*=j;

sum+=k;

}

printf("The result is %lf\n",sum);

}

2.共20个评委给选手评分,去掉1个最高分,去掉1个最低分,其余18名评委的平均分即选手最终得分

#include

main()

{

double score,min,max,avg=0;

int i;

//下面是为了输入第一个数

printf("Please input 20 scores...\n");

printf("Score 1: ");

scanf("%lf",&score);

min=score;max=score;

avg+=score;

// 下面为了输入剩余19个数

for(i=2;i<=20;i++)

{

do

{

printf("Score %d: ",i);

scanf("%lf",&score);

}

while(score<0);

if(score>max) max=score;

avg+=score;

}

avg=avg-min-max;

avg/=18;

printf("\nThe average score is %lf:\n",avg);

}

实验四数组和指针的应用

1.定义一个有10元素的一位数组count,从键盘上输入8个整数,将其按从大到小的顺序排列,并将排序后的

数组输出

(1)数组方法

#include

main()

{

int count[10],t,i,j;

printf("Please input 8 numbers:\n");

for(i=0;i<8;i++)

scanf("%d",&count[i]);

for(i=0;i<8;i++)

for(j=i+1;j<8;j++)

if (count[i]

{

t=count[i];

count[i]=count[j];

count[j]=t;

}

printf("The sorted array is as follows:\n");

for (i=0;i<8;i++)

printf("%d ",count[i]);

}

(2)指针方法

#include

main()

{

int count[10],t,i,j,*p=count;

printf("Please input 8 numbers:\n");

for(i=0;i<8;i++)

scanf("%d",&p[i]);

for(i=0;i<8;i++)

for(j=i+1;j<8;j++)

if (*(p+i)<*(p+j))

{

t=*(p+i);

*(p+i)=*(p+j);

*(p+j)=t;

}

printf("The sorted array is as follows:\n");

for (i=0;i<8;i++)

printf("%d ",count[i]);

}

2.输入2个字符串,将二者连接并输出,再将连接后的字符串反向排列并输出,并求连接后字符串的长度

#include

#include

main()

{

int i,len;

char a1[51],a2[26],t[51];

printf("Please input 2 strings:\n");

gets(a1);

gets(a2);

strcat(a1,a2);

printf("The joint string is: %s",a1);

len=strlen(a1);

printf("\nThe length of the linked string is: %d\n",len);

for(i=0;i

t[i]=a1[len-i-1];

t[len]=0;

strcpy(a1,t);

printf("The reversed string is: %s\n",a1);

}

相关文档
最新文档