本帖最后由 lumber2388779 于 2017-4-6 14:29 编辑
1.函数定义不能放在头文件中的,头文件只是用于放函数声明,还有变量定义等等,不能用于函数定义,你可以写多一个C文件用于放这些函数定义,头文件写上这些声明就可以了
2.你的initP并没有任何地方调用到这个函数,你所有创建出来的结构体指针都是空的,并没有指向任何结构体,这是你报错最主要原因
3.Headcus和Headpro这两个指针复用率太高,容易出错,建议使用typedef定义结构体进行使用
还有其他一些小问题,这些靠你自己去找下了
shangpinhuiyuan.cpp#include<iostream>
#include<fstream>
#include<string>
#include"shangpinhuiyuan.h"
using namespace std;
void initsystem()
{
cout<<"欢迎光临本超市"<<endl;
}
void managerwindow()
{
int k=1;
while(k)
{
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<<"10.查询让利情况。"<<endl;
cout<<"11.退出系统。"<<endl;
cin>>k;
switch(k){
case 1:showpro();break;
case 2:rcheck();break;
case 3:deletep();break;
case 4:addpro();break;
case 5:changep();break;
case 6:showcus();break;
case 7:changec();break;
case 8:deletec();break;
case 9:cout<<"暂定"<<endl;
}
}
}
void userwindow()
{
int i=1;
while(i)
{cout<<"请选择你要的服务"<<endl;
cout<<"1.选购商品。"<<endl;
cout<<"2.以管理员身份登入。"<<endl;
cout<<"3.退出商店。"<<endl;
cin>>i;
switch(i){
case 1:cout<<"1.选购商品。"<<endl;break;
case 2:managerwindow();break;
case 3:i=0;break;
default:cout<<"输入错误,请重新输入"<<endl;
}
}
}
int main()
{
initsystem();
initP();
while(1)
{
while(1)
{
userwindow();
break;
}
managerwindow();
break;
}
return 0;
}
shangpinhuiyuan.h#include<iostream>
#include<string>
using namespace std;
typedef struct Product
{
int pid;
string pname;
float pprice;
int pnum;
struct Product *next;
}*Headpro,xHeadpro;
typedef struct Customer
{
int cid;
string cname;
string ctele;
float cjifen;
struct Customer *next;
}*Headcus,xHeadcus;
void initP();
Headpro findpro(string a);
Headpro findpro(int a);
Headpro endp();
void addpro();
void showpro();
void deletep();
void check();
void rcheck();
void changep();
Headcus findcus(int a);
void showcus();
void deletec();
void changec();
func.cpp#include<iostream>
#include<string>
#include"Test7.h"
using namespace std;
Headpro headpro = NULL;
Headcus headcus = NULL;
void initP()//初始化
{
headpro=new xHeadpro;
headpro->pid=0000;
headpro->pname="water";
headpro->pnum=9;
headpro->pprice=1.5;
headpro->next=NULL;
headcus=new xHeadcus;
headcus->cid=0000;
headcus->cname="jiajia";
headcus->ctele="1111111";
headcus->cjifen=0;
headcus->next=NULL;
}
Headpro findpro(string a)//按名称查找商品
{
Headpro pro;
pro=new xHeadpro;
pro=headpro->next;
while(pro!=NULL&&pro->pname!=a)
{
pro=pro->next;
}
if(pro==NULL){cout<<"没有该商品"<<endl;return NULL;}
else return pro;
}
Headpro findpro(int a)//按数目查找商品
{
Headpro pro;
pro=headpro->next;
while(pro!=NULL&&pro->pnum<=a)
{
pro=pro->next;
}
if(pro==NULL){cout<<"已找出所有符合要求的商品。"<<endl;return NULL;}
else return pro;
}
Headpro endp()
{
Headpro prr;
prr=headpro;
if(prr->next!=NULL)
{
prr=prr->next;
}
return prr;
}
void addpro()//添加商品
{
Headpro have,proo;
proo=new xHeadpro;
cout<<"请输入要添加商品名称"<<endl;
string name;
cin>>name;
have=findpro(name);
if(have!=NULL)
{cout<<"已经存在此商品"<<endl;
return;}
have=endp();
have->next=proo;proo->next=NULL;
cout<<"请输入商品的具体信息"<<endl;
cout<<"商品ID"<<endl;cin>>proo->pid;
cout<<"商品名称"<<endl;cin>>proo->pname;
cout<<"商品价格"<<endl;cin>>proo->pprice;
cout<<"商品数目"<<endl;cin>>proo->pnum;
cout<<"添加商品成功"<<endl;
}
void showpro()//显示全部商品信息
{
Headpro pro;
if(headpro->next!=NULL)
{
cout<<"商品ID"<<','<<"商品名称"<<','<<"商品价格"<<','<<"商品数目"<<endl;
do
{
pro=headpro->next;
cout<<pro->pid<<" "<<pro->pname<<" "<<pro->pprice<<" "<<pro->pnum<<endl;
}
while(pro->next!=NULL);
}
}
void deletep()//删除商品
{
cout<<"输入要删除的商品名称"<<endl;
string name;
cin>>name;
Headpro pro,temp;
pro=findpro(name);
if(pro==NULL)
cout<<"不存在该商品"<<endl;
temp=headpro;
while(temp->next!=pro)
temp=temp->next;
temp->next=pro->next;
free(pro);
}
void check()//查询某一商品
{
cout<<"输入所要查找商品名称"<<endl;
string name;
cin>>name;
Headpro pro;
pro=findpro(name);
if(pro==NULL)
cout<<"不存在该商品"<<endl;
else{
cout<<"商品ID"<<','<<"商品名称"<<','<<"商品价格"<<','<<"商品数目"<<endl;
cout<<pro->pid<<" "<<pro->pname<<" "<<pro->pprice<<" "<<pro->pnum<<endl;}
}
void rcheck()//查询少于一定数目的商品
{
cout<<"输入要查询少于多少数目的商品"<<endl;
int i;
cin>>i;
Headpro pro;
pro=findpro(i);
while(pro!=NULL){
cout<<"商品ID"<<','<<"商品名称"<<','<<"商品价格"<<','<<"商品数目"<<endl;
cout<<pro->pid<<" "<<pro->pname<<" "<<pro->pprice<<" "<<pro->pnum<<endl;}
}
void changep()//修改商品信息
{
cout<<"输入要删除的商品名称"<<endl;
string name;
cin>>name;
Headpro pro;
pro=findpro(name);
if(pro==NULL)
cout<<"不存在该商品"<<endl;
int i=1;
while(i)
{
cout<<"请问你要修改该商品的:"<<endl;
cout<<" 1.ID 2.名称 3.价格 4.数目 5.不再修改"<<endl;
cin>>i;
switch(i)
{
case 1:cout<<"修改ID为:"<<endl;cin>>pro->pid;break;
case 2:cout<<"修改名称为:"<<endl;cin>>pro->pname;break;
case 3:cout<<"修改价格为:"<<endl;cin>>pro->pprice;break;
case 4:cout<<"修改数目为:"<<endl;cin>>pro->pnum;break;
case 5:i=0;break;
default:cout<<"请选择正确选项"<<endl;
}
}
}
Headcus findcus(int a)//按会员代号查询信息
{
Headcus cus;
cus=headcus->next;
while(cus!=NULL&&cus->cid!=a)
{
cus=cus->next;
}
if(cus==NULL){cout<<"没有该商品"<<endl;return NULL;}
else return cus;
}
void showcus()//显示会员信息
{
Headcus cus;
cus=headcus;
cout<<"会员代码"<<" "<<"会员姓名"<<" "<<"会员电话"<<" "<<"会员积分"<<endl;
if(cus->next!=NULL)
{
cus=cus->next;
cout<<cus->cid<<'.'<<cus->cname<<','<<cus->ctele<<','<<cus->cjifen<<endl;
}
}
void deletec()//删除会员
{
cout<<"输入要删除的会员ID"<<endl;
int i;
cin>>i;
Headcus cus,temp;
cus=findcus(i);
if(cus==NULL)
cout<<"不存在该会员"<<endl;
temp=headcus;
while(temp->next!=cus)
temp=temp->next;
temp->next=cus->next;
free(cus);
}
void changec()
{
cout<<"请输入要更改会员代号"<<endl;
int i;
cin>>i;
Headcus cus;
cus=findcus(i);
if(cus==NULL)
cout<<"不存在此会员"<<endl;
while(i)
{
cout<<"你要修改该会员的:"<<endl;
cout<<"1.代号"<<"2.姓名"<<"3.电话"<<"4.积分"<<"5.修改结束"<<endl;
switch(i)
{
case 1:cin>>cus->cid;break;
case 2:cin>>cus->cname;break;
case 3:cin>>cus->ctele;break;
case 4:cin>>cus->cjifen;break;
case 5:i=0;break;
default:cout<<"输入错误"<<endl;
}
}
}
这是我修改的代码 你看下 |