C语言复习题 下载本文

内容发布更新时间 : 2024/5/18 8:26:06星期一 下面是文章的全部内容请认真阅读。

第10章 文 件

}

main() { struct link *head; int s;

┆ s=sum(head);

┆ }

int sum( ) {

struct link *p; int s=0;

p=head->next; while(p) { s+= ; p= ; } return(s);

}

4.设有共用体类型和共用体变量定义如下:

union Utype { char ch; int n; long m; float x; double y; };

union Utype un; 并假定un的地址为ffca。

则un.n的地址是__________, un.y的地址是_________。执行赋值语句:un.n=321; 后,再执行语句:printf(\其输出值是_________。(提示:321D=101000001B) 5. 读程序,写出运行结果。 程序1代码: #include #include #define N 10 struct student {

char stuNum[20]; //学生学号 char stuName[20]; //学生姓名

int stuscore[4]; //学生3门课成绩及平均分数 };

int main() {

int i,j;

student stu[N]; int aver=0;

for(i=0; i

printf(\请输入第%d个学生学号:\ scanf(\

printf(\请输入第%d个学生姓名:\ scanf(\

printf(\请输入第%d个学生的三门课程成绩:\ for(j=0; j<3; j++)

scanf(\

stu[i].stuscore[3]=(stu[i].stuscore[0]+stu[i].stuscore[1]+stu[i].stuscore[2])/3; aver+=stu[i].stuscore[3]; }

printf(\门课程的总平均成绩:%d\\n\

第10章 文 件

int max=0,maxi; for(i=0;i

if(stu[i].stuscore[3]>max) { max=stu[i].stuscore[3]; maxi=i; } }

printf(\最高分学生\\n学号:%s\\n姓名:%s\\n3门课程成绩:%d %d %d\\n平均分数:%d\

stu[maxi].stuNum,stu[maxi].stuName,stu[maxi].stuscore[0],stu[maxi].stuscore[1],stu[maxi].stuscore[2],stu[maxi].stuscore[3]); }

程序2代码: #include #include #include struct student { int num; char name[10]; struct student *next; };

int main() { void print(struct student *head); struct student *creat(); struct student *del(struct student *head_a,struct student *head_b); struct student *head,*head_a,*head_b; printf(\初始化链表A********\\n\ head_a=creat(); printf(\输出链表A********\\n\ print(head_a); printf(\初始化链表B********\\n\ head_b=creat(); printf(\输出链表B********\\n\ print(head_b); printf(\从a链表中删去与b链表中有相同学号的那些节点********\\n\ head=del(head_a,head_b); print(head); return 0; }

struct student *creat() { struct student *head,*p,*q; p = q=(struct student *)malloc(sizeof(struct student)); head = NULL; printf(\请输入学生学号和姓名(输入学号0和姓名0停止接收数据):\ scanf(\ while(!(p->num = = 0 && strcmp(p->name,\ { if(head = = NULL) head = p; else q->next=p; q=p; p=(struct student *)malloc(sizeof(struct student)); printf(\请输入学生学号和姓名(输入学号0和姓名0停止接收数据):\

第10章 文 件

scanf(\ } q->next=NULL; return head; }

struct student *del(struct student *head_a,struct student *head_b) { struct student *p,*q,*t; p=q=head_a; if(head_b = = NULL) return head_a; while(p!=NULL) { t=head_b; while(t->next!=NULL&&(t->num!=p->num)) t=t->next; if(t->num= =p->num) { if(p= =head_a) { head_a=p->next; free(p); q=p=head_a; } else { q->next=p->next; free(p); p=q->next; } } else { q=p; p=p->next; } } return head_a; }

void print(struct student *head) { struct student *p; p = head; while(p!=NULL) { printf(\ p=p->next; } }

(3)当编辑完成源程序后,分别对其进行编译、连接和运行,分析其结果是否符合题目要求,符合后则对C语言源程序加上注释。

习题10

1. 系统的标准输出设备是( )。

A.键盘 B.显示器 C.硬盘 D.软盘 2. 函数调用语句“fseek(fp, 10L, 1)”的含义是( )。

第10章 文 件

A.将文件位置指针移到距离文件头10个字节处 B.将文件位置指针从当前位置前移10个字节处 C.将文件位置指针从文件末尾处前移10个字节处 D.将文件位置指针移到距离文件尾10个字节处

3. 从数据的存储形式来看,文件分为_____和_____两类。 4. 为什么要对fopen函数的返回值进行错误检查? 5. 从文件data1中读取10个字符,并显示到屏幕上。

6. 从键盘输入一些字符,并逐个把这些字符写入默认路径下的文件data1.c中,直到碰到一个“&”为止。 7.分析程序运行结果

程序1代码: #include

main() {

FILE *fp; Char ch; if((fp=fopen(“test1.c”,“r”))= =NULL) {

printf(“Cannot open the file!”); exit(0); }

while((ch=fgetc(fp)!=EOF)) {

putchar(ch); }

fclose(fp); }

程序2代码: #include

main() {

FILE *fp; int num=0;

Char ch; if((fp=fopen(“test2.c”,“r”))= =NULL) {

printf(“Cannot open the file!”); exit(0); }

while(!feof(fp)) {

fgetc(fp); num++; }

printf(“num=%d \\n”,num-1);

fclose(fp); }