#include
#include
#include
usingnamespace std
constint Maxr=100
constint Maxb=100
constint Maxbor=5
classReader
{
private:
inttag
intno
charname[10]
intborbook[Maxbor]
public:
Reader(){}
char*getname() {return name}
intgettag() {return tag}
intgetno() {return no}
voidsetname(char na[])
{
strcpy(name,na)
}
voiddelbook(){ tag=1}
voidaddreader(int n,char *na)
{
tag=0
no=n
strcpy(name,na)
for(inti=0i<Maxbori++)
borbook[i]=0
}
voidborrowbook(int bookid)
{
for(inti=0i<Maxbori++)
{
if (borbook[i]==0)
{
borbook[i]=bookid
return
}
}
}
intretbook(int bookid)
{
for(inti=0i<Maxbori++)
{
if(borbook[i]==bookid)
{
borbook[i]=0
return1
}
}
return0
}
voiddisp()
{
cout<<setw(5) <<no <<setw(10) <<name<<"借书编号:["
for(inti=0i<Maxbori++)
if(borbook[i]!=0)
cout<<borbook[i] <<"|"
cout<<"]"<<endl
}
}
classRDatabase
{
private:
inttop
Readerread[Maxr]
public:
RDatabase()
{
Readers
top=-1
fstreamfile("reader.txt",ios::in)
while(1)
{
file.read((char*)&s,sizeof(s))
if(!file)break
top++
read[top]=s
}
file.close()
}
voidclear()
{
top=-1
}
intaddreader(int n,char *na)
{
Reader*p=query(n)
if(p==NULL)
{
top++
read[top].addreader(n,na)
return1
}
return0
}
Reader*query(int readerid)
{
for(int i=0i<=topi++)
if(read[i].getno()==readerid &&
read[i].gettag()==0)
{
return&read[i]
}
returnNULL
}
voiddisp()
{
for(int i=0i<=topi++)
read[i].disp()
}
voidreaderdata()
~RDatabase()
{
fstreamfile("reader.txt",ios::out)
for(int i=0i<=topi++)
if(read[i].gettag()==0)
file.write((char*)&read[i],sizeof(read[i]))
file.close()
}
}
voidRDatabase::readerdata()
{
charchoice
charrname[20]
intreaderid
Reader*r
while(choice!='0')
{
cout<<"******欢迎使用图书销售管理系统终极版******”<<endl
cin>>choice
switch(choice)
{
case'1':
cout<<"****** *** 作菜单******:"
cin>>readerid
cout<<"******收银员******"
cin>>rname
addreader(readerid,rname)
break
case'2':
cout<<"******管理员******:"
cin>>readerid
r=query(readerid)
if(r==NULL)
{
cout<<" ******顾客****** "<<endl
break
}
cout<<"输入新的姓名:"
cin>>rname
r->setname(rname)
break
case'3':
cout<<" 输入图书编号:"
cin>>readerid
r=query(readerid)
if(r==NULL)
{
cout<<" 该图书不存在"<<endl
break
}
r->delbook()
break
case'4':
cout<<"读入图书编号:"
cin>>readerid
r=query(readerid)
if(r==NULL)
{
cout<<"该图书不存在"<<endl
break
}
r->disp()
break
case'5':
disp()
break
case'6':
clear()
break
default:cout<<"输入错误,请从新输入:"break
}
}
}
classBook
{
private:
inttag
intno
charname[20]
intonshelf
public:
Book(){}
char*getname() { return name}
intgetno(){ return no}
intgettag(){ return tag}
voidsetname(char na[])
{
strcpy(name,na)
}
voiddelbook(){ tag=1}
voidaddbook(int n,char *na)
{
tag=0
no=n
strcpy(name,na)
onshelf=1
}
intborrowbook()
{
if(onshelf==1)
{
onshelf=0
return1
}
return0
}
voidretbook()
{
onshelf=1
}
voiddisp()
{
cout<<setw(6) <<no <<setw(18) <<name <<setw(10)
<<(onshelf==1?"缺货")<<endl
}
}
classBDatabase
{
private:
inttop
Bookbook[Maxb]
public:
BDatabase()
{
Bookb
top=-1
fstreamfile("book.txt",ios::in)
while(1)
{
file.read((char*)&b,sizeof(b))
if(!file) break
top++
book[top]=b
}
file.close()
}
voidclear()
{
top=-1
}
intaddbook(int n,char *na)
{
Book*p=query(n)
if(NULL==p)
{
top++
book[top].addbook(n,na)
return1
}
return0
}
Book*query(int bookid)
{
for(int i=0i<=topi++)
if(book[i].getno()==bookid &&book[i].gettag()==0)
{
return&book[i]
}
returnNULL
}
voidbookdata()
voiddisp()
{
for(int i=0i<=topi++)
if(book[i].gettag()==0)
book[i].disp()
}
~BDatabase()
{
fstreamfile("book.txt",ios::out)
for(int i=0i<=topi++)
if(book[i].gettag()==0)
file.write((char*)&book[i],sizeof(book[i]))
file.close()
}
}
voidBDatabase::bookdata()
{
charchoice
charbname[40]
intbookid
Book*b
while(choice!='0')
{
cout<<"\n\n\n\t\t\t图书维护"<<endl<<endl
cin>>choice
switch(choice)
{
case'1':
cout<<"输入图书编号:"<<endl
cin>>bookid
cout<<"输入图书书名:"<<endl
cin>>bname
addbook(bookid,bname)
break
case'2':
cout<<"输入图书编号:"<<endl
cin>>bookid
b=query(bookid)
if(b==NULL)
{
cout<<" 该图书缺货"<<endl
break
}
cout<<"输入新的书名:"<<endl
cin>>bname
b->setname(bname)
break
case'3':
cout<<" 读入图书编号:"<<endl
cin>>bookid
b=query(bookid)
if(b==NULL)
{
cout<<" 该图书缺货"<<endl
break
}
b->delbook()
break
case'4':
cout<<" 读入图书编号:"<<endl
cin>>bookid
b=query(bookid)
if(b==NULL)
{
cout<<" 该图书不存在"<<endl
break
}
b->disp()
break
case'5':
disp()
break
case'6':
clear()
break
default:cout<<"输入错误,请从新输入:"
}
}
}
voidmain()
{
charchoice
intbookid,readerid
RDatabaseReaderDB
Reader*r
BDatabaseBookDB
Book*b
while(choice!='0')
{
cout<<endl<<endl<<"\t\t\t 图书销售管理系统\n\n\n"
cin>>choice
switch(choice)
{
case'1':
cout<<" 买书图书编号:"
cin>>readerid
cout<<" 图书编号:"
cin>>bookid
r=ReaderDB.query(readerid)
if(NULL==r)
{
cout<<" 不存在该图书,不能买书"<<endl
break
}
b=BookDB.query(bookid)
if(b==NULL)
{
cout<<" 不存在该图书,不能买书"<<endl
break
}
if(b->borrowbook()==0)
{
cout<<" 该图书已售完,不能买书"<<endl
break
}
r->borrowbook(b->getno())
break
case'2':
cout<<"买书\n 图书编号:"
cin>>readerid
cout<<" 图书编号:"
cin>>bookid
r=ReaderDB.query(readerid)
if(r==NULL)
{
cout<<" 不存在该图书,不能买书"<<endl
break
}
b=BookDB.query(bookid)
if(b==NULL)
{
cout<<" 不存在该图书,不能买书"<<endl
break
}
b->retbook()
r->retbook(b->getno())
break
case'3':
BookDB.bookdata()
break
case'4':
ReaderDB.readerdata()
break
default:cout<<"输入错误,请从新输入:"
}
}
}
//从键盘输入N个整型数,并求出最大值、总和以及平均值。//当输入为0时,程序结束并输出计算结果。
#include "stdio.h"
#include "stdlib.h"
main()
{float aver
int a[10000],temp,i,sum=0,max=-100000
printf("\n请输入数,以0表示结束:\n")
for(i=0i++)
{scanf("%d",&temp)
if(temp==0)
break
a[i]=temp
if(a[i]>max)
max=a[i]
sum=sum+a[i]
}
aver=sum/i
printf("\n最大值是%d 总和是:%d 平均值是:%f\n",max,sum,aver)
system("pause")
}
//1-1/2+1/3-1/4+1/5-1/6+……..+1/99-1/100
#include "stdio.h"
#include "stdlib.h"
main()
{float sum=0,temp
int i,fuhao=1
for(i=1i<=100i++)
{temp=1.0/i*fuhao
sum=sum+temp
fuhao=-fuhao
}
printf("\n总和是:%f\n",sum)
system("pause")
}
//设计一个竞赛用的程序:输入10个评委的评分
//用实型量,二位小数),去掉一个最高分和一个最低分,
//选手的最后得分为剩下分数的平均分数。最后输出最高分和最低分以及平均分。
#include "stdio.h"
#include "stdlib.h"
main()
{float a[10],max=-100000,min=100000,sum=0,aver
int i
printf("\n请输入10个数:\n")
for(i=0i<10i++)
{scanf("%f",&a[i])
if(a[i]>max)
max=a[i]
if(a[i]<min)
min=a[i]
sum=sum+a[i]
}
aver=sum/10
sum=sum-max-min
printf("\n最后得分为:%.2f\n",sum/8)
printf("\n最高分是:%.2f 最低分是:%.2f 平均分是:%.2f\n",max,min,aver)
system("pause")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)