将文件中的多个输入分隔为偶数或奇数 [英] Separating multiple inputs from a file into even or odd
问题描述
我需要从文件中抽取200个随机数,将其分为偶数和奇数,并使它们的偶数或奇数部分从最低到最高显示.
I need to take 200 randomizes numbers from a file, separate them into even and odd, and make them show up from lowest to highest in their even or odd parts.
从这里我得到了将它变成偶数和奇数的代码,但是原始的方法是10而不是200,并让用户输入数字.
I got the code for making it into even and odd from here but the original way had 10 instead of 200 and made the user input the numbers.
我对此进行了重新设计,但是得到了相同编号的重复,然后读取了抛出Exception的错误:写入写入冲突,在奇数[oddcnt ++] = arr [i]附近有一个标记.
I reworked it into this but I get a repeat of the same number then an error that reads Exception thrown: write access violation with a mark near the odd[oddcnt++] = arr[i];
到目前为止我的代码
#include <iostream>
#include <fstream>
using namespace std;
class Random
{
private:
int x, arr[200], even[200], odd[200], evncnt = 0, oddcnt = 0, i;
public:
void readFile();
};
void Random::readFile()
{
fstream File("Random.txt");
if (File.is_open())
{
while(File >> x)
{
for (i = 0; i < 200; i++)
{
arr[i] = x;
}
for (i = 0; i < 200; i++)
{
if (arr[i] % 2 == 0)
{
even[evncnt++] = arr[i];
}
else
{
odd[oddcnt++] = arr[i];
}
}
cout << "\n The even numbers are: ";
for (i = 0; i < evncnt; i++)
{
cout << even[i] << "";
}
cout << "\n The odd numbers are: ";
for (i = 0; i < oddcnt; i++)
{
cout << odd[i] << "";
}
}
File.close();
}
}
int main()
{
Random file;
file.readFile();
system("pause");
return 0;
}
推荐答案
约翰已经给出了答案.一切都很好.
The answer is given already by John. So everything OK.
此外,我想向您展示现代C ++的强大功能.尤其是使用算法时.您可以编写非常精美的解决方案.
Additionally, I would like to show you the power of modern C++. Especially with using algorithms. You can write very elegant solutions.
没有循环,只有几个变量.
Thers is no loop and only vey few variables.
您当然不会复制和粘贴它,因为您不会完全理解它.但这应该为您提供一个想法,该问题如何被分析,设计和编码.
You will of course not copy and paste it, because you will not understand it fully. But it should give you an idea how such a problem could be analysed, then designed, and then coded.
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
class Random {
std::vector<int> values{};
public:
void readFile(const std::string& fileName);
void printOdds();
void printEvens();
};
void Random::readFile(const std::string& fileName) {
// Open file and check, if it could be opened
if (std::ifstream inputFileStream(fileName); inputFileStream) {
// Clear old content in our class
values.clear();
// Copy the contents from the vile into our vector
std::copy(std::istream_iterator<int>(inputFileStream), {}, std::back_inserter(values));
// sort the values
std::sort(values.begin(), values.end());
}
else { // File could not be opened
std::cerr << "\n*** Error: File could not be opened: " << fileName << "\n\n";
}
}
// Copy all odd values to std::cout
void Random::printOdds() {
std::copy_if(values.begin(), values.end(), std::ostream_iterator<int>(std::cout, " "), [](const int i) { return (i % 2) != 0; });
std::cout << "\n\n";
}
// Copy all even values to std::cout
void Random::printEvens() {
std::copy_if(values.begin(), values.end(), std::ostream_iterator<int>(std::cout, " "), [](const int i) { return (i % 2) == 0; });
std::cout << "\n\n";
}
// Driver code
int main() {
Random r;
r.readFile("r:\\random.txt");
r.printEvens();
r.printOdds();
return 0;
}
这篇关于将文件中的多个输入分隔为偶数或奇数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!