标签:struct ace cin ase 输出 new nbsp next stream
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int status;
typedef int ElemType;
string h1,h2,h3;
int length=0;
typedef struct{
string id;
string name;
double price;
}Book;
typedef struct LNode
{
Book data;
struct LNode *next;
}LNode,*LinkList;
status InitList_L(LinkList &L)
{
L=new LNode;
L->next=NULL;
return OK;
}
status CreatList(LinkList &L)
{
LinkList p,r;
fstream file;
file.open("d:\\book.txt");
if(!file)
{
cout<<"错误\n";
exit(ERROR);
}
file>>h1>>h2>>h3;
r=L;
while(!file.eof())
{
p=new LNode;
file>>p->data.id>>p->data.name>>p->data.price;
r->next=p;
p->next=NULL;
r=p;
length++;
}
return OK;
}
status SearchList(LinkList &L,int e)
{
LinkList p;
int i=0;
p=L->next;
while(p->next!=NULL)
{
if(p->data.price==e)
{
cout<<setw(10)<<p->data.id<<setw(30)<<p->data.name<<setw(10)<<p->data.price<<endl;
i++;
}
p=p->next;
}
if(i==0)
return ERROR;
else
return OK;
}
status ShuChu(LinkList &L)
{
LinkList p;
p=L->next;
while(L->next!=NULL && p->next!=NULL)
{
cout<<setw(10)<<p->data.id<<setw(30)<<p->data.name<<setw(10)<<p->data.price<<endl;
p=p->next;
}
}
status Insert(LinkList &L,int i
)
{
Book book;
do
{
cout<<"请输入要插入的位置:\n";
cin>>i;
}while(i<=0&&i>length);
cout<<"请输入要插入的内容\n";
cin>>book.id>>book.name>>book.price;
LinkList p,r;
r=new LNode;
r->data.id=book.id;
r->data.name=book.name;
r->data.price=book.price;
p=L;
for(int j=1;j<i;j++)
{
p=p->next;
}
r->next=p->next;
p->next=r;
return OK;
}
status DeleteList(LinkList &L,int i)
{
LinkList p,r;
p=L;
for(int j=1;j<i;j++)
{
p=p->next;
}
r=p->next;
p->next=r->next;
free(r);
}
int main()
{
int choose=-1,i;
LinkList L;
cout<<"请选择:\n";
cout<<"1.创建表格\n";
cout<<"2.插入数据\n";
cout<<"3.查找\n";
cout<<"4.插入\n";
cout<<"5. 删除\n";
cout<<"6. 输出数据\n";
cout<<"0. 退出\n";
while(choose)
{
cin>>choose;
switch(choose)
{
case 1://创建单链表
if(InitList_L(L))
cout<<"创建成功\n";
else
cout<<"创建失败\n";
break;
case 2://插入数据
if(CreatList(L))
cout<<"数据插入成功\n";
else
cout<<"数据插入失败\n";
break;
case 3://按价格查找
int e;
cout<<"请输入所要查找的价格\n";
cin>>e;
if(!SearchList(L,e))
cout<<"查找失败\n";
break;
case 4://插入新数据
if(Insert(L,i))
cout<<"插入成功\n";
break;
case 5://删除数据
int i;
do
{
cout<<"请输入要删除的输的位置\n";
cin>>i;
}while(i<=0&&i>length);
if(DeleteList(L,i))
cout<<"删除成功\n";
else
cout<<"删除失败\n";
break;
case 6://输出数据
ShuChu(L);
break;
}
}
return 0;
}
标签:struct ace cin ase 输出 new nbsp next stream
原文地址:http://www.cnblogs.com/2446291782Single/p/7531516.html