【发布时间】:2016-01-08 10:54:57
【问题描述】:
好的,我现在正在处理这个项目。程序读取一个文件,该文件可以有任意数量的行,并且文件中的每个项目都是不同的类型,如下所示:
1002 Hammer 23.65 203
1024 Nails 6.95 400
1276 Screwdriver 13.95 251
1385 Elec_Drill 45.69 132
1462 Air_Filter 7.95 500
第一个数字是双精度类型的产品编号,第二个是字符串类型,然后是每个价格的浮点数,然后是销售数字的整数。程序读入这些,然后对它们进行排序并输出最高销量和最低销量的商品。
我已经为此工作了一周,这就是我所拥有的
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;
struct Sales
{
string prodName;
double proNum;
string price;
string sold;
};
void sortArray(struct database, int); //Function prototypes
void sortString(string[], int);
void showArray(struct database[], int);
bool sales_sorter(Sales const& lhs, Sales const& rhs);
int main()
{
ifstream fin;
fin.open("sales.txt");
if (fin.fail())
{
cout << "Failed to open file" << endl;
}
vector<Sales> database(5);
string line;
int i = 0;
while (!fin.eof())
{
for (int j = 0; j < 5; j++)
{
if (j == 0) // name
{
fin >> database[i].proNum;
}
else if (j == 1) //
{
fin >> database[i].prodName;
}
else if (j == 2)
{
fin >> database[i].price;
}
else if (j == 3)
{
fin >> database[i].sold;
}
}
i++; //move to next item
}
}
std::sort(sales.begin(), sales.end(), &sales_sorter);
cout << &sales_sorter;
/* for (int x = 0; x < 5; x++) //Just used to make sure the array is working
{
cout << database[x].proNum << endl;
}
for (int x = 0; x < 5; x++)
{
cout << database[x].prodName << endl;
}
for (int x = 0; x < 5; x++)
{
cout << database[x].price << endl;
}
for (int x = 0; x < 5; x++)
{
cout << database[x].sold << endl;
} */
system("pause");
return 0;
}
void sortArray(double database[], int)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (5 - 1); count++)
{
if (database[count] > database[count + 1])
{
temp = database[count];
database[count] = database[count + 1];
database[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void showArray(double database[], int)
{
for (int count = 0; count < 5; count++)
cout << database[count] << " ";
cout << endl;
}
bool sales_sorter(Sales const& lhs, Sales const& rhs)
{
if (lhs.prodName != rhs.prodName)
return lhs.prodName < rhs.prodName;
if (lhs.proNum != rhs.proNum)
return lhs.proNum < rhs.proNum;
if (lhs.price != rhs.price)
return lhs.price < rhs.price;
return lhs.sold < rhs.sold;
}
现在我从thread 中得到了排序的想法,但是我的排序布尔抛出了这个错误:“错误应该是一个“;”但是没有地方可以放置它而不破坏东西,有人可以帮我弄清楚如何排序。我经历了很多不同的线程,但所有这些项目都需要排序,我似乎找不到任何关于指向数组的数据结构!
【问题讨论】:
-
您正在尝试在
main中定义函数sales_sorter。你不能那样做。 -
@molbdnilo 谢谢!我不知道这个