TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/*
- 功能:有一批图书,每本书有:书名(name),作者(author) , 编号(num),出版日期(date)四个数据,
- 希望输入后按书名的字母顺序将各书的记录排列好,供以后查询。今输入一本书的书名,
- 如果查询到库中有此书,打印出此书的书名,作者,编号和出版日期。如果查不到此书,则打印出“无此书”。
-
-
- 时间:17:02 2013/10/26
- */
-
- #include<stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define N 80
-
- struct infoBook
- {
- char name[N];
- char author[N];
- int num;
- int date;
- };
-
- void main()
- {
- struct infoBook a[5]={{"ebooks","yincheng",5,18880808},{"lbook","haoyun",34,12330205},{"c++","wangwen",1,19870731},
- {"C primer","vicky",2,19880310},
- {"data struction","ben",3,19550302}};
-
- for(int i=0;i<5;i++) //将初始化信息打印到屏幕上
- {
- printf("%-15s\t\t%s\t\t%d\t%d\n",a.name,a.author,a.num,a.date);
- }
- printf("\n\n After order!\n");
- for(int i=0;i<4;i++) // 对书本进行排序
- {
- for (int j=i+1;j<5;j++)
- {
- char t=a.name[0];
- char s=a[j].name[0];
-
- if(t>s)
- {
- struct infoBook temp=a;
- a=a[j];
- a[j]=temp;
- }
- }
- }
- for(int i=0;i<5;i++) //将排序后信息打印到屏幕上
- {
- printf("%-15s\t\t%s\t\t%d\t%d\n",a.name,a.author,a.num,a.date);
- }
-
- puts("please enter the book");
- char books[N];
- gets_s(books); // 输入书名
-
- for(int i=0;i<5;i++) // 对输入书名进行查询,如果查询到,则打印详细信息
- {
- if(strcmp(books,a.name)==0)
- {
- printf("%-15s\t\t%s\t\t%d\t%d\n",a.name,a.author,a.num,a.date);
- system("pause");
- return;
- }
- }
-
- printf("NO");
- system("pause");
- }</pre><br><br></pre></pre>
|
|