C语言程序设计第九章作业
- 格式:docx
- 大小:14.27 KB
- 文档页数:4
西安交大C++程序设计第九章作业(总20页)本页仅作为文档封面,使用时可以删除This document is for reference only-rar21year.March西安交通大学实验报告课程__计算机程序设计__实验名称__继承__第 1 页共页系别____ ______ 实验日期 2014 年 5 月日专业班级__ ___组别_____________ 实验报告日期 2014 年月日姓名___ ______学号_ _ 报告退发 ( 订正、重做 )同组人_________________________________ 教师审批签字一、实验目的熟练继承的用法,进一步训练类的编程,并练习不同继承方式下对基类的成员的访问的控制。
二、实验内容(一)第一题:从类Person中派生出一个教师类,新增的属性有:专业、职称和主讲课程(一门),并为这些属性定义相应的方法。
1.源程序代码:#include<iostream>using namespace std;class Person{protected:char *Name;char Sex;int Age;public:Person(){Name="csj";Sex='M';Age=18;}Person(char *name,char sex,int age){Register(name,sex,age);}~Person(){delete []Name;}void Register(char *name,char sex,int age){int m=strlen(name);Name=new char[m+1];strcpy(Name,name);Sex=sex;;Age=age;}void print(){cout<<"姓名:"<<Name<<"\t性别:"<<Sex<<"\t年龄:"<<Age<<endl;}};class Teacher:public Person{protected:char *zhuanye;char *zhicheng;char *course;public:Teacher();Teacher(char*,char,int,char*,char*,char*);void Register(char*,char,int,char*,char*,char*);~Teacher();void print();};Teacher::Teacher(){Person::Register("罗先觉",'M',61);strcpy(zhuanye,"电气工程及其自动化");strcpy(zhicheng,"教授");strcpy(course,"电路");}Teacher::Teacher(char *name,char sex,int age,char *zy,char *zc,char *cor){Register(name,sex,age,zy,zc,cor);}void Teacher::Register(char *name,char sex,int age,char *zy,char *zc,char *cor) {Person::Register(name,sex,age);zhuanye=new char[strlen(zy)+1];strcpy(zhuanye,zy);zhicheng=new char[strlen(zc)+1];strcpy(zhicheng,zc);course=new char[strlen(cor)+1];strcpy(course,cor);}Teacher::~Teacher(){delete []zhuanye;delete []zhicheng;delete []course;}void Teacher::print(){cout<<"姓名:"<<Name<<"\t性别:"<<Sex<<"\t年龄:"<<Age<<"\t 专业"<<zhuanye<<"\t职称:"<<zhicheng<<"\t主讲课程:"<<course<<endl;}int main(){Person person;("csj",'M',18);();Teacher teacher("lll",'M',56,"math","jiaoshou","gaoshu");();("某某",'M',58,"数学","教授","高等数学");();return 0;}2.实验结果:(二)第二题:第二题:许多研究生既有学生的属性,又有教师的属性。
9-3编写程序,使用结构体类型,输出一年十二个月的英文名称及相应天数。
解:#include "stdio.h"struct date{char month[10] ;int daynumber ;}main(){int i ;struct datea[12]={{"January",31},{"February",29},{"March",31},{"Aprial",30},{ "May",31},{"June",30},{"july",31},{"August",31},{"September",30}, {"October",31},{"November",30},{"December",31}} ;for(i=0;i<12;i++);printf("%d 月:%s %d\n",i+1,a[i].month,a[i].daynumber) ;}思考:如何对结构体变量进行初始化?对结构体变量的引用为何要体现为分量(或成员)的引用?9-4 编写程序求空间任一点到原点的距离,点用结构体描述。
并请考虑求空间中任意两点的距离的程序。
解:#include "stdio.h"#include "math.h"struct point{float x ;float y ;float z ;} main(){double d1,d2,d ;struct point p1,p2 ;printf("请输入第一个点的坐标:");scanf("%f,%f,%f",&p1.x,&p1.y,&p1.z);printf("请输入第二个点的坐标:");scanf("%f,%f,%f",&p2.x,&p2.y,&p2.z);d1=sqrt(p1.x*p1.x+p1.y*p1.y+p1.z*p1.z);d2=sqrt(p2.x*p2.x+p2.y*p2.y+p2.z*p2.z);d=sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)+(p2.z-p1.z)*( p2.z-p1.z));printf("第一个点到原点的距离:%f\n",d1);printf("第二个点到原点的距离:%f\n",d2);printf("两点间的距离:%f\n",d);}9-5 编写输入、输出10个朋友数据的通讯录程序,每个朋友数据包括姓名、地址、邮编、电话、传呼、手机等数据。
c语言第九章题库及详解答案C语言第九章题库及详解答案一、选择题1. 在C语言中,以下哪个关键字用于定义数组?A. arrayB. listC. setD. define2. 以下哪个选项是正确的C语言数组声明?A. int myArray[];B. int myArray[10] = {};C. int myArray = 10;D. int myArray(10);3. 数组元素的默认初始化值是什么?A. 0B. 1C. -1D. 随机值4. 在C语言中,数组的索引是从哪个数字开始的?A. 0B. 1C. -1D. 105. 以下哪个函数可以用于计算数组中元素的个数?A. count()B. size()C. length()D. sizeof()二、填空题6. 在C语言中,声明一个具有10个整数元素的数组的语句是:________。
答案:int myArray[10];7. 如果数组的索引从0开始,那么数组myArray[10]的最后一个元素的索引是:________。
答案:98. 要初始化一个数组的所有元素为0,可以使用:________。
答案:int myArray[10] = {0};9. 在C语言中,可以使用________运算符来访问数组的元素。
答案:[]10. 当数组作为参数传递给函数时,实际上传递的是数组的________。
答案:首地址三、简答题11. 解释C语言中数组的内存分配方式。
答案:在C语言中,数组是连续存储在内存中的。
数组的内存分配是静态的,即在编译时分配。
数组的元素按照声明的顺序在内存中连续排列。
12. 说明数组和指针在C语言中的关系。
答案:在C语言中,数组名可以作为指针使用。
数组名代表数组的首地址。
当数组作为参数传递给函数时,数组名退化为指向数组第一个元素的指针。
四、编程题13. 编写一个C语言程序,实现对一个整数数组的排序。
答案:```c#include <stdio.h>void sortArray(int arr[], int size) {int i, j, temp;for (i = 0; i < size - 1; i++) {for (j = i + 1; j < size; j++) {if (arr[i] > arr[j]) {temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}}int main() {int myArray[] = {5, 3, 8, 2, 1};int size = sizeof(myArray) / sizeof(myArray[0]);sortArray(myArray, size);printf("Sorted array: ");for (int i = 0; i < size; i++) {printf("%d ", myArray[i]);}return 0;}```14. 编写一个C语言程序,实现查找数组中的最大值和最小值。
#include<stdio.h>struct student{int xuehao;char name[20];float yuwen,shuxue,yingyu,ave;};float aver(float yuwen,float shuxue,float yingyu){float y;y=(yuwen+shuxue+yingyu)/3.0;return y;}main(){struct student a;printf("Enter xuehao:\n");scanf("%d",&a.xuehao);printf("Enter name:\n");scanf("%s",&);printf("Enter yuwen:\n");scanf("%f",&a.yuwen);printf("Enter shuxue:\n");scanf("%f",&a.shuxue);printf("Enter yingyu:\n");scanf("%f",&a.yingyu);a.ave=aver(a.yuwen,a.shuxue,a.yingyu);printf("xuehao:%d name:%s yuwen:%f shuxue:%f yingyu:%f ave:%f\n",a.xuehao,,a.yuwen,a.shuxue,a.yingyu,a.ave);}/*练习9-6*/#include<stdio.h>struct student{int xuehao;char name[20];float yuwen,shuxue,yingyu,ave;};float aver(float yuwen,float shuxue,float yingyu){y=(yuwen+shuxue+yingyu)/3.0;return y;}main(){int i;struct student a;for(i=1;i<=100;i++){printf("请输入第%d个学生的信息:\n",i);printf("请输入学号:\n");scanf("%d",&a.xuehao);printf("请输入姓名:\n");scanf("%s",&);printf("请输入语文成绩:\n");scanf("%f",&a.yuwen);printf("请输入数学成绩:\n");scanf("%f",&a.shuxue);printf("请输入英语成绩:\n");scanf("%f",&a.yingyu);a.ave=aver(a.yuwen,a.shuxue,a.yingyu);printf("学号:%d 姓名:%s 语文:%f 数学:%f 英语:%f 平均分:%f\n",a.xuehao,,a.yuwen,a.shuxue,a.yingyu,a.ave);}}。
第九章习题参考答案一、选择题9.1 答案:B9.2 答案:C9.3 答案:D9.4 答案:B9.5 答案:D9.6 答案:C9.7 答案:C9.8 答案:D9.9 答案:B9.10 答案:D二、读程序回答问题9.11 答案:C9.12 答案:A9.13 答案:A9.14 答案:D9.15 答案:C三、程序填空9.16答案:①FILE *fp;② "Ef9-16.txt","w"或"Ef9-16.txt","w+";③ch,fp。
9.17答案:①Ef9-17.dat;② 8*sizeof(char)。
9.18 答案:①fopen; ② SEEK_END或2。
9.19 答案:①fname; ②fp。
9.20 答案:①argc!=2; ②r; ③NULL; ④row++ ; ⑤fclose(fp)。
四、编写程序9.21 从键盘输入一行字符串,将其中的小字母全部转换成大写字母,然后输出到一个磁盘文件" Ef9-21.txt"中保存。
参考源程序如下:#include <stdio.h>void main( ){FILE *fp;char str[100];int i;if ((fp=fopen("Ef9-21.txt ", "w")) == NULL) {printf ("Cannot open the file.\n");exit(0);}printf ("Input a string:");gets (str); /* 读入一行字符串 */for (i=0; str[i]&&i<100; i++) { /* 处理该行中的每一个字符 */if (str[i] >= 'a' && str[i] <= 'z') /* 若是小写字母 */str[i] -= 'a' - 'A'; /* 将小写字母转换为大写字母 */fputc (str[i], fp); /* 将转换后的字符写入文件 */}fclose (fp); /* 结束文件输入操作关闭文件 */}9.22文件Ef9-22.txt中存放了一个学生各门课程的考试分数,数据之间以逗号分割。
C语言第九章参考答案1.选择题:12345 67890 12ADCDB BCDDC BB2.填空题:(1)指针或者地址(2)110(3)①char *p; ②p=&ch; ③scanf("%c",p); ④*p='a'; ⑤printf("%c",*p);(4)10 (5)0、7 (6)ab (7)abcdcd (8) 7ㄩ1(9)void (*p)(int * ,int*); (10)r+b[k] (11) '\0' 、n++; (12)aegi 3.改错题:(1) 第一处改正:* sub=x-y第二处改正:scanf("%f%f",&x,&y);第三处改正:calc(x,y,&add,&sub);(2)第一处:char swap (char *p1,char*p2)改为void swap (char *p1,char*p2)第二处:strcpy(p,p1)改为strcpy(p,p2)(3)第一处:p1=p1+m改为p1=p1+m-1第二处:*p1=*p2改为*p2=*p1第三处:*p2="\0"改为*p2='\0'(4)第一处:char *fun(char *str,char t)改为char *fun(char *str,char *t)第二处:s=NuLL改为s=NULL;第三处:if(r==p)改为if(*r==*p)(5)第一处:fun(int **b,int n)改为fun(int (*b)[N],int n)第二处:b[j][k]=k*j 改为b[j][k]=(k+1)*(j+1)4编程题(1)/*习题9-4-1 */void move(int array[20],int n,int m) ;main(){ int number[20],n,m,i;printf("How many numbers?"); /*共有多少个数*/scanf("%d",&n);printf("Input %d numbers:\n",n); /*输入n个数*/for(i=0;i<n;i++)scanf("%d",&number[i]);printf("How many place you want to move?"); /*后移多少个位置*/scanf("%d",&m);move(number,n,m); /*调用move函数*/ printf("Now,they are:\n");for(i=0;i<n;i++)printf("%d ",number[i]);}void move(int array[20],int n,int m) /*循环后移函数*/{ int *p,array_end;array_end=*(array+n-1);for(p=array+n-1;p>array;p--)*p=*(p-1);*array=array_end;m--;if(m>0) move(array,n,m); /*递归调用,当循环次数m减至0时,停止调用*/}(2)/*习题9-4-2 */#include<stdio.h>#include<string.h>#define TOTAL 6int mseek(char*str[],char xstr[],int n){ int i;for(i=0; i<n; i++){ if(strcmp(str[i],xstr)==0)return 1;}return 0;}main(){ char*name[TOTAL]={"Lining","Linshan","Tianyuan","Zhangqiang","Haipo","Fangbing"};char xname[20];printf("enter a name:");gets(xname);if(mseek(name,xname,TOTAL))printf("Found!\n");elseprintf("Not found!\n");}(3)/*习题9-4-3 */#include <stdio.h>#include <string.h>void fun(char *str,int num[4]){ int i;for(i=0; i<4; i++) num[i]=0;while(*str!='\0'){ if(*str>='a' && *str<='z' || *str>='A' && *str<='Z')num[0]++;else if(*str==' ')num[1]++;else if(*str>='0' && *str<='9')num[2]++;elsenum[3]++;str++;}}#define N 80main(){ int string[N];int n[4],i;gets(string);fun(string,n);for(i=0; i<4; i++)printf("%d\t",n[i]);}(4)/*习题9-4-4 *//* 调试时,可这样输入数据:*//*11 12 13 14 1521 22 23 24 2531 32 33 34 3541 42 43 44 4551 52 53 54 55 */#include <stdio.h>main(){ int a[5][5],*p,i,j;void change(int *p);printf("Input matrix:\n");for(i=0;i<5;i++) /*输入矩阵*/for(j=0;j<5;j++)scanf("%d",&a[i][j]);p=&a[0][0]; /*使p指向0行0列元素*/ change(p); /*调用函数, 实现交换*/ printf("Now, matrix: \n");for(i=0;i<5;i++) /*输出已交换的矩阵*/{ for(j=0;j<5;j++)printf("%4d",a[i][j]);printf("\n");}}void change(int *p) /*交换函数*/{ int i,j,temp;int *pmax,*pmin;pmax=p;pmin=p;for(i=0;i<5;i++) /*找最大值和最小值的地址,并赋给pmax,pmin*/ for(j=0;j<5;j++){ if(*pmax<*(p+5*i+j)) pmax=p+5*i+j;if(*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*(p+12); /*将最大值换给中心元素*/*(p+12)=*pmax;*pmax=temp;temp=*p; /*将最小值换给左上角元素*/*p=*pmin;*pmin=temp;pmin=p+1;for(i=0;i<5;i++) /*找第二最小值的地址赋给pmin*/ for(j=0;j<5;j++)if(((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j))) pmin=p+5*i+j;temp=*pmin; /*将第二最小值换给右上角元素*/*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for(i=0;i<5;i++) /*找第三最小值的地址赋给pmin*/ for(j=0;j<5;j++)if(((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j; /*将第三最小值换给左下角元素*/ temp=*pmin;*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for(i=0;i<5;i++) /*找第四最小值的地址赋给pmin*/ for(j=0;j<5;j++)if(((p+5*i+j)!=p)&&((p+5*i+j)!=(p+4))&&((p+5*i+j)!=(p+20))&&(*pmin>*(p+5*i+j))) pmin=p+5*i+j;temp=*pmin; /*将第四最小值换给右下角元素*/*pmin=*(p+24);*(p+24)=temp;}(5)/*习题9-4-5 *//*可以专门编写一个函数求各学生的平均分,存到aver[4]数组*/#include <stdio.h>void avcour1(float score[][5]);void fali2(int num[4],float score[4][5]);void good(int num[4],float score[4][5]);main(){int i,j,num[4];//数组num代表学号float score[4][5];printf("Input NO. and scores: \n");for(i=0;i<4;i++){ printf("NO.");scanf("%d",&num[i]);printf("scores:");for(j=0;j<5;j++)scanf("%f",&score[i][j]);}printf("\n\n");avcour1(score); /*求出第一门课的平均成绩*/ printf("\n\n");fali2(num,score); /*找出2门课不及格的学生*/printf("\n\n");good(num,score); /*找出成绩好的学生*/}void avcour1(float score[][5]) /*第一门课的平均成绩的函数*/{ int i;float sum,average1;sum=0.0;for(i=0;i<4;i++)sum=sum+score[0][i]; /*累计每个学生的得分*/ average1=sum/4; /*计算平均成绩*/printf("course 1 average score: %6.2f. \n",average1);}void fali2(int num[4],float score[4][5])/*找两门以上课程不及格的学生的函数*/{ int i,j,k,label;float sum=0;printf("= = = = = = = =Student who is fail = = = = = = = = = = = =\n");printf(" NO.");for(i=0;i<5;i++)printf("%10d",i+1);printf(" average\n");for(i=0;i<4;i++){ label=0;for(j=0;j<5;j++)if((score[i][j])<60.0) label++;if(label>=2){ printf("%5d",num[i]);for(k=0;k<5;k++){ printf("%10.2f",score[i][k]);sum+=score[i][k];}printf("%10.2f\n",sum/5);}}}void good(int num[4],float score[4][5])/*找成绩优秀的学生(各门85分以上或平均90分以上)的函数*/ { int i,j,k,n;float sum=0,aver[4];printf("= = = = = = = =Student whose score is good= = = = = = = =\n");printf(" NO.");for(i=0;i<5;i++)printf("%10d",i+1);printf(" average\n");for(i=0;i<4;i++){ n=0;sum=0;for(j=0;j<5;j++){if((score[i][j])>85.0) n++;sum+=score[i][j];}aver[i]=sum/5;if((n==5)||(aver[i]>=90)){ printf("%5d",num[i]);for(k=0;k<5;k++)printf("%10.2f",score[i][k]);printf("%10.2f\n",aver[i]);}}}(6)/*习题9-4-6*/#include <math.h>double sigma(double (*fn)(double),double l,double u){ double sum=0,d;for(d=l; d<u; d+=0.1)sum+=fn(d);return sum;}void main(){ double sum;sum=sigma(sin,0.1,1.0);printf("sum of sin from 0.1 to 1.0 is: %f\n",sum);sum=sigma(cos,0.5,3.0);printf("sum of cos from 0.5 to 3.0 is: %f\n",sum);}(7)/*习题9-4-7 */main(){ int i;char *month_name(int n);printf("input Month No.:\n");scanf("%d",&i);printf("Month No.:%2d --> %s\n",i,month_name(i)); /*调用指针函数month_name()*/ }char *month_name(int n)/*定义一个指针函数month_name(),返回一个指向字符串的指针*/{ static char *name[]={"Illegal Month","January", "February", "March", "April","May", "June", "July", "August","September", "October", "November", "December"};return((n<1||n>12)?name[0]:name[n]);}(8)/*习题9-4-8 */#include <stdio.h>#include <string.h>#define N 10main(){ void sort(char *p[]);int i;char *p[N],str[N][20];for(i=0;i<N;i++)p[i]=str[i]; /*将第i个字符串的首地址赋予指针数组p的第i个元素*/ printf("Input strings:\n");for(i=0;i<N;i++)scanf("%s",p[i]);sort(p);printf("Now, the sequence is:\n");for(i=0;i<N;i++)printf("%s\n",p[i]);}void sort(char *p[]){ int i,j;char *temp;for(i=0;i<N-1;i++)for(j=0;j<N-1-i;j++)if(strcmp(*(p+j),*(p+j+1))>0){ temp=*(p+j);*(p+j)=*(p+j+1);*(p+j+1)=temp;}}(9)/*习题9-4-9 */#include <stdio.h>#define LINEMAX 20 /*定义字符串的最大长度*/main(){ void sort(char **p);int i;char **p,*pstr[5],str[5][LINEMAX];for(i=0;i<5;i++)pstr[i]=str[i]; /*将第i个字符串的首地址赋予指针数组pstr的第i 个元素*/printf("Input 5 strings:\n");for(i=0;i<5;i++)scanf("%s",pstr[i]);p=pstr;sort(p);printf("strings sorted:\n");for(i=0;i<5;i++)printf("%s\n",pstr[i]);}void sort(char **p) /*冒泡法对5个字符串排序的函数*/ { int i,j;char *temp;for(i=0;i<5;i++){ for(j=i+1;j<5;j++){ if(strcmp(*(p+i),*(p+j))>0) /*比较后交换字符串地址*/{ temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;}}}}(10)void StrOR(char xx[][80],int maxline){ int i,righto,j,s,k;char temp[80];for(i=0; i<maxline; i++)for(j=strlen(xx[i])-1; j>=0; j--){ k=0; memset(temp,0,80);if(xx[i][j]=='o'){ righto=j;for(s=righto+1; s<strlen(xx[i]); s++)temp[k++]=xx[i][s];for(s=0;s<righto;s++)if(xx[i][s]!='o') temp[k++]=xx[i][s];strcpy(xx[i],temp);}else continue;}}C语言第十章参考答案1. 选择dccda cab2..填空(1)struct studentstrcmp(str,stu[i].name)==0break;(2)p=personp-person<3old=p->age;q->name,q->age(3)p!=NULLc++p->next(4)&per[i].body.eye&per[i].body.f.height&per[i].body.f.weight3.编程题(1)#include <stdio.h>struct data{ int year;int month;int day;};main(){ struct data a;int monthnum[12]={31,28,31,30,31,30,31,31,30,31,30,31};int i,sum=0;scanf("%d%d%d",&a.year,&a.month,&a.day);for(i=0;i<a.month-1;i++)sum+=monthnum[i];sum+=a.day;if(a.year%4==0 && a.year%100!=0 ||a.year%400==0)sum+=1;printf("%d年%d月%d日is the %d day",a.year,a.month,a.day,sum); }(2)#include <stdio.h>#include <stdlib.h>struct study{ float chinese;float maths;float english;float avg;};main(){ struct study student;scanf("%f%f%f",&student.chinese,&student.maths,&student.english);student.avg=(student.chinese+student.maths+student.english)/3;printf("average score is %f\n",student.avg);}(3)#include <stdio.h>#include <stdlib.h>struct study{ int num;float chinese;float maths;float english;float avg;};main(){ struct study s[3];struct study *p;for(p=s;p<s+3;p++){ scanf("%d%f%f%f",&(p->num),&(p->chinese),&(p->maths),&(p->english));p->avg=(p->chinese+p->maths+p->english)/3;}for(p=s;p<s+3;p++)printf("%d %3.1f %3.1f %3.1f %3.1f\n",p->num,p->chinese,p->maths,p->english,p->a vg);}(4)#include <stdio.h>#include <string.h>#define N 3typedef struct{char dm[5]; /*产品代码*/char mc[11]; /* 产品名称*/int dj; /* 单价*/int sl; /* 数量*/long je; /* 金额*/} PRO;void SortDat(PRO sell[],int n){ int i,j;PRO xy;for(i=0; i<N-1; i++)for(j=i+1; j<N; j++)if(strcmp(sell[i].mc,sell[j].mc)>0||strcmp(sell[i].mc,sell[j].mc)==0&&sell[i].je>sell[j].je) { xy=sell[i];sell[i]=sell[j];sell[j]=xy;}}void main(){ PRO sell[N];int i;for(i=0; i<N; i++){scanf("%s%s",sell[i].dm,sell[i].mc); //可这样输入,如:101 aaascanf("%d%d",&sell[i].dj,&sell[i].sl); //可这样输入,如:10 20sell[i].je=sell[i].dj*sell[i].sl;}SortDat(sell,N);printf("dm\t\tmc\t\tdj\tsl\tje\n");for(i=0; i<N; i++){printf("%s\t\t%s\t\t%d\t%d\t%ld\n",sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);}}(5)#include <stdio.h>#include <stdlib.h>main(){int *pi;int i,j,t,n;printf("输入整数个数:");scanf("%d",&n);pi=(int *)malloc(n*sizeof(int));printf("输入整数:");for(i=0; i<n; i++)scanf("%d",&pi[i]);for(i=0; i<n-1; i++)for(j=i+1; j<n; j++)if(pi[i]>pi[j]){t=pi[i]; pi[i]=pi[j]; p i[j]=t;}for(i=0; i<n; i++)printf("%d ",pi[i]);printf("\n");free(pi);}#include <stdio.h>#include <stdlib.h>struct stu{ int num;char name[20];int age;struct stu *next;};void list(struct stu *head){ struct stu *p;printf("The list records are:\n");p=head;if(head!=NULL)do{ printf("%d\t%s\t%d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);elseprintf("The list is null");}struct stu * insert(struct stu *head,struct stu *stud){ struct stu *p0,*p1,*p2;p1=head; /*p1指向链表第一个节点*/ p0=stud; /*p0指向要插入的节点*/if(head==NULL) /*原链表是空表*/{ head=p0;p0->next=NULL; /*p0作为头指针*/ }else{ while((p1!=NULL)&&(p0->num>=p1->num)){ p2=p1;p1=p1->next;} /*p1指针后移*/if(p1!=NULL){ if(head==p1) head=p0;/*插入链表开头*/else p2->next=p0; /*插入到p2节点之后*/p0->next=p1;}else{ p2->next=p0;p0->next=NULL;}/*插入到最后*/return head;}main(){ struct stu *newstu,*head;head=NULL;int num;scanf("%d",&num);while(num!=0){ newstu=(struct stu *)malloc(sizeof(struct stu));newstu->num=num;scanf("%s",newstu->name);scanf("%d",&newstu->age);head=insert(head,newstu);scanf("%d",&num);}list(head);}(7)#include <stdio.h>void partition(unsigned long int num){ union a{ unsigned short int part[2];unsigned long int w;}n,*p;p=&n;n.w=num;printf("long int=%lx\n",num);printf("low part num=%0x,high part num=%0x\n",p->part[0],p->part[1]); }main(){ unsigned long int x;x=0x23456789; //这是为了调试方便,应改为scanf函数partition(x);}C语言第十一章参考答案一、选择1.B2.A3.B4.C5.B6.C7.D8.C9.C、D 10.A 11.A 12.C 13.B二、填空1 :fopen(fname,"w")ch2:"r"fgetc(fp) 或getc(fp)3:"bi.dat"&jfp4:==NULLflag==1s[strlen(s)-1]=='\n'三、改错题1:第一处改为:long num=0;第二处改为: !feof(fp) 或!=0改为==02: 第一处改为:rewind(fp)第二处改为: fgetc(fp)!= '\n' '\0'后面加上&& feof(fp)!=1四、编程题(1)#include<stdio.h>#include <stdlib.h>#include <string.h>main(){FILE *fp;char str[100];int i=0;if((fp=fopen("myfile","w"))==NULL){ printf("Can not open the file.\n");exit(0);}printf("Input a string:\n");gets(str);while(str[i]!='!'){ if(str[i]>='a'&& str[i]<='z')str[i]=str[i]-32;fputc(str[i],fp);i++;}fclose(fp);fp=fopen("myfile","r");fgets(str,strlen(str)+1,fp);printf("%s\n",str);fclose(fp);}(2)#include<stdio.h>struct student{ char num[10];char name[8];int score[3];float ave;}stu[5];main(){int i,j,sum;FILE *fp;for(i=0;i<5;i++){ printf("\nInput score of student %d:\n",i+1);printf("NO.:");scanf("%s",stu[i].num);printf("name:");scanf("%s",stu[i].name);sum=0;for(j=0;j<3;j++){ printf("score %d:",j+1);scanf("%d",&stu[i].score[j]);sum+=stu[i].score[j];}stu[i].ave=sum/3.0;}fp=fopen("stud","w");for(i=0;i<5;i++)if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)printf("File write error\n");fclose(fp);fp=fopen("stud","r");for(i=0;i<5;i++){ fread(&stu[i],sizeof(struct student),1,fp);printf("%s,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);}}(3)#include<stdio.h>#include <stdlib.h>main(){char s[80];int a;FILE *fp;if((fp=fopen("test","w"))==NULL){ printf("Cannot open file.\n");exit(1);}fscanf(stdin,"%s%d",s,&a); //相当于scanffprintf(fp,"%s %d",s,a);fclose(fp);if((fp=fopen("test","r"))==NULL){ printf("Cannot open file.\n");exit(1);}fscanf(fp,"%s %d",s,&a);fprintf(stdout,"%s %d\n",s,a); //相当于printf fclose(fp);}(4)#include<stdio.h>#include <stdlib.h>main(){FILE *fp;int i,j,n,i1;char c[100],t,ch;if((fp=fopen("A","r"))==NULL){printf("Can not open the file.\n");exit(0);}printf("\nfile A:\n");for(i=0;(ch=fgetc(fp))!=EOF;i++){c[i]=ch;putchar(c[i]);}fclose(fp);i1=i;if((fp=fopen("B","r"))==NULL){ printf("\n Can not open the file.");exit(0);}printf("\nfile B:\n");for(i=i1;(ch=fgetc(fp))!=EOF;i++){c[i]=ch;putchar(c[i]);}fclose(fp);n=i;for(i=0;i<n;i++)for(j=i+1;j<n;j++)if(c[i]>c[j]){ t=c[i];c[i]=c[j];c[j]=t;}printf("\n file C:\n");fp=fopen("c","w");for(i=0;i<n;i++){ putc(c[i],fp);putchar(c[i]);}fclose(fp);}(5)#include<stdio.h>main(){FILE *fp1,*fp2;char ch;fp1=fopen("file1.c","r");fp2=fopen("file2.c","w");ch=fgetc(fp1);while(!feof(fp1)){putchar(ch);ch=fgetc(fp1);}rewind(fp1);while(!feof(fp1))fputc(fgetc(fp1),fp2);fclose(fp1);fclose(fp2);}(6)#include<stdio.h>main(){FILE *fp; long position;fp=fopen("data.txt","w");position=ftell(fp);printf("position=%ld\n",position);fprintf(fp,"Sample data\n");position=ftell(fp);printf("position=%ld\n",position);fclose(fp);}第12章1.选择题(1)D (2)C (3)D (4)D (5)C(6)D (7)D (8)B (9)A (10).C2.填空题(1) //(2) public、private 、protected(3) 函数类型类名::函数名(形参表列) 或类型类名::函数名(参数表)(4) 内联函数(5) 构造函数(6) 类名, 创建对象时(7) 私有(8) 私有(9) 基类的构造函数成员对象的构造函数派生类本身的构造函数3.概念题(1)答:本质差别是C++是面向对象的,而C语言是面向过程的。
C语言程序设计(江宝钏著)清华大学出版社第9章习题答案(1)习题9 答案第5 题:#includevoid infoinput(struct student* st,int num);void levelcount(struct student* st,int num);//定义一个结构student 表示学生基本情况struct student{char stuno[20];char stuname[20];double cgrade;char gradelevel;};int main(){struct student ss[100]; //根据题意,一个班学生人数不超过100 个int snum=0;while (snum<=0||snum>100){printf("请输入班级的学生数n<=100:");scanf("%d",&snum);}infoinput(ss,snum);levelcount(ss,snum);return 0;}//输入基本数据void infoinput(struct student* st,int num){int i=0;while(i<num){< bdsfid="89" p=""></num){<>printf("请输入学生%1d 的学号、姓名、计算机成绩:",i+1);scanf("%s%s%lf",st[i].stuno,st[i].stuname,&st[i].cgrade); while (st[i].cgrade<0||st[i].cgrade>100){printf("请输入学生%1d 的计算机成绩[0-100]:",i+1);scanf("%lf",&st[i].cgrade);}if (st[i].cgrade>=90) st[i].gradelevel='A';else if(st[i].cgrade>=70) st[i].gradelevel='B';else if(st[i].cgrade>=60) st[i].gradelevel='C';else st[i].gradelevel='D';i++;}}//统计等级人数void levelcount(struct student* st,int num){int levelA=0,levelB=0,levelC=0,levelD=0;int i;for(i=0;i<num;i++){< bdsfid="107" p=""></num;i++){<> switch (st[i].gradelevel){case 'A':levelA++;break;case 'B':levelB++;break;case 'C':levelC++;break;case 'D':levelD++;break;};}printf("统计结果:A:%d 人,B:%d 人,C:%d 人,D:%d 人\n",levelA,levelB,levelC,levelD);}第6 题:#include#includevoid finit();void fcreat();void fdelete();void fupdate();void ffind();//定义日期struct date{int year;int month;int day;};//定义一个结构student 表示学生基本情况struct llist{char name[20];struct date birthday;char homeno[10];char mobileno[20];};struct llist addlist[50];//如果不用链表做,假设://通讯录每个位置记录一个联系人信息//如果某位置联系人姓名信息为空,表示该位置无联系人int main(){int menuno=0;while (1){printf("\n 请选择操作功能:\n");printf("[1]新建:\n");printf("[2]删除:\n");printf("[3]修改:\n");printf("[4]查询:\n");printf("[5]退出:\n");scanf("%d",&menuno);switch(menuno){case 1:fcreat();break;case 2:fdelete();break;case 3:fupdate();break;case 4:ffind();break;case 5:return 0;default:printf("错误选项,请重新选择!\n");};}return 0;}void finit(){int i;for(i=0;i<50;i++) {strcpy(addlist[i].name,"");addlist[i].birthday.year=0;addlist[i].birthday.month=0;addlist[i].birthday.day=0;strcpy(addlist[i].homeno,"");strcpy(addlist[i].mobileno,"");}}void fcreat(){int i;char oname[20];int oyear;int omonth;int oday;char ohomeno[10];char omobileno[20];printf("请输入联系人信息姓名、出生年、月、日、家庭电话、手机号:\n"); scanf("%s%d%d%d%s%s",oname,&oyear,&omonth,&oday,oho meno,omobileno); for(i=0;i<50;i++) {if (strlen(addlist[i].name)==0) {strcpy(addlist[i].name,oname);addlist[i].birthday.year=oyear;addlist[i].birthday.month=omonth;addlist[i].birthday.day=oday;strcpy(addlist[i].homeno,ohomeno);strcpy(addlist[i].mobileno,omobileno);printf("已增加新联系人\n");break;}}if (i>=50) printf("通讯录满,无法增加新联系人\n");void fdelete(){int i;char oname[20];printf("请输入联系人姓名:\n");scanf("%s",oname);for(i=0;i<50;i++) {if (strcmp(addlist[i].name,oname)==0) {strcpy(addlist[i].name,"");addlist[i].birthday.year=0;addlist[i].birthday.month=0;addlist[i].birthday.day=0;strcpy(addlist[i].homeno,"");strcpy(addlist[i].mobileno,"");printf("已删除该联系人%s\n",oname);break;}if (i>=50) printf("无该联系人,无法删除\n");}void fupdate(){int i;char oname[20];char nname[20];int nyear;int nmonth;int nday;char nhomeno[10];char nmobileno[20];printf("请输入联系人姓名:\n");scanf("%s",oname);printf("请输入联系人信息姓名、出生年、月、日、家庭电话、手机号:\n"); scanf("%s%d%d%d%s%s",nname,&nyear,&nmonth,&nday,nho meno,nmobileno); for(i=0;i<50;i++) {if (strcmp(addlist[i].name,oname)==0) {strcpy(addlist[i].name,nname);addlist[i].birthday.year=nyear;addlist[i].birthday.month=nmonth;addlist[i].birthday.day=nday;strcpy(addlist[i].homeno,nhomeno);strcpy(addlist[i].mobileno,nmobileno);printf("已修改联系人%s\n",nname);break;}if (i>=50) printf("无该联系人,无法修改\n");void ffind(){int i;char oname[20];printf("请输入联系人姓名:\n");scanf("%s",oname);for(i=0;i<50;i++) {if (strcmp(addlist[i].name,oname)==0) {printf("姓名:%s",addlist[i].name);printf(" 出生日期:%1d 年%1d 月%1d 日",addlist[i].birthday.year,addlist[i].birthday.month,addlist[i].bi rthday.day); printf("家庭电话:%s",addlist[i].homeno);printf("手机:%s",addlist[i].mobileno);break;}}if (i>=50) printf("无该联系人\n");}第7 题:#includevoid salaryinput(struct salary* sl,int num);void salarylist(struct salary* sl,int num);struct salary{char id[20];char name[20];double base;double merit;double subsidiary;double insurance;double tax;double real;};int main(){struct salary ss[100]; //工资人数不超过100 个int snum=0;while (snum<=0||snum>100){printf("请输员工人数n<=100:");scanf("%d",&snum);}salaryinput(ss,snum);salarylist(ss,snum);return 0;void salaryinput(struct salary* sl,int num){int i=0;while(i<num){< bdsfid="279" p=""></num){<>printf("请输入员工%1d 的编号、姓名、基本工资、绩效工资、津贴工资、保险:\n",i+1); scanf("%s%s%lf%lf%lf%lf",sl[i].id,sl[i].name,&sl[i].base,&sl[i].meri t,&sl[i].su bsidiary,&sl[i].insurance);sl[i].tax=(sl[i].base+sl[i].merit+sl[i].subsidiary)*0.05;sl[i].real=(sl[i].base+sl[i].merit+sl[i].subsidiary-sl[i].insurance-sl[i].tax); i++;}}void salarylist(struct salary* sl,int num){int i;for(i=0;i<num;i++){< bdsfid="289" p=""></num;i++){<>printf("员工的编号、姓名、实发工资如下:",i+1);printf("%s %s %lf\n",sl[i].id,sl[i].name,sl[i].real);}}第8 题:#include#includevoid linkcreat();void linklist();struct Node{char data;struct Node *link;};Node *snode=NULL,*tmpnode,*lastnode;int main(){linkcreat();linklist();//最好增加一个释放链表的函数,为什么?return 0;}void linkcreat(){char c;lastnode=snode;printf("请输入一行字符:\n");while(scanf("%c",&c),c!='\n'){tmpnode=(Node*)malloc(sizeof(Node)); tmpnode->data=c;if (snode==NULL) snode=tmpnode;else lastnode->link=tmpnode; lastnode=tmpnode; }lastnode->link=NULL;}void linklist(){tmpnode=snode;while(tmpnode!=NULL){printf("%c",tmpnode->data); tmpnode=tmpnode->link; }printf("\n");}。
c语言习题第九章答案C语言习题第九章答案第九章是C语言学习中的一个重要章节,主要涉及指针和动态内存分配的知识。
在这一章中,我们将学习如何使用指针来操作内存,并且了解动态内存分配的概念和用法。
本文将为大家提供第九章习题的答案,帮助大家更好地理解和掌握这些知识。
1. 以下代码的输出结果是什么?```c#include <stdio.h>int main() {int arr[] = {1, 2, 3, 4, 5};int *ptr = arr;printf("%d\n", *ptr++);printf("%d\n", *ptr++);printf("%d\n", *ptr++);return 0;}```答案:输出结果为1、2、3。
解析:在这段代码中,我们定义了一个整型数组arr,并且定义了一个指针ptr,将arr的首地址赋值给ptr。
在输出语句中,我们使用了后置自增运算符,这意味着先输出ptr指向的值,然后再将指针ptr的值加1。
因此,第一个输出语句输出的是arr[0]的值1,第二个输出语句输出的是arr[1]的值2,第三个输出语句输出的是arr[2]的值3。
2. 以下代码的输出结果是什么?```c#include <stdio.h>int main() {int arr[] = {1, 2, 3, 4, 5};int *ptr = arr;printf("%d\n", *(ptr++));printf("%d\n", *(ptr++));printf("%d\n", *(ptr++));return 0;}```答案:输出结果为1、2、3。
解析:这段代码与上一题的代码非常相似,唯一的区别在于输出语句中对指针的解引用操作使用了括号。
这是因为后置自增运算符的优先级比解引用运算符高,为了保证先解引用再自增,我们需要使用括号来明确优先级。
#include<stdio.h>#include<stdlib.h>/*int days(inty,intm,int d) //计算天数{int days=0,i;int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};if(y%4==0&&y%100!=0||y%400==0) //判断是否为闰年a[1]+=1;if(m==1)return days;else{for(i=0;i<m-1;i++)days+=a[i];days+=d;return days;}}struct date{int year;int month;int day;int days;}a;int main(){printf("enter date:");scanf("%d %d %d",&a.year,&a.month,&a.day);a.days=days(a.year,a.month,a.day);printf("%d年%d月%d日是该年的第%d天\n",a.year,a.month,a.day,a.days); }*//*#define N 10 //第3、4题时N为5,第5题时N为10 struct student{intnum;char name[20];float score[3];float ave; //第3、4、5题共用一个结构体类型}stu[N];*/void print(struct student a[]){inti;printf("学号姓名\t三门课成绩\n");for(i=0;i<N;i++)printf("%ld %s\t%-5.1f %-5.1f %-5.1f\n",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].sc ore[2]);}int main(){inti;printf("请输入%d个学生的信息:学号、姓名、三门课成绩:\n",N);for(i=0;i<N;i++)scanf("%d %s %f %f %f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].sc ore[2]);print(stu);}*//*void print(struct student a[]){inti;printf("学号姓名\t三门课成绩\n");for(i=0;i<N;i++)printf("%ld %s\t%-5.1f %-5.1f %-5.1f\n",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].sc ore[2]);}void input(struct student a[]) //在上一题的基础上编写input函数{inti;printf("请输入%d个学生的信息:学号、姓名、三门课成绩:\n",N);for(i=0;i<N;i++)scanf("%d %s %f %f %f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].sc ore[2]);}int main(){input(stu);print(stu);*//*测试数据10101 wu 70 71 9010102 chen 60 64 9010103 guo 80 78 9010104 lu 80 64 9010105 xu 60 65 9010106 huang 90 78 9010107 chen 70 66 9010108 rong 90 72 9010109 yang 50 63 9010110 zhang 50 71 90*//*int main(){inti,m=0;float average=0;printf("请输入%d个学生的信息:学号、姓名、三门课成绩:\n",N);for(i=0;i<N;i++){scanf("%d %s %f %f %f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].sc ore[2]);stu[i].ave=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;average+=stu[i].ave/N;}for(i=1;i<N;i++)if(stu[i].ave>stu[m].ave)m=i;printf("三门课程总平均成绩为:%5.1f\n成绩最高的学生是:\n学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",average,stu[m].num,stu[m].name,stu[m].score[0],stu[m].score[1],stu[m].score[2],st u[m].ave);}*//*#define N 13 //定义人数struct a{intnum; //原来的序号int count; //报数数目struct a *next;};int main(){inti,j=1;struct a *p1,*p2,b[N];p1=b;for(i=0;i<N;i++){b[i].num=i+1; //定义序号为1到13if(i==N-1)b[i].next=&b[0]; //将最后一个节点的指针变量指向第一个节点elseb[i].next=&b[i+1]; //将每个节点的指针变量指向下一个节点}while(p1->next!=p1) //p1的next成员指向自己时表明只剩最后一个人{p1->count=j; //报数if(j==2)p2=p1; //p2的作用是标记报数为2的人if(j==3){j=1;p2->next=p1->next;//将报数为3的next成员赋值给上一个报数为2的next成员,使之指向下一个报数为1的//成员;}elsej+=1;p1=p1->next; //p1指向下一个count不为3的成员}printf("最后留在圈子的人原来的序号为:%d\n",p1->num);}*//*# define L sizeof(struct student)struct student{longnum;float score;struct student *next;};int n;struct student *creat(void) //生成单向动态链表的函数{struct student *head;struct student *p1,*p2;n=0;p1=p2=malloc(L);scanf("%ld,%f",&p1->num,&p1->score);head=NULL;while(p1->num!=0){n+=1;if(n==1)head=p1;else p2->next=p1;p2=p1;p1=malloc(L);scanf("%ld,%f",&p1->num,&p1->score);}p2->next=NULL;return head;}void print(struct student *head) //输出链表的函数{struct student *p=head;printf("\nnow,these records are:\n");while(p!=NULL){printf("%ld %5.1f\n",p->num,p->score);p=p->next;}}struct student *del1(struct student *head,longnum) //删除指定节点的函数,方法一,指定删除节点的数据{struct student *p1,*p2;p1=head;if(p1->num==num)return head=p1->next;else{while(p1->num!=num){p2=p1;p1=p1->next;}p2->next=p1->next;p1->next=NULL;return head;}}struct student *del2(struct student *head,int n) //方法二,指定删除节点序号{struct student *p1,*p2;inti=1;p1=head;if(i==n)return head=p1->next;else{while(i++<n){p2=p1;p1=p1->next;}p2->next=p1->next;p1->next=NULL;return head;}}int main(){struct student *head;longnum; //int n;head=creat();print(head);printf("\n输入要删除学生的学号:"); //printf("\n输入要删除节点序号:");scanf("%ld",&num); //scanf("%d",&n);head=del1(head,num); //head=del2(head,n);print(head);}*//*# define L sizeof(struct student)struct student{longnum;float score;struct student *next;};int n;struct student *creat(void) //生成单向动态链表的函数{struct student *head;struct student *p1,*p2;n=0;p1=p2=malloc(L);scanf("%ld,%f",&p1->num,&p1->score);head=NULL;while(p1->num!=0){n+=1;if(n==1)head=p1;else p2->next=p1;p2=p1;p1=malloc(L);scanf("%ld,%f",&p1->num,&p1->score);}p2->next=NULL;return head;}void print(struct student *head) //输出链表的函数{struct student *p=head;printf("\nnow,these records are:\n");while(p!=NULL){printf("%ld %5.1f\n",p->num,p->score);p=p->next;}}struct student *del1(struct student *head,longnum)//删除指定节点的函数,num为指定删除节点的数据{struct student *p1,*p2;p1=head;if(p1->num==num)return head=p1->next;else{while(p1->num!=num){p2=p1;p1=p1->next;}p2->next=p1->next;p1->next=NULL;return head;}}struct student *insert(struct student *head,struct student *p,int n) //插入节点的函数,n为新节点序号{struct student *p1,*p2;inti=1;p1=head;if(i==n){p->next=p1;head=p;}else{while(i++<n){p2=p1;p1=p1->next;}p2->next=p;p->next=p1;}return head;}int main(){struct student *head,*p;int n;longnum;head=creat();print(head);printf("\n输入要删除学生的学号:");scanf("%d",&num);head=del(head,num);print(head);p=malloc(L);//为插入的新节点开辟单元,否则p的值不确定printf("\n输入要添加学生的学号,成绩,序号:");scanf("%ld,%f,%d",&p->num,&p->score,&n);head=insert(head,p,n);print(head);}*//*# define L sizeof(struct student)struct student{intnum;float score;struct student *next;};int n;struct student *creat(void) //生成单向动态链表的函数{struct student *head;struct student *p1,*p2;n=0;p1=p2=malloc(L);scanf("%d %f",&p1->num,&p1->score);head=NULL;while(p1->num!=0){n+=1;if(n==1)head=p1;else p2->next=p1;p2=p1;p1=malloc(L);scanf("%d %f",&p1->num,&p1->score);}p2->next=NULL;return head;}void print(struct student *head) //输出链表的函数{struct student *p=head;printf("\nnow,these records are:\n");while(p!=NULL){printf("%d %5.1f\n",p->num,p->score);p=p->next;}}struct student *sort(struct student *head) //建立链表排序函数{struct student *p1,*p2;int t;float s;p1=head;p2=p1->next;while(p1&&p2)//不能写为p1,否则当p1指向最后一个结点时,最后一句循环语句出问题{while(p2){if(p1->num>p2->num){t=p1->num;s=p1->score;p1->num=p2->num;p1->score=p2->score;p2->num=t;p2->score=s;p2=p2->next;}else //不能省略else语句,否则遇到p1->num<p2->num时无限循环p2=p2->next;}p1=p1->next;p2=p1->next;}return head;}struct student *cat(struct student *head1,struct student *head2) //建立链表合并函数{struct student *p,*t;p=head1;while(p){t=p; //循环结束时t将指向a链表的最后一个指针p=p->next;}t->next=head2;return head1;}int main(){struct student *a,*b;printf("输入链表a:\n");a=creat();printf("输入链表b:\n");b=creat();print(sort(cat(a,b)));}*//*测试数据10018 8910016 6410014 8110012 940 010017 6710015 6810013 7610011 850 0*//*# define L sizeof(struct student)struct student{longnum;char name[20];struct student *next;};int n;struct student *creat(void) //生成单向动态链表的函数{struct student *head;struct student *p1,*p2;n=0;p1=p2=malloc(L);scanf("%ld %s",&p1->num,p1->name);head=NULL;while(p1->num!=0){n+=1;if(n==1)head=p1;else p2->next=p1;p2=p1;p1=malloc(L);scanf("%ld %s",&p1->num,p1->name);}p2->next=NULL;return head;}void print(struct student *head) //输出链表的函数{struct student *p=head;printf("\n现在链表a为:\n");while(p!=NULL){printf("%ld %s\n",p->num,p->name);p=p->next;}}struct student *delsame(struct student *a,struct student *b) //从a中删去与b相同学号的节点{struct student *p1,*p2,*p3;longnum;ints,k=0;p3=p1=a;p2=b;while(p1){s=0;num=p1->num;while(p2){if(p2->num!=num)p2=p2->next;else{s=1;break;}}if(s==1)p3->next=p1->next; //若a最后一个相同,p3的指针数据为null else{p3=p1; //每找到一个与b不相等的指针时,p3指向它k+=1; //每找到一个与b不相等的指针时,k累加1if(k==1) //找到第一个与b不相等的指针时,将头指针赋值给aa=p1;}p1=p1->next; //p1指向下一个指针p2=b; //p2重新指向链表b开头}if(k==0) //k=0表明a,b链表相同,返回null return a=NULL;elsereturn a;}int main(){struct student *a,*b;printf("输入链表a:\n");a=creat();printf("输入链表b:\n");b=creat();a=delsame(a,b);print(a);}*//*#define L sizeof(structinf)structinf{longnum;int age;char name[20];char sex;structinf *next;};int n;structinf *creat(void) //生成单向动态链表的函数{structinf *head;structinf *p1,*p2;n=0;p1=p2=malloc(L);scanf("%ld %s %c %d",&p1->num,p1->name,&p1->sex,&p1->age);head=NULL;while(p1->num!=0){n+=1;if(n==1)head=p1;else p2->next=p1;p2=p1;p1=malloc(L);scanf("%ld %s %c %d",&p1->num,p1->name,&p1->sex,&p1->age);}p2->next=NULL;return head;}void print(structinf *head) //输出链表的函数{structinf *p=head;printf("\nnow,theseinf are:\n");while(p!=NULL){printf("%ld %s %c %d\n",p->num,p->name,p->sex,p->age);p=p->next;}}structinf *delage(structinf *head,int a) //删除指定年龄节点的函数{structinf *p1,*p2;p1=head;while(p1->age==a)p1=p1->next;head=p1;p2=p1;while(p1->next!=NULL){if(p1->age==a){p2->next=p1->next;p1=p1->next;}else{p2=p1;p1=p1->next;}}if(p1->age==a)p2->next=NULL;return head;}int main(){structinf *head;int a;head=creat();print(head);printf("\n输入要删除的年龄:");scanf("%d",&a);head=delage(head,a);print(head);}*//*测试数据10009 chen f 2410010 wang m 2810011 li f 2810012 zhao m 2810013 chen f 2410014 wei m 2510015 yang f 2610016 tian m 2810016 tian m 2810017 mei f 2710018 liu m 2810019 chen f 24*/。
一、单选题(每小题10分,共100分,得分90 分)
1、有关宏定义的正确说明是_____。
A、可出现在一行中的任何位置
B、只能放在程序的开头,且每一个宏定义单独占一行
C、可出现在程序的任何位置
D、以#开头的行,可出现在程序的任何位置,通常每一个宏定义只能单独占一行,使用字符“\”可实现一个宏定义占用若干行
你的回答:D (√) 参考答案:D
2、结构是C语言的构造数据类型。
下面定义了一个职工结构employee、结构变量emp1和结构指针变量p:
struct employee{
int num; //职工编号
char name[10]; //职工姓名
}emp1,*p;
p=&emp1;
正确使用结构变量emp1的语句是:
A、scanf("%d%s",&p->num,p->name);
B、scanf("%d%s",&p->num,&p->name);
C、scanf("%d%s",&p.num,);
D、scanf("%d%s",&p.num,&);
你的回答:A (√) 参考答案:A
3、下面定义了一个通信录结构friends_list、结构变量friend1
struct friends_list{
int num; //编号
char name[10]; //姓名
char telephone[13]; //电话
int age; //年龄
}friend1;
正确使用结构变量friend1的语句是
A、="xiaojie";
B、friend1->name="xiaojie";
C、
D、
你的回答:A (√) 参考答案:
4、下面定义了一个通信录结构friends_list、结构变量friend1
struct friends_list{
int num; //编号
char name[10]; //姓名
char telephone[13]; //电话
int age; //年龄
}friend1;
正确使用结构变量friend1的语句是
A、="xiaojie";
B、friend1->name="xiaojie";
C、strcpy(,"xiaojie");
D、strcpy(friend1->name,"xiaojie");
你的回答:C (√) 参考答案:C
5、下面定义了一个通信录结构friends_list、结构变量friend1
struct friends_list{
int num; //编号
char name[10]; //姓名
char telephone[13]; //电话
int age; //年龄
}friend1;
正确使用结构变量friend1的scanf语句是
A、sacnf("%d%s%s%d",&friend1.num,&,&friend1.telephone,&friend1.age);
B、sacnf("%d%s%s%d",&friend1.num,,friend1.telephone,&friend1.age);
C、sacnf("%d%s%s%d",friend1.num,&,&friend1.telephone,friend1.age);
D、sacnf("%d%s%s%d",friend1.num,,friend1.telephone,friend1.age);
你的回答:B (√) 参考答案:B
6、5、下面定义了一个日期结构date、结构变量date1和date2
struct date{
int year;
int month;
int day;
}date1={2000,1,1},date2;
对结构变量date2正确操作的语句是
A、date2=date1;
B、strcpy(date2,date1);
C、date2={2010,12,1};
D、date2={2010-12-1};
你的回答:A (√) 参考答案:A
7、下面定义了一个日期结构struct date
struct date{
int year;
int month;
int day;
}date1;
结构类型占用的内存空间可用sizeof来计算,正确的sizeof格式是
A.sizeof(date1)
B.sizeof(struct date)
C.sizeof(struct date date1)
D.sizeof(date)
A、A正确
B、B正确
C、A和B都正确
D、C和D都正确
你的回答:C (√) 参考答案:C
8、下面定义了一个日期结构struct date
struct date{
int year;
int month;
int day;
}date1;
该结构类型占用的内存空间是多少字节
A、6
B、9
C、12
D、15
你的回答:C (√) 参考答案:C
9、下面定义了一个平面点结构struct point、结构变量point1和结构指针p struct point{
double x;
double y;
}point1,*p=&point1;
欲将点(100,100)赋值给point1,则不正确的操作语句是
A、point1.x=100;point1.y=100;
B、*p.x=100;*p.point1.y=100;
C、(*p).x=100;(*p).point1.y=100;
D、p->x=100;p->y=100;
你的回答:B (√) 参考答案:B
10、关于嵌套结构的定义描述,正确的是:
A、在定义嵌套的结构类型时,必须先定义成员的结构类型,再定义主结构类型
B、在定义嵌套的结构类型时,必须先定义主结构类型,再定义成员的结构类型
C、在定义嵌套的结构类型时,成员的结构类型和主结构类型的先后顺序无关紧要
D、以上描述都不正确
你的回答:A (√) 参考答案:A。