C++ 结构阵列奇偶故障
我的程序生成一个带有随机数的文本文件,例如:C++ 结构阵列奇偶故障,c++,structure,C++,Structure,我的程序生成一个带有随机数的文本文件,例如: 123 1234 12345 123456 程序接收该文件并将随机数据放入结构数组中。 然后它处理数组并输出一个如下所示的文件: 基于上述infle.txt生成的outfile.txt: No Type Odd Even Sum Digit 1234 Even 2 2 10 4 23467 Odd 2 3 22 5 123 Odd 2 1 6 3 但我遇到的问题是,一切都很好,
123
1234
12345
123456
程序接收该文件并将随机数据放入结构数组中。
然后它处理数组并输出一个如下所示的文件:
基于上述infle.txt生成的outfile.txt:
No Type Odd Even Sum Digit
1234 Even 2 2 10 4
23467 Odd 2 3 22 5
123 Odd 2 1 6 3
但我遇到的问题是,一切都很好,正如预期的那样,除了我的奇偶计数每隔一行就停止工作!例如,第一行,它将输出正确的结果,第二行它不会,第三行它将再次工作,依此类推
//oddcount, evencount. (WORKS, BUT ONLY EVERY OTHER LINE)
int countEven;
int countOdd;
for (int i = 0; i < size; i++)
{
countEven = 0;
countOdd = 0;
while (t[i].no > 0)
{
if (t[i].no % 2 == 1)
{
countOdd++;
t[i].no/= 10;
}
else
{
countEven++;
t[i].no/=10;
}
n[i].oddDigits = countOdd;
n[i].evenDigits = countEven;
} i++;
}
//oddcount,evencount。(工作,但仅每隔一行)
整数偶数;
int countOdd;
对于(int i=0;i0)
{
如果(t[i]。否%2==1)
{
countOdd++;
t[i].no/=10;
}
其他的
{
countEven++;
t[i].no/=10;
}
n[i].odddights=countOdd;
n[i]。偶数位数=偶数;
}i++;
}
下面是完整的代码
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int MAX = 100;
enum NumType {Odd, Even};
struct Number
{
int no;
NumType type;
int oddDigits;
int evenDigits;
int sumDigits;
int noDigits;
};
void arrayToOutfile (ofstream&, char [], Number [], int);
void constructInfile (fstream&, char []);
int constructArray (fstream&, char [], Number []);
void processArray (Number [], int);
NumType whatType (int);
void getStringLabel (NumType, char []);
int main ()
{
srand (time_t(NULL));
fstream inFile;
char fileName [MAX];
cout << "Enter filename: ";
cin >> fileName;
constructInfile (inFile, fileName);
cout << "----------------------------------------" << endl;
Number n [MAX];
int size = constructArray(inFile, fileName, n);
processArray (n, size);
cout << "----------------------------------------" << endl;
ofstream outFile;
cout << "Enter the output filename: ";
cin >> fileName;
arrayToOutfile (outFile, fileName, n, size);
}
void constructInfile (fstream& inFile, char fileName[])
{
inFile.open (fileName, ios::out);
if (!inFile)
{
cout << fileName << " cant be created for write"
<< endl;
exit (-1);
}
int size = rand() % 51+ 50;
for (int i = 0; i < size; i++)
{
inFile << rand () % 901 + 100 << endl
<< rand () % 90001 + 10000 << endl
<< rand () % 900001 + 100000 << endl;
i++;
}
cout << "written to outfile successfully" << endl;
inFile.close();
}
int constructArray (fstream& inFile, char fileName[], Number n[])
{
inFile.open (fileName, ios::in);
if (!inFile)
{
cout << fileName << " cant be accessed for array creation."
<< endl;
exit (-1);
}
cout << "Begin file to array" << endl;
int i = 0;
while (inFile >> n[i].no)
{
++i;
}
inFile.close();
cout << "File to array transfer success" << endl;
return i;
}
void processArray (Number n [], int size)
{
cout << "Begin processing array" << endl;
//Odd or Even Enum Label
for (int i = 0; i < size; i++)
{
n[i].type = whatType (n[i].no);
}
//copy number n array to temp n array
Number t [MAX];
for (int i = 0; i < size; i++)
{
t[i].no = n[i].no;
}
for (int i = 0; i <size; i++)
{
n[i].evenDigits = 0;
n[i].oddDigits = 0;
}
//oddcount, evencount. (WORKS, BUT ONLY EVERY OTHER LINE)
int countEven;
int countOdd;
for (int i = 0; i < size; i++)
{
countEven = 0;
countOdd = 0;
while (t[i].no > 0)
{
if (t[i].no % 2 == 1)
{
countOdd++;
t[i].no/= 10;
}
else
{
countEven++;
t[i].no/=10;
}
n[i].oddDigits = countOdd;
n[i].evenDigits = countEven;
} i++;
}
//copy number n array to temp n array again.
for (int i = 0; i < size; i++)
{
t[i].no = n[i].no;
}
//Sum digits (WORKS!!!)
//SET TO DEFAULT 0 FOR SUMDIGITS
for (int i = 0; i <size; i++)
{
n[i].sumDigits = 0;
}
for (int i = 0; i < size;)
{
while (t[i].no > 0)
{
n[i].sumDigits = n[i].sumDigits + t[i].no % 10;
t[i].no /= 10;
}i++;
}
//copy number n array to temp n array again.
for (int i = 0; i < size; i++)
{
t[i].no = n[i].no;
}
//SET TO DEFAULT 0 for COUNT DIGITS
for (int i = 0; i <size; i++)
{
n[i].noDigits = 0;
}
//DIGIT COUNT
for ( int i = 0; i < size; i++)
{
int countDigits = 0;
while (t[i].no != 0)
{
t[i].no /= 10;
countDigits++;
}
n[i].noDigits = countDigits;
}
for (int i = 0; i < size; i++)
{
}
cout << "The array was processed" << endl;
}
//Enumerated Number type.
NumType whatType (int n)
{
if (n % 10 % 2 == 1)
return Odd;
else
return Even;
}
//From Array to Outfile.
void arrayToOutfile (ofstream& outFile, char fileName[], Number n[], int size)
{
outFile.open (fileName);
if (!outFile)
{
cout << "Array to " << fileName << " failed" << endl;
exit (-1);
}
cout << "Begin from array to " << fileName << endl;
cout << fixed << showpoint << setprecision (3);
char label [MAX];
outFile << "No" << "\t"
<< "Type" << "\t"
<< "Odd" << "\t"
<< "Even" << "\t"
<< "Sum" << "\t"
<< "Digit"
<< endl;
for (int i = 0; i < size; i++)
{
getStringLabel (n[i].type, label);
outFile << n[i].no << "\t"
<< label << "\t"
<< n[i].oddDigits << "\t"
<< n[i].evenDigits << "\t"
<< n[i].sumDigits << "\t"
<< n[i].noDigits << endl;
}
outFile.close();
cout << "Array to outfile OK" << endl;
}
//Enumeration String label
void getStringLabel (NumType type, char label[])
{
switch (type)
{
case Odd: strcpy (label, "Odd");
break;
case Even: strcpy (label, "Even");
break;
default: strcpy (label, "err");
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数int MAX=100;
枚举NumType{奇数,偶数};
结构编号
{
国际贸易编号;
核型型;
整数位数;
整数位数;
整数和数字;
智力障碍;
};
void数组输出文件(流和,字符[],数字[],整数);
void constructInfile(fstream&,char[]);
int-constructArray(fstream&,char[],Number[]);
void processArray(数字[],int);
NumType whatttype(int);
void getStringLabel(NumType,char[]);
int main()
{
srand(time_t(NULL));
河道充填;
字符文件名[MAX];
cout>文件名;
constructInfile(inFile,文件名);
您可以调用i++两次,一次在for(…)中,第二次在for循环体中,在while循环体结束后。我尝试通过codepad.org运行您的代码,但遗憾的是,没有文件它无法工作。他给出了一个文件的示例;)然而,我编译时没有第二个“i++”在for循环中,结果是正确的…所以威尔伯特的解决方案是有效的。是的,这也是我的第一个猜测。我检查了修改后的代码,它也有效了,所以你肯定是对的;-)谢谢,谢谢你,我的好先生们。当你连续4天死记硬背/编写代码时,诸如此类的事情很容易就过去了。这是我在这里的第一篇帖子,我是v我对这里的运作方式非常满意。:)