【c++程序】图书馆购书系统(尚存问题)

#include<iostream>
#include<string>
using namespace std;
class book
{
public:
	int num;
	float price;
	book *next;
};
book *head=NULL;
bool check(string str)//检查是否是数字
{
	for(int i=0;i<str.length();i++)
	{
		if((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
			return false;
	}
}
book *creat()
{
	book *p1,*p2;
	p1=new book;
	head=p1;
	p2=p1;
	cout<<"请输入图书的编号,以0结束"<<endl;
	string str;
	cin>>str;
	while(!check(str))
	{
		cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
		cin>>str;
	}
	p1->num=atoi(str.c_str());//将c++字符串通过成员函数c_str()转换成c语言格式的字符串;atoi将其转换成整数
	if(p1->num!=0)
	{
		cout<<"请输入图书的价格"<<endl;
		cin>>str;
	while(!check(str))
	{
		cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
		cin>>str;
	}
	p1->price=atof(str.c_str());//c++字符串通过成员函数c_str()转换成c语言格式的字符串;atof将其转换成浮点型
	}
	else 
	{
		delete p1;p2=NULL;p2->next=NULL;head=NULL;return head;
	}
	while(p1->num!=0)
	{
		p2=p1;
		p1=new book;
		cout<<"请输入图书的编号,以0结束"<<endl;
		cin>>str;
		while(!check(str))
		{
			cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
			cin>>str;
		}
		p1->num=atoi(str.c_str());//将c++字符串通过成员函数c_str()转换成c语言格式的字符串;atoi将其转换成整数
		if(p1->num!=0)
		{
			cout<<"请输入图书的价格"<<endl;
			cin>>str;
		while(!check(str))
		{
			cout<<"输入的不是数字,请重新输入,按0返回"<<endl;
			cin>>str;
		}
		p1->num=atoi(str.c_str());//将c++字符串通过成员函数c_str()转换成c语言格式的字符串;atoi将其转换成整数
		}
		p2->next=p1;
	}
	delete p1;
	p2->next=NULL;
	return head;
}
void showbook(book*head)
{
	cout<<"图书的信息:"<<endl;
	while(head)
	{
		cout<<"图书编号"<<head->num<<"\t";
		cout<<"图书价格"<<head->price<<endl;
		head=head->next;
	}
}
void Delete(book*head,int num)
{
	book*l;
	if(head->num==num)
	{
		l=head;
		head=head->next;
		::head=head;
		delete l;
		cout<<"操作成功"<<endl;
	}
	while(head)
	{
		if(head->next==NULL)
		{
			cout<<"找不到要删除的编号"<<endl;
			return;
		}
		if(head->next->num==num)
		{
			l=head->next;
			head->next=l->next;
			delete l;
			cout<<"操作成功"<<endl;
			return ;
		}
		head=head->next;
	}
	cout<<"找不到要删除的内容"<<endl;
}
void insert(book*head,int num,float price)
{
	book*list=new book;
	book*l;
	while(head)
	{
		l=head;
		head=head->next;
	}
	l->next=list;
	list->num=num;
	list->price=price;
	list->next=NULL;
}
int GetBookNum(book*head)
{
	int num=0;
	while(head)
	{
		num++;
		head=head->next;
	}
	return num;
}
int main()
{
	//book*head=NULL;
	head=creat();
	showbook(head);
	cout<<"请输入你要删除的图书编号"<<endl;
	int BookNum;
	cin>>BookNum;
	Delete(head,BookNum);
	showbook(head);
	cout<<"请输入你要插入的编号"<<endl;
	cin>>BookNum;
	cout<<"请输入你要插入的价格"<<endl;
	float BookPrice;
	cin>>BookPrice;
	insert(head,BookNum);
	showbook(head);
	cout<<"图书数目:"<<GetBookNum(head)<<endl;
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值