划句顾 发表于 2021-9-29 21:34:03

C++:基于顺序存储结构的图书信息表的创建和输出

本帖最后由 划句顾 于 2021-9-29 21:35 编辑

作业题啦~~~{:10_327:}记录作业,以后期末考可以拿来看看复习一下嘿嘿
/**
@time:2021/9/29
@author:LaoGu
@purpose:基于顺序存储结构的图书信息表的创建和输出
**/
#include<iostream>
#include<string.h>

using namespace std;//加了这句话,下面的cout和cin就不用加std:: ,例如std::cout<<

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSIZE 10000

typedef struct tagBook{
char no;
char name;
float price;
}Book;

typedef struct tagSqList{
Book *elem;
int length;
}SqList;

int main(){
        //声明函数
        Status InitList_Sq(SqList &L);
        Status PrintList_Sq(SqList &L);
        Status CreationList_Sq(SqList &L,char *no,char *name,float &price);
        SqList L;    //定义L为SqList
        InitList_Sq(L);//初始化L
        char no,name;
        float price;
    cout<<"ISBN "<<"书名   "<<"价格   "<<endl;
        while(cin>>no>>name>>price){
          if(!strcmp(no,"0")&&!strcmp(name,"0")&&price==0.0)
                  break;
          CreationList_Sq(L,no,name,price);
        }
        PrintList_Sq(L);
        return 0;
}


//函数
Status InitList_Sq(SqList &L){
L.elem = new Book;
if(!L.elem) exit(OVERFLOW);
L.length = 0;//空表长度为0
return OK;
}

Status CreationList_Sq(SqList &L,char *no,char *name,float &price){
Book B;
strcpy(B.no,no);
strcpy(B.name,name);
B.price = price;
L.elem = B;
L.length++;
return OK;
}

Status PrintList_Sq(SqList &L){
    cout<<endl; //空一行
        cout<<L.length<<endl;//输出书本的总数
    cout<<"ISBN"<<" 书名   "<<"价格   "<<endl;
        for(int i = 0; i<L.length;i++){
          cout<<L.elem.no<<"   "<<L.elem.name<<"";
          printf("%.2f\n",L.elem.price);
        }
       return OK;
}


static/image/hrline/5.gif
运行结构如下:



static/image/hrline/5.gif
QaQ 作业还要画流程图!!!giao要命{:10_247:}

tomok 发表于 2021-11-5 09:06:58

学习了

页: [1]
查看完整版本: C++:基于顺序存储结构的图书信息表的创建和输出