C++问题,求解答
求教小菜鸟 2014-06-08 04:15:13 假设某超级市场销售有m种商品(假设商品的编号为1,2,3,,m),有n台前台收款机(假设收款机的编号为1,2,3,,n)进行收款,以记录的形式提供给计算机,每个记录表示某台收款机的一种商品一次交易的数量和销售额。记录由4个域组成:收款机编号、商品编号、销售数量、销售金额。构造一个结构体类型,每次销售数据以一个结构体变量保存在一个数据文件中。 实现要求:
⑴ 编写实现将数据记录插入到数据文件的最后的函数;
⑵ 编写以收款机为单位的数据分类处理函数。构造n个单链表,每个链表保存一台收款机的销售记录,这n个单链表的头指针存放在一个指针数组中,通过数组的下标就可以知道是哪台收款机。读取数据文件的记录,将所有的销售记录(数据文件中的全部记录)分解插入到n个单链表;
#include<iostream>
#include<fstream>
#include<string>
const int M=5;//商品总类
const int N=3;//收款机总数
using namespace std;
class Goods1
{
private:
int no1; //收款机编号
int no2; //商品编号
int num; //销售总量
float total; //销售总额
public:
void setno1()
{
cout<<"请输入收款机编号(0~2):";
cin>>no1;
}
void setno2()
{
cout<<"请输入商品编号(0~4):";
cin>>no2;
}
void setnum()
{
cout<<"请输入销售总量:";
cin>>num;
}
void settotal()
{
cout<<"请输入销售总额:";
cin>>total;
}
int getno1()
{
return no1;
}
int getno2()
{
return no2;
}
int getnum()
{
return num;
}
float gettotal()
{
return total;
}
};
struct Goods2
{
int no1;
int no2;
int num;
float total;
struct Goods2* next;
};
void input()
{
ofstream outfile("file.txt",ios::app);
if(!outfile)
{
cout<<"outfile can't be opened:";
exit(1);
}
Goods1 goods[100];
char ch;
int i=0;
while(1)
{
cout<<"你想继续输入更多计入吗?(y/n):";
cin>>ch;
if(ch=='n' || ch=='N')
break;
i++;
goods[i].setno1();
goods[i].setno2();
goods[i].setnum();
goods[i].settotal();
outfile.write((char*)&goods[i],sizeof(Goods1));
}
outfile.close();
}
void classify1(Goods2* goods2[3])//以收款机为单位对数据分类
{
Goods2* p,* temp[3];
Goods1 goods1[100];
ifstream infile("file.txt");
if(!infile)
{
cout<<"infile can't be opened:";
exit(1);
}
infile.seekg(0);
int i=1;
infile.read((char *)&goods1[i],sizeof(Goods1));
while(infile)
{
p=new Goods2;
p->no1=goods1[i].getno1();
p->no2=goods1[i].getno2();
p->num=goods1[i].getnum();
p->total=goods1[i].gettotal();
if(goods1[i].getno1()==0)
{
if(goods2[0]==NULL)
goods2[0]=p;
else
temp[0]->next=p;
temp[0]=p;
}
if(goods1[i].getno1()==1)
{
temp[1]=p;
if(goods2[1]==NULL)
goods2[1]=p;
else
temp[1]->next=p;
temp[1]=p;
}
if(goods1[i].getno1()==2)
{
temp[2]=p;
if(goods2[2]==NULL)
goods2[2]=p;
else
temp[2]->next=p;
temp[2]=p;
}
i++;
infile.read((char *)&goods1[i],sizeof(Goods1));
}
temp[0]->next=NULL;
temp[1]->next=NULL;
temp[2]->next=NULL;
}
int main()
{
Goods2* goods2[3]={NULL,NULL,NULL};
int i;
cout<<"\t***********************************************"<<endl;
cout<<"\t* 选择功能 *"<<endl;
cout<<"\t* 1、插入记录 *"<<endl;
cout<<"\t* 2、以收款机为单位对数据分类 *"<<endl;
cout<<"\t* 0、退出 *"<<endl;
cout<<"\t***********************************************"<<endl;
cout<<"please choose functions(0~2):";
while(i>=0||i<<2)
{
cin>>i;
switch(i)
{
case 1: input();break;
case 2: classify1(goods2);break;
case 0: return 0;
}
cout<<"please choose functions(0~2):";
}
return 0;
}
编译没错,选项2运行不了。求解答