C语言程序设计第二版课后习题答案(5)
实训题
(1)图书馆的图书检索上包括:书名、作者姓名、出版日期、登录号、书价等内容。试根据上述五项内容定义一个结构体类型,声明结构体变量book,再从键盘为变量book输入数据,并从屏幕输出数据。
(2)构体数组(可以使用链表),将5名考生数据输入计算机,并列表从屏幕输出。编写函数,通过调用函数实现:
a. 找出成绩最高的考生的有关信息,并在屏幕上输出。
b. 按考生准考证号码由大到小排序。
考生数据包括:准考证号码、姓名、性别、年龄、成绩。
解:(1) struct
{ char name[20];
char author[20];
char date[10];
int loadno;
float price;
}book;
main()
{printf("enter bookname author date loadno price:\n");
scanf("%s %s %s %d %f",&book.name,&book.author,\
&book.date,&book.loadno,&book.price);
printf("bookname author date loadno price:\n");
printf("%-10s %-10s %-10s %-6d %-6.2f",book.name,book.author,\
book.date,book.loadno,book.price);
}
本程序结果是:
enter bookname author date loadno price:
c tanhaoqiang 1982.6 119 30.00
bookname author date loadno price:
C tanhaoqiang 1982.6 119 30.00
(2)
struct student
{int number;
char name[10];
char sex;
int age;
float score;
}st[5];
input_sc(s)
struct student s[];
{ int i,j;
float h;
for(i=0;i<5;i++)
{
printf("Please input number, sex ,age, score ,name:\n");
scanf("%d,%c,%d,%f,%s",&s[i].number,&s[i].sex,&s[i].age,&h,s[i].name);
s[i].score=h;
}
}
output_sc(s)
struct student s[];
{int i;
printf("number name sex age score:\n");
for(i=0;i<5;i++)
{
printf("%-8d",s[i].number);
printf("%-4s",s[i].name);
printf("%-5c",s[i].sex);
printf("%-5d",s[i].age);
printf("%-7f",s[i].score);
printf("\n");
}
}
max_st(s)
struct student s[];
{ int i,max=0,k=0;
for(i=0;i<5;i++)
if(max
max=s[i].score;
printf("max is :\n");
for(i=0;i<5;i++)
if(s[i].score==max)
{
printf("%-8d",s[i].number);
printf("%-4s",s[i].name);
printf("%-5c",s[i].sex);
printf("%-5d",s[i].age);
printf("%-7f",s[i].score);
printf("\n");
}
}
sort(s)
struct student s[];
{ int i,j,n,m;
char a[10],c;
float t;
for(i=0;i<5;i++)
for(j=0;j<4;j++)
if(s[j].score>s[j+1].score)
{n=s[j].number;
strcpy(a,s[j].name);
c=s[j].sex;
m=s[j].age;
t=s[j].score;
s[j].number=s[j+1].number;
strcpy(s[j].name,s[j+1].name);
s[j].sex=s[j+1].sex;
s[j].age=s[j+1].age;
s[j].score=s[j+1].score;
s[j+1].number=n;
strcpy(s[j+1].name,a);
s[j+1].sex=c;
s[j+1].age=m;
s[j+1].score=t;
}
}
main()
{ int i;
struct student st[5];
clrscr();
input_sc(st);
output_sc(st);
max_st(st);
sort(st);
printf("form big to small:\n");
output_sc(st);
}
本程序结果是:
Please input number, sex ,age, score ,name:
1,m,23,90,li
Please input number, sex ,age, score ,name:
2,n,22,89,liu
Please input number, sex ,age, score ,name:
3,m,23,99,wu
Please input number, sex ,age, score ,name:
4,m,22,99,guo
Please input number, sex ,age, score ,name:
5,n,22,78,zhu
number name sex age score:
1 li m 23 90.000000
2 liu n 22 89.000000
3 wu m 23 99.000000
4 guo m 22 99.000000
5 zhu n 22 78.000000
max is :
3 wu m 23 99.000000
4 guo m 22 99.000000
form big to small:
number name sex age score:
5 zhu n 22 78.000000
2 liu n 22 89.000000
1 li m 23 90.000000
3 wu m 23 99.000000
4 guo m 22 99.000000
(3) type test.txt<回车>
C语言程序设计第10章 文件
程序设计题
1.写一个程序,建立一个abc文本文件,向其中写入“spring summer fall winter”字符串,然后显示该文件的内容。
解:#include
#include
main( )
{FILE *fp;
char msg[ ]=" spring summer fall winter ";
char buf[30];
if((fp=fopen("abc.txt","w+"))==NULL)
{printf("can not creat abc file\n");
exit(1);
}
fwrite(msg,strlen(msg)+1,1,fp);
fseek(fp,SEEK_SET,0);
fread(buf,strlen(msg)+1,1,fp);
printf("%s\n",buf);
fclose(fp);
}
2. 编写一个程序对打开文件test.txt,统计该文件中字符的个数。
解:#include "stdio.h"
#define NULL 0
void main()
{FILE *fp1;
int total=0;
if((fp1=fopen(“test.txt”,"r"))==NULL)
{printf("Cannot open test.txt.\n”);
exit(1); }
else
{ while((c=fgetc(fp1))!=EOF)
if(c>='0'&&c<='9')
total++;
printf("\nThere are %d character .\n",total);
fclose(fp1);
} }
项目实训题
编写一个程序对ASCII文件test.txt中得字符做一个统计,统计该文件中字母、数字和其他字符的个数。输出统计结果分别到屏幕和文件result.txt。test.txt文件名由命令行参数输入。例如,假设C源程序的可执行文件名为count.exe, 则命令行count test.txt表示对文件test.txt进行统计。
解:#include "stdio.h"
#define NULL 0
#include "process.h"
void main(int argc,char *argv[])
{FILE *fp1,*fp2;
int digital,character,other;
char c;
digital=0;character=0;other=0;
if(argc!=2)
{printf("Command error!You would enter like count test.txt!");
exit(1);
}
else
if((fp1=fopen(argv[1],"r"))==NULL)
{printf("Cannot open %s\n",argv[1]);
exit(1);
}
else
{while((c=fgetc(fp1))!=EOF)
{if(c>='0'&&c<='9')
digital++;
else if(c>='a'&&c<='z'||c>='A'&&c<='Z')
character++;
else
other++; }
if((fp2=fopen("result.txt","w"))==NULL)
{printf("file %s open error\n","result.txt");
exit(1);
}
fputs("There are ",fp2);
fprintf(fp2,"%d ",digital);
fputs("digital ",fp2);
fprintf(fp2,"%d ",character);
fputs("character and ",fp2);
fprintf(fp2,"%d ",other);
fputs("other.\n",fp2);
printf("\nThere are %d digital %d character and %d other.\n",\
digital,character,other);
fclose(fp1);
fclose(fp2);
}
}
在DOS下使用命令:
count test.txt
屏幕显示:
There are 4 digital 5 character and 4 other.
result.txt文件中显示与屏幕一致!
以上是学习啦小编整理了c语言程序设计第二版课后习题答案大全,有帮助到你吗?
C语言程序设计相关课后习题答案:
1.c语言程序设计第三版课后习题答案
2.c语言程序设计课后习题答案
3.C语言课后习题答案
4.《C语言程序设计教程》(第三版)课后习题答案