填空题(C语言) 联系客服

发布时间 : 星期三 文章填空题(C语言)更新完毕开始阅读

填空题(C) 1

填空题

请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的BLANKl.c中。

不得增行或删行,也不得更改程序的结构!

do {

printf(\p=p->next; } while(p!=NULL);

printf(\ } }

1.给定程序中己建立一个带有头结点的单向链表,链表中的各结点按结点数据域中的数据从小到大顺序链接。函数fun的功能是:把形参x的值放入一个新结点并插入到链表中,插入后各结点仍保持从小到大顺序排列。

#include

#include #define N 8 typedef struct list { int data;

struct list *next; } SLIST;

void fun( SLIST *h, int x) { SLIST *p, *q, *s;

s=(SLIST *)malloc(sizeof(SLIST)); /**********found**********/ s->data=___1___; q=h;

p=h->next;

while(p!=NULL && x>p->data) { /**********found**********/ q=___2___; p=p->next; }

s->next=p;

/**********found**********/ q->next=___3___; }

SLIST *creatlist(int *a) { SLIST *h,*p,*q; int i;

h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i

{ q=(SLIST *)malloc(sizeof(SLIST)); q->data=a[i]; p->next=q; p=q; }

p->next=0; return h;

}

void outlist(SLIST *h) { SLIST *p; p=h->next;

if (p==NULL)

printf(\

else

{ printf(\

main()

{ SLIST *head; int x;

int a[N]={11,12,15,18,19,22,25,29}; head=creatlist(a);

printf(\ outlist(head);

printf(\ scanf(\ fun(head,x);

printf(\ outlist(head); }

答案:【1】x 【2】p 【3】s

2.给定程序的功能是;调用函数fun将指定源文件中的内容复制到指定的目标文件中,复制成功时函数返回值是1,失败时返回值为O。在复制的过程中,把复制的内容输出到终端屏幕。主函数中源文件名放在变量sfname中,目标文件名放在变量tfname中。

#include

#include

int fun(char *source, char *target) { FILE *fs,*ft; char ch;

/**********found**********/

if((fs=fopen(source, ___1___))==NULL) return 0;

if((ft=fopen(target, \ return 0;

printf(\ ch=fgetc(fs);

/**********found**********/ while(!feof(___2___)) { putchar( ch );

/**********found**********/ fputc(ch,___3___); ch=fgetc(fs);

}

fclose(fs); fclose(ft); printf(\ return 1; } main()

填空题(C) 2

{ char sfname[20] =\ FILE *myf; int i; char c; myf=fopen(sfname,\

printf(\ for(i=1; i<30; i++){

c='A'+rand()%;fprintf(myf,\

printf(\

fclose(myf);printf(\

if (fun(sfname, tfname)) printf(\ else printf(\}

答案:【1】“r” 【2】fs 【3】ft

3.给定程序中,函数fun的功能是:将自然数1~10以及它们的平方根写到名为myfile3.txt的文本文件中,然后再顺序读出显示在屏幕上。 #include #include

int fun(char *fname )

{ FILE *fp; int i,n; float x;

if((fp=fopen(fname, \ for(i=1;i<=10;i++)

/**********found**********/

fprintf(___1___,\ printf(\/**********found**********/

___2___;

printf(\/**********found**********/

if((fp=fopen(___3___,\ return 0;

fscanf(fp,\

while(!feof(fp))

{ printf(\ fclose(fp); return 1; }

main()

{ char fname[]=\ fun(fname); }

答案:【1】fp 【2】fclose(fp) 【3】fname

4. 给定程序的功能是:从键盘输入若干行文件(每行不超过80个字符),写到文件myfile4.txt中,用-1作为字符串输入结束的标志。然后将文件的内容读出显示在屏幕上。文件的读写分别由自定义函数ReadText和WriteText实现。

#include #include

#include void WriteText(FILE *); void ReadText(FILE *); main()

{ FILE *fp;

if((fp=fopen(\ { printf(\ WriteText(fp); fclose(fp);

if((fp=fopen(\ { printf(\ ReadText(fp);

fclose(fp); }

/**********found**********/ void WriteText(FILE ___1___) { char str[81];

printf(\ gets(str);

while(strcmp(str,\

/**********found**********/

fputs(___2___,fw); fputs(\ gets(str); } }

void ReadText(FILE *fr) { char str[81];

printf(\file and output to screen :\\n\ fgets(str,81,fr); while( !feof(fr) ) {

/**********found**********/ printf(\ fgets(str,81,fr); }

}

答案:【1】*fw 【2】str 【3】str

5. 给定程序的功能是调用fun函数建立班级通讯录。通讯录中记录每位学生的编号、姓名和电话号码。班级的人数和学生的信息从键盘读入,每个人的信息作为一个数据块写到名为myfile5.dat的二进制文件中。 #include #include #define N 5 typedef struct { int num; char name[10]; char tel[10]; }STYPE;

填空题(C) 3

void check();

/**********found**********/ int fun(___1___ *std) {

/**********found**********/ ___2___ *fp; int i;

if((fp=fopen(\ return(0);

printf(\ for(i=0; i

/**********found**********/

fwrite(&std[i], sizeof(STYPE), 1, ___3___); fclose(fp); return (1); } main()

{ STYPE s[10]={ {1,\

{3,\{5,\

int k; k=fun(s);

if (k==1)

{ printf(\ else

printf(\}

void check()

{ FILE *fp; int i; STYPE s[10];

if((fp=fopen(\ { printf(\

printf(\ printf(\ for(i=0; i

{ fread(&s[i],sizeof(STYPE),1, fp);

printf(\ }

fclose(fp); }

答案:【1】STYPE 【2】FILE 【3】fp

6.给定程序功能是用冒泡法对6个字符串进行排序。 #include #define MAXLINE 20 fun ( char *pstr[6]) { int i, j ;

char *p ;

for (i = 0 ; i < 5 ; i++ ) { for (j = i + 1; j < 6; j++) {

/**************found**************/ if(strcmp(*(pstr+i),___1___)>0) {

p = *(pstr + i) ; /**************found**************/ pstr[i] = ___2___ ; /**************found**************/ *(pstr + j) = ___3___ ; } } } }

main( )

{ int i ;

char *pstr[6], str[6][MAXLINE] ;

for(i = 0; i < 6 ; i++) pstr[i] = str[i] ;

printf( \6 string(1 string at each line): \\n\) ; for(i = 0 ; i < 6 ; i++) scanf(\ fun(pstr) ;

printf(\ for(i = 0 ; i < 6 ; i++) printf(\pstr[i]) ; }

答案:【1】*(pstr+j) 【2】pstr[j] 【3】p

7.给定程序的功能是将十进制正整数m转换成k进制(2≤k≤9)数的数字输出。例如,若输入8和2,则应输出1000(即十进制数8转换成二进制表示是1000)。 #include void fun( int m, int k ) { int aa[20], i;

for( i = 0; m; i++ ) { /**********found**********/ aa[i] = ___1___;

/**********found**********/ m /= ___2___; }

for( ; i; i-- )

/**********found**********/

printf( \}

main()

{ int b, n;

printf( \enter a number and a base:\\n\); scanf( \ fun( n, b ); }

答案:【1】m%k 【2】k 【3】aa

8.给定程序的功能是将未在字符串s中出现,而在字符串t中出现的字符,形成一个新的字符串放在u中,u中字符

填空题(C) 4

按原字符串中字符顺序排序,但去掉重复字符。 例如:当s=“12345”,t=“24677”时,u中的字符为:“67”。 #include #include

void fun (char *s, char *t, char *u) { int i, j, sl, tl, k, ul=0; sl = strlen(s); tl = strlen(t); for (i=0; i

{ for (j=0; j

if (t[i] == s[j]) break; if (j>=sl)

{ for (k=0; k

/************found************/

if (t[i] == u[k]) ___1___ ; if (k>=ul)

/************found************/ u[ul++] = ___2___ ; } }

/************found************/ ___3___='\\0'; }

main()

{ char s[100], t[100], u[100];

printf(\ printf(\ fun(s, t, u);

printf(\}

答案:【1】break 【2】t[i] 【3】u[ul] 9.给定程序的功能是将在字符串s中出现、而未在字符串t中出现的字符形成一个新的字符串放在u中,u中字符按原字符串中字符顺序排列,不去掉重复字符。 例如:当s=“112345”,t=“2467”时,u中的字符串为“1135”。 #include #include

void fun (char *s,char *t, char *u) { int i, j, sl, tl;

sl = strlen(s); tl = strlen(t); for (i=0; i

{ for (j=0; j

/************found************/ if (s[i] == t[j]) ___1___ ; if (j>=tl)

/************found************/ *u++ = ___2___; }

/************found************/

___3___ = '\\0'; }

main()

{ char s[100], t[100], u[100];

printf(\

printf(\

fun(s, t, u);

printf(\}

答案:【1】break 【2】s[i] 【3】*u

10.给定程序的功能是将在字符串s中下标为奇数位置上的字符,紧随其后重复出现一次,放在一个新串t中,t中字符按原字符串中字符出现的逆序排列。 (注意0为偶数)

#include #include

void fun (char *s, char *t) { int i, j, sl;

sl = strlen(s);

/************found************/ if(sl%2) sl-=2; ___1___ sl--; /************found************/ for (i=sl, j=___2___; i>=0; i-=2) { t[2*j] = s[i];

/************found************/ t[2*j +1] = ___3___ ; j++; }

t[2*j]='\\0'; }

main()

{ char s[100], t[100];

printf(\ fun(s, t);

printf(\}

答案:【1】else 【2】0 【3】s[i]

11.给定程序的功能是将大写字母转换为对应小写字母之后的第五个字母;若小写字母为v~z,使小写字母的值减21。转换后的小写字母作为函数值返回。例如,若形参是字母A,则转换为小写字母f;若形参是字母W则转换为小写字母b。

#include #include char fun(char c)