2016-05-26 34 views
-1

我需要做的是:我现在有一个向量行,v[0]是第一行等等。我想将每行的第一个数字作为挑战,并将每行的第二个数字作为裁判读取,然后在代码中应用条件。我想用stringstream来读取行中的数字。使用stringstream和getline来读取每行的前两个数字

我的代码现在在做什么:它只读取每行的第一个数字。所以第一行的第一个数字是挑战,第二行的第一个数字是法官,第三行的第一个数字是挑战。

std::vector<string> v; 
    string line; 
    int i; 
    double challenge; 
    int judge; 

    while (getline(cin, line)) { 
     if (line.empty()) { 
      break; 
     } 

     v.push_back(line); 
    } 

    for (i = 0; i < v.size();i++) { 
     cin >> v[i]; 
     std::stringstream ss(v[i]); 
     ss << v[i]; 
     ss >> challenge >> judge; 

     if (challenge < 1 || challenge > 5) { 
      cout << "bad_difficulty" << endl; //must add the condition or empty 
      v.erase(v.begin() + i); 
     } 

     if (judge != 5 || judge != 7) { 
      cout << "bad_judges" << endl; //must add the condition or empty 
      v.erase(v.begin() + i); 
     } 

     cout << v[i] << endl; 

    } 

    return 0; 
} 

例如:

Input: 
5.1 7 5.4 3.0 9.6 2.9 2.8 2.0 5.4 
-3.8 7 2.9 1.1 5.7 7.2 4.8 8.5 3.9 
2.2 5 9.4 4.7 7.3 1.9 5.7 6.0 7.1 
2.4 6 9.2 5.2 1.0 2.9 4.9 7.4 7.9 
2.1 7 7.9 4.9 0.0 7.2 9.1 7.8 6.7 4.3 
3.8 5 
2.0 
4.0 7 2.4 1.9 3.2 8.3 14.8 0.1 9.7 
2.5 7 8.4 -8.0 5.0 6.0 8.0 1.3 3.3 
1.6 -1 9.5 2.5 5.8 7.9 5.5 1.6 7.9 

Output should be: 
bad_difficulty 
bad_difficulty 
2.2 5 9.4 4.7 7.3 1.9 5.7 6.0 7.1 
bad_judges 
2.1 7 7.9 4.9 0.0 7.2 9.1 7.8 6.7 4.3 
3.8 5 
bad_judges 
4.0 7 2.4 1.9 3.2 8.3 14.8 0.1 9.7 
2.5 7 8.4 -8.0 5.0 6.0 8.0 1.3 3.3 
bad_judges 

Current Output: 
bad_difficulty 
bad_judges 
2.2 5 9.4 4.7 7.3 1.9 5.7 6.0 7.1 
bad_judges 
2.1 7 7.9 4.9 0.0 7.2 9.1 7.8 6.7 4.3 
bad_judges 
2.0 
bad_judges 
2.5 7 8.4 -8.0 5.0 6.0 8.0 1.3 3.3 
bad_judges 
1.6 -1 9.5 2.5 5.8 7.9 5.5 1.6 7.9 
+1

这是什么问题? '法官!= 5 ||法官!= 7'对我来说不好。 – LogicStuff

+0

我发表了一个例子。第二个号码的判断号码应该是5或7,否则就是bad_judges – user3328381

+0

你目前得到了什么输出? –

回答

0

让我们通过删除循环行走。

i = 0 
v[0] contains line 1. 
Line 1 is 5.1 7 
challenge > 5, remove 0. The vector shifts up by 1 v[0] now contains line 2 
Judge == 7 do not remove 0 
increment i 

i = 1 
v[1] contains line 3. Line 2 has been skipped 
Line 3 is 2.2 5 
challenge < 5, do not remove 1. 
Judge is not 5 or 7. remove 1. The vector shifts up by 1 v[1] now contains line 4 
increment i 

i = 2 
v[2] contains line 5. Line 4 has been skipped 
Line 5 is 2.1 7 
challenge < 5, do not remove 2. 
Judge is 7. do not remove 2. 
increment i 

i = 3 
v[3] contains line 6. 
Line 6 is 3.8 5 
challenge < 5, do not remove 3. 
Judge is 5. do not remove 3. 
increment i 

i = 3 
v[3] contains line 7. 
Line 7 is 2.0 
challenge < 5, do not remove 3. 
Judge is UNDEFINED! PANIC! PANIC! Crom only knows what happens. 
increment i 

无论如何,这里的基本模式应该是清楚的。当您从vector中移除一个元素时,所有后续元素都会上移。解决方案:删除元素时,请勿增加ielse声明将为您处理此问题。

接下来,因为有两个单独的if语句可能两个条件都为真,v[i]将被删除两次。有很多方法可以解决这个问题。 Manthan Tilva与continue的解决方案简单而有效,但这可以通过else if或将两个测试滚动到相同的if更明显地处理。

第三,使用未从流中读取的值是未定义的。不应该使用。放弃这条线,不用再看了。 if (ss >> challenge >> judge)将在这里帮助。

+0

你的代码工作曼陀!还有一步要走得更远。我需要阅读向量中法官之后的所有数字(比较这些数字并得到最高的数字,并且看看在每个5名裁判之后我们是否有正好5个数字,并且在每个7名裁判之后我们有7名裁判) 。是否有可能在for循环(i = 0; i > vector [i]。我怎么可能做到这一点? – user3328381

相关问题