如何对循环的结果值进行排序?

时间:2014-09-07 07:19:48

标签: c++

这是一个程序,它读取文本文件并告诉用户行数和行中的最大单词数。有两个主要命令,numrows filename和numcols filename。第一个适用于我,但numcols filename命令应该为用户提供文件中一行中的最大字数。例如,如果文本文件包含两行,第1行有2个单词而第2行有5个单词,则输入numcols filename时,输出应为5.

目前,我的循环找到每行中的单词数,但我不确定如何使用这些值并在输入命令时找到最大的单词。任何帮助,将不胜感激!谢谢!

    #include <iostream>                                                       
    #include <fstream>  
    #include <istream>
    #include <cstdlib>
    #include <sstream>
    #include <algorithm>

    using namespace std;               

    void prompt()
    {
        cout << " >> ";
    }

    int main() 
    {
        string fname, line, strn, input;                                                         

        ifstream ifs; // input file stream         
        istringstream ss1,ss2;                                 
        int i;        
        cout << " ** USAGE ** " << endl;
        cout << " " << endl;
        cout << " numrows <filename> - displays the number of lines " << endl;
        cout << " numcols <filename> - displays the number of columns" << endl;   
        cout << " quit/exit/bye - exit the program" << endl; 
        cout << " " << endl;  
        prompt();

        while (getline(cin, strn)){
        ss1.str (strn);

        ss1 >> input;
        ss1 >> fname;

        ifs.open(fname.c_str()); 

        if (ifs.fail()) {                                                       
            cerr << "ERROR: Failed to open file " << fname << endl;   
            ifs.clear();     

        } else {  
            if (strn == "numrows " + fname) {
                int number_of_lines = 0;
                string line;                                                                          
                while (getline(ifs, line))      
                    ++number_of_lines;
                cout << number_of_lines << endl;
                prompt();

            }   else if (strn == "numcols " + fname) {
                string line1;
                while ( getline(ifs,line1) )
                {                                       
                    int words=0; 
                    istringstream is(line1); 
                    while(is >> line1) 
                    ++words; 

                    cout << max << endl;                            
            }                                                                                       
                prompt();

            }   else if (strn == "quit" || strn == "exit" || strn == "bye"){
                cout << "TESTING " << endl;
                return (0);

            }  else {
                cout << "Invalid Command! Retry.." << endl;
                prompt();
            }   
        }                     
        ifs.close();                      
        }
        return 0;                                                                   
    }

1 个答案:

答案 0 :(得分:0)

引入一个新变量max_words,它保存到目前为止已处理的行中的最大字数。读取一个新行,计算单词并将其与max_words进行比较,如果单词计数较大,则将值分配给单词计数,否则保留该值。

int max_words = 0;
while (getline(ifs, line1)) {
    int words = 0;
    istringstream is(line1);
    while (is >> line1) { ++words; }
    if (words > max_words) {
         max_words = words;
    }
}
cout << max_words;

您可以使用0初始化max_words并获取正确的值,因为没有字数&lt; 0