如何在while循环中添加数字?

时间:2018-03-04 07:07:11

标签: c++

代码输出单独的行,但我希望它将所有行添加到一起。

示例testdata文件:

Iris Pruysen参加2014年田径残奥会;

Iris Pruysen,一名假腿运动员,参加2014年法国巴黎StadeSébastienCharléty田径残奥会的跳远比赛。今天是2017年7月27日。

输出是:

Average number of letters per sentence 163.000  
Total number of digits is 8  
Average number of letters per sentence 5.000  
Total number of digits is 6

如何只有一行平均字母数和一行总数?

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
using namespace std;
int main()
{
  int letters; //Total number of letters                                                                                                                                           
  int digits; //Total number of digits                                                                                                                                             
  double sentencecount; //Number of sentences                                                                                                                                      
  float averageletters; //Average number of letters per sentence                                                                                                                   
  int linecount=0; //Count of lines                                                                                                                                                
  char current; //Current character                                                                                                                                                                                                           
  cout << fixed << setprecision(3);
  cin.get(current);
  while (cin) //while receiving input                                                                                                                                              
    {
      digits = 0;
      letters = 0;
      linecount++;
      while (current != '.' && current != '!' && current != '?')        //checks each line                                                                                         
        {
          if (isalpha(current))//counts alphabet characters                                                                                                                        
            letters++;
          if (isdigit(current))//counts digits                                                                                                                                     
            digits++;
          cin.get (current);
        }
      sentencecount = letters/linecount;//finds average letters per sentence                                                                                                       
      cin.get (current);
      cout << "Average number of letters per sentence " << sentencecount << endl << "Total number of digits is " <<  digits << endl;
    }
  //  cout << "Average number of letters per sentence " << sentencecount << endl << "Total number of digits is " <<  digits << endl;                                               
  return 0;
}

2 个答案:

答案 0 :(得分:1)

这是我清理代码的努力。这真的是在正确的时间做正确的事情。你需要考虑循环内部应该是什么以及外部应该是什么。例如,显而易见的是,如果你试图总计字母数,那么在循环中将字母数设置为零是错误的。

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
using namespace std;
int main()
{
  int letters = 0; // Total number of letters                                                                                                                                           
  int digits = 0; // Total number of digits                                                                                                                                             
  double sentencecount; // Number of sentences                                                                                                                                      
  float averageletters; // Average number of letters per sentence                                                                                                                   
  int linecount=0; // Count of lines                                                                                                                                                
  char current; // Current character                                                                                                                                                                                                           
  cout << fixed << setprecision(3);
  cin.get(current);
  while (cin) // While receiving input                                                                                                                                              
  {
    linecount++;
    while (current != '.' && current != '!' && current != '?') // Checks each line                                                                                         
    {
        if (isalpha(current)) // Counts alphabet characters                                                                                                                        
          letters++;
        if (isdigit(current)) // Counts digits                                                                                                                                     
          digits++;
        cin.get (current);
    }

    cin.get (current);
  }
  sentencecount = letters/linecount; // Finds average letters per sentence                   
  cout << "Average number of letters per sentence " << sentencecount << endl
       << "Total number of digits is " << digits << endl;                                               
  return 0;
}

免责声明,我实际上没有测试过上面的代码。

答案 1 :(得分:0)

只使用一个while循环就可以简化此代码。行数不计算在内。使用if (current == '\n') linecount++;计算行数。

目前,"!!!"被视为3个句子。检查前一个字符以正确识别句子的结尾。

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    int letters = 0;
    int digits = 0;
    int sentences = 0;

    stringstream source;
    source << "!!!Abc def 1 2 3 100. Sentence 2.";

    char current;
    char previous = 0;

    while(source.get(current))
    {
        if(isalnum(previous) && (current == '.' || current == '!' || current == '?'))
            sentences++;
        else if(isalpha(current))
            letters++;
        else if(isdigit(current))
            digits++;
        previous = current;
    }

    cout << "digits " << digits << endl;
    cout << "letters " << letters << endl;
    cout << "sentences " << sentences << endl;
    cout << "total average " << (float)letters/sentences << endl;

    return 0;
}

请注意,当isalpha(c)c

之一时,!?.将返回零