描述
定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的
图书数据来完成图书信息表的创建,然后统计图书表中的图书个数,
同时逐行输出每本图书的信息。
输入
输入n+1行,其中前n行是n本图书的信息(书号、书名、价格),
每本图书信息占一行,书号、书名、价格用空格分隔,价格之后
没有空格。最后第n+1行是输入结束标志:0 0 0(空格分隔的三个0)
。其中书号和书名为字符串类型,价格为浮点数类型。
输出
总计n+1行,第1行是所创建的图书表中的图书个数,后n行是n本图书
的信息(书号、书名、价格),每本图书信息占一行,书号、书名、
价格用空格分隔。其中价格输出保留两位小数。
样例输入1 复制
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
0 0 0
样例输出1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
runtime代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#define MAXSIZE 10000
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef struct
{
char no[20];
char name[50];
float price;
}Book;
typedef struct
{
Book *elem;
int length;
}SqList;
SqList L;
bool IninList(SqList &L)
{
L.elem=new Book[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
int main()
{
IninList(L);
char a[20];
char b[20];
float c;
int start=0;
while(scanf("%s%s%f",&a,&b,&c)&&strcmp(a,"0")&&strcmp(b,"0")&&c!=0)
{
strcpy(L.elem[start].no,a);
strcpy(L.elem[start].name,b);
L.elem[start].price=c;
L.length++;
start++;
}
cout<<L.length<<endl;
for(int i=0;i<=L.length-1;i++)
{
printf("%s %s %.2f\n",L.elem[i].no,L.elem[i].name,L.elem[i].price);
}
return 0;
}
经过分析,可能是处理的不够合理,一个点是strcmp,还有个精度(关于0)问题,
还有delete,,还有预定义OVERFLOE存在。最后这都不是主要问题,原来是b[20]开的
小了,不细心啊。原题是name[50],最后改了就能过了。
然后还有就是虽然改了就能过,但是写的还是不好,下面是最终的
严谨版本。不是说之前的思路有问题,只是代码吗,还是严谨点好。
1 | #include<cstdio> |