Esempio n. 1
0
int main(int argc, const char * argv[]) {
    string pattern = ";";           //";" as spliter
    string data[1024][3];           //Store the valid data and being sorted
    int n = 0;                      //The nth valid line
    
    //Read files according to the names input on the command line
    for (int f = 1; f<argc; f++) {
        vector<string> result;
        //string fileName = argv[f];
        ifstream in(argv[f]);
        string line;
        
    if(in){
        while(getline(in,line)){
            
            // Check if each line of data can be splitted into 3 parts
            char s[1024];
            line.copy(s, line.length());
            int splitCheck = 0;
            for (int snum = 0; snum < line.length(); snum++) {
                if (s[snum] == ';') splitCheck++;
            }
            
            if (splitCheck == 2) {
                result = split(line, pattern); //Split current line of data by ";"
                
                // Check the empty item
                int checkFormat = 0;
                if ((result[0] == "") && (result[1] == "") && (result[2] == "")) {
                    checkFormat++;
                }
                
                // Check the duplicate item
                int checkSame = 0;
                for (int check = 0; check < n ; check++) {
                    if (data[check][0] == result[0] && data[check][1] == result[1] && data[check][1] == result[1]) {
                        checkSame++;
                    }
                }
                
                // Check if the input age is a number (Check data type)
                int checkInt = 0;
                string age = result[1];
                for (int ageNum = 0 ; ageNum<age.length(); ageNum++) {
                    if (isdigit(result[1][ageNum])){
                        checkInt++;
                    }
                }
                
                // Check if the input age is in the normal range
                int checkAge = 0;
                int a = atoi(age.c_str());
                if (a>=0 && a <= 200) {
                    checkAge++;
                }
                
                // If passing all the checking processes, store the current line to the data array
                if (checkFormat == 0 && checkSame == 0 && checkInt > 0 && checkAge > 0) {
                    for (int i = 0; i<3; i++) {
                        data[n][i] = result[i];
                    }
                    n++;    // Ready for the next item
                }
            }
        }
        in.close();//File closed
    }
    else{
        cout<<"no such file"<<endl;
    }
    }
    
   
    Trees tr;        // Initialize a Tree
    
    // Put the stored data into the Tree for sorting
    for (int i = 0; i<n; i++) {
        int age = atoi(data[i][1].c_str());
        string code = data[i][0];
        string name = data[i][2];
        tr.create(age,code,name);
    }
    cout<<" code   age    name"<<endl;
    tr.disp();      // Output the results
    cout<<endl;
    return 0;
}