11 、人员的记录由编号和出生年 、月 、日组成 ,N名人员的数据已在主函数中存入结构体数组std中 。函数fun的功能是:找出指定出生年份的人员 ,将其数据放在形参k所指的数组中 ,由主函数输出 ,同时由函数值返回满足指定条件的人数 。
请在程序的下划线处填入正确的内容并把下划线删除 ,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
12 、给定程序中 ,函数fun的功能是:将形参指针所指结构体数组中的三个元素按num成员进行升序排列 。
请在程序的下划线处填入正确的内容并把下划线删除 ,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdio.h>
typedef struct
{ int num;
char name[10];
}PERSON;
/**********found**********/
void fun(PERSON ___1___)
{
/**********found**********/
___2___ temp;
if(std[0].num>std[1].num)
{ temp=std[0]; std[0]=std[1]; std[1]=temp; }
if(std[0].num>std[2].num)
{ temp=std[0]; std[0]=std[2]; std[2]=temp; }
if(std[1].num>std[2].num)
{ temp=std[1]; std[1]=std[2]; std[2]=temp; }
}
int main()
{
PERSON std[ ]={ 5,"Zhanghu",2,"WangLi",6,"LinMin" };
int i;
/**********found**********/
fun(___3___);
printf("\nThe result is :\n");
for(i=0; i<3; i++)
printf("%d,%s\n",std[i].num,std[i].name);
return 0;
}
13 、下列给定程序中 ,函数fun的功能是:在带头结点的单向链表中 ,查找数据域中值为ch的结点 。找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为ch的结点 ,函数返回0值 。
请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(char *);
void outlist(SLIST *);
int fun( SLIST *h, char ch)
{ SLIST *p; int n=0;
p=h->next;
/**********found**********/
while(p!=___1___)
{ n++;
/**********found**********/
if (p->data==ch) return ___2___;
else p=p->next;
}
return 0;
}
int main()
{ SLIST *head; int k; char ch;
char a[N]={m,p,g,a,w,x,r,d};
head=creatlist(a);
outlist(head);
printf("Enter a letter:");
scanf("%c",&ch);
/**********found**********/
k=fun(___3___);
if (k==0) printf("\nNot found!\n");
else printf("The sequence number is : %d\n",k);
return 0;
}
SLIST *creatlist(char *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; 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("\nThe list is NULL!\n");
else
{ printf("\nHead");
do
{ printf("->%c",p->data); p=p->next; }
while(p!=NULL);
printf("->End\n");
}
}
14 、给定程序中 ,函数fun的功能是将形参给定的字符串 、整数 、浮点数写到文本文件中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上 。
请在程序的下划线处填入正确的内容并把下划线删除 ,使程序得出正确的结果 。
注意:请勿改动main函数和其他函数中的任何内容 ,仅在函数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");
ch = fgetc(fp);
/**********found**********/
while (!feof(__2__))
{
/**********found**********/
putchar(__3__);
ch = fgetc(fp);
}
putchar(\n);
fclose(fp);
}
int main()
{ char a[10]="Hello!"; int b=12345;
double c= 98.76;
fun(a,b,c);
return 0;
}
15 、下列给定程序的功能是:调用函数fun将指定源文件中的内容复制到指定的目标文件中 ,复制成功时函数返回1 ,失败时返回0 。在复制的过程中 ,把复制的内容输出到屏幕。主函数中源文件名放在变量sfname中 ,目标文件名放在变量tfname中 。
请在下划线处填入正确的内容并将下划线删除 ,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdio.h>
#include <stdlib.h>
int fun(char *source, char *target)
{ FILE *fs,*ft; char ch;
/**********found**********/
if((fs=fopen(source, ___1___))==NULL)
return 0;
if((ft=fopen(target, "w"))==NULL)
return 0;
printf("\nThe data in file :\n");
ch=fgetc(fs);
/**********found**********/
while(!feof(___2___))
{ putchar( ch );
/**********found**********/
fputc(ch,___3___);
ch=fgetc(fs);
}
fclose(fs); fclose(ft);
printf("\n");
return 1;
}
int main()
{ char sfname[20] ="myfile1",tfname[20]="myfile2";
FILE *myf; int i; char c;
myf=fopen(sfname,"w");
printf("\nThe original data :\n");
for(i=1; i<30; i++){ c=A+rand()%25;fprintf(myf,"%c",c); printf("%c",c); }
fclose(myf);printf("\n");
if (fun(sfname, tfname)) printf("Succeed!");
else printf("Fail!");
return 0;
}
16 、给定程序中 ,函数fun的功能是:调用随机函数产生20个互不相同的整数放在形参a所指数组中(此数组在主函数中已置0)。
请在程序的下划线处填入正确的内容并把下划线删除 ,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdlib.h>
#include <stdio.h>
#define N 20
void fun(int *a)
{
int i, x, n=0;
x=rand()%20;
/**********found**********/
while (n<__1__)
{ for(i=0; i<n; i++ )
/**********found**********/
if( x==a[i] )
__2__;
/**********found**********/
if( i==__3__)
{a[n]=x; n++; }
x=rand()%20;
}
}
int main()
{
int x[N]={0} ,i;
fun( x );
printf("The result : \n");
for( i=0; i<N; i++ )
{
printf("%4d",x[i]);
if((i+1)%5==0)printf("\n");
}
printf("\n");
return 0;
}
17 、给定程序的主函数中 ,已给出由结构体构成的链表结点a 、b、c,各结点的数据域中均存入字符 ,函数fun()的作用是:将a 、b 、c三个结点链接成一个单向链表 ,并输出链表结点中的数据 。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdio.h>
typedef struct list
{ char data;
struct list *next;
} Q;
void fun( Q *pa, Q *pb, Q *pc)
{ Q *p;
/**********found**********/
pa->next=___1___;
pb->next=pc;
p=pa;
while( p )
{
/**********found**********/
printf(" %c",____2_____);
/**********found**********/
p=____3____;
}
printf("\n");
}
int main()
{ Q a, b, c;
a.data=E; b.data=F; c.data=G; c.next=NULL;
fun( &a, &b, &c );
return 0;
}
18、程序通过定义学生结构体变量 ,存储了学生的学号 、姓名和三门课的成绩 。所有学生数据均以二进制方式输出到文件中 。函数
fun的功能是从形参filename所指的文件中读入学生数据 ,并按照学号从小到大排序后 ,再用二进制方式把排序后的学生数据输出到filename所指的文件中 ,覆盖原来的文件内容 。
请在程序的下划线处填入正确的内容并把下划线删除 ,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdio.h>
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];} STU;
void fun(char *filename)
{
FILE *fp; int i, j;
STU s[N], t;
/**********found**********/
fp=fopen(filename, __1__);
fread(s,sizeof(STU),N,fp);
fclose(fp);
for (i=0; i<N-1; i++)
for (j=i+1; j<N; j++)
if (s[i].sno __2__ s[j].sno)
{
t = s[i]; s[i] = s[j]; s[j] = t;
}
fp=fopen(filename,"wb");
/**********found**********/
__3__(s, sizeof(STU), N, fp);
fclose(fp);
}
int main()
{ STU t[N]={ {10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78},{10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87},{10001,"MaChao", 91, 92, 77}}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), 5, fp);
fclose(fp);
printf("\nThe original data :\n");
for (j=0; j<N; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",t[j].sno, t[j].name);
for (i=0; i<3; i++) printf("%6.2f ", t[j].score[i]);
printf("\n");
}
fun("student.dat");
printf("\nThe data after sorting :\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), 5, fp);
fclose(fp);
for (j=0; j<N; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
return 0;
}
19 、函数fun的功能是:在有n个元素的结构体数组std中 ,查找有不及格科目的学生 ,找到后输出学生的学号;函数的返回值是有不及格科目的学生人数 。
例如 ,主函数中给出了4名学生的数据 ,则程序运行的结果为:
学号:N1002 学号:N1006
共有2位学生有不及格科目
请在程序的下划线处填入正确的内容 ,并把下划线删除,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdio.h>
typedef struct
{ char num[8];
double score[2];
/**********found**********/
} __(1)__ ;
int fun(STU std[ ], int n)
{ int i, k=0;
for(i=0; i<n; i++)
/**********found**********/
if( std[i].score[0]<60__(2)__std[i].score[1]<60 )
{ k++;printf("学号:%s ",std[i].num);}
/**********found**********/
return __(3)__ ;
}
int main()
{ STU std[4]={ "N1001", 76.5,82.0 ,"N1002", 53.5,73.0,
"N1005", 80.5,66.0,"N1006", 81.0,56.0 };
printf( "\n共有%d位学生有不及格科目\n" , fun(std,4) );
return 0;
}
20 、用筛选法可得到
2~
n(n<
10000)之间的所有素数 ,方法是:首先从素数
2开始,将所有
2的倍数的数从数表中删去(把数表中相应位置的值置成
0);接着从数表中找下一个非
0数 ,并从数表中删去该数的所有倍数;依此类推 ,直到所找的下一个数等于n为止 。这样会得到一个序列:
2,
3,
5,
7,
11,
13,
17,
19,
23 ,…
函数
fun的作用是:用筛选法找出所有小于等于n的素数 ,并统计素数的个数作为函数值返回 。
请在程序的下划线处填入正确的内容并把下划线删除 ,使程序得出正确的结果 。
注意:不得增行或删行 ,也不得更改程序的结构!
#include <stdio.h>
int fun(int n)
{ int a[10000], i,j, count=0;
for (i=2; i<=n; i++) a[i] = i;
i = 2;
while (i<n)
{
/**********found**********/
for (j=a[i]*2; j<=n; j+=___1___)
a[j] = 0;
i++;
/**********found**********/
while (___2___==0)
i++;
}
printf("\nThe prime number between 2 to %d\n", n);
for (i=2; i<=n; i++)
/**********found**********/
if (a[i]!=___3___)
{ count++;
printf( count%15?"%5d":"\n%5d",a[i]);
}
return count;
}
int main()
{ int n=20, r;
r = fun(n);
printf("\nThe number of prime is : %d\n", r);
return 0;
}
11 、(1)std[i].year (2)std[i] (3) n
12 、(1)*std (2)PERSON (3)std
13 、(1)NULL (2)n (3) head,ch
14 、(1)FILE * (2)fp (3) ch
15 、(1)"r" (2)fs (3) ft
16 、(1)20 (2)break (3) n
17 、(1)pb (2)p->data (3)p->nex
18 、(1) "rb" (2)> (3) fwrite
19 、(1)STU (2) || (3)k
20、(1)a[i] (2)a[i] (3)0
参考答案
声明:本站所有文章 ,如无特殊说明或标注 ,均为本站原创发布。任何个人或组织 ,在未征得本站同意时 ,禁止复制 、盗用 、采集、发布本站内容到任何网站 、书籍等各类媒体平台 。如若本站内容侵犯了原著者的合法权益 ,可联系我们进行处理 。