我做了一个程序,人们在其中创建一个拍卖,然后将其保存为一个.txt文件,如下所示:
Auction name: Books
Item to sell: Lord of the rings
Description: words........
Reserve price: 12
然后,在一个单独的项目中,我做了一个类似的事情,但使用了投标,它也保存在一个带有结构的.txt文件中:
// "personName.txt"
24 ( <-- Example number of the person's bid)
问题是现在我必须制作多个"出价"文件,并将它们相互比较和拍卖文件,以确定谁赢得了拍卖。
如何做到这一点?
我不确定为什么要对文件执行此任务,因为将文件加载到程序中非常昂贵。但是,你的任务是可以完成的(也就是说,如果我准确地理解你在问什么;你的问题写得不是很清楚(。
首先,确保包含来自 STL 的正确标头
#include <iostream> // Assuming you have console IO functionality
#include <filesystem> // Accessing all files in a directory for searching
#include <fstream> // Loading files into the program
#include <string> // Assuming you have names for bidders
#include <vector> // Load the files' contents into a vector
正如您可能从包含及其解释中可以看出的那样,这是我对该计划的计划:
- 将所有拍卖文件存储在只有拍卖文件的目录中。这将节省时间,因为程序不必检查一堆我们不感兴趣的文件。
- 使用
std::filesystem::directory_iterator
,从上面搜索目录中的所有文件,并将其内容保存到std::vector
- 使用 max 函数确定最大出价并输出其名称
在实践中,这意味着我们可能需要一个结构向量,这些结构具有 (1( 投标人的名称和 (2( 他们的投标。然后,我们使用max函数(如上所述(比较他们的所有出价。当我们找到最大值时,我们将能够非常轻松地找到与该出价相关的名称出价者并输出到屏幕。
.txt
文件(对于我的解决方案(每个文件严格有两行。第一行是投标人的名字,第二行是投标人的投标。这将与我们用于将这些文件加载到程序中的结构完全一致。
所以,这是程序(确保您使用的是C++17
(:
#include <filesystem> // std::filesystem : path, directory_iterator, current_path, parent_path
#include <fstream> // std::ifstream
#include <iomanip> // std::setpercision
#include <iostream> // std::cout, std::cerr, std::endl
#include <limits> // std::numeric_limits
#include <string> // std::string
#include <vector> // std::vector
namespace fs = std::filesystem;
// The max size of a file stream saved into this variables
constexpr auto STREAM_MAX = std::numeric_limits<std::streamsize>::max();
// The directory to the folder with all the bidders in it
// What I've done here is used the a folder in the same parent
// directory as this file. So we go one step out of the
// current path, then go into the "bids" folder. However,
// I compiled this in a different directory hierarchy, so I
// commented the '.parent_path()' part out.
const auto BIDDING_PATH =
fs::path(fs::current_path()/*.parent_path()*/ / "bids");
/**
This will be the struct we use in the vector
@param name The name of the bidder
@param bid The price of the bidder's bid
*/
struct Bidder
{
std::string name;
double bid;
// Define a constructor for std::vector::emplace_back
Bidder(std::string name, double bid) :
name(std::move(name)), bid(bid)
{}
};
std::vector<Bidder> load_bids()
{
std::vector<Bidder> all_bidders;
// Loop through every file in the 'BIDDING_PATH' directory
// (not recursively)
for (auto file : fs::directory_iterator(BIDDING_PATH))
{
std::ifstream cur_file(file.path().c_str());
if (cur_file.is_open())
{ // Load file into a struct and add it to `all_bidders`
std::string cur_name;
double cur_bid;
// Read the contents of the file into the above variables
std::getline(cur_file, cur_name);
cur_file >> cur_bid;
// Now get rid of the newline character left behind by the '>>'
// operator from 'cur_file >> cur_bid'
cur_file.ignore(STREAM_MAX, 'n');
// Add the bidder to the `all_bidders` vector
all_bidders.emplace_back(cur_name, cur_bid);
// Some debugging feedback
std::cout
<< "File loaded: " << file.path().string() << 'n'
<< "Bidder's name: " << all_bidders.back().name << 'n'
<< "Bidder's bid: " << all_bidders.back().bid << 'n'
<< std::endl;
}
else
{
std::cerr
<< "Error opening file: " << file.path().string()
<< std::endl;
}
}
return all_bidders;
}
Bidder find_max_bidder(const std::vector<Bidder>& bidders)
{
// Keeps track of the winning bidder
// Intially set to a null value
Bidder max_bidder("", -1.0);
for (const auto& cur_bidder : bidders)
{
if (cur_bidder.bid > max_bidder.bid)
{
max_bidder.name = cur_bidder.name;
max_bidder.bid = cur_bidder.bid;
}
}
// Check if no max was found
if (max_bidder.bid == -1.0)
{
std::cerr
<< "Error: No max bidder was foundn"
<< "Are there any bidding files in the given directory?n"
<< std::endl;
}
return max_bidder;
}
int main()
{
// Load all bidders into the `all_bidders` vector, then
// calculate the max bid
std::vector<Bidder> all_bidders = load_bids();
Bidder winning_bidder = find_max_bidder(all_bidders);
// Print out the max bid
std::cout
<< "The winner of this auction is " << winning_bidder.name << "!n"
<< "They bid $" << winning_bidder.bid << std::setprecision(2) << "!n"
<< std::endl;
// Keep the console from closing
std::cin.get();
return 0;
}
请注意以下几点:
- 您需要将其编译为
C++17
;我将MSVC与Visual Studio 2019一起使用 - 还有一些额外的
#include
不需要来解释我们的游戏计划,但我添加了格式化或让我的生活更轻松或其他东西
如果需要 - ,您可以删除"
// Some debugging feedback
- 我没有涵盖您问题的第一部分,因为它 (1( 看起来不像一个问题,(2( 您似乎对该部分没有任何担忧
此外,我使用此批处理文件生成了一组随机数据:
@echo off
setlocal EnableDelayedExpansion
set wkdir=!cd!
goto :begin
:begin
cd "..bids"
set rand_bid=
for /l %%a in (1,1,100) do (
echo Bidder%%a > bidder%%a.txt
set /a rand_bid=!random! %% 1000
echo !rand_bid!.00 >> bidder%%a.txt
)
cd !wkdir!
goto :end
:end
exit
运行该批处理文件然后运行我的程序后,这是我的输出:
File loaded: C:DevC++OthersDevBoxTestingbidsbidder1.txt
Bidder's name: Bidder1
Bidder's bid: 219
File loaded: C:DevC++OthersDevBoxTestingbidsbidder10.txt
Bidder's name: Bidder10
Bidder's bid: 693
File loaded: C:DevC++OthersDevBoxTestingbidsbidder100.txt
Bidder's name: Bidder100
Bidder's bid: 275
File loaded: C:DevC++OthersDevBoxTestingbidsbidder11.txt
Bidder's name: Bidder11
Bidder's bid: 669
File loaded: C:DevC++OthersDevBoxTestingbidsbidder12.txt
Bidder's name: Bidder12
Bidder's bid: 720
File loaded: C:DevC++OthersDevBoxTestingbidsbidder13.txt
Bidder's name: Bidder13
Bidder's bid: 741
File loaded: C:DevC++OthersDevBoxTestingbidsbidder14.txt
Bidder's name: Bidder14
Bidder's bid: 342
File loaded: C:DevC++OthersDevBoxTestingbidsbidder15.txt
Bidder's name: Bidder15
Bidder's bid: 124
File loaded: C:DevC++OthersDevBoxTestingbidsbidder16.txt
Bidder's name: Bidder16
Bidder's bid: 627
File loaded: C:DevC++OthersDevBoxTestingbidsbidder17.txt
Bidder's name: Bidder17
Bidder's bid: 244
File loaded: C:DevC++OthersDevBoxTestingbidsbidder18.txt
Bidder's name: Bidder18
Bidder's bid: 61
File loaded: C:DevC++OthersDevBoxTestingbidsbidder19.txt
Bidder's name: Bidder19
Bidder's bid: 79
File loaded: C:DevC++OthersDevBoxTestingbidsbidder2.txt
Bidder's name: Bidder2
Bidder's bid: 979
File loaded: C:DevC++OthersDevBoxTestingbidsbidder20.txt
Bidder's name: Bidder20
Bidder's bid: 211
File loaded: C:DevC++OthersDevBoxTestingbidsbidder21.txt
Bidder's name: Bidder21
Bidder's bid: 168
File loaded: C:DevC++OthersDevBoxTestingbidsbidder22.txt
Bidder's name: Bidder22
Bidder's bid: 44
File loaded: C:DevC++OthersDevBoxTestingbidsbidder23.txt
Bidder's name: Bidder23
Bidder's bid: 902
File loaded: C:DevC++OthersDevBoxTestingbidsbidder24.txt
Bidder's name: Bidder24
Bidder's bid: 753
File loaded: C:DevC++OthersDevBoxTestingbidsbidder25.txt
Bidder's name: Bidder25
Bidder's bid: 975
File loaded: C:DevC++OthersDevBoxTestingbidsbidder26.txt
Bidder's name: Bidder26
Bidder's bid: 220
File loaded: C:DevC++OthersDevBoxTestingbidsbidder27.txt
Bidder's name: Bidder27
Bidder's bid: 576
File loaded: C:DevC++OthersDevBoxTestingbidsbidder28.txt
Bidder's name: Bidder28
Bidder's bid: 541
File loaded: C:DevC++OthersDevBoxTestingbidsbidder29.txt
Bidder's name: Bidder29
Bidder's bid: 249
File loaded: C:DevC++OthersDevBoxTestingbidsbidder3.txt
Bidder's name: Bidder3
Bidder's bid: 788
File loaded: C:DevC++OthersDevBoxTestingbidsbidder30.txt
Bidder's name: Bidder30
Bidder's bid: 661
File loaded: C:DevC++OthersDevBoxTestingbidsbidder31.txt
Bidder's name: Bidder31
Bidder's bid: 98
File loaded: C:DevC++OthersDevBoxTestingbidsbidder32.txt
Bidder's name: Bidder32
Bidder's bid: 307
File loaded: C:DevC++OthersDevBoxTestingbidsbidder33.txt
Bidder's name: Bidder33
Bidder's bid: 698
File loaded: C:DevC++OthersDevBoxTestingbidsbidder34.txt
Bidder's name: Bidder34
Bidder's bid: 287
File loaded: C:DevC++OthersDevBoxTestingbidsbidder35.txt
Bidder's name: Bidder35
Bidder's bid: 489
File loaded: C:DevC++OthersDevBoxTestingbidsbidder36.txt
Bidder's name: Bidder36
Bidder's bid: 685
File loaded: C:DevC++OthersDevBoxTestingbidsbidder37.txt
Bidder's name: Bidder37
Bidder's bid: 196
File loaded: C:DevC++OthersDevBoxTestingbidsbidder38.txt
Bidder's name: Bidder38
Bidder's bid: 321
File loaded: C:DevC++OthersDevBoxTestingbidsbidder39.txt
Bidder's name: Bidder39
Bidder's bid: 332
File loaded: C:DevC++OthersDevBoxTestingbidsbidder4.txt
Bidder's name: Bidder4
Bidder's bid: 267
File loaded: C:DevC++OthersDevBoxTestingbidsbidder40.txt
Bidder's name: Bidder40
Bidder's bid: 593
File loaded: C:DevC++OthersDevBoxTestingbidsbidder41.txt
Bidder's name: Bidder41
Bidder's bid: 485
File loaded: C:DevC++OthersDevBoxTestingbidsbidder42.txt
Bidder's name: Bidder42
Bidder's bid: 863
File loaded: C:DevC++OthersDevBoxTestingbidsbidder43.txt
Bidder's name: Bidder43
Bidder's bid: 869
File loaded: C:DevC++OthersDevBoxTestingbidsbidder44.txt
Bidder's name: Bidder44
Bidder's bid: 612
File loaded: C:DevC++OthersDevBoxTestingbidsbidder45.txt
Bidder's name: Bidder45
Bidder's bid: 165
File loaded: C:DevC++OthersDevBoxTestingbidsbidder46.txt
Bidder's name: Bidder46
Bidder's bid: 889
File loaded: C:DevC++OthersDevBoxTestingbidsbidder47.txt
Bidder's name: Bidder47
Bidder's bid: 554
File loaded: C:DevC++OthersDevBoxTestingbidsbidder48.txt
Bidder's name: Bidder48
Bidder's bid: 783
File loaded: C:DevC++OthersDevBoxTestingbidsbidder49.txt
Bidder's name: Bidder49
Bidder's bid: 747
File loaded: C:DevC++OthersDevBoxTestingbidsbidder5.txt
Bidder's name: Bidder5
Bidder's bid: 966
File loaded: C:DevC++OthersDevBoxTestingbidsbidder50.txt
Bidder's name: Bidder50
Bidder's bid: 322
File loaded: C:DevC++OthersDevBoxTestingbidsbidder51.txt
Bidder's name: Bidder51
Bidder's bid: 775
File loaded: C:DevC++OthersDevBoxTestingbidsbidder52.txt
Bidder's name: Bidder52
Bidder's bid: 306
File loaded: C:DevC++OthersDevBoxTestingbidsbidder53.txt
Bidder's name: Bidder53
Bidder's bid: 376
File loaded: C:DevC++OthersDevBoxTestingbidsbidder54.txt
Bidder's name: Bidder54
Bidder's bid: 895
File loaded: C:DevC++OthersDevBoxTestingbidsbidder55.txt
Bidder's name: Bidder55
Bidder's bid: 374
File loaded: C:DevC++OthersDevBoxTestingbidsbidder56.txt
Bidder's name: Bidder56
Bidder's bid: 166
File loaded: C:DevC++OthersDevBoxTestingbidsbidder57.txt
Bidder's name: Bidder57
Bidder's bid: 332
File loaded: C:DevC++OthersDevBoxTestingbidsbidder58.txt
Bidder's name: Bidder58
Bidder's bid: 665
File loaded: C:DevC++OthersDevBoxTestingbidsbidder59.txt
Bidder's name: Bidder59
Bidder's bid: 177
File loaded: C:DevC++OthersDevBoxTestingbidsbidder6.txt
Bidder's name: Bidder6
Bidder's bid: 888
File loaded: C:DevC++OthersDevBoxTestingbidsbidder60.txt
Bidder's name: Bidder60
Bidder's bid: 399
File loaded: C:DevC++OthersDevBoxTestingbidsbidder61.txt
Bidder's name: Bidder61
Bidder's bid: 271
File loaded: C:DevC++OthersDevBoxTestingbidsbidder62.txt
Bidder's name: Bidder62
Bidder's bid: 996
File loaded: C:DevC++OthersDevBoxTestingbidsbidder63.txt
Bidder's name: Bidder63
Bidder's bid: 487
File loaded: C:DevC++OthersDevBoxTestingbidsbidder64.txt
Bidder's name: Bidder64
Bidder's bid: 874
File loaded: C:DevC++OthersDevBoxTestingbidsbidder65.txt
Bidder's name: Bidder65
Bidder's bid: 499
File loaded: C:DevC++OthersDevBoxTestingbidsbidder66.txt
Bidder's name: Bidder66
Bidder's bid: 72
File loaded: C:DevC++OthersDevBoxTestingbidsbidder67.txt
Bidder's name: Bidder67
Bidder's bid: 902
File loaded: C:DevC++OthersDevBoxTestingbidsbidder68.txt
Bidder's name: Bidder68
Bidder's bid: 66
File loaded: C:DevC++OthersDevBoxTestingbidsbidder69.txt
Bidder's name: Bidder69
Bidder's bid: 64
File loaded: C:DevC++OthersDevBoxTestingbidsbidder7.txt
Bidder's name: Bidder7
Bidder's bid: 246
File loaded: C:DevC++OthersDevBoxTestingbidsbidder70.txt
Bidder's name: Bidder70
Bidder's bid: 167
File loaded: C:DevC++OthersDevBoxTestingbidsbidder71.txt
Bidder's name: Bidder71
Bidder's bid: 961
File loaded: C:DevC++OthersDevBoxTestingbidsbidder72.txt
Bidder's name: Bidder72
Bidder's bid: 250
File loaded: C:DevC++OthersDevBoxTestingbidsbidder73.txt
Bidder's name: Bidder73
Bidder's bid: 280
File loaded: C:DevC++OthersDevBoxTestingbidsbidder74.txt
Bidder's name: Bidder74
Bidder's bid: 185
File loaded: C:DevC++OthersDevBoxTestingbidsbidder75.txt
Bidder's name: Bidder75
Bidder's bid: 962
File loaded: C:DevC++OthersDevBoxTestingbidsbidder76.txt
Bidder's name: Bidder76
Bidder's bid: 174
File loaded: C:DevC++OthersDevBoxTestingbidsbidder77.txt
Bidder's name: Bidder77
Bidder's bid: 157
File loaded: C:DevC++OthersDevBoxTestingbidsbidder78.txt
Bidder's name: Bidder78
Bidder's bid: 221
File loaded: C:DevC++OthersDevBoxTestingbidsbidder79.txt
Bidder's name: Bidder79
Bidder's bid: 312
File loaded: C:DevC++OthersDevBoxTestingbidsbidder8.txt
Bidder's name: Bidder8
Bidder's bid: 189
File loaded: C:DevC++OthersDevBoxTestingbidsbidder80.txt
Bidder's name: Bidder80
Bidder's bid: 842
File loaded: C:DevC++OthersDevBoxTestingbidsbidder81.txt
Bidder's name: Bidder81
Bidder's bid: 415
File loaded: C:DevC++OthersDevBoxTestingbidsbidder82.txt
Bidder's name: Bidder82
Bidder's bid: 313
File loaded: C:DevC++OthersDevBoxTestingbidsbidder83.txt
Bidder's name: Bidder83
Bidder's bid: 220
File loaded: C:DevC++OthersDevBoxTestingbidsbidder84.txt
Bidder's name: Bidder84
Bidder's bid: 200
File loaded: C:DevC++OthersDevBoxTestingbidsbidder85.txt
Bidder's name: Bidder85
Bidder's bid: 738
File loaded: C:DevC++OthersDevBoxTestingbidsbidder86.txt
Bidder's name: Bidder86
Bidder's bid: 323
File loaded: C:DevC++OthersDevBoxTestingbidsbidder87.txt
Bidder's name: Bidder87
Bidder's bid: 529
File loaded: C:DevC++OthersDevBoxTestingbidsbidder88.txt
Bidder's name: Bidder88
Bidder's bid: 196
File loaded: C:DevC++OthersDevBoxTestingbidsbidder89.txt
Bidder's name: Bidder89
Bidder's bid: 991
File loaded: C:DevC++OthersDevBoxTestingbidsbidder9.txt
Bidder's name: Bidder9
Bidder's bid: 917
File loaded: C:DevC++OthersDevBoxTestingbidsbidder90.txt
Bidder's name: Bidder90
Bidder's bid: 441
File loaded: C:DevC++OthersDevBoxTestingbidsbidder91.txt
Bidder's name: Bidder91
Bidder's bid: 691
File loaded: C:DevC++OthersDevBoxTestingbidsbidder92.txt
Bidder's name: Bidder92
Bidder's bid: 578
File loaded: C:DevC++OthersDevBoxTestingbidsbidder93.txt
Bidder's name: Bidder93
Bidder's bid: 234
File loaded: C:DevC++OthersDevBoxTestingbidsbidder94.txt
Bidder's name: Bidder94
Bidder's bid: 638
File loaded: C:DevC++OthersDevBoxTestingbidsbidder95.txt
Bidder's name: Bidder95
Bidder's bid: 569
File loaded: C:DevC++OthersDevBoxTestingbidsbidder96.txt
Bidder's name: Bidder96
Bidder's bid: 267
File loaded: C:DevC++OthersDevBoxTestingbidsbidder97.txt
Bidder's name: Bidder97
Bidder's bid: 788
File loaded: C:DevC++OthersDevBoxTestingbidsbidder98.txt
Bidder's name: Bidder98
Bidder's bid: 790
File loaded: C:DevC++OthersDevBoxTestingbidsbidder99.txt
Bidder's name: Bidder99
Bidder's bid: 781
The winner of this auction is Bidder62 !
They bid $996!
我希望这有所帮助!如果您有任何问题,请随时提问!