C語(yǔ)言程序設(shè)計(jì)第二版課后習(xí)題答案(5)
實(shí)訓(xùn)題
(1)圖書(shū)館的圖書(shū)檢索上包括:書(shū)名、作者姓名、出版日期、登錄號(hào)、書(shū)價(jià)等內(nèi)容。試根據(jù)上述五項(xiàng)內(nèi)容定義一個(gè)結(jié)構(gòu)體類(lèi)型,聲明結(jié)構(gòu)體變量book,再?gòu)逆I盤(pán)為變量book輸入數(shù)據(jù),并從屏幕輸出數(shù)據(jù)。
(2)構(gòu)體數(shù)組(可以使用鏈表),將5名考生數(shù)據(jù)輸入計(jì)算機(jī),并列表從屏幕輸出。編寫(xiě)函數(shù),通過(guò)調(diào)用函數(shù)實(shí)現(xiàn):
a. 找出成績(jī)最高的考生的有關(guān)信息,并在屏幕上輸出。
b. 按考生準(zhǔn)考證號(hào)碼由大到小排序。
考生數(shù)據(jù)包括:準(zhǔn)考證號(hào)碼、姓名、性別、年齡、成績(jī)。
解:(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);
}
本程序結(jié)果是:
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);
}
本程序結(jié)果是:
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<回車(chē)>
C語(yǔ)言程序設(shè)計(jì)第10章 文件
程序設(shè)計(jì)題
1.寫(xiě)一個(gè)程序,建立一個(gè)abc文本文件,向其中寫(xiě)入“spring summer fall winter”字符串,然后顯示該文件的內(nèi)容。
解:#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. 編寫(xiě)一個(gè)程序?qū)Υ蜷_(kāi)文件test.txt,統(tǒng)計(jì)該文件中字符的個(gè)數(shù)。
解:#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);
} }
項(xiàng)目實(shí)訓(xùn)題
編寫(xiě)一個(gè)程序?qū)SCII文件test.txt中得字符做一個(gè)統(tǒng)計(jì),統(tǒng)計(jì)該文件中字母、數(shù)字和其他字符的個(gè)數(shù)。輸出統(tǒng)計(jì)結(jié)果分別到屏幕和文件result.txt。test.txt文件名由命令行參數(shù)輸入。例如,假設(shè)C源程序的可執(zhí)行文件名為count.exe, 則命令行count test.txt表示對(duì)文件test.txt進(jìn)行統(tǒng)計(jì)。
解:#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文件中顯示與屏幕一致!
以上是學(xué)習(xí)啦小編整理了c語(yǔ)言程序設(shè)計(jì)第二版課后習(xí)題答案大全,有幫助到你嗎?
C語(yǔ)言程序設(shè)計(jì)相關(guān)課后習(xí)題答案:
1.c語(yǔ)言程序設(shè)計(jì)第三版課后習(xí)題答案
2.c語(yǔ)言程序設(shè)計(jì)課后習(xí)題答案
3.C語(yǔ)言課后習(xí)題答案
4.《C語(yǔ)言程序設(shè)計(jì)教程》(第三版)課后習(xí)題答案
