好文档 - 专业文书写作范文服务资料分享网站

C语言程序设计第三版谭浩强课后习题答案完整版

天下 分享 时间: 加入收藏 我要投稿 点赞

{unsigned moveright(unsigned,int); unsigned moveleft(unsigned,int); unsigned a; int n;

printf(“\\n input an octal number:”); scanf(“%o”,&a); printf(“input n:”); scanf(“%d”,&n); if(n>0)

{moveright(a,n);

printf(“result:%o\\n”,moveright(a,n)); } else {n=-n;

moveleft(a,n);

printf(“result:%o\\n”,moveleft(a,n)); } }

unsigned moveright(unsigned value,int n) {unsigned z;

z=(value>>n)|(value<<(16-n)); return(z); }

unsigned moveleft(unsigned value,int n ) {unsigned z;

z=(value>>(16-n))|(value<

12.5设计一个函数,使给出一个数的原码,能得到该数的补码。 解: main()

{unsigned int a;

unsigned int getbits(unsigned); pritf(“\\nInput an octal number:”); scanf(“%o”,&a);

printf(“result:%o\\n”,getbits(a)); }

unsigned int getbits(unsigned value) {unsigned int z; z=value&0100000; if(z==0100000) z=~value+1; else

z=value; return(z); }

第十三章

13.4从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。输入的字符串以“!”结束。 解:#include main() {

File *fp;

Char str[100]; Int I=0;

If((fp=fopen(“test”,”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(“test”,”r”); fgets(str,strlen(str)+1,fp); printf(“%s\\n”,str); fclose(fp); }

13.5有两个磁盘文件”A”和”B”,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件”C”中。 解:#include main() {

FILE *fp; Int I,j,n,i1;

Char c[100],t ,ch;

If((fp=fopen(“a1”,”r”))==NULL) {printf(“can not open the file\\n”); exit(0); }

printf(“\\n file A:\\n”);

for(I=0;(ch=fgetc(fp)!=EOF;I++) {c[i]=ch;

putchar(c[i]); }

fclose(fp);

26

i1=I;

if((fp=fopen(“b1”,”r”))==NULL) {printf(“\\n can not ipen the file”); exit(0); }

printf(“\\n file B:\\n”);

for(I=i1;(ch=fgenc(fp))!=EOF;I++) {c[i]=ch;

putchar(c[i]); }

fclose(fp); n=I;

for(i=0;Ic[j]) {t=c[i]; c[i]=c[j]; c[j]=t;

printf(“\\n file C:\\n”); fp=fopen(“c1”,”w”); for(I=0;I

fclose(fp); }

13.6有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号、姓名、三门课成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。 解:

#include 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(“\\n input 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); } }

13.7将上题stud文件中的学生数据按平均分进行排序处理,并将已排序的学生数据存入一个新文件stu-sort中。 解:

#include #define N 10 struct student {char num[10]; char name[8]; int score[3]; float ave; }st[N],temp; main() {

FILE *fp; Int I,j,n;

If((fp=fopen(“stud”,”r”))==NULL) {printf(“can not open the file”); exit(0); }

printf(“\\n file ?stud?:”);

for(I=0;fread(&st[i],sizef(struct student),1,fp)!=0;I++) {printf(“\\n%8s%8s”,st[i].num,,st[i].name); for(j=0;j<3;j++)

printf(“?”,st[i].score[j]); printf(“.f”,st[i].ave); fclose(fp); n=I;

27

for(I=0;I

printf(“\\nnow:”);

fp=fopen(“stu-sort”,”w”); for(I=0;I

{fwrite(&st[i],sizeof(struct student),1,fp); printf(“\\n%8s%8s”,st[i].num,st[i].name); for(j=0;j<3;j++)

printf(“?”,st[i].score[j]); printf(“.2f”,st[i].ave); fclose(fp); }

13.8将上题以排序的学生成绩文件进行插入处理。插入一个学生的3门课成绩,程序先计算新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新文件。

解:#include struct student {char num[10]; char name[8]; int score[3]; float ave; }st[10],s; main()

{FILE *fp, * fp1 ; int I,j,t,n;

printf(“\\n NO.:”); scanf(“%s”,s.num); printf(“name:”); scanf(“%s”,s.name);

printf(“score1,score2,score3:”);

scanf(“%d,%d,%d”,&s. score[0], &s. score[1], &s. score[2]); s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0; if((fp=fopen(“stu_sort”,”r”))==NULL) {printf(“can not open file.”); exit(0); }

printf(“original data:\\n”);

for(I=0;fread(&st[i],sizeof(struct student),1,fp)!=0;I++) {printf(“\\n%8s%8s”,st[i].num,st[i].name); for(j=0;j<3;j++)

printf(“?”,st[i].score[j]); printf(“.2f”,st[i].ave);

}

n=I;

for(t=0;st[t].ave>s.ave&&t

fp1=fopen(“sort1.dat”,”w”); for(I=p;j

{fwrite(&st[i],sizeof(stuct student),1,fp1); print(“\\n%8s%8s”,st[i],num,st[i].name); for(j=0;j<3;j++)

ptintf(“?”,st[i].score[j]); printf(“.2f”,st[i].ave); }

fwrite(&s,sizeof(struct student),1,fp1);

printf(“\\n%8s%7s}}}.2f”,s.num,s.name,s.score[0],s.score[1],s.score[2],s.ave); for(I=t;I

{fwrite(&st[i],sizeof(struct student),1,fp1); printf(“\\n %8s%8s”,st[i].num,st[i].name); for(j=0;j<3;j++)

printf(“?”,st[i].score[j]); printf(“10.2f”,st[i].ave); fclose(fp); fclose(fp1); }

13.9上题结果仍存入原有的stu_sort文件而不另建立新文件。

解: #include struct student {char num[10]; char name[8]; int score[3]; float ave; }st[10],s; main()

{FILE *fp, *fp1; int I ,j,t,n;

printf(“\\nNO.:”); scanf(“%s?,s.num); printf(“name:”); scanf(“%s?,s.name);

printf(“score1,score2,score3:”);

scanf(“%d%d%d”,&s.score[0]+&s.score[1]+&s.score[1], &s.score[2]);

s.ave=( s.score[0]+ s.score[1]+ s.score[2])/3.0; if((fp=fopen(“stu=sort”,”r”))==NULL) {printf(“can not open the file.”); exit(0);

28

}

printf(“original data:\\n”);

for(I=0;fread(&st[i],sizeof(struct student),1,fp)!=0;I++) {printf(“\\n%8s%8s”,st[i].num,st[i].name); for(j=0;j<3;j++)

printf(“?”,st[i].score[j]); printf(“.2f”,st[i].ave); }

fclose(fp); n=I;

for(t=0;st[t].ave>s.ave+&&t

fp=fopen(“stu_sort”,”w”); for(I=0;I

{fwrite(&st[i],sizeof(struct student),1,fp);

printf(“\\n%9s%8s???.2f”,s.num,s.name,s.score[0],s.score[1] s.score[2] s.ave); for(I=t;I

{fwrit(&sr[i],sizeof(struct srudent),1,fp); printf(“\\n %8s%8s”,st[i].num,st[i].name); for(j=0;j<3;j++)

printf(“?”,st[i].score[j]); printf(“.2f”,st[i].ave); }

fclose(fp); }

13.10 有一磁盘文件emploee,内存放职工的数据。每个职工的数据包括:职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。要求将职工名和工资的信息单独抽出来另建一个简明的职工工资文件。 解:#include struct emploee {char num[6]; char name[10]; char sex[2]; int age;

char addr[20]; int salary; char health[8]; char class[10]; }en[10]; struct emp

{char name[10]; int salary; }em-case[10]; main()

{FILE *fp1, *fp2;

int I,j;

if ((fp1=fopen(“emploee”,”r”))==NULL) {printf(“can not open the file.”); exit(0); }

printf(“\\n NO. name sex age addr salary health class\\n”);

for(I=0;fread(&em[i],sizeof(struct emploee),1,fp1)!=p;I++)

{printf(“\\n%4s%8s%4s%6ss%6ss%8s”,em[i].num,em[i].name,em[i].sex, em[i].age, em[i].addr, em[i].salary, em[i].health, em[i].class);

strcpy(em_case[i].name, em[i].name); em_case[i].salary=en[i].salary; }

printf(“\\n\\n*****************************************”);

if((fp2=fopen(“emp_salary”,”wb”))==NULL) {printf(“can not open the file.”); exit(0);}

for(j=0;j

{if(fwrite(&en_case[j],sizeof(struct emp),1,fp2)!=1) printf(“error!”);

printf(“\\n sd”,em_case[j].name,em_case[j].salary); }

printf(“\\n*******************************************”);

fclose(fp1); fclose(fp2); }

13.11从上题的“职工工资文件”中删区一个职工的数据,再存回原文件。

解:#incude #incude struct emploee {char name[10]; int salary; }emp[20]; main() {FILE *fp; int I,j,n,flag; char name[10]; int salary;

if((fp=fopen(“emp_salary”,”rb”))==NULL) {printf(“can not open file.”); exit(0); }

printf(“\\n original data:”);

29

for(I=0;fead(&emp[i],sizeof(struct emploee),1,fp)!=0;I++) printf(“\\n %8s }”,emp[i].name,emp[i].salary); fclose(fp); n=I;

printf(“\\n input name deleted:”); scanf(“%s”,name);

for(flag=1,I=0;flag&&I

{if(strcmp(name,emp[i].name)==0) {for(j=I;j

{strcmp(name,emp[i].name)==0 {for(j=I;j

{strcpy(emp[j].name,emp[j+1].name); emp[j].salary=emp[j+1].salary; }

flag=0; }

} if(!flag)

n=n-1; else

printf(“\\n Now,the content of file:\\n”); fp=fopen(“emp-dalary”,”wb”); for(I=p;I

fwrite(&emp[i],sizeof(struct emploee),1,fp); fclose(fp);

fp=fopen(“emp_salary”,”r”);

for(I=0;fread(&emp[i],sezeof(struct emploee),1,fp)!=0;I++) printf(“\\n%8s}”,emp[i].name,emp[i].salary); fclose(fp); }

13.12 从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中。再从该文件中读入这些数据,将其中小写字母转换成大写字母后在显示屏上输出。 解: #include main() {int I,flag; char str[80],c; FILE *fp;

Fp=fopen(“text”,”w”); Flag=1;

While(flag==1)

{printf(“\\n Input string:\\n”); ges(str);

fprintf(fp,”%s”,str); printf(“\\nContinue?”); c=getchar();

if((c==?N?)||(c==?n?)) flag=0;

getchar(); }

fcolse()fp;

fp=fopen(“text”,”r”);

while(fscanf(fp,”%s”,str)!=EOF) {for(I=0;str[i]!=?\\0?;I++)

if((str[i]>=?a?)&& (str[i]<=?z?)) str[i]-=32; printf(“\\n%s\\n”,str); }

fclose(fp); }

30

C语言程序设计第三版谭浩强课后习题答案完整版

{unsignedmoveright(unsigned,int);unsignedmoveleft(unsigned,int);unsigneda;intn;printf(“\\ninputanoctalnumber:”);scanf(“%o”,&a);printf(“inputn:”);scanf(“%d”,&n)
推荐度:
点击下载文档文档为doc格式
1ogc78lgwy37lyd0yejc
领取福利

微信扫码领取福利

微信扫码分享