一、程序填空题
在此程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从此文本文件中逐个读个并显示在终端屏幕上。
#include <stdio.h>
void fun(char *s, int a, double f)
{
/**********found**********/
__1__ fp;
char ch;
fp = fopen("file1.txt", "w");
fprintf(fp, "%s %d %f\n", s, a, f);
fclose(fp);
fp = fopen("file1.txt", "r");
printf("\nThe result :\n\n");
ch = fgetc(fp);
/**********found**********/
while (!feof(__2__))
{
/**********found**********/
putchar(__3__);
ch = fgetc(fp);
}
putchar('\n');
fclose(fp);
}
void main()
{ char a[10]="Hello!"; int b=12345;
double c= 98.76;
fun(a,b,c);
}
答案:(1) FILE* (2) fp (3) ch
二、程序修改题
在此程序中,函数fun的功能是:依次取出字符串中所有的数字字符,形成新的字符串,并取代原字符串。
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void fun(char *s)
{int i,j;
for(i=0,j=0; s[i]!= '\0'; i++)
if(s[i]>= '0'&&s[i]<= '9')
/*************found**************/
s[j]=s[i];
/*************found**************/
s[j]=”\0”;
}
void main()
{char item[80];
system("CLS");
printf("\nEnter a string: ");gets(item);
printf("\n\nThe string is:%s\n",item);
fun(item);
printf("\n\nThe string of changing is :%s\n",item);
}
答案:(1) s[j++]=s[i]; (2) s[j]='\0';
三、程序设计题
在此程序中,编写函数fun,该函数的功能是:将M行N列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。
例如,若二维数组中的数据为:
则字符串中的内容应是:WSHWSHWSHWSH。
#include<stdio.h>
#define M 3
#define N 4
void fun(char (*s)[N],char *b)
{
}
void main()
{
FILE *wf;
char a[100],w[M][N]={{ 'W', 'W', 'W', 'W'},{'S', 'S', 'S', 'S'},{'H', 'H', 'H', 'H'}};
int i,j;
printf("The matrix:\n");
for(i=0;i<M;i++)
{ for(j=0;j<N;j++)
printf("%3c",w[i][j]);
printf("\n");
}
fun(w,a);
printf("The A string:\n");
puts(a);
printf("\n\n");
/******************************/
wf=fopen("out.dat","w");
fprintf(wf,"%s",a);
fclose(wf);
/*****************************/
}
答案:
int i,j,k=0;
for(i=0;i<N;i++) /*按列的顺序依次放到一个字符串中*/
for(j=0;j<M;j++)
b[k++]=s[j][i];
b[k]='\0';