C语言第十一章习题答案
C语言程序设计(第三版)谭浩强著 各章习题答疑

以八、 /* 以八、十六进制输出 */
} ???? 是对应的带符号短整数。 输出结果是八进制整数 十六进制整数
2
3.9 计算某个表达式的值。 通用程序清单如下: stdio.h” #include ”stdio.h stdio.h void main() { ?????? x; 计算表达式值存入x x=( 表达式 );/* 计算表达式值存入x */ printf(”% printf( %?\n”,x); ,x); } ?????? 表达式对应的数据类型符(需事先判断) ? 表达式为整型,则为d 表达式为实型,则为f 3
第七章习题答疑(二)
折半查找法在排序数组中查找某个数。 上课时已有提示。 7.13 字符串连接(b[]中字符串连接到a[]中字符串后) 设:a[10]=”12 ,b[]=”abc 12”, abc” 12 abc (1)找到a串的’\0’的下标(i=2) ’ (2)j=0; 当型循环(b[j]!=’\0’) ’\ ’ a[i]=b[j],i++,j++ (3)a[i]=’\0’ \ 7.15 字符串复制(b[]中字符串复制到a[]中) 设:a[10]=”12”,b[]=”abc” (1)i=0; 当型循环(b[i]!=’\0’) a[i]=b[i],i++ (2)a[i]=’\0’ 7.9
18
第十章习题答疑(一)
输入3个整数,按照由小到大顺序输出。 int x,y,z,*px=&x,*py=&y,*pz=&x; 用以前的程序,其中的x、y、z换成*px、*py、*pz 10.3 输入10个整数存入一维数组a[10],将其中最小数与第1 个数交换,最大数与最后一个数交换。 要求编3个函数: 10 10 输入10个整数、交换、输出交换后的10个整数。 输入函数: void in(int *p,int n) 输出函数: void out(int *p,int n) 交换函数: void change(int *p,int n) 其中寻找最大最小数的方法: int *q,*max,*min; for (max=min=p,q=p+1;q<p+n;q++) { if(*max<*q) max=q; if(*min>*q) min=q; } 19 交换的方法:*p和*min,*(p+n-1)和*max 10.1
第11章c语言课后答案

2: /*打开文件,关闭文件的含义是什么?为什么要打开关闭文件?*/
答: 打开文件的含义:要把文件使用方式写入FILE结构体变量中;返回一个FILE结构体指针,指向该文件的FILE结构
体变量,通过该文件指针可以对文件进行操作。
关闭文件的含义:文件指针不再指向原来指向的文件。
为什么要打开文件:(1)要确定文件的使用方式;(2)要得到一个指向文件的指针;(3)还需检测文件是否存在,
磁盘是否出故障等。
为什么要关闭文件:(1)防止文件被误用;(2)具体系统中同时打开的文件的数目是有限的。
{ k=i;
for(j=i+1;j<n;j++)
if(stu1[i].ave<stu1[j].ave)k=j;
if(k!=i)
{ change=stu1[i];stu1[i]=stu1[j];stu1[j]=change;}
}
fp=fopen("stu2","wb");
exit(1);
}
printf("\nInput char: ");
while((str=fgetc(stdin))!='#')
fputc(str,fp);
fclose(fp);
fp=fopen("text.txt","r");
while((str=fgetc(fp))!=EOF)
for(i=0;i<n;i++)
{ printf("Input the message of %d student\n",i+1);
C语言程序设计习题集(有答案)

高等教育自学考试C语言程序设计(二)课程代号(05772)同步练习题班级学号姓名二O 一六年三月目录第一章C语言概述第二章基本数据类型第三章控制结构第四章运算符和表达式第五章函数第六章数组第七章字符与字符串第八章变量类别与编译预处第九章指针第十章结构体、共用体和枚举类型第十一章文件第一章C语言概述一、单项选择题1.在计算机上可以直接运行的程序是()。
A.高级语言程序B.汇编语言程序C.机器语言程序D.C语言程序2. 一个C语言程序是由()A.若干函数组成B.若干过程组成C.若干主程序组成D.若干子程序组成3. C语言不具有的特点是()A.具有结构化的控制语句B.数据类型丰富C.语法限制不太严格,程序设计自由度大D.在可移植性上,C语言比其他语言差4. 以下叙述不正确的是()A.一个C语言程序可由一个或多个函数组成B.一个C语言程序必须包含一个主函数C.C语言程序的基本组成单位是函数D.在C语言程序中,注释说明只能位于一条语句的后面5. 以下叙述正确的是()A.C语言比其他语言高级B.C语言可以不用编译就能被计算机识别和执行C.C语言以接近英语国家的自然语言和数学语言作为语言的表达形式D.C语言出现的最晚,所以具有其他语言的一切优点二、填空1. 计算机语言的发展经过了、和等阶段。
2. C语言既适合编写,也适合编写应用软件。
标准答案一、单项选择题CADDC二、填空1. 机器语言,汇编语言,高级语言2. 系统软件第二章基本数据类型一、单项选择题1.正确的C语言用户自定义标识符是()A.printB.floatC.when?D.random%22.属于C语言基本数据类型的是( )A.指针型B.无符号整型C.数组型D.结构型3.C语言的长整型数值在计算机中占用的字节个数是( )A.1B.2C.3D.44.C语言中,″\x3d″在内存中占用的字节数是( )A.1B.2C.4D.55.下列关于C语言的叙述错误的是( )A. 英文字母大小写不加以区分B. 不同类型的变量可以出现在同一个表达式中C. 在赋值表达式中赋值号两边的类型可以不同D. 某些运算符在不同的场合可以有不同的含义6.下列转义字符中错误的是( )A. ′\000′B. ′\14′C. ′\x111′D. ′\2′7.下列标识符中,不是C语言保留字的是( )A. charB. whileC. minD. default8.下列保留字中用于构成循环结构的是()A.ifB.whileC.switchD.default9. 数据-324在二进制文件和文本文件中所占的字节数分别是()A.2,2B.2,4C.4,2D.4,410. 请选出可以作为C语言用户标识符的一组标识符()A.void, define, WORD B.A3_B3, _123, abcC.FOR, -abc, Case D.2a, Do, Sizeof11.下列运算符优先级最高的是()A.关系运算符B.赋值运算符C.算术运算符D.逻辑运算符12. sizeof(float)是()A.一种函数调用B.一个不合法的表示形式C.一个整型表达式D.一个浮点表达式13. 下列叙述不正确的是()A.一个C语言程序可由一个或多个函数组成B.一个C语言程序必须包含一个main函数C.C语言程序的基本组成单位是函数D.在C语言程序中,注释说明只能位于一条语句的后面14. 编译C语言程序时,程序中的注释部分将()A.不参加编译,也不会出现在目标程序中B.参加编译,但不会出现在目标程序中C.不参加编译,但会出现在目标程序中D.参加编译,并会出现在目标程序中15. 下列字符串常量不正确的是()A.'abc' B."12'12" C."0" D." "16. 下列4个选项中,均是合法整型常量的是()A.160 -0xffff 011B.-0xcdf 01ª0xeC.-01 986,012 0668D.-0x48a 2e5 0x17. 以下选项中不属于C语言类型的是()A.signed short int B.unsigned long intC.unsigned int D.long short18. 数值029是一个()A.八进制数B.十六进制数C.十进制数D.非法数19. 在C语言中,要求运算数必须是整型的运算符是()A./ B.++ C.!=D.%20. 当c的值不为0时,以下能将c的值赋给变量a,b 的是()A.c=b=a B.(a=c) || (b=c)C.(a=c) && (b=c) D.a=c=b二、填空1. 表示空类型的保留字是_____________。
第11章谭浩强C习题及解答

class Student1: protected Student {public:
void get_value1( ) { cout<<"please input num, name, sex, age, addr: "<<endl; cin>>num>>name>>sex>>age>>addr; } void display_1( ) { display( ); cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl; }
void f4( ); private:
int p; }; int main( ) { A a1;
B b1; C c1; return 0; }
//C为B的公用派生类
//a1是基类A的对象 //b1是派生类B的对象 //c1是派生类C的对象
问: (1) 在main函数中能否用b1.i, b1.j 和 b1.k引用 派生类B对象b1中基类A的成员? (2) 派生类B中的成员函数能否调用基类A中的 成员函数f1和f2? (3) 派生类B中的成员函数能否引用基类A中的 数据成员i, j, k ?
#include <iostream> #include <string> using namespace std; class Student {public: void get_value( )
{ cout<<"please input num,name,sex: "; cin>>num>>name>>sex; } void display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } private : int num; string name; char sex; };
C语言 习题十一 参考答案

3.已有一个存放数千种仓库物质信息的文件 CK,每个信息元素含两个内容:物质编号 kno 和库存量 KNOM。请编程通过检查全库物质的库存量,建立一个新的文件 XK,它包含所有库存量大于 100 的物质 的编号和库存量。 参考程序: /*p360_3.c*/ #include "stdio.h" main() {int n,m;
{scanf("%d%s",&n,xm); for(j=0;j<5;j++) scanf("%d",&sc[i][j]); }
fp=fopen("abc.txt","w"); for(i=0;i<N;i++)
fprintf(fp,"%5d%s%4d%4d%4d%4d%4d\n",n,xm,sc[i][0],sc[i][1],sc[i][2],sc[i][3],sc[i][4]); fclose(fp); } /*p360_1a.c*/ #include "stdio.h" main() {FILE *fp; int num,mark[5],i,sum,n=0; float av,t[5]={0}; char name[20]; if((fp=fopen("abd.txt","w"))==NULL)
FILE *fp1,*fp2; fp1=fopen("ck.txt","r"); fp2=fopen("xk.txt","w"); while(feof(fp1)==0) {fscanf(fp1,"%d,%d",&n,&m);
CPrimer 第11章泛型算法课后习题答案

第11章-泛型算法1.algorithm头文件定义了一个名为count的函数,其功能类似于find。
这个函数使用一对迭代器和一个值做参数,返回这个值出现的次数的统计结果。
编写程序读取一系列int型数据,并将它们存储到vector对象中然后统计某个指定的值出现了多少次。
// 11.17_11.1_int_to_vector_count.cpp : 定义控制台应用程序的入口点。
//#include"stdafx.h"#include<vector>#include<iostream>#include<algorithm>using namespace std;int _tmain(int argc, _TCHAR* argv[]){cout << "\tInput some int numbers ( ctrl + z to end):\n\t ";vector<int> iVec;int iVal;while ( cin >> iVal )iVec.push_back( iVal );cout << "\n\tInput a num to search in the iVec: ";cin.clear();cin >> iVal;int iCnt = 0;if ( iCnt = count( iVec.begin(), iVec.end(), iVal )){cout << "\n\tThe value " << iVal << " occurs " << iCnt << " times." << endl;}system("pause");return 0;}2.重复前面的程序,但是,将读入的值存储到一个string类型的list对象中。
《C语言程序设计教程》(第三版)课后习题参考答案(精选5篇)[修改版]
![《C语言程序设计教程》(第三版)课后习题参考答案(精选5篇)[修改版]](https://img.taocdn.com/s3/m/893b22c5a32d7375a517800b.png)
第一篇:《C语言程序设计教程》(第三版)课后习题参考答案C语言程序设计课后习题参考答案习题一一、单项选择题1、C2、B3、B4、C5、D6、A7、C8、A二、填空题1、判断条件2、面向过程编程3、结构化4、程序5、面向对象方法6、基本功能操作、控制结构7、有穷性8、直到型循环结构9、算法10、可读性11、模块化12、对问题的分解和模块的划分习题二一、单项选择题1、B2、D3、C4、B5、A二、填空题1、主2、C编译系统3、结构化4、程序5、面向对象方法6、.OBJ7、库函数8、直到型循环结构习题三一、单项选择题1、D2、B3、A4、C9、B10、C11、A12、D13、C17、B18、C19、C20、D21、A25、D26、A二、填空题1、补码2、10^-138~10^1 38、15~166、A7、B6、D7、B14、B15、C22、D8、C8、D16、A24、D、A3、实4、单目运算符、自右向左5、函数调用6、65,89习题四一、单项选择题1、D2、C3、D4、A5、D6、B7、A8、C9、B10、B二、填空题1、两, ;2、5.1690003、-200 2500、i=-200,j=2500回车、i=-200回车j=2500回车4、a=98,b=765.000000,c=4321.0000005、100 25.81 1.89234、100,25.81,1.89234、100回车25.81回车1.89234回车6、0,0,37、38、scanf(“%lf %lf %lf”,&a,&b,&c);9、13 13.000000 13.00000010、c=b-a;a=b+c;习题五一、单项选择题1、B2、D3、C4、B5、B6、D7、A8、B二、填空题1、1 、02、k!=03、if(x>4||xelse printf(“error!”); 4、if(((x>=1&&x=200&&x Printf(“%d”,x);5、16、17、10! Right!8、a=09、2,110、0习题六一、单项选择题9、D1、B2、C3、C4、B5、C6、B7、C8、A二、填空题1、无穷次2、83、205、3.66、*#*#*#$7、828、d=1.0 、k++、k9、!(x习题七一、单项选择题1、B2、D3、C4、C5、A二、填空题1、1 2 4 8 16 32 64 128 256 5122、a[age]++、i=18;i3、break、i==84、a[i]>b[i]、i3、j5、b[j]=0、b[j]=a[j][k]习题八一、单项选择题1、B3、C4、A5、A9、D10、B11、A12、C13、A二、填空题1、return1、return n+sum(n-1)2、return1、n*facto(n-1)习题九一、单项选择题1、D2、C3、D4、A5、C9、B10、C11、A13、B17、C18、A19、B20、C二、填空题1、int 、return z2、*p++3、’\0’、++4、p、max*q6、A7、D6、A7、C14、C15、B6、C7、C14、A15、D8、A8、D8、C习题十一、单项选择题1、D2、D3、A4、5、B6、A7、C8、B9、D10、11、C12、D13、D14、C二、填空题1、34 122、ARRAY a[10],b[10],c[10];3、2 34、ab、cd5、(*b).day、b->day6、adghi mnohi no7、(struct node *)、!=’\n’、p=top;8、p1=p1->next9、(struct list *)、(struct list *)、return (n)习题十一一、单项选择题1、A2、A3、B4、A5、B9、A10、B11、B12、B13、C17、D二、填空题1、ASCII(文本)、二进制2、pf=fopen(“A:\zk04\data xfile.dat”,”w”);3、fputc()、fputs()、fscanf()、fread()4、(后两个空)文件结束符、非0值7、B14、C15、D8、A16、A第二篇:C语言程序设计教程课后习题参考答案《C语言程序设计教程》课后习题参考答案习题1 1. (1)编译、链接.exe (2)函数主函数(或main函数)(3)编辑编译链接2.(1)-(5):DDBBC (6)-(10):ABBBC 3.(1)答:C语言简洁、紧凑,使用方便、灵活;C语言是高级语言,同时具备了低级语言的特征;C 语言是结构化程序设计语言,具有结构化的程序控制语句;C语言有各种各样的数据类型;C语言可移植性好;生成目标代码质量高,程序执行效率高。
C语言程序设计第九-十一章习题参考答案

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程序设计(第三版)习题答案(11章) 谭浩强著(4)_官田

for(j=0;j<3;j++)
printf("%7d",stu[i].score[j]);
printf("%6.2f\n",stu[i].avr);
}
11.2
struct dt
{int year;
int month;
int day;
}date;
main()
{
scanf("%d,%d,%d",&date.year,&date.month,&date.day);
case 5:days=date.day+120;break;
case 6:days=date.day+151;break;
case 7:days=date.day+181;break;
case 8:days=date.day+212;break;
11.1
struct
{int year;
int month;
int day;
}date;
main()
{int days;
scanf("%d,%d,%d",&date.year,&date.month,&date.day);
取消回复score\n");
scanf("%d",&stu[i].score[j]);
}
}
average=0;
max=0;
计算机二级C语言字符串习题答案

21 C
解析: char *s1=”abc”,即定义了一个指针变量s1,并且s1指向的是这个字符串的第一个元素,即指向了字符‘a’。同样的s2指向了字符‘1’。swap函数中的语句的功能是交换变量*x和*y两个变量的值,则最后输出的结果为s1所指的字符串的内容为:1bc,s2所指的字符串的内容为:a23。
2)j=2; p[i][j]!=’\0’,真值,(p[i][j]-‘0’)%2即:(‘9’-‘0’)%2为真值,num=10*3+9=39;j=j+2=5;
3) j=5; p[i][j]!=’\0’,假值。退出该循环语句。
7)i=6;i<7;p[i][0]==’T’为假值,n=2;i++。变量i的值为7;
8)i=7;i<7;假值。退出循环。
所以最后输出的是fun(str)即返回的就是n的值,即2.
23 A
解析:fun1函数中的(*q)++将指针q所指向的字符进行一次自加运算,所以输出结果为A.
p=&a[3],即变量p得到的是字符‘g’的首地址。
1) i=0,i<4真值,执行内循环。\
j=1;j<5真值,执行后的结果为p[0]=”aabdfg”,p[1]=”abc”.j++后,j=2;
j=2;j<5 真值,执行后的结果为p[0]= ”aabdfg”,p[2]=”abbd”, j++后,j=3;
(*q)++代表的是,先计算出*q的值,然后计算变量的自加。执行循环:
1)*q!=’\0’为真值,(*q)++得到的结果为h,q++,指向了字符‘r’.
谭浩强c语言程序设计习题集

目录重要次重要重复或欲删除基础第一章 C语言概述1.1 选择题1.2 填空题第二章数据类型、运算符与表达式2.1 选择题2.2 填空题第三章最简单的C程序设计3.1 选择题3.2 填空题第四章逻辑运算和判断选取控制4.1 选择题4.2 填空题4.3 编程题第五章循环控制5.1 选择题5.2 填空题5.3 编程题第六章数组6.1 选择题6.2 填空题6.3 编程题第七章函数7.1 选择题7.2 填空题7.3 编程题第八章编译预处理8.1 选择题8.2 填空题8.3 编程题第九章指针9.1 选择题9.2 填空题9.3 编程题第十章结构体和共用体10.1 选择题10.2 填空题10.3 编程题第十一章位运算11.1 选择题11.2 填空题11.3 编程题第十二章文件12.1 选择题12.2 填空题12.3 编程题第一章 C语言概述1.1 选择题*1.1一个C程序的执行是从。
A)本程序的main函数开始,到main函数结束B)本程序文件的第一个函数开始,到本程序文件的最后一个函数结束C)本程序的main函数开始,到本程序文件的最后一个函数结束D)本程序文件的第一个函数开始,到本程序main函数结束参考答案:A参考分析:C语言总是从main函数开始,main函数结束。
但是C语言中存在一个exit(0)函数,它可以使得程序在任何时候、任何位置结束程序的运行。
如果不考虑exit(0)等函数的特殊作用,C则总是在main函数结束。
*1.2以下叙述正确的是。
A)在C程序中,main函数必须位于程序的最前面B)在C程序的每一行只能写一条语句C)C语言本身没有输入输出语句D)在对一个C程序进行编译的过程中,可发现注释中的拼写错误参考答案:C参考分析:C程序对main函数的位置没有任何要求;其书写格式自由,一行可以写多条语句,一条语句(多关键字语句)可以写在多行;C语言忽略注释,把注释看作是一个空格,不会对注释中的内容进行语法检查。
《C Primer Plus》第六版 第十一章编程练习答案

1#include<stdio.h>#include<string.h>#define SIZE 100void input(char *, int );int main(void){char arr[SIZE];int n;puts("input the number of n:");scanf("%d", &n);getchar();puts("input your string: ");input(arr, n);printf("%s\n", arr);getchar();return 0;}void input(char *Arr, int len){int i;for (i=0; i<len; i++){*(Arr+i)=getchar();}*(Arr+i)='\0';while(getchar() !='\n'){continue;}}2.#include<stdio.h>#include<string.h>#define SIZE 100void input(char *, int );int main(void){char arr[SIZE];int n;puts("input the number of n:");scanf("%d", &n);getchar();puts("input your string: ");input(arr, n);puts(arr);getchar();return 0;}void input(char *Arr, int len){int i;for (i=0; i<len; i++){*(Arr+i)=getchar();if (*(Arr+i) ==' ' || *(Arr+i)=='\t'|| *(Arr+i)== '\n'){break;}}*(Arr+i)='\0';while(getchar() !='\n'){continue;}}3.#include<stdio.h>#include<string.h>#define SIZE 100void input(char * );int main(void){char arr[SIZE];puts("input your string: ");input(arr);puts(arr);getchar();return 0;}void input(char *Arr){char ch;int i=1;do{ch=getchar();}while(ch ==' ' || ch =='\t' || ch =='\n');Arr[0]=ch;while( (ch=getchar()) &&( ch!=' '&&ch!='\t'&&ch!='\n')) {Arr[i]=ch;i++;}Arr[i]='\0';while (getchar()!='\n'){continue;}}4.#include<stdio.h>#include<string.h>#define SIZE 100void input(char *, int);int main(void){char arr[SIZE];int n;puts("input the number of n:");scanf("%d", &n);puts("input your string: ");input(arr, n);puts(arr);getchar();return 0;}void input(char *Arr, int len){char ch;int i=1;do{ch=getchar();}while(ch ==' ' || ch =='\t' || ch =='\n');Arr[0]=ch;while( (ch=getchar()) &&( ch!=' '&&ch!='\t'&&ch!='\n') &&i<len) {Arr[i]=ch;i++;}Arr[i]='\0';while (getchar()!='\n'){continue;}}5.#include<stdio.h>#include<string.h>#define SIZE 100char *find(char *, char);int main(void){char arr[SIZE];char ch;char *ps;while(1){puts("input a string:");fgets(arr, 100, stdin);puts("input a character:");ch=getchar();ps=find(arr, ch);if(ps){puts("Find the character!");}else{puts("Con't find the charcter!");}getchar();}getchar();return 0;}char *find(char *Arr, char c){int len=strlen(Arr);char *p=Arr;int i;for (i=0; i<len; i++){if (c== *p){return p;}p++;}if (i==len){return NULL;}return 0;}6.#include<stdio.h>#include<string.h>#define SIZE 100int is_within(char *, char);int main(void){char arr[SIZE];char ch;int re;while(1){puts("input a string:");fgets(arr, 100, stdin);puts("input a character:");ch=getchar();re=is_within(arr, ch);if(re){puts("Find the character!");}else{puts("Con't find the charcter!");}getchar();}getchar();return 0;}int is_within(char *Arr, char c){int len=strlen(Arr);char *p=Arr;int i;for (i=0; i<len; i++){if (c== *p){return 1;}p++;}if (i==len){return 0;}else{return -1;}}7.#include<stdio.h>#include<string.h>#define SIZE 20char *mystrncpy(char *, char *, int ); int main(void){char s1[SIZE];char s2[SIZE];char *ps;int n;while(1){puts("input string s2:");gets(s2);puts("input string s1:");gets(s1);puts("Input the number of n:");scanf("%d", &n);ps=mystrncpy(s1,s2, n );puts("After copy:");puts(ps);getchar();}getchar();return 0;}char *mystrncpy(char *dst, char *src, int len) {int i;char *p1, *p2;int L=strlen(src);p2=src;p1=dst;for (i=0; i<(L<len? L:len); i++){*(p1+i)=*(p2+i);}return p1;}8.#include<stdio.h>#include<string.h>#define SIZE 20char *string_in(char *, char * );int main(void){char s1[SIZE];char s2[SIZE];char *ps;while(1){puts("input string s2:");gets(s2);puts("input string s1:");gets(s1);ps=string_in(s1,s2);if (ps){puts("Find it!");}else{puts("Con't find it!");}getchar();}getchar();return 0;}char *string_in(char *s1, char *s2){int len1, len2;int i, j, temp;char *p1=s1;char *p2=s2;len1=strlen(s1);len2=strlen(s2);if (*p1=='\0' || *p2 =='\0') return NULL;for (i=0; i<=(len1-len2); i++){temp=i;j=0;while(p2[j]!='\0'&& p1[temp]==p2[j]){temp++;j++;}if (j==len2) return(p1+temp);}if (i>(len1-len2)) return NULL;}9#include<stdio.h>#include<string.h>#define SIZE 20void fun(char *);int main(void){char arr[SIZE];while(1){puts("input a string:");gets(arr);fun(arr);puts(arr);getchar();}getchar();return 0;}void fun(char *Arr){char *p=Arr;int len=strlen(Arr);int i;char ch;for (i=0; i<len/2; i++){ch=p[i];p[i]=p[len-i-1];p[len-i-1]=ch;}}10#include<stdio.h>#include<string.h>#define SIZE 20void fun(char *);int main(void){char arr[SIZE];while(1){puts("input a string:");gets(arr);if (arr[0]=='\n') break;fun(arr);puts(arr);getchar();}getchar();return 0;}void fun(char *Arr){int i, j;char temp[SIZE];i=j=0;while(Arr[i]!='\0'){if (Arr[i]!=' '){temp[j]=Arr[i];j++;}i++;}strcpy(Arr, temp);Arr[j]='\0';}11.#include<stdio.h>#include<string.h>#define SIZE 20#define LIM 3void display(char *String[]); void Ascii( char *String[]); void Length(char*String[]); void FirstC(char*String[]); int First_word(char String[]);int main(void){char input[LIM][SIZE];char ch;char*ptr[LIM];while (1){int ct=0;puts("please input 10 strings:");while (ct<LIM ){gets(input[ct]);ptr[ct]=input[ct];ct++;}puts("*********************************************");puts("a. print the original strings:");puts("b. print the strings with ASCII order:");puts("c. print the strings with length order:");puts("d. print the strings with first character order:");puts("e. quit.");puts("**********input your choice ***********");ch=getchar();switch (ch){case'a':display(ptr); break;case'b':Ascii(ptr); break;case'c':Length(ptr); break;case'd':FirstC(ptr); break;default:return 0;}getchar();}getchar();return 0;}void display(char *String[]){int i;for (i=0; i<LIM; i++){puts(String[i]);}}void Ascii(char *String[]){char *p;int i,j;for (i=0; i<LIM; i++){for (j=0; j<LIM-i-1; j++){if (strcmp(String[j], String[j+1])>0){p=String[j];String[j]=String[j+1];String[j+1]=p;}}}display(String);}void Length(char *String[]){char *p;int i,j;for (i=0; i<LIM; i++){for (j=0; j<LIM-i-1; j++){if (strlen(String[j])>strlen(String[j+1])){p=String[j];String[j]=String[j+1];String[j+1]=p;}}}display(String);int First_word(char String[]){int i=0;while (String[i] != ' '){i++;}return i;}void FirstC(char *String[]){char *p;int i,j;for (i=0; i<LIM; i++){for (j=0; j<LIM-i-1; j++){if (First_word(String[j])>First_word(String[j+1])){p=String[j];String[j]=String[j+1];String[j+1]=p;}}}display(String);}12#include<stdio.h>#include<string.h>#include<ctype.h>int main(void){int ch;int word, large, small, punctuation, num, begin;word=large=small=punctuation=num=begin=0;while( (ch=getchar()) != EOF){if (isdigit(ch)){num++;}if(ispunct(ch)){punctuation++;}if (isupper(ch)){large++;}if (islower(ch)){small++;}if ( isalpha(ch)){if (begin==0){word++;begin=1;}}else{begin=0;}}printf("words=%d, large=%d, small=%d, punc=%d, num=%d\n", word, large, small, punctuation, num);getchar();return 0;}13.#include<stdio.h>#include<string.h>void Reverse(char *, int );int main(void){char str[100];int len;puts("Enter a string:");gets(str);len=strlen(str);Reverse(str, len);getchar();return 0;}void Reverse(char *str, int n) {int i;char ch;for (i=0; i<n/2; i++){ch=str[i];str[i]=str[n-i-1];str[n-i-1]=ch;}for (i=0; i<n; i++){putchar(str[i]);}putchar('\n');}14.#include<stdio.h>#include<stdlib.h>#include<math.h>int main(int argc, char *argv[]) {double number, value;int p;printf("The command lines has %d arguments:\n", argc-1);number=atoi(argv[1]);p=atoi(argv[2]);value=pow(number, p);printf("the result=%lf\n", value);getchar();return 0;}15.#include<stdio.h>#include<math.h>#include<string.h>#include<ctype.h>int myatoi(char *);int main(int argc, char *argv[]){char str[50]={0};int re;while(gets(str)){re=myatoi(str);printf("number=%d\n", re);}getchar();return 0;}int myatoi(char *p){int len=strlen(p);int i;int n=0;for (i=0; i<len; i++){if (!isdigit(p[i])){return 0;}else{n=n*10+(p[i]-'0');}}return n;}16.#include<stdio.h>#include<math.h>#include<string.h>#include<ctype.h>int main(int argc, char *argv[]){char ch;if (argv[1][0]=='-'){if (argv[1][1]=='l'){while ((ch=getchar())!=EOF){putchar(tolower(ch));}}elseif (argv[1][1]=='u'){while ((ch=getchar())!=EOF){putchar(toupper(ch));}}else{while ((ch=getchar())!=EOF){putchar(ch);}}}getchar();return 0; }。
C++Primer第5版第十一章课后练习答案

C++Primer第5版第⼗⼀章课后练习答案练习11.1map和vector相⽐是通过关键字⽽不是位置来查找值。
练习11.2list:需要在中间进⾏操作的情况。
vector:若没有必要使⽤其他容器则优先使⽤deque:只需要在头尾进⾏操作的情况map:字典set:key-value相同的集合练习11.3int main(int argc, char* argv[]){map<string, size_t> word_count;string word;while (cin >> word) {++word_count[word];}for (const auto& w : word_count) {cout << w.first << "" << w.second << endl;}}练习11.4int main(int argc, char* argv[]){map<string, size_t> word_count;string word;while (cin >> word) {word.erase(remove_if(word.begin(), word.end(), [](char& c) {if (isupper(c)) { tolower(c); } return ispunct(c); }));++word_count[word];}for (const auto& w : word_count) {cout << w.first << "" << w.second << endl;}}练习11.5map是关键字-值对的集合,set是关键字的集合,看使⽤场景需要key-value还是key集合练习11.6set是关联容器,进⾏查找、修改操作效率⾼list是顺序容器,插⼊删除操作效率低,随机访问速度慢练习11.7int main(int argc, char* argv[]){map<string, vector<string>> familys;string surname, name;while (cin >> surname>> name) {familys[surname].emplace_back(name);}for (const auto& f : familys) {cout << f.first << ":";for (const auto& n : f.second) {cout << n << "";}cout << endl;}}练习11.8int main(int argc, char* argv[]){vector<string> words;string word;while (cin >> word) {words.emplace_back(word);}auto it = unique(words.begin(), words.end());words.erase(it, words.end());}set查找速度快练习11.9map<string, list<size_t>> m;练习11.10不能,因为map所提供的操作必须在关键字类型上定义⼀个严格弱序,⽽迭代器之间是⽆法⽐较的练习11.11int main(int argc, char* argv[]){typedef bool (*Comp)(const Sales_data&, const Sales_data&);//和decltype(compareIsbn)*等价multiset<Sales_data, Comp>bookStore(Comp);}练习11.12int main(int argc, char* argv[]){vector<string>str_vec(10);vector<int> i_vec(10);vector<pair<string, int>> psi1;vector<pair<string, int>> psi2;vector<pair<string, int>> psi3;string str, int num;for (auto i = 0; i < 10; ++i) {cin >> str >> num;str_vec.emplace_back(str);i_vec.emplace_back(num);}for (auto i = 0; i < 10; ++i) {psi1.push_back({ str_vec[i],i_vec[i] });psi1.emplace_back(pair<string, int>(str_vec[i], i_vec[i]));psi1.emplace_back(make_pair(str_vec[i], i_vec[i]));}}练习11.13int main(int argc, char* argv[]){vector<string>str_vec(10);vector<int> i_vec(10);vector<pair<string, int>> psi1;vector<pair<string, int>> psi2;vector<pair<string, int>> psi3;string str, int num;for (auto i = 0; i < 10; ++i) {cin >> str >> num;str_vec.emplace_back(str);i_vec.emplace_back(num);}for (auto i = 0; i < 10; ++i) {psi1.push_back({ str_vec[i],i_vec[i] });//不能⽤emplace_back来进⾏pair的列表初始化psi1.emplace_back(pair<string, int>(str_vec[i], i_vec[i]));psi1.emplace_back(make_pair(str_vec[i], i_vec[i]));}}练习11.14int main(int argc, char* argv[]){map<string, vector<pair<string, string>>> familys;string surname, name, birthday;while (cin >> surname >> name>>birthday) {familys[surname].emplace_back(make_pair(name, birthday));}for (const auto& f : familys) {cout << f.first << ":";for (const auto& n : f.second) {cout << n.first << "'s birthday is "<<n.second<<"";}cout << endl;}}练习11.15mapped_type:vector<int>key_type:intvalue_type:pair<const int,vector<int>>练习11.16int main(int argc, char* argv[]){map<int, int>i_i_map;auto it = i_i_map.begin();(*it).second = 5;}练习11.17copy(v.begin(), v.end(), inserter(c, c.end()));//调⽤insert成员函数插⼊到multiset尾后迭代器之前copy(v.begin(), v.end(), back_inserter(c));//multiset没有push_back成员函数,因此⽆法使⽤copy(c.begin(), c.end(), inserter(v,v.end()));//调⽤insert成员函数插⼊到vector尾后迭代器之前copy(c.begin(), c.end(), back_inserter(v));//调⽤push_back成员函数插⼊到vector尾后迭代器之前练习11.18map<string,size_t>::iterator练习11.19int main(int argc, char* argv[]){typedef bool (*Comp)(const Sales_data&, const Sales_data&);//和decltype(compareIsbn)*等价multiset<Sales_data, Comp>bookStore(compareIsbn);multiset<Sales_data, Comp>::iterator it = bookStore.begin();}练习11.20int main(int argc, char* argv[]){map<string, size_t> word_count;string word;while (cin >> word) {auto ret = word_count.insert({ word,1 });if (!ret.second)++ret.first->second;}for (const auto& w : word_count) {cout << w.first << "" << w.second << endl;}}//下标操作更容易编写和阅读练习11.21输⼊word,并将其设置为关键字插⼊到容器中,对应的值为0,然后对值进⾏递增,若此时容器中已有重复关键字则直接对该关键字对应的值进⾏递增。
《精通C程序设计教程》第十、十一章部分习题答案

《精通C程序设计教程》第十、十一章部分习题答案第十章10.2 read=0, green=1, yellow=5, white=6, black=710.6 42, 110.8#include "stdio.h"typedef struct student { long xh;char xm[21];int s1,s2,s3; } STU;#define N 5void inp_stu(STU a[N]){ int i;printf("Input %d students data\n",N);printf("xh xm s1 s2 s3\n");for(i=0;i<N;i++) scanf("%ld%s%d%d%d",&a[i].xh,a[i].xm,&a[i].s1,&a[i].s2,&a[i].s3); }void out_stu(STU a[N],int p[N]){ int i;for(i=0;i<N;i++) printf("%ld %s %d %d %d\n", \a[p[i]].xh,a[p[i]].xm,a[p[i]].s1,a[p[i]].s2,a[p[i]].s3);}#define SUM(a,i) (a[i].s1+a[i].s2+a[i].s3)void Sort(STU a[N],int p[N]) /* 索引冒泡排序*/{ int i,j,t;for(i=0;i<N;i++) p[i]=i;for(i=1;i<N;i++)for(j=0;j<N-i;j++)if(SUM(a,p[j])<SUM(a,p[j+1])) { t=p[j];p[j]=p[j+1];p[j+1]=t; }}void main(){ STU a[N];int p[N];inp_stu(a);Sort(a,p);out_stu(a,p);}10.10#include "stdio.h"#include "math.h"typedef struct { int y,m,d; } DA TE;long days(DA TE *p){ long m,n=(p->y-1)*365+(p->y-1)/4-(p->y-1)/100+(p->y-1)/400;for(m=1;m<p->m-1;m++)switch(m){ case 4:case 6:case 9:case 11:n+=30;break;case 2:n+=28+(p->y%4==0&&p->y%100!=0||p->y%400==0);break;default:n+=31;}return n;}void main(){ DA TE a1,a2;long n;while(1){ printf("Input date 1(y m d):");scanf("%d%d%d",&a1.y,&a1.m,&a1.d);if(a1.y<=0) break;printf("Input date 2(y m d):");scanf("%d%d%d",&a2.y,&a2.m,&a2.d);n=labs(days(&a1)-days(&a2));printf("The difference days=%ld\n",n);}}10.11#include "stdio.h"#include "conio.h"typedef struct { int h,m,s; } TIME;void inc_time(TIME *p){ int cy=1;p->s+=cy;cy=p->s/60;p->s%=60;p->m+=cy;cy=p->m/60;p->m%=60;p->h+=cy;}void dec_time(TIME *p){ int cy=1;p->s-=cy;cy=p->s<0;p->s=(p->s%60+60)%60;p->m-=cy;cy=p->m<0;p->m=(p->m%60+60)%60;p->h-=cy;}void main(){ char ch;TIME t;printf("Input a time(h m s):");scanf("%d%d%d",&t.h,&t.m,&t.s);while(1){ ch=getche();if(ch=='+') inc_time(&t);if(ch=='-') dec_time(&t);if(ch=='Q'||ch=='q') break;printf("\r%d:%d:%d\n",t.h,t.m,t.s);}10.14#include "stdio.h" /* 本例请用C++调制*/typedef struct node { int no,quantity;struct node *next; } NodeTp; void DscIns(NodeTp *h,NodeTp *s){ NodeTp *p=h->next,*pr=h;while(p&&p->quantity>s->quantity) { pr=p;p=p->next; }pr->next=s;s->next=p;}void Out(NodeTp *h){ NodeTp *p=h->next;while(p) { printf("%d,%d\n",p->no,p->quantity);p=p->next;}}void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}void main(){ NodeTp *h,*s,*ps;int no,quantity;h=new NodeTp;h->next=NULL;while(1){ printf("Input no and quantity:");scanf("%d%d",&no,&quantity);if(no==0) break;ps=h;s=h->next;while(s&&s->no!=no) { ps=s;s=s->next; }if(s==NULL) { s=new NodeTp;s->no=no;s->quantity=quantity; } else { s->quantity+=quantity;ps->next=s->next; }DscIns(h,s);}Out(h);Ers(h);}10.15#include "stdio.h" /* 本例请用C++调试*/#define N 10typedef struct node { int data;struct node *next; } NodeTp;NodeTp *Crt(int n){ int i;NodeTp *h,*p,*last;h=new NodeTp;h->data=1;last=h;for(i=2;i<=n;i++){ p=new NodeTp;p->data=i;last->next=p;last=p;}last->next=NULL;return h;void Out(NodeTp *h){ while(h) { printf("%6d",h->data);h=h->next; }}void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}NodeTp *Chg(NodeTp *h){ NodeTp *p1,*p2,*p,*last;p1=h;if(!p1) return h;p2=h->next;if(!p2) return h;h=NULL;while(1){ p=p2->next;if(h==NULL) { h=p2;p2->next=p1;p1->next=p; }else { last->next=p2;p2->next=p1;p1->next=p; }last=p1;p1=p;if(!p1) break;p2=p->next;if(!p2) break;}return h;}void main(){ NodeTp *h=Crt(N);h=Chg(h);Out(h);printf("\n");Ers(h);}10.16#include "stdio.h"typedef struct node { int data;struct node *next; }NodeTp;void AscIns(NodeTp *h,int x) /* 带附加头结点升序链表插入结点*/ { NodeTp *pr=h,*p=h->next,*s=new NodeTp;s->data=x;while(p&&p->data<x) { pr=p;p=p->next; }pr->next=s;s->next=p;}NodeTp *Crt(){ NodeTp *h=new NodeTp;int x;h->next=NULL;printf("Input integers until input positive integers or zero:\n");while(1){ scanf("%d",&x);if(x<=0) break;AscIns(h,x);}return h;}void Out(NodeTp *h){ h=h->next;while(h) { printf("%6d",h->data);h=h->next; } }void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}void DelRep(NodeTp *h){ NodeTp *p=h->next,*pn;while(1){ pn=p->next;if(pn==NULL) break;if(pn->data==p->data) { p->next=pn->next;delete pn; }else p=pn;}}void main(){ NodeTp *h=Crt();DelRep(h);Out(h);printf("\n");Ers(h);}10.17#include "stdio.h"typedef struct node { int data;struct node *next; }NodeTp; NodeTp *Crt(){ NodeTp *h=new NodeTp,*p;int x;h->next=NULL;printf("Input integers until input positive integers:\n"); while(1){ scanf("%d",&x);if(x<=0) break;p=new NodeTp;p->data=x;p->next=h->next;h->next=p;}return h;}void Out(NodeTp *h){ h=h->next;while(h) { printf("%6d",h->data);h=h->next; } }void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}void Del(NodeTp *h){ NodeTp *pr=h,*p=h->next;while(p)if(p->data>5&&p->data<20) { pr->next=p->next;delete p;p=pr->next; }else {pr=p;p=p->next;}}void main(){ NodeTp *h=Crt();Del(h);Out(h);printf("\n");Ers(h);}10.19NodeTp *GetPre(NodeTp *p){ NodeTp *pr=p;while(pr->next!=p) { pr=pr->next; }}10.20void link(NodeTp *p){ NodeTp *pr=p,*q=p;p=p->next;while(pr!=q) { p->previous=pr;pr=p;p=p->next; }}10.27 7 3 B最初为p2指针变量分配的空间丢失第十一章11.5#include "stdio.h"void main(){ FILE *fp;int i,j,c,n,w=4;printf("Input n=");scanf("%d",&n);if(n<=0) { printf("number of rows must be greater than 0.\n");return; }fp=fopen("a2.txt","w");for(i=0;i<n;i++){ printf("%*s",w*(n-i),"");fprintf(fp,"%*s",w*(n-i),"");c=1;printf("%*d",w,c);fprintf(fp,"%*d",w,c);for(j=1;j<=i;j++) { c=c*(i-j+1)/j;printf("%*d",2*w,c);fprintf(fp,"%*d",2*w,c); } printf("\n");fprintf(fp,"\n");}fclose(fp);}11.7#include "stdio.h"void main(){ FILE *fr;int ce,cd,c;char fname[81],ch;c=ce=cd=0;printf("Input a text file name:\n");scanf("%s",fname);fr=fopen(fname,"rb");if(!fr) { printf("File %s not found.\n",fname);return; }while(1){ ch=fgetc(fr);if(feof(fr)) break;c++;if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') ce++;if(ch>'0'&&ch<='9') cd++;}fclose(fr);printf("number of letters=%d,number of digits=%d,others=%d\n",ce,cd,c-ce-cd); }11.10#include "stdio.h"#include "math.h"void main(){ FILE *fw;int m,i,c,k;fw=fopen("prime.txt","w");if(!fw) { printf("Can't create the text file prime.txt\n");return; }fprintf(fw,"%6d",2);c=1;for(m=3;m<10000;m+=2){ k=(int)sqrt(m+1);for(i=3;i<=k;i+=2) if(m%i==0) break;if(i>k) { fprintf(fw,"%6d",m);c++;if(c%10==0) fprintf(fw,"\n"); }}fclose(fw);}11.12#include "stdio.h"void main(){ FILE *fp;char ch,fname[81];printf("Input a text file name:\n");scanf("%s",fname);fp=fopen(fname,"rb+");if(!fp) { printf("File %s not found.\n",fname);return; }ch=fgetc(fp);while(!feof(fp)){ if(ch>='A'&&ch<='Z') ch+=32;fseek(fp,-1L,SEEK_CUR);fputc(ch,fp);fseek(fp,0L,SEEK_CUR);ch=fgetc(fp);}fclose(fp);}11.22 char *argv[] 3 !feof(f1) ch, f2 11.23 0 “r”或”rb”feof(fp) ch==32 11.24 添加一行字符到字符文件b.txt11.25 A C D。
第11章谭浩强C++习题及解答

int main() { Student1 stud; stud.get_value1(); stud.display_1(); return 0; }
3. 将例 将例11.3的程序修改、补充,写成 的程序修改、 的程序修改 补充, 一个完整、正确的程序, 保护继承方 一个完整、正确的程序,用保护继承方 式。在程序中应包括输入数据的函数。 在程序中应包括输入数据的函数。
class Student1: private Student {public: void get_value1( ) { get_value(); cout<<"please input age, address: "; cin>>age>>addr; } void display_1( ) { display( ); cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl; } private: int age; string addr; };
class Student1: protected Student {public: void get_value1( ) { cout<<"please input age, address: "; cin>>age>>addr; } void display_1( ) { cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl; } private: int age; string addr; };
C++第11章习题解答

第十一章标准模板库(STL)习题一. 基本概念与基础知识自测题11.1填空题11.1.1 STL大量使用继承和虚函数是(1)(填对或错)。
因为(2)。
答案:(1)错(2)它使用的是模板技术,追求的是运行的效率,避免了虚函数的开销11.1.2 有两种STL容器:(1)和(2)。
STL不用new和delete,而用(3)实现各种控制内存分配和释放的方法。
答案:(1)第一类容器(2)近容器(3)分配子(allocator)11.1.3 五种主要迭代子类型为(1)、(2)、(3)、(4)和(5)。
STL算法用(6)间接操作容器元素。
sort算法要求用(7)迭代子。
答案:(1)输入(InputIterator)(2)输出(OutputIterator)(3)正向(ForwardIterator)(4)双向(BidirectionalIterator)(5)随机访问(RandomAccessIterator)(6)迭代子(7)随机访问(RandomAccessIterator)11.1.4 三种STL容器适配器是(1)、(2)和(3)。
答案:(1)stack(栈)(2)queue(队列)(3)priority_queue(优先级队列)11.1.5 成员函数end()得到容器(1)的位置,而rend得到容器(2)的位置。
算法通常返回(3)。
答案:(1)最后一个元素的后继位置(2)引用容器第一个元素的前导位置。
实际上这是该容器前后反转之后的end()(3)迭代子11.1.6 适配器是(1),它依附于一个(2)容器上,它没有自己的(3)函数和(4)函数,而借用其实现类的对应函数。
答案:(1)不独立的(2)顺序(3)构造函数(4)析构函数11.1.7 返回布尔值的函数对象称为(1),默认的是(2)操作符。
答案:(1)谓词(predicate)(2)小于比较操作符“<”11.1.8C++标准库中给出的泛型算法包括(1)种算法。
C语言程序设计 – 第 11 章课后习题

C语言程序设计– 第十一章课后习题一、选择题1. C语言可以处理的文件类型是(B)A. 文本文件和数据文件B. 文本文件和二进制文件C. 数据文件和二进制文件D. 以上答案都不完全(解释:文本文件、数据文件、二进制文件等C语言都可以处理)2. 对文件操作的一般步骤是(A)A. 打开文件→操作文件→关闭文件B. 操作文件→修改文件→关闭文件C. 读写文件→打开文件→关闭文件D. 读文件→写文件→关闭文件(解释:对于缓冲文件系统,操作文件之前,需要在内存中建立一个“文件信息区”(称“打开文件”),对文件的操作都是对这个信息区操作,文件操作完毕后,需要将信息区清除以便释放内存(称“关闭文件”)。
)3. 要打开一个已存在的非空二进制文件“file”用于随机读写,正确的语句是(B)A. fp = fopen("file", "rb");B. fp = fopen("file", "rb+");C. fp = fopen("file", "wb");D. fp = fopen("file", "wb+");(解释:文件已经存在且非空,D会将文件原有的内容清空。
)4. 当顺利执行了文件关闭操作时,fclose函数的返回值是(C)A. -1B. TRUEC. 0D. 1(解释:文件顺利关闭时返回0,失败返回EOF(-1))5. 标准库函数fgets(s, n, f)的功能是(B)A. 从文件f中读取长度为n的字符串存入指针s所指的内存B. 从文件f中读取长度不超过n-1的字符串存入指针s所指的内存C. 从文件f中读取n个字符串存入s所指的内存D. 从文件f中读取n-1个字符串存入指针s所指的内存(解释:在读出n-1个字符之前,如果遇到了换行符或EOF,则读出结束。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
11.1#include <iostream>struct date{int year;int month;int day;}date;void main(){static int day_table[]={31,28,31,30,31,30,31,31,30,31,30,31};int i,day_sum;printf("输入年,月,日\n\n");scanf("%d%d%d",&date.year,&date.month,&date.day);day_sum=0;for(i=0;i<date.month-1;i++)day_sum+=day_table[i];day_sum+=date.day;if((date.year%4==0)&&(date.year%100!=0)||(date.year%400==0)&&(date.m onth>2))day_sum+=1;printf("\n\n该日在本年中是第%d天\n\n",day_sum);}11.2#include <iostream>struct date{int year;int month;}date;void main(){int days(int y,int m,int d);printf("输入年,月,日\n\n");scanf("%d%d%d",&date.year,&date.month,&date.day);printf("\n\n该日在本年中是第%d天\n\n",days(date.year,date.month,date.day));}int days(int y,int m,int d){static int day_table[]={31,28,31,30,31,30,31,31,30,31,30,31}; int i,day_sum;day_sum=0;for(i=0;i<m-1;i++)day_sum+=day_table[i];day_sum+=date.day;if((y%4==0)&&(y%100!=0)||(y%400==0)&&(m>2))day_sum+=1;return day_sum;}11.3#include <iostream>#define N 2struct stu{int num;char name[10];float score[3];}stu[N];void main(){void print(struct stu *p);int i,j;struct stu *p;p=stu;for(i=0;i<N;i++){printf("\n\n输入第%d个学生的成绩\n\n",i+1);printf("输入学号:");scanf("%d",&((p+i)->num));printf("\n输入姓名:");scanf("%s",(p+i)->name);for(j=0;j<3;j++){printf("\n输入成绩%d:",j+1);scanf("%f",&((p+i)->score[j]));}}print(stu);}void print(struct stu *p){int i,j;printf("学号姓名成绩1 成绩2 成绩3\n\n"); for(i=0;i<N;i++){printf("%-8d%-8s",(p+i)->num,(p+i)->name);for(j=0;j<3;j++){printf("%-8f",(p+i)->score[j]);}printf("\n\n");}}11.4#include <iostream>#define N 2struct stu{int num;char name[10];float score[3];}stu[N];void main(){void input(struct stu *p);void print(struct stu *p);struct stu *p;p=stu;input(p);print(p);}void input(struct stu *p){int i,j;for(i=0;i<N;i++){printf("\n\n输入第%d个学生的成绩\n\n",i+1);printf("输入学号:");scanf("%d",&((p+i)->num));printf("\n输入姓名:");scanf("%s",(p+i)->name);for(j=0;j<3;j++){printf("\n输入成绩%d:",j+1);scanf("%f",&((p+i)->score[j]));}}}void print(struct stu *p){int i,j;printf("学号姓名成绩1 成绩2 成绩3\n\n"); for(i=0;i<N;i++){printf("%-7d%-7s",(p+i)->num,(p+i)->name);for(j=0;j<3;j++){printf("%-9.2f",(p+i)->score[j]);}printf("\n\n");}}11.5#include <iostream>#define N 3struct stu{char num[10];char name[10];float score[3];float average;}stu[N];void main(){void input(struct stu *p);void aver(struct stu *p);float aver_total(struct stu *p);void print(struct stu *p);struct stu *p;p=stu;input(p);aver(p);print(p);}void input(struct stu *p){int i,j;printf("按顺序依次输入学生的学号,姓名,成绩1,成绩2,成绩3\n\n"); for(i=0;i<N;i++){scanf("%s%s",(p+i)->num,(p+i)->name);for(j=0;j<3;j++)scanf("%f",&((p+i)->score[j]));}}void aver(struct stu *p){int i,j;float sum;for(i=0;i<N;i++){sum=0;for(j=0;j<3;j++){sum+=(p+i)->score[j];}(p+i)->average=sum/3;}}float aver_total(struct stu *p){float sum=0;for(;p<stu+N;p++)sum+=p->average;return (sum/N);}void print(struct stu *p){int i,j;struct stu *q;float max=0;printf("三门课的总平均成绩为:%f\n\n",aver_total(p));for(i=0;i<N;i++){if((p+i)->average>max){max=(p+i)->average;q=p+i;}}printf("最高分学生的数据为:\n\n");printf("学号姓名成绩1 成绩2 成绩3 平均成绩\n\n"); printf("%-7s%-7s",q->num,q->name);for(j=0;j<3;j++){printf("%-8.2f",q->score[j]);}printf("%-8.2f",q->average);printf("\n\n");}11.8#include <iostream>#include <malloc.h>#define NULL 0#define LEN sizeof(struct student)struct student{long int num;float score;struct student *next;}list_a,list_b;int n,sum;void main(){struct student *creat(void);struct student *insert(struct student *ha,struct student *hb); void print(struct student *head);struct student *ahead,*bhead,*abh;ahead=creat();sum=n;bhead=creat();sum=sum+n;abh=insert(ahead,bhead);print(abh);}struct student *creat(void){struct student *head;struct student *p1,*p2;n=0;p1=p2=(struct student *)malloc(LEN);printf("输入学生的学号和成绩\n\n");printf("如果输入的学号为0,就停止输入\n\n");scanf("%ld%f",&p1->num,&p1->score);head=NULL;while(p1->num!=0){n=n+1;if(n==1) head=p1;else p2->next=p1;p2=p1;p1=(struct student *)malloc(LEN);scanf("%ld%f",&p1->num,&p1->score);}p2->next=NULL;return(head);}struct student *insert(struct student *ha,struct student *hb) {struct student *pa1,*pa2,*pb1,*pb2;pa1=pa2=ha;pb1=pb2=hb;if(ha==NULL&&hb!=NULL) ha=hb;else{while((pb1->num>pa1->num)&&(pa1->next!=NULL)){pa2=pa1;pa1=pa1->next;if(pb1->num<=pa1->num){if(ha==pa1) ha=pb1;else pa2->next=pb1;pb2=pb1;pb1=pb1->next;pa2=pa1;pa1=pb2;pb2->next=pa2;}}if((pb1->num>pa1->num)&&(pa1->next==NULL)) pa1->next=pb1;}return (ha);}void print(struct student *head){struct student *p;printf("\n\n共有%d条记录,分别为:\n\n",sum); p=head;if(head!=NULL)do{printf("%ld%8.2f\n",p->num,p->score);p=p->next;}while(p!=NULL);}11.9#include <iostream>#include <malloc.h>#define N 13struct person{int num;int next;}link[N+1];void main(){int i,count,h;for(i=1;i<=N;i++){if(i==N) link[i].next=1;else link[i].next=i+1;link[i].num=i;}printf("\n\n");count=0;h=N;printf("退出者依次是:\n\n");while(count<N-1){i=0;while(i!=3){h=link[h].next;if(link[h].num) i++;}printf("%d ",link[h].num);link[h].num=0;count++;}printf("\n\n最后的成员是:");for(i=1;i<=N;i++){if(link[i].num) printf("%d\n\n",link[i].num); }}11.10#include <iostream>#include <malloc.h>#define LEN sizeof(struct student)#define NULL 0struct student{long int num;char name[20];struct student *next;}a,b;int n;void main(){struct student *creat(void);struct student *dele(struct student *ahead,struct student *bhead); void print(struct student *head);struct student *p1,*p2;printf("输入a中同学的学号,姓名\n\n");p1=creat();printf("输入b中同学的学号,姓名\n\n");p2=creat();print(dele(p1,p2));}struct student *creat(void){struct student *head;struct student *p1,*p2;n=0;p1=p2=(struct student *)malloc(LEN);scanf("%d%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=(struct student *)malloc(LEN);scanf("%d%s",&p1->num,p1->name);}p2->next=NULL;return(head);}struct student *dele(struct student *ahead,struct student *bhead) {struct student *ah,*bh;struct student *ap1,*ap2,*bp1,*bp2;ap1=ap2=ahead;bp1=bp2=bhead;while(ap1->next){while(bp1->next){if(ap1->num!=bp1->num){bp2=bp1;bp1=bp1->next;}else{if(ap1==ahead) ahead=ap1->next;else ap2->next=ap1;ap2=ap1;ap1=ap1->next;break;}}}return(ahead);}void print(struct student *head){struct student *p;printf("删除后a中学生学号及成绩分别为:\n\n"); p=head;if(head!=NULL){do{printf("%ld%s",p->num,p->name);p=p->next;}while(p!=NULL);}}。