嗨,我在C ++输入文件中将字符与整数分开时遇到问题。文件内容的一个例子如下:
D 5 C 7 C 9 H 4 S 8 D 11 H 13
我想在一个数组中读取和存储字符并整理它们自己的数组,我知道如何使用fstream但我仍然坚持分离这些项目。我将非常感谢您提供的任何帮助,并提前感谢您。
上周我解决了类似的问题,但是在该任务中没有任何字符,下面给出了代码:
#include <iostream>
#include <fstream>
using namespace std;
int frequencyCal(unsigned short int[][5], unsigned short int, ofstream&);
unsigned short int ScoreForRow(unsigned short int[][5], unsigned short int, ofstream&);
int main() {
unsigned short int iArray[50][5];
unsigned short int rows = 0;
ifstream fin("YahtzeIn.txt");
ofstream fout("YahtzeOut.txt");
for(unsigned short int i = 0; i < 50; i++) {
rows += 1;
for(unsigned short int j = 0; j < 5; j++) {
fin >> iArray[i][j];
cout << iArray[i][j] << " ";
fout << iArray[i][j] << " ";
}
cout << " ==> " << ScoreForRow(iArray,i,fout) << endl;
fout << " ==> " << ScoreForRow(iArray,i,fout) << endl;
if(fin.eof())
break;
}
cout << endl;
fout << endl;
frequencyCal(iArray, rows, fout);
return 0;
}
int frequencyCal(unsigned short int in_array[][5], unsigned short int filerows, ofstream& outfile) {
unsigned short int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;
// Count the numbers in each row;
for(unsigned short int i = 0; i < filerows; i++)
{
for(unsigned short int j = 0; j < 5; j++ )
{
// Identify the number and add one to a counter variable
if(in_array[i][j] == 1) {
num1+=1;
}
else if(in_array[i][j] == 2) {
num2+=1;
}
else if(in_array[i][j] == 3) {
num3+=1;
}
else if(in_array[i][j] == 4) {
num4+=1;
}
else if(in_array[i][j] == 5) {
num5+=1;
}
else if(in_array[i][j] == 6) {
num6+=1;
}
}
}
cout << "Dice frequency" << endl;
cout << "==============" << endl;
cout << "1's 2's 3's 4's 5's 6's" << endl;
outfile << "Dice frequency" << endl;
outfile << "==============" << endl;
outfile << "1's 2's 3's 4's 5's 6's" << endl;
// Print and display the values of the variables;
cout << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << " " << num6;
outfile << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << " " << num6;
return 0;
}
unsigned short int ScoreForRow(unsigned short int in_array[][5], unsigned short int i, ofstream& outfile) {
unsigned short int numb1 = 0, numb2 = 0, numb3 = 0, numb4 = 0, numb5 = 0, numb6 = 0, score = 0;
bool four = false, five = false;
for(unsigned short int j = 0; j < 5; j++ )
{
// Sum up all numbers in a row
score += in_array[i][j];
// check if number occurs twice, three times or five times
if(in_array[i][j] == 1) {
numb1+=1;
if(numb1 == 4) {
four = true;
}
else if(numb1 == 5) {
four = false;
five = true;
}
}
else if(in_array[i][j] == 2) {
numb2+=1;
if(numb2 == 4) {
four = true;
}
else if(numb2 == 5) {
four = false;
five = true;
}
}
else if(in_array[i][j] == 3) {
numb3+=1;
if(numb3 == 4) {
four = true;
}
else if(numb3 == 5) {
four = false;
five = true;
}
}
else if(in_array[i][j] == 4) {
numb4+=1;
if(numb4 == 4) {
four = true;
}
else if(numb4 == 5) {
four = false;
five = true;
}
}
else if(in_array[i][j] == 5) {
numb5+=1;
if(numb5 == 4) {
four = true;
}
else if(numb5 == 5) {
four = false;
five = true;
}
}
else if(in_array[i][j] == 6) {
numb6+=1;
if(numb6 == 4) {
four = true;
}
else if(numb6 == 5) {
four = false;
five = true;
}
}
}
// Check if a number appears twice and another occurs 3 times, if so, update score to 25;
if(((numb1 == 2) || (numb2 == 2) || (numb3 == 2) || (numb4 == 2) || (numb5 == 2) || (numb6 == 2))
&& ((numb1 == 3) || (numb2 == 3) || (numb3 == 3) || (numb4 == 3) || (numb5 == 3) || (numb6 == 3))) {
score = 25;
}
// Check if a number appears four times
else if(four == true) {
score = 30;
}
// Check if a number appears five times
else if(five == true) {
score = 50;
}
// the value returned is either sum or number generated by the conditions
return score;
}
答案 0 :(得分:0)
从上面的代码中你可以在字符数组中更改iArray,之后你可以使用强制转换来读取整数。希望这有帮助