![]() |
#2
rjsp2018-12-17 09:52
|
运行时出现了
只有本站会员才能查看附件,请 登录
查了一下说是栈堆溢出,求求大神解释一下,改如何处理

头文件:

#include<iostream>
#include<fstream>
using namespace std;
struct goods
{
int num; //货物编号
int times; //货物被购买的次数
};
class SeqList
{
private:
int length;
int maxsize;
goods *data;
public:
SeqList(int MaxListSize); //构造函数
~SeqList() { delete[] data; } //析构函数
void CreateList(ifstream &F); //创建表
void PrintList(ofstream &F); //打印表
int Partition(int i, int j); //快速排序一趟划分
int get_length();
};
SeqList::SeqList(int MaxListSize)
{
maxsize = MaxListSize;
data = new goods[maxsize + 1];
length = 0;
data->times = { 0 }; //给所有的商品的次数赋初值为0
}
void SeqList::CreateList(ifstream &F)
{
int x;
while (F >> x)
{
if (data[x].times == 0) //如果商品x没出现过,则长度加1
{
length++;
data[x].times++;
}
else
data[x].times++;
}
}
//打印表——待会儿要改成存入文件
void SeqList::PrintList(ofstream &F)
{
for (int i = 1; i <= length; i++)
{
F << i << "号货架——" << data[i].num << "号商品" << endl;
}
}
//交换排序--对子区间进行快速排序
int SeqList::Partition(int i, int j)
{ //对R[i]…R[j]区间内的记录进行一次划分排序
goods x = data[i]; //用区间的第一个记录为基准
while (i<j) {
while (i<j&&data[j].times >= x.times)
j--; //从j所指位置起向前(左)搜索
if (i<j) {
data[i] = data[j];
i++;
}
while (i<j&&data[i].times <= x.times)
i++; //从i所指位置起向后(右)搜索
if (i<j) {
data[j] = data[i];
j--;
}
}
data[i] = x; //基准记录x位于最终排序的位置i上
return i;
}
//快速排序 注意:QuickSort不是成员函数,也不是友元函数。是C++函数。
void QuickSort(SeqList &R, int low, int high)
{ //对顺序表R中的子区间进行快速排序
int p = R.Partition(low, high); //做一次划分排序
QuickSort(R, low, p - 1); //对左区间递归排序
QuickSort(R, p + 1, high); //对右区间递归排序
}
int SeqList::get_length()
{
return length;
}
#include<fstream>
using namespace std;
struct goods
{
int num; //货物编号
int times; //货物被购买的次数
};
class SeqList
{
private:
int length;
int maxsize;
goods *data;
public:
SeqList(int MaxListSize); //构造函数
~SeqList() { delete[] data; } //析构函数
void CreateList(ifstream &F); //创建表
void PrintList(ofstream &F); //打印表
int Partition(int i, int j); //快速排序一趟划分
int get_length();
};
SeqList::SeqList(int MaxListSize)
{
maxsize = MaxListSize;
data = new goods[maxsize + 1];
length = 0;
data->times = { 0 }; //给所有的商品的次数赋初值为0
}
void SeqList::CreateList(ifstream &F)
{
int x;
while (F >> x)
{
if (data[x].times == 0) //如果商品x没出现过,则长度加1
{
length++;
data[x].times++;
}
else
data[x].times++;
}
}
//打印表——待会儿要改成存入文件
void SeqList::PrintList(ofstream &F)
{
for (int i = 1; i <= length; i++)
{
F << i << "号货架——" << data[i].num << "号商品" << endl;
}
}
//交换排序--对子区间进行快速排序
int SeqList::Partition(int i, int j)
{ //对R[i]…R[j]区间内的记录进行一次划分排序
goods x = data[i]; //用区间的第一个记录为基准
while (i<j) {
while (i<j&&data[j].times >= x.times)
j--; //从j所指位置起向前(左)搜索
if (i<j) {
data[i] = data[j];
i++;
}
while (i<j&&data[i].times <= x.times)
i++; //从i所指位置起向后(右)搜索
if (i<j) {
data[j] = data[i];
j--;
}
}
data[i] = x; //基准记录x位于最终排序的位置i上
return i;
}
//快速排序 注意:QuickSort不是成员函数,也不是友元函数。是C++函数。
void QuickSort(SeqList &R, int low, int high)
{ //对顺序表R中的子区间进行快速排序
int p = R.Partition(low, high); //做一次划分排序
QuickSort(R, low, p - 1); //对左区间递归排序
QuickSort(R, p + 1, high); //对右区间递归排序
}
int SeqList::get_length()
{
return length;
}
CPP文件:

#include<iostream>
#include<fstream>
#include<string>
#include"SeqList.h"
using namespace std;
void main()
{
ifstream testfile;
ofstream savefile;
testfile.open("retail.dat");
savefile.open("savefile.dat");
SeqList L(16470);
L.CreateList(testfile);
int len = L.get_length();
QuickSort(L, 1, len);
L.PrintList(savefile);
testfile.close();
savefile.close();
L.~SeqList();
system("pause");
}
#include<fstream>
#include<string>
#include"SeqList.h"
using namespace std;
void main()
{
ifstream testfile;
ofstream savefile;
testfile.open("retail.dat");
savefile.open("savefile.dat");
SeqList L(16470);
L.CreateList(testfile);
int len = L.get_length();
QuickSort(L, 1, len);
L.PrintList(savefile);
testfile.close();
savefile.close();
L.~SeqList();
system("pause");
}