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运行不了。求解答
...全文
234 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
91program 2014-06-09
  • 打赏
  • 举报
回复
个人认为这种问题,调试一下应该还容易发现问题的,这就是能力的表现啊!
赵4老师 2014-06-09
  • 打赏
  • 举报
回复
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...
 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
while (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
while (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。
子夜一段星 2014-06-09
  • 打赏
  • 举报
回复
while(i>=0||i<<2)?应该改成与and,后面的是位移操作付,应该改成小于等于。代码太长了没看

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧