关于用链表方式实现对超市定价管理
#include<iostream>#include<string>
using namespace std;
struct supermarket
{int num;string name;double price_trade;int pre_days;double price_retail;int count;supermarket *next;};
supermarket *creat(void);
void sum(supermarket *,int);
int n;
int main()
{
char c;
int number;
bool prime=true;
supermarket *head,*q;
cout<<"calculate retail price(y).exit program(n)."<<endl;
cin>>c;
if(c=='y'|| c=='Y')
head=creat();
else return 0;
while(prime)
{
cin>>number;
sum(head,number);
cout<<"continue or not?"<<endl;
cin>>c;
if(c=='y'|| c=='Y')
prime=true;
else prime=false;
}
q=head;
cout<<"商品的销售情况:"<<endl;
while(head!=0&&q!=0)
{
cout<<q->num<<' '<<q->name<<' '<<q->price_retail<<q->count;
q=q->next;
}
return 0;
}
supermarket *creat(void)
{
supermarket *head,*p1,*p2;
n=0;
p1=p2=new supermarket;
cout<<"请输入各个商品的编号,名称,批发价,预售天数:"<<endl;
cin>>p1->num>>p1->name>>p1->price_trade>>p1->pre_days;
p1->price_retail=p1->price_trade+p1->pre_days/4;
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=new supermarket;
p1->price_retail=p1->price_trade+p1->pre_days/4;
}
p2->next=NULL;
return head;
}
void sum(supermarket *head,int n)
{
supermarket *p;
p=head;
p->count=0;
if(head!=NULL)
while(p!=NULL)
{
if(p->num==n)
{p->count=p->count+1;break;}
p=p->next;
}
}
编译没问题,但是运行不出结果,求指教!