C语言上机试题:15
1.题目:编一C程序,能实现如下功能:从键盘输入一整数序列,直到不能从键盘输入整数为止。并按输入顺序输出到指定的文件中。(注:可执行程序命名为e15_1.exe)
程序设计:
#include "stdio.h"
main()
{FILE *fp;
char fname[40];
int x;
printf("Please enter a filename:\n");
scanf("%s%*c",fname);
if ((fp=fopen(fname,"w"))==NULL)
{printf("%s can not open!\n",fname);
exit(0);
}
while (scanf("%d",&x)==1)
fprintf(fp,"%d\t",x);
fclose(fp);
}
2.题目:编一C程序,能实现如下功能:输入n个实数,求出这n个实数中第二大的实数,并输出。(注:可执行程序命名为e15_2.exe)
程序设计:
main()
{float a[80],max1,max2;
int n,i;
printf("Please enter a int number n=");
scanf("%d",&n);
printf("\nPlease enter %d float numbers:\n",n);
for (i=0;iscanf("%f",&a[i]);
if (a[0]>a[1])
{max1=a[0];max2=a[1];}
else {max1=a[1];max2=a[0];}
for (i=2;i{if (a[i]>max1)
{max2=max1;max1=a[i];}
else if (a[i]>max2) max2=a[i];
}
printf("The two max is %f!\n",max2);
}