丰满少妇女人a毛片视频-酒色成人网-日韩欧美一-日韩精品一区二区av在线观看-成人久久免费-欧美精品一二三四区-国产午夜免费-亚洲男人第一天堂-一区二区三区福利视频-午夜激情影院-av中文天堂在线-免费一区二区-欧美日韩xxx-91区视频-亚洲另类激情专区小说图片-黄色的网站在线观看-香蕉精品在线

高分網(wǎng) > 答案大全 > 作業(yè)答案 > 課后作業(yè)答案 >

C語(yǔ)言程序設(shè)計(jì)第二版課后習(xí)題答案(5)

時(shí)間: 春燕2 課后作業(yè)答案

  實(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í)題答案

23306 衡南县| 新巴尔虎右旗| 涟水县| 太仆寺旗| 多伦县| 贵德县| 宜昌市| 安图县| 赤壁市| 密云县| 银川市| 古交市| 永宁县| 荔波县| 苏尼特左旗| 夹江县| 河间市| 康保县| 衡阳县| 阿荣旗| 新宾| 茂名市| 平塘县| 大竹县| 准格尔旗| 临沧市| 阜康市| 桑植县| 大邑县| 公安县| 兰考县| 晋宁县| 平利县| 镇远县| 西乌珠穆沁旗| 仁怀市| 平和县| 九台市| 湾仔区| 香河县| 嵩明县|