- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个程序,人们可以在其中创建一个拍卖,然后将其保存为如下所示的.txt文件:
Auction name: Books
Item to sell: Lord of the rings
Description: words........
Reserve price: 12
// "personName.txt"
24 ( <-- Example number of the person's bid)
最佳答案
我不确定为什么要对文件执行此任务,因为将文件加载到程序中非常昂贵。但是,您的任务可以完成(也就是说,如果我准确地理解了您的要求;您的问题并未写得很清楚)。
首先,请确保包含来自STL的适当 header
#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
.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 found\n"
<< "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
”下的部分
@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:\Dev\C++\Others\DevBox\Testing\bids\bidder1.txt
Bidder's name: Bidder1
Bidder's bid: 219
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder10.txt
Bidder's name: Bidder10
Bidder's bid: 693
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder100.txt
Bidder's name: Bidder100
Bidder's bid: 275
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder11.txt
Bidder's name: Bidder11
Bidder's bid: 669
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder12.txt
Bidder's name: Bidder12
Bidder's bid: 720
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder13.txt
Bidder's name: Bidder13
Bidder's bid: 741
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder14.txt
Bidder's name: Bidder14
Bidder's bid: 342
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder15.txt
Bidder's name: Bidder15
Bidder's bid: 124
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder16.txt
Bidder's name: Bidder16
Bidder's bid: 627
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder17.txt
Bidder's name: Bidder17
Bidder's bid: 244
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder18.txt
Bidder's name: Bidder18
Bidder's bid: 61
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder19.txt
Bidder's name: Bidder19
Bidder's bid: 79
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder2.txt
Bidder's name: Bidder2
Bidder's bid: 979
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder20.txt
Bidder's name: Bidder20
Bidder's bid: 211
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder21.txt
Bidder's name: Bidder21
Bidder's bid: 168
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder22.txt
Bidder's name: Bidder22
Bidder's bid: 44
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder23.txt
Bidder's name: Bidder23
Bidder's bid: 902
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder24.txt
Bidder's name: Bidder24
Bidder's bid: 753
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder25.txt
Bidder's name: Bidder25
Bidder's bid: 975
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder26.txt
Bidder's name: Bidder26
Bidder's bid: 220
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder27.txt
Bidder's name: Bidder27
Bidder's bid: 576
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder28.txt
Bidder's name: Bidder28
Bidder's bid: 541
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder29.txt
Bidder's name: Bidder29
Bidder's bid: 249
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder3.txt
Bidder's name: Bidder3
Bidder's bid: 788
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder30.txt
Bidder's name: Bidder30
Bidder's bid: 661
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder31.txt
Bidder's name: Bidder31
Bidder's bid: 98
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder32.txt
Bidder's name: Bidder32
Bidder's bid: 307
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder33.txt
Bidder's name: Bidder33
Bidder's bid: 698
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder34.txt
Bidder's name: Bidder34
Bidder's bid: 287
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder35.txt
Bidder's name: Bidder35
Bidder's bid: 489
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder36.txt
Bidder's name: Bidder36
Bidder's bid: 685
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder37.txt
Bidder's name: Bidder37
Bidder's bid: 196
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder38.txt
Bidder's name: Bidder38
Bidder's bid: 321
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder39.txt
Bidder's name: Bidder39
Bidder's bid: 332
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder4.txt
Bidder's name: Bidder4
Bidder's bid: 267
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder40.txt
Bidder's name: Bidder40
Bidder's bid: 593
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder41.txt
Bidder's name: Bidder41
Bidder's bid: 485
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder42.txt
Bidder's name: Bidder42
Bidder's bid: 863
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder43.txt
Bidder's name: Bidder43
Bidder's bid: 869
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder44.txt
Bidder's name: Bidder44
Bidder's bid: 612
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder45.txt
Bidder's name: Bidder45
Bidder's bid: 165
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder46.txt
Bidder's name: Bidder46
Bidder's bid: 889
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder47.txt
Bidder's name: Bidder47
Bidder's bid: 554
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder48.txt
Bidder's name: Bidder48
Bidder's bid: 783
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder49.txt
Bidder's name: Bidder49
Bidder's bid: 747
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder5.txt
Bidder's name: Bidder5
Bidder's bid: 966
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder50.txt
Bidder's name: Bidder50
Bidder's bid: 322
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder51.txt
Bidder's name: Bidder51
Bidder's bid: 775
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder52.txt
Bidder's name: Bidder52
Bidder's bid: 306
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder53.txt
Bidder's name: Bidder53
Bidder's bid: 376
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder54.txt
Bidder's name: Bidder54
Bidder's bid: 895
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder55.txt
Bidder's name: Bidder55
Bidder's bid: 374
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder56.txt
Bidder's name: Bidder56
Bidder's bid: 166
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder57.txt
Bidder's name: Bidder57
Bidder's bid: 332
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder58.txt
Bidder's name: Bidder58
Bidder's bid: 665
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder59.txt
Bidder's name: Bidder59
Bidder's bid: 177
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder6.txt
Bidder's name: Bidder6
Bidder's bid: 888
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder60.txt
Bidder's name: Bidder60
Bidder's bid: 399
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder61.txt
Bidder's name: Bidder61
Bidder's bid: 271
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder62.txt
Bidder's name: Bidder62
Bidder's bid: 996
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder63.txt
Bidder's name: Bidder63
Bidder's bid: 487
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder64.txt
Bidder's name: Bidder64
Bidder's bid: 874
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder65.txt
Bidder's name: Bidder65
Bidder's bid: 499
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder66.txt
Bidder's name: Bidder66
Bidder's bid: 72
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder67.txt
Bidder's name: Bidder67
Bidder's bid: 902
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder68.txt
Bidder's name: Bidder68
Bidder's bid: 66
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder69.txt
Bidder's name: Bidder69
Bidder's bid: 64
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder7.txt
Bidder's name: Bidder7
Bidder's bid: 246
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder70.txt
Bidder's name: Bidder70
Bidder's bid: 167
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder71.txt
Bidder's name: Bidder71
Bidder's bid: 961
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder72.txt
Bidder's name: Bidder72
Bidder's bid: 250
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder73.txt
Bidder's name: Bidder73
Bidder's bid: 280
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder74.txt
Bidder's name: Bidder74
Bidder's bid: 185
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder75.txt
Bidder's name: Bidder75
Bidder's bid: 962
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder76.txt
Bidder's name: Bidder76
Bidder's bid: 174
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder77.txt
Bidder's name: Bidder77
Bidder's bid: 157
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder78.txt
Bidder's name: Bidder78
Bidder's bid: 221
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder79.txt
Bidder's name: Bidder79
Bidder's bid: 312
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder8.txt
Bidder's name: Bidder8
Bidder's bid: 189
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder80.txt
Bidder's name: Bidder80
Bidder's bid: 842
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder81.txt
Bidder's name: Bidder81
Bidder's bid: 415
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder82.txt
Bidder's name: Bidder82
Bidder's bid: 313
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder83.txt
Bidder's name: Bidder83
Bidder's bid: 220
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder84.txt
Bidder's name: Bidder84
Bidder's bid: 200
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder85.txt
Bidder's name: Bidder85
Bidder's bid: 738
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder86.txt
Bidder's name: Bidder86
Bidder's bid: 323
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder87.txt
Bidder's name: Bidder87
Bidder's bid: 529
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder88.txt
Bidder's name: Bidder88
Bidder's bid: 196
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder89.txt
Bidder's name: Bidder89
Bidder's bid: 991
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder9.txt
Bidder's name: Bidder9
Bidder's bid: 917
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder90.txt
Bidder's name: Bidder90
Bidder's bid: 441
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder91.txt
Bidder's name: Bidder91
Bidder's bid: 691
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder92.txt
Bidder's name: Bidder92
Bidder's bid: 578
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder93.txt
Bidder's name: Bidder93
Bidder's bid: 234
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder94.txt
Bidder's name: Bidder94
Bidder's bid: 638
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder95.txt
Bidder's name: Bidder95
Bidder's bid: 569
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder96.txt
Bidder's name: Bidder96
Bidder's bid: 267
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder97.txt
Bidder's name: Bidder97
Bidder's bid: 788
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder98.txt
Bidder's name: Bidder98
Bidder's bid: 790
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder99.txt
Bidder's name: Bidder99
Bidder's bid: 781
The winner of this auction is Bidder62 !
They bid $996!
关于c++ - 如何在C++中比较多个.txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59259277/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!