C语言第9次实训题目及参考答案

合集下载

C语言实验9(附参考答案)

C语言实验9(附参考答案)

C语言实验9(附参考答案)实验9:选择结构(2)实验目的:(1)进一步掌握if语句和switch语句;(2)掌握用条件运算符实现选择;(3)掌握选择结构的嵌套,掌握简单的算法。

(4)能灵活应用下列语句:if,if-else,switch,break。

实验内容:1、若希望当A的值为奇数时,表达式的值为“真”,A的值为偶数时,表达式的值为“假”,则以下不能满足要求的表达式是( C )。

A)A%2==1 B)!(A%2==0) C)!(A%2) D)A%2 2、以下程序的运行结果是 F 。

main() { } if(2*2==5<2*2==4) print(“T”); else printf(“F”);3、下列程序的运行结果是。

#include void main(void) { int x,y=1,z; if(y!=0) x=5;printf(“\\t%d\\n”,x); if(y==0) x=4; else x=5;printf(“\\t%d\\n”,x); x=1;1} if(y<0) if(y>0) x=4; else x=5; printf(“\\t%d\\n”,x); 4、根据程序的功能,将以下程序补充完整以下程序计算某年某月有几天。

其中判别闰年的条件是:能被4整除但不能被100整除的年是闰年,能被400整除的年也是闰年。

请在内填入正确内容。

main() { int yy,mm,len; printf(“year,month=”);scanf(“%d %d”,&yy,&mm); switch(mm) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: len=31 ; break; case 4: case 6: case 9: case 11: len=30; break; case 2:if(yy%4==0&&yy0!=0||yy@0==0) len=29 ; else len=28 ; break; default: printf(“input error”); break; } printf(“the length of %d %dis %d\\n”,yy,mm,len); } 二、编程:21、(必做)试编程判断输入的正整数是否既是5又是7的整倍数。

C语言程序设计_模块九位操作训练

C语言程序设计_模块九位操作训练

运算。
做中学9-6
位段成员的输出。
程序清单 #include <stdio.h> void main(){ struct { unsigned a:1; unsigned b:3; int :3; unsigned c:4; }bit,*pb; bit.a=1; bit.b=7; bit.c=15; 输出结果 1,7,15 0 2 f
知识点
运算规则 只有对应的位都为0时,按位或运算的结果才为0,其他的 情况均为1,即: 0|0=0 0|1=1 1|0=1 1|1=1 示例分析:X=(12)10=(00001100)2,Y=(9)10=( 00001001)2 X | Y Z 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 0 0 1 1
如:假设有X=l0101110B,想取X的低4位。将X与Y=00001111B相与,即 可得到X的低4位。则有:
1 & 0 0
0 0 0
1 0 0
0 0 0
1 1 1
1 1 1
1 1 1
0 1 0
9.1.2 按位或运算(|) 做中学9-2 程序清单 #include<stdio.h> void main(){ int X=12,Y=9; int Z; Z=X|Y; printf(“Z=%d\n”;Z); } 输出结果 Z=13 假设有X=12,Y=9,Z= X | Y(按位或),则求Z 的值。
位段可以含无名成员,这时它只用来作填充或调整位置, 无名的位段成员是不能使用的。 位段结构中可以包含整型变量或数组成员,但变量或数组 名后不能跟冒号和位数,系统自动将他们从新的存储单元 开始存放。 位段结构变量可以按位段初始化,初值表中,不需要初始 化的位段用逗号跳过。

C语言习题及答案(第九章)

C语言习题及答案(第九章)

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语言实验册完整答案

实验2答案五.程序改错#include<stdi o.h>int main(void){int x=3,y;y = x*x;printf("%d = %d * %d\n",y,x,x); /* 输出*/printf("%d * %d = %d\n",x,x,y);return 0;}六、一般编程题6.1#include<stdio.h>int main(void){int x,y;printf("Input x: ");scanf("%d",&x);printf("Input y: ");scanf("%d",&y);printf("%d + %d = %d\n",x,y,x+y);printf("%d - %d = %d\n",x,y,x-y);printf("%d * %d = %d\n",x,y,x*y);printf("%d / %d = %d\n",x,y,x/y);printf("%d %% %d = %d\n",x,y,x%y);return 0;}6.2#include<stdio.h>int main(void){ float x; double y;printf("Input x: ");scanf("%f",&x);printf("Input y: ");scanf("%lf",&y);printf("%f + %f = %f\n",x,y,x+y);printf("%f - %f = %f\n",x,y,x-y);printf("%f * %f = %f\n",x,y,x*y);printf("%f / %f = %f\n",x,y,x/y);return 0;}6.3#include<stdio.h>int main(void){ char c;printf("Input c: ");scanf("%c",&c);printf("%c\n",c+32);return 0;}6.4#include<stdio.h>int main(void){int celsius,fahr;fahr = 150;celsius = 5*fahr/9 - 5*32/9;printf("fahr = %d, celsius = %d\n", fahr, celsius);return 0;} 如果使用公式9)32(5-⨯=f c 计算结果不一样,celsius = 65。

c语言第九章题库及详解答案

c语言第九章题库及详解答案

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语言程序,实现查找数组中的最大值和最小值。

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语言实验9~12答案

C语言实验9~12答案

C语言实验9~12答案实验9 指针概念及指针与一维数组的关系任务一1.阅读分析下面程序,写出运行结果,理解指针概念及“*”与“&”的含义。

a=100,b+2=12*p1=100,*p2+2=122.上机验证下列程序的运行结果。

a=5,b=7,p1=1245052,p2=1245048&a=1245052,5,&b=1245048,b=73.分析下面程序,并运行,理解用指针变量进行输入、输出。

输入并验证输出结果即可4.编写程序,实现从键盘输入两个数,输出时从大到小排列(利用指针概念)。

5,9a=5,b=9max=9,min=5任务2 使用指针访问一维数组1.阅读分析下面程序,写出运行结果,理解指针与数组关系。

s=92.阅读下面程序,理解指针、数组的多种表示法。

a[0]=1p[0]=1*(p+0)=1*(a+0)=1a[1]=2p[1]=2*(p+1)=2*(a+1)=2a[2]=3p[2]=3*(p+2)=3*(a+2)=33.在空格中填上语句,以实现字符串复制的功能。

*p2=*p1;p2++;p1++;4.下面程序完成从键盘输入两个字符串a和b,再将a和b的对应位置字符中的较大者存放在数组c中,当一个字符串结束时停止比较,此时将另一个字符串剩下的字符直接放在数组c的后面,填空完成该程序。

#include#includevoid main( ){int k=0;char a[80],b[80],c[80]={'\0'},*p,*q;p=a;q=b;gets(a);gets(b);while(*p!='\0'&&*q!='\0'){if(*p>*q) c[k]=*p;else c[k]=*q;p++;q++;k++;}if(*p!= '\0') strcat(c,p);else strcat(c,q);puts(c );}5.指出下面程序的问题所在(输出a数组的10个元素),理解指针与数组下标的关系及指针变化情况。

C语言第九章习题带答案

C语言第九章习题带答案

练习9-1答案一、选择题1.typedef unsigned long LONG的作用是( D )。

A.建立了一种新的数据类型B.定义了一个整形变量C.定义了一个长整型变量D.定义了一个新的数据类型标识符2.下面的4个运算符中,优先级最低的是( D )。

A.( ) B.. C.-> D.++3.已知:struct{int i;char c;float a;} test;则sizeof(test)的值是( D )。

A.4 B.5 C.6 D.74.当声明一个结构变量时系统分配给它的内存是( A )。

A.各成员所需内存量的总和B.结构中第一个成员所需内存量C.成员中占内存量最大者所需的容量D.结构中最后一个成员所需内存量5.以下对结构类型变量的定义中错误的是( D )。

A.typedef struct student B.struct student{ i nt num; {int num;float age; float age;} STUDENT std1; } std1;C.struct D.struct{ i nt num; {int num;float age; float age;} std1; } student;struct student std1;6.根据下面的定义,能打印出字母M的语句是( D )。

struct person{char name[9];int age;};struct person class[10]={"John", 17, "Paul", 19, "Mary", 18, "adam", 16};A.printf("%c\n", class[3].name); B.printf("%c\n", class[3].name[1]);C.printf("%c\n", class[2].name[1]); D.printf("%c\n", class[2].name[0]); 7.以下scanf函数调用语句中对结构变量成员的错误引用是( D )。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、程序填空。 /*求 1!+2!+3!+...+7!,7!表示 7 的阶乘。*/ 答案:5913 #include <stdio.h> long jc( long x) { long k,fac=1; for(k=1;________;k++) fac *=k ; ________; } void main( ) { long n,sum=0; long jc(long x); for(n=1;n<=7;n++) ________; printf("\n%ld",sum); } 答案:k<=x return fac sum+=jc(n) 2、用递归方法计算 n! (运算规则:1!=1,n!=(n-1)!*n (n>=2) ) 参考代码: #include <stdio.h> long jc( long x) { if(x==1) return 1; else return x*jc(x-1); } void main( ) { int n,sum; scanf("%d",&n); sum=jc(n); printf("\n%ld",sum); } 3、求斐波那契数列的第n项。 (运算规则:f(1)=1,f(2)=1,f(n)=f(n-1)+f(n-2) (n>2) ) 参考代码: #include <stdio.h> int f(int x) { if(x==1||x==2) return 1; else return f(x-1)+f(x-2); }
void main( ) { int n,fib; scanf("%d",&n); fib=f(n); printf("\n%d",fib); }
附加题:输出一个整数的各位数字,用逗号隔开。 (尝试正序和反序输出) 参考代码: 反序输出: #include <stdio.h> void digit(int n) { if(n==0) return; else { printf("%d,",n%10); digit(n/10); } } void main(void) { digit(2013); } 正序输出: #include <stdio.h> void digit(int n) { if(n==0) return; else { digit(n/10); printf("%d,",n%10); } } void main(void) { di
相关文档
最新文档