简单的增删查改和排序
#include <iostream>
#include <string>
#define MAXSIZE 100
using namespace std;
//结构体
typedef struct node
{
string name;
string id; //sex age
int price;
}node,*lnode;
typedef struct
{
node data[MAXSIZE];
int length;
}sequence,*link_sequence;
//函数声明区
void show_menu(void); //展示主菜单
void add(sequence * S); //添加
void show(sequence * S); //输出
void delete_info(sequence * S); //删除
void search(sequence * S); //查找
void change_info(sequence * S); //修改
int whether_exist(sequence * S,string name);
void NumberOfBook(sequence * S);
void sorting(sequence * S); //按照价格排序
void reverse(sequence * S); //逆置
void priceUpdate(sequence * S);
int main(void)
{
sequence S;
S.length = 0; //初始化
int select;
while(true)
{
show_menu();
cin >> select;
switch(select)
{
case 1:
add(&S);
break;
case 2:
show(&S);
break;
case 3:
delete_info(&S);
break;
case 4:
search(&S);
break;
case 5:
change_info(&S);
break;
case 6:
NumberOfBook(&S);
break;
case 7:
sorting(&S);
break;
case 8:
reverse(&S);
break;
case 9:
priceUpdate(&S);
break;
case 0:
cout << "欢迎下次使用!" << endl;
return 0;
break;
default:
break;
}
}
return 0;
}
void priceUpdate(sequence * S) {
for(int i = 0 ; i < S->length ; i ++) {
if(S->data[i].price < 45) {
S->data[i].price = (int)S->data[i].price * 1.2;
} else {
S->data[i].price = (int)S->data[i].price * 1.1;
}
}
cout << "价格更新成功! " << endl;
system("pause");
system("cls");
}
void reverse(sequence * S) {
node temp;
temp.id = "";
temp.name = "";
temp.price = 0;
for(int i = 0 ; i < S->length/2 ; i ++) {
temp = S->data[i];
S->data[i] = S->data[S->length-i-1];
S->data[S->length-i-1] = temp;
}
cout << "逆序成功! " << endl;
system("pause");
system("cls");
}
void sorting(sequence * S) {
node temp;
temp.id = "";
temp.name = "";
temp.price = 0;
for(int i = 0 ; i < S->length ; i ++) {
for(int j = i+1 ; j < S->length ; j ++) {
if(S->data[i].price > S->data[j].price) {
temp = S->data[i];
S->data[i] = S->data[j];
S->data[j] = temp;
}
}
}
cout << "排序完毕! " << endl;
system("pause");
system("cls");
}
void NumberOfBook(sequence * S) {
cout << "有" << S->length << "本书" << endl;
system("pause");
system("cls");
return;
}
void show_menu(void)
{
cout << "==========图书管理系统==========" << endl;
cout << "* *" << endl;
cout << "* 1-录入图书信息 *" << endl;
cout << "* 2-打印图书信息 *" << endl;
cout << "* 3-删除图书信息 *" << endl;
cout << "* 4-查找图书 *" << endl;
cout << "* 5-修改图书信息 *" << endl;
cout << "* 6-查询图书数量 *" << endl;
cout << "* 7-按照价格升序排序 *" << endl;
cout << "* 8-将图书逆序排序 *" << endl;
cout << "* 9-价格更新 *" << endl;
cout << "* 0-退出图书管理系统 *" << endl;
cout << "* *" << endl;
cout << "================================" << endl;
cout << "请输入选择:";
}
void add(sequence * S)
{
if(S->length == MAXSIZE)
{
cout << "信息已满,无法添加!" << endl;
return;
}
else
{
//姓名
string name;
cout << "请输入图书名:";
cin >> name;
S->data[S->length].name = name;
//书籍编号
string id;
cout << "请输入图书编号:";
cin >> id;
S->data[S->length].id = id;
//价格
int price;
cout << "请输入价格:";
cin >> price;
S->data[S->length].price = price;
S->length++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
void show(sequence * S)
{
// cout << "书名" << '\t' << "图书编号" << '\t' << "价格" << endl;
if(S->length == 0)
{
cout << "无信息!" << endl;
}
else
{
for(int i = 0 ; i < S->length ; i++)
{
cout << "书名:" << S->data[i].name << '\t' << "课程编号:" << S->data[i].id << '\t' << "价格:" << S->data[i].price << endl;
}
}
system("pause");
system("cls");
}
int whether_exist(sequence * S,string permit_number)
{
for(int i = 0 ; i < S->length ; i++)
{
if(S->data[i].id == permit_number)
{
return i;
}
}
return -1; //查找不到则返回-1
}
void delete_info(sequence * S)
{
int ret;
string search_number;
cout << "请输入所要查找的图书编号:";
cin >> search_number;
ret = whether_exist(S,search_number);
if(ret == -1)
{
cout << "查无此书!" << endl;
}
else
{
for(int i = ret ; i < S->length ; i++)
{
S->data[i] = S->data[i+1];
}
S->length--;
cout << "删除成功!" << endl;
}
system("pause");
system("cls");
}
void search(sequence * S)
{
if(S->length == 0)
{
cout << "无书!" << endl;
system("pause");
system("cls");
return;
}
cout << "请输入要查找的图书编号:";
string search_permit_name;
cin >> search_permit_name;
int ret;
ret = whether_exist(S,search_permit_name);
if(ret == -1)
{
cout << "查无此书!" << endl;
}
else
{
cout << "书名:" << S->data[ret].name << '\t' << "课程编号:" << S->data[ret].id << '\t' << "价格:" << S->data[ret].price << endl;
}
system("pause");
system("cls");
}
void change_info(sequence * S)
{
cout << "请输入要修改的图书编号:" ;
string num;
cin >> num;
int ret;
ret = whether_exist(S,num);
if(ret == -1)
{
cout << "此书不存在!" << endl;
}
else
{
//图书名
string name;
cout << "请输入书名:";
cin >> name;
S->data[ret].name = name;
//图书编号
string id;
cout << "请输入图书编号:";
cin >> id;
S->data[ret].id = id;
//价格
int price;
cout << "Enter the price of the book:";
cin >> price;
S->data[ret].price = price;
cout << "修改成功" << endl;
}
system("pause");
system("cls");
}