我迷路了,我很确定这段代码有95%错误,我需要帮助。我需要做的是获取一个.txt文件并将这些单词分成类型,价格和通缉或出售。它基本上像craigslist一样工作,但要求是我计算操作量并删除想要和出售的struct-array如果找到匹配。我真的不确定如何在匹配后从数组中删除该项目并且我一直收到错误说" std :: ifstream没有名为&#34的成员;找到"。
下面是代码:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
// Maximum amount to read from a line in the file
const int SIZE = 100; // Size of input array
char input[SIZE]; // To hold file input
struct itemArray
{
string type;
int price;
int status; //for sale =0, wanted = 1
int length;
};
void findItem(itemArray[i].type, itemArray[i].price)//searches to see if there is a match, if there is it deletes it from the array
{
for (int x = 0; x < itemArray.length) //will search for everything in the array
{
if itemArray[i].type == itemArray[x].type && itemArray[i].price >= itemArray[x].price && itemArray[x].status ==0;) //if the item outside of the function matches in both type and price then sell it
{
}
}
}
// Function prototypes
int main()
{
int index;
int length;
string line;
ifstream myfile("messageBoard.txt"); //opening the file
if(myfile.is_open())
{
while (myfile.good())
{
getline(myfile, line);
int indexofcomma = myfile.find(",");
itemArray[index].type = myfile.substr(0, indexofcomma); //setting the type of item
string newstr = myfile.substr(indexofcomma+2, myfile.length()-1); //shortening the string to get rest of information
int newcomma = newstr.find(","); //separate the the word into a item when a comma is detected
if (newstr.substr(0, newcomma) == "wanted")
{
itemArray[index].forSale = false; //prints out a 0
//findItem(itemArray.type, itemArray.status-1, itemArray.price);
} else {
itemArray[index].forSale = true; //prints out a 1
}
itemArray[index].price = stoi(newstr.substr(newcomma+2, newstr.length())); //converts string to int and sets price of Item
index++;
length++;//keeping track of the length of the array
cout << itemsArray[10].type << " " << itemsArray[10].price << endl;
}
for(int i = 0; i <= itemArray.length; i++)//loop to find every item that is wanted in the list
{
if (itemArray[i].status = 1)//if item is wanted
{
findItem(itemArray.type[i], itemArray[i].price)//look for a seller with a similar price
}
}
}
myfile.close;
return 0;
}
这是我使用
的.txtchicken, for sale, 60
microwave, wanted, 201
bike, for sale, 60
bike, wanted, 50
microwave, for sale, 200
chicken, for sale, 25
chicken, wanted, 25
microwave, wanted, 10
microwave, for sale, 2
chicken, for sale, 100
bike, wanted, 100
chicken, for sale, 5
truck, wanted, 1000
bike, for sale, 50
chicken, for sale, 5
bike, for sale, 500
chicken, for sale, 1
chicken, for sale, 25
bike, wanted, 60
truck, wanted, 2000
truck, for sale, 2500
bike, wanted, 100
truck, for sale, 300
bike, for sale, 100
chicken, for sale, 10000
truck, for sale, 2000
truck, wanted, 1000
dresser, for sale, 20
truck, wanted, 9000
truck, wanted, 8000
truck, for sale, 4000
dresser, for sale, 2
dresser, wanted, 800
microwave, wanted, 70
truck, for sale, 2000
truck, for sale, 2000
truck, wanted, 1000
microwave, for sale, 60
dresser, for sale, 2000
dresser, wanted, 60
dresser, wanted, 50
truck, wanted, 1000
truck, for sale, 500
truck, for sale, 1500
dresser, for sale, 100
dresser, wanted, 200
dresser, for sale, 450
truck, for sale, 2000
truck, wanted, 1000
truck, for sale, 500
dresser, for sale, 500
dresser, wanted, 200
chicken, wanted, 5
chicken, for sale, 5
truck, wanted, 1000
chicken, for sale, 1
chicken, for sale, 25
chicken, wanted, 16
truck, wanted, 2000
chicken, for sale, 15
chicken, wanted, 5
chicken, for sale, 2
microwave, wanted, 15
microwave, wanted, 75
microwave, for sale, 65
chicken, for sale, 5
bike, for sale, 50
chicken, for sale, 1
chicken, for sale, 25
bike, for sale, 50
chicken, wanted, 25
microwave, wanted, 10
microwave, for sale, 2
chicken, for sale, 1
chicken, for sale, 25
bike, for sale, 50
chicken, wanted, 25
bike, wanted, 30
chicken, wanted, 16
chicken, for sale, 15
microwave, wanted, 70
microwave, for sale, 60
microwave, wanted, 50
bike, wanted, 75
truck, wanted, 1000
microwave, wanted, 201
microwave, for sale, 200
truck, wanted, 1000
chicken, for sale, 25
chicken, wanted, 25
truck, wanted, 1000
microwave, wanted, 10
bike, for sale, 10
truck, for sale, 2000
microwave, for sale, 2
truck, wanted, 4000
truck, for sale, 2000
microwave, for sale, 2
truck, wanted, 4000
bike, for sale, 100
感谢所有帮助,称我为白痴或者你需要什么,但我需要洞察力。
答案 0 :(得分:5)
学习它。爱它。跟着它。编写代码,其中每个部分只有一个责任。
在这种情况下,我开始使用的结构除了存储输入文件中的一个项目外什么都不做:
struct item {
std::string item;
std::string action;
int price;
};
然后我写了一段代码,只是从文件中读取一个项目到结构中:
std::istream &operator>>(std::istream &is, item &i) {
std::getline(is, i.name, ',');
std::getline(is, i.action, ',');
return is >> i.price;
}
然后编写代码以将文件中的数据读入项目向量:
std::ifstream infile("input.txt");
std::vector<item> items {
std::istream_iterator<item>(infile),
std::istream_iterator<Item>(),
};
从那里,听起来你想要将数据划分为&#34;想要&#34;物品,以及&#34;待售的部分&#34;项目。那么你就匹配&#34;出售&#34;带有&#34;想要物品的物品&#34;物品描述相同,销售价格小于或等于购买价格。
为此,您可能希望从std::partition
开始,将待售商品与所需商品分开。
然后您使用std::sort
对每一半进行排序(可能是名称和价格)。
最后,您可以使用std::set_symmetric_difference
删除匹配的通缉/待售商品。
答案 1 :(得分:0)
Jerry Coffin概述了基于需求的软件开发的基本思想,并提供了第一步:决定如何存储基本数据(&#34;架构,&#34;如果你愿意的话)。
接下来,您需要找到一个可以解决问题的算法。你已经用一些迟钝的机械术语来描述它,但这个程序的目的似乎是,#匹配买家和卖家,并打印无法匹配的列表。&#34; < / p>
让我们通过一系列买家和一份卖家来解决这个问题。如果要价和售价完全相等,我们可以代理交易。 (这不是特别现实,但似乎是示例数据集中反映的意图。)
如果买家和卖家列表都已排序,我们可以按照相同的顺序阅读它们,并快速查看缺少的人。把它想象成一个出勤点名:很容易看出谁在课堂上缺席,给出了一份注册学生名单和一个按字母顺序排列的登录表。通过algorithms in the C++ standard library列表扫描,我可以找到进行此类&#34;出勤检查的std::set_symmetric_difference。&#34;
要对数据进行排序,我们需要定义一个小于关系。我们会在卖家之前对买家进行分类,以便将分类数据的两半视为单独的列表。这种关系可能被称为operator <
,使其成为运算符重载。一旦他们分开,我们需要一个不同的不足关系来定义&#34; alphabetization&#34; &#34;出勤检查。&#34;这个名称更具描述性,item_price_comparison
,因为它忽略了有关买/卖的信息。 (它将始终用于比较一个买方与一个卖方。)
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
typedef std::string item_action;
const item_action sell = "for sale";
const item_action buy = "wanted";
struct item {
item_action action;
std::string item;
int price;
};
// Compare such that matching "buy" and "sell" are equal.
// (Neither is less-than the other.)
bool item_price_comparison( item const & lhs, item const & rhs ) {
return lhs.item < rhs.item? true
: rhs.item < lhs.item? false
: lhs.price < rhs.price;
}
// Compare such that sells come before buys.
bool operator < ( item const & lhs, item const & rhs ) {
return lhs.action < rhs.action? true
: rhs.action < lhs.action? false
: lhs.item < rhs.item? true
: rhs.item < lhs.item? false
: lhs.price < rhs.price;
}
std::istream & operator >> ( std::istream & s, item & i ) {
std::getline( s >> std::ws, i.item, ',');
std::getline( s >> std::ws, i.action, ',');
s >> i.price;
return s;
}
std::ostream & operator << ( std::ostream & s, item const & i ) {
s << i.item << ", ";
s << i.action << ", ";
s << i.price << "\n";
return s;
}
int main() {
std::ifstream file( "messageBoard.txt" ); // Open the file.
std::vector< item > items; // Create the "database."
std::copy( std::istream_iterator< item >( file ), {},
std::back_inserter( items ) ); // Read the data.
std::sort( items.begin(), items.end() ); // Organize the data.
// Split list: items before first_buyer are for sale, after are wanted.
auto first_buyer = std::lower_bound( items.begin(), items.end(),
item{ buy } );
std::set_symmetric_difference( // Find mismatches between...
items.begin(), first_buyer, // ... the list of sellers...
first_buyer, items.end(), // ... and the list of buyers...
std::ostream_iterator< item >( std::cout ), // and print the results.
item_price_comparison // Let matching buy and sell be equal.
);
}
通过将买家与要价较低的卖家进行匹配,一个小小的改变将使该计划成为更好的经纪人。
// Compare such that matching "buy" and "sell" are equal.
// (Neither shall be less-than the other if they have a deal.)
// Assume that we are comparing one buy with one sell.
bool item_price_comparison( item const & lhs, item const & rhs ) {
item const & buyer = lhs.action == buy? lhs : rhs;
item const & seller = lhs.action == sell? lhs : rhs;
return lhs.item < rhs.item? true
: rhs.item < lhs.item? false
: seller.price < buyer.price? false // We have a deal.
: lhs.price < rhs.price; // Usual rule applies if no deal.
}
该经纪人将挑选第一批买家,这些买家是报价最低的买家,并且在收益率上留下很高的出价。您可以通过最大限度地排序来解决这个问题,但这远远超出了要求。