2012-11-30 82 views
1

好的,我的代碼很長。但是這個有點棘手,所以我只是在這裏裸露。 我什麼程序做是生成隨機數的文本文件,如:結構陣列奇/偶毛刺

123 
1234 
12345 
123456 

而且該方案需要在該文件中,並把隨機數據到一個結構數組。 它然後處理數組和輸出,看起來像這樣的文件:

outfile.txt出品基於上述infile.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++; 

} 

這裏是整個代碼。

#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"); 
    } 
} 

傢伙,是的,我知道的代碼是非常非結構化和凌亂尤其是在處理陣列功能 - 我往往會很快,但首先清理了一切,你爲什麼發生這種情況的專業意見,將不勝感激。

-Ace

+0

我試圖通過codepad.org運行你的代碼,但遺憾的是它沒有文件就無法運行。 –

+0

他舉了一個例子,說明文件的外觀如何; )然而,我編譯時沒有第二個「i ++」int for循環,結果是正確的...所以威爾伯特的解決方案工作。 – tamasgal

回答

3

你叫我++兩次,內一次(...),第二次在for循環體,而身體結束後。

+0

是的,這也是我的第一個猜測。我檢查了修改後的代碼,它的工作原理,所以你肯定是對的;-) – tamasgal

+0

謝謝你,謝謝你,我的好先生。當你連續四天填寫/編碼時,這些事情就很容易通過你。這是我在這裏的第一篇文章,我對這裏運行的方式非常滿意。 :) –