C程序设计 第九章课后答案

合集下载

面向对象程序设计C课后题答案

面向对象程序设计C课后题答案

第一章:面向对象程序设计概述[1_1]什么是面向对象程序设计?面向对象程序设计是一种新型的程序设计范型。

这种范型的主要特征是:程序=对象+消息。

面向对象程序的基本元素是对象,面向对象程序的主要结构特点是:第一:程序一般由类的定义和类的使用两部分组成,在主程序中定义各对象并规定它们之间传递消息的规律。

第二:程序中的一切操作都是通过向对象发送消息来实现的,对象接受到消息后,启动有关方法完成相应的操作。

面向对象程序设计方法模拟人类习惯的解题方法,代表了计算机程序设计新颖的思维方式。

这种方法的提出是软件开发方法的一场革命,是目前解决软件开发面临困难的最有希望、最有前途的方法之一。

[1_2]什么是类?什么是对象?对象与类的关系是什么?在面向对象程序设计中,对象是描述其属性的数据以及对这些数据施加的一组操作封装在一起构成的统一体。

对象可以认为是:数据+操作在面向对象程序设计中,类就是具有相同的数据和相同的操作的一组对象的集合,也就是说,类是对具有相同数据结构和相同操作的一类对象的描述。

类和对象之间的关系是抽象和具体的关系。

类是多个对象进行综合抽象的结果,一个对象是类的一个实例。

在面向对象程序设计中,总是先声明类,再由类生成对象。

类是建立对象的“摸板”,按照这个摸板所建立的一个个具体的对象,就是类的实际例子,通常称为实例。

[1_3]现实世界中的对象有哪些特征?请举例说明。

对象是现实世界中的一个实体,其具有以下一些特征:(1)每一个对象必须有一个名字以区别于其他对象。

(2)需要用属性来描述它的某些特性。

(3)有一组操作,每一个操作决定了对象的一种行为。

(4)对象的操作可以分为两类:一类是自身所承受的操作,一类是施加于其他对象的操作。

例如:雇员刘名是一个对象对象名:刘名对象的属性:年龄:36 生日:1966.10.1 工资:2000 部门:人事部对象的操作:吃饭开车[1_4]什么是消息?消息具有什么性质?在面向对象程序设计中,一个对象向另一个对象发出的请求被称为“消息”。

《C语言程序设计教程》第三版课后习题参考答案

《C语言程序设计教程》第三版课后习题参考答案

《C语言程序设计教程》第三版课后习题参考答案C语言程序设计教程第三版课后习题参考答案第一章:C语言概述1.1 C语言的特点答案:C语言是一种通用的、面向过程的程序设计语言,具有高效、简洁、灵活等特点。

它提供了丰富的程序设计元素和功能,适用于各种不同的应用领域。

1.2 C语言程序的基本结构答案:C语言程序由预处理指令、函数声明、函数定义、变量声明和语句组成。

其中,预处理指令用来引入头文件或定义宏,函数声明用来声明函数的名称和参数,函数定义用来实现函数的功能,变量声明用来声明变量的类型和名称,语句用来表达具体的计算过程。

1.3 C语言的数据类型答案:C语言提供了多种数据类型,包括基本类型(整型、浮点型、字符型等)和派生类型(数组、指针、结构体等)。

每种数据类型在内存中占据一定的存储空间,并具有特定的取值范围和操作规则。

1.4 C语言的运算符和表达式答案:C语言支持各种运算符和表达式,例如算术运算符(+、-、*、/等)、关系运算符(>、<、==等)、逻辑运算符(&&、||、!等)等。

通过运算符和表达式可以进行各种数值计算和逻辑判断。

第二章:基本数据类型与运算2.1 整型数据类型答案:C语言提供了不同长度的整型数据类型,包括有符号整型(int、long等)和无符号整型(unsigned int、unsigned long等)。

整型数据类型可以表示整数值,并具有不同的取值范围。

2.2 浮点型数据类型答案:C语言提供了浮点型数据类型(float、double等),用来表示带小数部分的实数值。

浮点型数据可以表示较大或较小的数值,并具有一定的精度。

2.3 字符型数据类型答案:C语言提供了字符型数据类型(char),用来表示单个字符。

字符型数据可以用于表示各种字符(包括字母、数字、符号等)。

2.4 布尔型数据类型答案:C语言不直接支持布尔型数据类型,但可以使用整型数据类型来表示布尔值(0表示假、非零表示真)。

C++课本课后习题09答案

C++课本课后习题09答案

习题9答案1.解答1:#include<iostream.h>void swap( int *pa, int *pb, int *pc){int t;if(*pa>*pb){ t=*pa; *pa=*pb; *pb=t; }if(*pa>*pc){ t=*pa; *pa=*pc; *pc=t; }if(*pb>*pc){ t=*pb; *pb=*pc; *pc=t; }}void main(void){int a, b, c;cin>>a>>b>>c;swap(&a,&b,&c);cout<<a<<'\t'<<b<<'\t'<<c<<'\n';}解答2:#include<iostream.h>void swap( int *m, int *n){int t;t=*m; *m=*n; *n=t;}void swap( int *pa, int *pb, int *pc){if(*pa>*pb) swap(pa, pb);if(*pa>*pc) swap(pa, pc);if(*pb>*pc) swap(pb, pc);}void main(void){int a, b, c;cin>>a>>b>>c;swap(&a,&b,&c);cout<<a<<'\t'<<b<<'\t'<<c<<'\n';}2.解答①(指针做参数):#include <iostream.h>void stat(char *str, int *letter, int *digit, int *other){char c;for( ; *str; str++)c=*str;if(('a'<=c&&c<='z')||('A'<=c&&c<='Z'))(*letter)++;else if('0'<=c && c<='9')(*digit)++;else(*other)++;}}void main( ){char str[100];int letter=0, digit=0, other=0; //变量letter用于统计字母的个数cin.getline(str,100); //变量digit用于统计数字字符的个数stat(str, &letter, &digit, &other); //变量other用于统计其他字符的个数cout<<"letter="<<letter<<endl;cout<<" digit="<<digit<<endl;cout<<" other="<<other<<endl;}程序的一次运行过程如下:She has 5 apples. <回车>letter=12digit=1other=4解答②(引用做参数):#include <iostream.h>void stat(char *str, int &letter, int &digit, int &other){char c;for( ; *str; str++){c=*str;if(('a'<=c&&c<='z')||('A'<=c&&c<='Z'))letter++;else if('0'<=c&&c<='9')digit++;elseother++;}}void main( ){char str[100];int letter=0, digit=0, other=0;cin.getline(str,100);stat(str, letter, digit, other);cout<<"letter="<<letter<<endl;cout<<" digit="<<digit<<endl;cout<<" other="<<other<<endl;}3.#include <iostream.h>void output(int *p, int n){for(int i=0; i<n; i++)cout<<*p++<<'\t';cout<<'\n';}void change(int a[10], int n){int t,*pend,*p,*maxp,*minp;minp=a;pend=a+n;for(p=a+1; p<pend; p++)if(*p<*minp) minp=p;if(minp!=a){t=*minp; *minp=*a; *a=t;}maxp=a;for(p=a+1;p<pend;p++)if(*p>*maxp) maxp=p;if(maxp!=a+n-1){t=*maxp; *maxp=*(a+n-1); *(a+n-1)=t;}}void main (){int a[10],n,i;cin>>n;for(i=0; i<n; i++)cin>> *(a+i);output(a,n); // 输出原数组change(a,n);output(a,n); // 输出调换后的数组}4.解答:#include <iostream.h>void output(int *p, int n){for(int i=0; i<n; i++)cout<<*p++<<'\t';cout<<'\n';}void right_move(int a[10], int n, int k){int *p1, *p2, *tp;tp=new int[k];p1=a+n-k;for(int i=0; i<k; i++) //将数组右侧k个数复制到临时数组中{*tp=*p1;tp++;p1++;}p1=a+n-1;p2=a+n-1-k;for(i=0; i<n-k; i++) //将数组前n-k个数移到数组最右侧{*p1 = *p2;p1--; p2--;}tp--;for(i=0; i<k; i++) //将临时数组中的k个元素复制到数组最前面{*p1 = *tp;p1--; tp--;}tp++;delete [] tp;}void main (){int a[10],k,i;cout<<"Please input 10 elements: ";for(i=0; i<10; i++)cin>> *(a+i);cout<<"Please input k: ";cin>>k;if(k<0)k=10+k;output(a,10); // 输出原数组right_move(a,10, k);output(a,10); // 输出调换后的数组}5.解答:#include <iostream.h>void select_odd_index(char *str1, char *str2){str1++;while(*str1){*str2 = *str1;str2++;str1++;if(*str1=='\0')break;str1++;}*str2='\0';}void main (){char str1[100],str2[50];cout<<"Please input a string: ";cin.getline(str1,100);select_odd_index(str1, str2);cout<<str2<<endl;}6.解答:#include <iostream.h>#include <string.h>void select_max(char *p1, char *p2){char temp[100];int maxlen=0,len;while(*p1){while(*p1==' ') p1++;len=0;while( *p1!=' ' && *p1!='\0' ){*(temp+len)=*p1;len++;p1++;}*(temp+len)='\0';if(len>maxlen){maxlen=len;strcpy(p2,temp);}}}void main (){char main_str[100],sub_str[50];cout<<"Please input main string: ";cin.getline(main_str,100);select_max(main_str, sub_str);cout<<sub_str<<endl;}7.解答:#include <iostream.h>int find(int a[ ][5], int *rowp, int *colp){int i, j, flag, max, maxj;for(i=0; i<4; i++){max=a[i][0];maxj=0;for(j=1; j<5; j++) // 找出第i行中最大值,并记下该最大值所在的列号。

c程序设计第四版(谭浩强)第九章答案

c程序设计第四版(谭浩强)第九章答案

#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*/。

昆明理工大学C语言程序设计课后习题答案

昆明理工大学C语言程序设计课后习题答案

昆明理工大学C语言程序设计课后习题答案第1章认识C语言(一)、是非题1.程序是指挥计算机进行各种信息处理任务的一组指令序列。

A.对B.错2.机器语言与硬件平台相关,但汇编语言和硬件平台无关。

A.对B.错3.编译型高级语言明显优于解释型高级语言。

A.对B.错4.C语言把高级语言的基本结构和低级语言的实用性紧密结合起来,不仅适合编写应用软件,而且适于编写系统软件。

A.对B.错5.面向对象的程序设计方法明显优于面向过程的程序设计方法。

A.对B.错6.计算机算法要有一个明确的起点和确定的步骤序列。

A.对B.错7.main函数是C程序的入口,由计算机系统负责调用。

A.对B.错8.在C语言中,同一行上可以写一条或多条语句,但一条语句不能写在多行上。

A.对B.错9.在C语言中,扩展名为.h的文件称为头文件,常用于组织C标准函数库中的函数。

A.对B.错10.注释语句会增加编译结果的复杂性,因此要尽量减少注释语句的数量。

A.对B.错11.声明变量的含义是通知编译系统为变量分配合适大小的存储空间。

A.对B.错12.C语言不允许使用关键字作为变量的名称,但可以使用保留字为变量命名。

A.对B.错13.符号常量提高了程序的可读性,但降低了程序维护的方便性。

A.对B.错14.变量声明把一个具体的标识符名称和计算机内存中的一个特殊的位置联系起来,同时确定了该位置存储的信息类型。

A.对B.错15. C语言允许在同一条语句中定义多个相同类型的变量,其间用分号进行分隔。

A.对B.错16.在ISO/ANSI 1999规定中,允许将变量声明放在程序中的任何位置。

A.对B.错17.不同类型的数据在内存中所占存储单元的大小不同,内部存储方式不同,取值范围不同,甚至能够参与的运算种类也不相同。

A.对B.错18.有符号和无符号整数的区别仅在于对数据最高位的解释不同。

若最高位解释为数据位,则为signed类型;若最高位解释为符号位,则为unsigned类型。

C程序设计(谭浩强)课后习题答案选讲9

C程序设计(谭浩强)课后习题答案选讲9

偷懒做法:不对原数组进行排 序,重新建立一个数组,每次 从原数组中挑选最大的值,存 入新数组,并将原数组中相应 值置0. 注意:小数据量可以,大数据 量时,务必用排序算法。
//保存数据 FILE* fp_stu_sort = fopen("stu_sort.txt", "w"); if (!fp_stu_sort) { printf("can not open stu_sort.txt\n"); } for (int i = 0; i < STUDENT_NUM; i++) { fprintf(fp_stu_sort, "%s\t%s\t", stu3[i].number, stu3[i].name); for (int j = 0; j < COURSE_NUM; j++) { fprintf(fp_stu_sort, "%f\t", stu3[i].course[j]); } fprintf(fp_stu_sort, "%f\n", stu3[i].mean); } fclose(fp_stu_sort);
//创建文件 employee emp = {"John", "001", "male", 27, "科大西区", 10000, "good", "Ph.D."}; FILE* employee_data = fopen("employee_data.txt", "w"); if (!employee_data) { printf("can not open employee_data.txt\n"); } fprintf(employee_data, "%s\n%s\n%s\n%d\n%s\n%d\n%s\n%s\n", , emp.number, emp.sex, emp.age, emp.address, emp.salary, emp.health, emp.culture); fclose(employee_data);

C程序设计(第三版)习题答案(9、11章) 谭浩强著

C程序设计(第三版)习题答案(9、11章) 谭浩强著
printf("The best student is %s,sum=%d\n",stu[maxi].name,max);
}
printf("value=%format\t",x1);printf("value=%format\t",x2);putchar('\n');
输出结果:
value=5.000000ormat value=5.000000ormat
value=3.000000ormat value=8.000000ormat
break;
case 3:scanf("%s",s);
STRING(s);
break;
default:printf("error");
}
}
9.8main()
{int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
main()
{int a,b;
scanf("%d,%d",&a,&b);
printf("%d",SURPLUS(a,b));
}
9.3#include"math.h"
#define S(a,b,c) ((a+b+c)/2)
#define AREA(a,b,c) (sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)))
scanf("%d",&stu.score[j]);

C++程序设计基础课后答案 第九章

C++程序设计基础课后答案  第九章

9.1 阅读下列程序,写出执行结果1. #include <iostream.h>template <typename T>void fun( T &x, T &y ){ T temp;temp = x; x = y; y = temp;}void main(){ int i , j;i = 10; j = 20;fun( i, j );cout << "i = " << i << '\t' << "j = " << j << endl;double a , b;a = 1.1;b = 2.2;fun( a, b );cout << "a = " << a << '\t' << "b = " << b << endl;}2. #include <iostream.h>template <typename T>class Base{ public:Base( T i , T j ) { x = i; y = j; }T sum() { return x + y; }private:T x , y;} ;void main(){ Base<double> obj2(3.3,5.5);cout << obj2.sum() << endl;Base<int> obj1(3,5);cout << obj1.sum() << endl;}9.2 思考题1.抽象类和模板都是提供抽象的机制,请分析它们的区别和应用场合。

2.类属参数可以实现类型转换吗?如果不行,应该如何处理?3.类模板能够声明什么形式的友员?当类模板的友员是函数模板时,它们可以定义不同形式的类属参数吗?请你写个验证程序试一试。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C程序设计谭浩强第九章课后答案
1
#include<stdio.h>
struct Days
{int year;
int month;
int day;
};
int main()
{
intmds[12]={31,28,31,30,31,30,31,31,30,31,30,31},i,d;
struct Days a;
printf("请输入年、月、日\n");
scanf("%d,%d,%d",&a.year,&a.month,&a.day);
d=a.day;
for(i=0;i<a.month-1;i++)
d+=mds[i];
if((a.year%4==0&&a.year%100!=0||a.year%400==0)&&a.month>2) d++;
printf("%d月%d日是%d年的第%d天\n",a.month,a.day,a.year,d);
return 0;
}
2
#include<stdio.h>
struct Days
{int year;
int month;
int day;
};
int main()
{
int days(struct Days a);
struct Days a;
printf("请输入年、月、日\n");
scanf("%d,%d,%d",&a.year,&a.month,&a.day);
printf("%d月%d日是%d年的第%d天\n",a.month,a.day,a.year,days(a));
return 0;
}
int days(struct Days a)
{
intmds[12]={31,28,31,30,31,30,31,31,30,31,30,31},i,d;
d=a.day;
for(i=0;i<a.month-1;i++)
d+=mds[i];
if((a.year%4==0&&a.year%100!=0||a.year%400==0)&&a.month>2)
d++;
return d;
}
3
#include<stdio.h>
struct Student
{intnum;
char name[20];
int score[3];
};
int main()
{
void print(struct Student a[]);
struct Student a[5];
inti,j;
printf("请依次输入5名学生的学号:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i].num);
printf("请依次输入5名学生的姓名:\n");
for(i=0;i<5;i++)
scanf("%s",a[i].name);
printf("请依次输入5名学生的成绩:\n");
for(i=0;i<5;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i].score[j]);
print(a);
return 0;
}
void print(struct Student a[])
{
struct Student *p=a;
printf("学号姓名语文数学英语\n");
for(;p<a+5;p++)
printf("00%d %-14s %4d %4d %4d\n",p->num,p->name,p->score[0],p->score[1],p->score[2]) ;
}
4
#include<stdio.h>
struct Student
{intnum;
char name[20];
int score[3];
};
int main()
{
void print(struct Student a[]);
void input(struct Student a[]);
struct Student a[5];
input(a);
print(a);
return 0;
}
void input(struct Student a[])
{
inti,j;
printf("请依次输入5名学生的学号:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i].num);
printf("请依次输入5名学生的姓名:\n");
for(i=0;i<5;i++)
scanf("%s",a[i].name);
printf("请依次输入5名学生的成绩:\n");
for(i=0;i<5;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i].score[j]);
}
void print(struct Student a[])
{
struct Student *p=a;
printf("学号姓名语文数学英语\n");
for(;p<a+5;p++)
printf("00%d %-14s %4d %4d %4d\n",p->num,p->name,p->score[0],p->score[1],p->score[2]) ;
}
#include<stdio.h>
struct Student
{intnum;
char name[20];
int score[3];
};
int main()
{
void print(struct Student a[]);
void input(struct Student a[]);
struct Student a[10];
input(a);
print(a);
return 0;
}
void input(struct Student a[])
{
inti,j;
printf("请依次输入10名学生的学号:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i].num);
printf("请依次输入10名学生的姓名:\n");
for(i=0;i<10;i++)
scanf("%s",a[i].name);
printf("请依次输入10名学生的成绩:\n");
for(i=0;i<10;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i].score[j]);
}
void print(struct Student *p,av)
{
struct Student *p=a;
printf("学号姓名语文数学英语平均成绩\n");
printf("00%d %-14s %4d %4d %4d %4d\n",p->num,p->name,p->score[0],p->score[1],p->scor e[2],av);
}。

相关文档
最新文档