如何从文本文件中的每个字符串中获取最大值 - C++
how to get max values from each string in a text file - C++
我有一个这样的文本文件;
1 13 330 323 18 1 40 410413 45 1 28 381 347 16 1 16 230 261 27
2 6 208 218 8 2 24 253 277 21 2 13 223 244 14 2 10 177 185 6
3 0 12 1 1 3 20 417 416 18 3 23 322 320 23 3 5 21 23 4
4 1 7 18 2 4 11 149 138 11 4 11 120 116 10 4 2 27 24 3
我想取每个字符串的最大值。例如,在第一个字符串中,我有 413 作为最高数字,对于第二个字符串,我有 277。我有40行这样的行。我使用了这段代码,但我的代码无法正常工作 - 我知道我做错了 顺便说一句 - 它接受所有数组,只取 1 个最高值。我想我需要两个 for 循环来执行此操作,但我已经做错了第一个,并且没有第二个:)也许这可以通过"getline"功能来完成,我真的不知道,但我需要你的帮助 atm...谢谢。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <string.h>
using std::cin;
using std::endl;
using std::cout;
using namespace std;
int main()
{
int a[20][40];
int x,y;
int sum = 0;
FILE *myDataFile1;
ofstream myOutFile1;
myOutFile1.open ("highestValues.txt");
myDataFile1 = fopen("input.txt", "r");
for ( x = 0; x < 20; x++)
{
for ( y = 0; y < 40; y++)
{
a[x][y] = 0;
}
}
for (x = 0; x < 20; x++)
{
for ( y = 0; y < 40; y++)
{
fscanf(myDataFile1, "%d,", &a[x][y] );
}
}
for (x = 0; x < 20; x++)
{
for ( y = 0; y < 40; y++)
{
sum = a[x][y];
}
}
int maxValue = 0;
for(x = 1; x < 20; x++)
{
for(y = 1; y < 40; y++)
{
if(a[x][y] > maxValue)
{
maxValue = a[x][y];
}
}
}
if (myOutFile1.is_open())
{
myOutFile1 << left << setw (5) << maxValue << endl;
}
cout << "The highest value is: " << maxValue << endl;
}
}
一种可能的解决方案:
std::fstream fs("test.txt");
std::string line;
while(std::getline(fs, line)) {
std::stringstream str(line);
std::istream_iterator<int> begin(str), end;
std::vector<int> vec(begin, end)
std::cout << *std::max_element(vec.begin(), vec.end()) << std::endl;
}
[编辑]
您的代码更像 C - 就像C++版本(如您标记问题时)看起来像上面的代码。
您可以在 http://en.cppreference.com/w/上找到标题,为了使这个答案完整,我在下面添加它们:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <fstream>
这是一个活生生的例子
看起来您的主要问题是读取文件。 我建议从这样的框架开始,而不是硬编码数字 20 和 40。
#include <fstream>
#include <sstream>
#include <iostream>
int main (void)
{
std::ifstream infile("input.txt");
std::string line;
while (std::getline(infile, line)) // std::getline returns infile, which evaluates to false at the end of the file.
{
std::cout << "nNew Line.n";
std::istringstream iss(line); // Turn the line into its own stream
int a;
while (iss >> a) // iss evaluates to false at the end of the stream
{
std::cout << a << " ";
// You can find the max element here.
// Exercise left for the reader.
}
}
return 0;
}
逐行阅读文件对做出这个答案非常有帮助,我建议也阅读一下。
你做了一个 20X40 的矩阵,所以我想你已经知道一行中的数字数量。你做错的是你的矩阵大小和索引。您的输入有 40 行,因此行 = 40,一行中有 20 个整数,因此有 20 列。
现在,您的矩阵如下所示:
int a[40][20];
现在只需使用 for 循环和 fscanf
或 ifstream
读取矩阵中的整数。
这是您的新代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <string.h>
using std::cin;
using std::endl;
using std::cout;
using namespace std;
int main()
{
int a[40][20];
int x,y;
int sum = 0;
FILE *myDataFile1;
ofstream myOutFile1;
myOutFile1.open ("highestValues.txt",ios::out);
myDataFile1 = fopen("input.txt", "r");
for ( x = 0; x < 40; x++)
for ( y = 0; y < 20; y++)
fscanf(myDataFile1,"%d ",&a[x][y]); //removed the comma, since your integers are not comma saperated
for (x = 0; x < 40; x++)
{
int maximum = a[x][0];
for ( y = 0; y < 20; y++)
maximum = max(maximum,a[x][y]);
myOutFile1 << "Maximum for line "<<x<<": "<<maximum << endl;
}
fclose(myDataFile1);
myOutFile1.close();
return 0;
}
首先,确定是否要在代码中使用 C++ 或 C。不要将两者混为一谈,它只是看起来很丑。ifstream 用于输入。流用于输出。">>"和"<<"操作员也为他们工作,这就是为什么C++很舒服。我想每行有 20 个整数吧?所以你要做的是为 3 行做 3 个循环,并将每个整数添加到一个数组[n] 中,其中 0<=n<=3 是线。在代码中:
ifstream input;
int max[3] /*Because you have 3 lines in your quote*/, a;
input.open(...);
/* don't forget max[n] iniliazation */
for (int i = 0; i < 3; i++)
for (int j = 0; j < 20; j++)
input >> a;
max[i] = max[i] < a ? a : max[i];
每行都有最大值
- 从C字符串中获取奇怪的字符串长度
- 获取字符串的长度并将其分配给数组
- 我如何获取从 C++ 到 C 的映射<字符串、int>#
- 调用CreateProcess()并获取字符串的返回值
- 从字符串中获取结构的大小
- Cryptopp:获取密码输入的填充字符串
- 获取无效的字符串大小
- 获取 OID(类型::b_oid)作为 MongoDB C++驱动程序中的字符串
- C ++编译器如何获取argv字符串的长度
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 如何使用字符串获取用户控制台输入
- 通过字符串获取小部件
- 无法从字符串获取数字输入
- 在C++的静态方法中将类名作为字符串获取
- 从设备实例路径字符串获取设备实例 DWORD
- HRESULT到字符串(获取文档路径)C++
- 如何从http请求体字符串获取文件名
- 如何使用带有垃圾字符的子字符串获取字符串
- 通过字符串获取对话框项的可能性
- 有没有办法将 WinAPI 错误标志作为字符串获取