图书信息管理系统(C语言)

题目信息:

每种图书包括下列三部分信息:书号,书名,价格

查找:根据指定的书号查找相关图书的信息,并返回该图书在表中的位置

插入

删除

修改:根据指定的书号,修改图书价格

排序:将表中图书按照价格由低到高排序

计数:计算表中图书数量

#include<iostream>

#include<string.h>

using namespace std;

typedef struct{

char no[20];

char name[50];

float price;

}ElemType;

typedef struct Node{

ElemType data;

struct Node *next;

}Node;

int Init(Node *&L){ //初始化表

L = new Node;

L->next = NULL;

return 1;

}

void Creat(Node *L){ //创建表

int n;

cout<<"please input the number of Elem: ";

cin>>n;

Node *s;

cout<<"please input BookNumber,BookName,BookPrice:"<<endl ;

for(int i = 0; i < n; i++){

s = new Node;

cin>>s->data.no>>s->data.name>>s->data.price;

s->next = L->next;

L->next = s;

}

}

int Show(Node *L){ //显示表中所有元素

cout<<"所有图书信息:"<<endl;

Node *p = L->next;

if(!p){

cout<<"表为空!";

return 0;

}

while(p){

cout<<p->data.no<<" / "<<p->data.name<<" / "<<p->data.price<<" / "<<endl;

p = p->next;

}

cout<<endl<<endl;

}

void Locate(Node *L, ElemType e) { //根据书号返回元素所在位置

int Length(Node *L);

int i = 1;

Node *p;

p = L->next;

while (p && strcmp(p->data.no,e.no) != 0){

p = p->next;

i++;

}

if(i > Length(L))cout<<"无本书信息!"<<endl;

else cout<<"本书所在位置:"<<i<<endl;

}

int Input(Node *L, int i, ElemType x){ //在表中插入元素

Node *p = L;

int j = 0;

while((j+1 < i)&&p){

p = p->next;

j++;

}

if(!p||(j+1) > i){

cout<<"插入位置错误!";

return 0;

}

Node *s = new Node;

s->data = x;

s->next = p->next;

p->next = s;

return 1;

}

int Delete(Node *L, int i){ //在表中删除元素

Node *p = L;

int j = 0;

while((j+1 < i)&&p->next){

p = p->next;

j++;

}

if(!(p->next)||(j+1) > i){

cout<<"删除位置错误!";

return 0;

}

Node *s = p->next;

p->next = s->next;

delete s;

return 1;

}

int Length(Node *L){ //返回表的长度

cout<<endl;

Node *s = L->next;

int length = 0;

while(s){

s = s->next;

length++;

}

cout<<"表长:"<<length<<endl;

return length;

}

void ChangePrice(Node *L){ //根据指定书号修改图书价格

char noo[20];

float np;

cout<<"请输入要修改元素的书号(目的:改价格): ";

cin>>noo;

Node *p;

p = L->next;

while (p && strcmp(p->data.no,noo) != 0){

p = p->next;

}

if(p){

cout<<"此书原价格:"<<p->data.price<<endl;

cout<<"请输入此书新价格:";

cin>>np;

p->data.price = np;

cout<<"价格改动成功!"<<endl;

}

else cout<<"无此书号!";

}

int OrderPrice(Node *L){ //将图书按照价格从低到高排序

ElemType t;

Node *end = NULL;

Node *b = L->next;

while(L->next != end){

while(b->next != end){

if(b->data.price > b->next->data.price){

t = b->data;

b->data = b->next->data;

b->next->data = t;

}

b = b->next;

}

end = b;

b = L->next;

}

cout<<"排序后:"<<endl;

return 1;

}

int main(){

ElemType x,y;

Node *List;

Init(List);

Creat(List);

Show(List);

cout<<"please input no,name,price of x(用于在表中查找是否有此书信息):";

cin>>x.no>>x.name>>x.price;

Locate(List, x);//根据书号返回元素所在位置

cout<<"please input no,name,price of y(用于在表中添加此书信息):";

cin>>y.no>>y.name>>y.price;

Input(List,3,y);//在表中插入元素

cout<<endl<<"after input elem:"<<endl;

Show(List);

Delete(List,2);//在表中删除元素

cout<<endl<<"after delete elem:"<<endl;

Show(List);

Length(List);

ChangePrice(List); //根据指定书号修改图书价格

OrderPrice(List); //将图书按照价格从低到高排序

Show(List);

}

图书信息管理系统(C语言)

相关推荐