Chapter 5 Exercises

5.1

Null statement is a single semicolon; somewhere in the grammar of the program requires a statement but does not require logic, then you should use an empty statement

5.2

When the curly brackets sequence of statements, somewhere in the grammar of the program requires a statement, but multiple statements on the need to use logic

5.3

//p5_3.cpp
#include <iostream>
int main ()
{

    int sum = 0;
    for (int val = 1; val <= 10; sum += val, ++val )
    {
        ;
    }   
    std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;

    return 0;
}
 
5.4
a) iter uninitialized
string::iterator iter = s.begin();
while(iter != s.end())
{}
b) if the statement is not required
 
5.5
//p5_5.cpp -- 
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main ()
{
    const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
    string lettergrade;
    int grade;
    cin >> grade;
    if(grade<60)
        lettergrade = scores[0];
    else
    {
        lettergrade = scores[(grade - 50)/10];
        if (grade != 100)
        {
            if(grade%10 > 7)
                lettergrade += "+";
            else if(grade%10 < 3)
                lettergrade += "-";
        }
    }
    cout << grade <<" : " << lettergrade << endl;

    return 0;
    
}
 
5.6
//p5_6.cpp - rewrite 5.5
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main ()
{
    const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
    string lettergrade;
    int grade;
    cin >> grade;
    grade<60 ? lettergrade = scores[0]: 
    lettergrade = scores[(grade - 50)/10],((grade != 100) && (grade%10 > 7))? lettergrade += "+" : 
    (grade%10 < 3)? lettergrade += "-": lettergrade ;
    cout << grade <<" : " << lettergrade << endl;

    return 0;
    
}
 
5.7
a )
if(ival1 != ival2)
  ival1 = ival2; // semicolon
else
  ival1 ival2 = = 0;
b )
if (ival1 < minval)
{
  = Ival manner;
  occurs = 1;  
}
c ) 
if (int ival = get_value())
  cout << "ival = " << ival << endl;
else
  cout << "ival = 0\n";
d )
if (ival == 0)
  ival = get_value();
 
Some simple questions do not waste time
 
5.9
//p5_9.cpp - use if statistics vowels
#include <iostream>
using namespace std;

int main ()
{
    char temp;
    int count = 0;
    while(cin >> temp)
        if(temp == 'a'||temp == 'e'||temp =='i'||temp == 'o'|| temp == 'u')
            ++count;
    cout << "count = " << count << endl;

    return 0;
}
 
5.12
//p5_12.cpp
#include <iostream>
using namespace std;

int main ()
{
    char temp;
    unsigned ffCnt = 0, flCnt = 0,  fiCnt = 0;
    while(cin >> temp)
    {
        if(temp == 'f')
        {
           if(cin >> temp) 
            {
                switch(temp)
                {
                    case 'f': ffCnt++;
                    break;
                    case 'l': flCnt++;
                    break;
                    case 'i': fiCnt++;
                    break;
                    default: break;
                }
            }
        }
    }
    cout << "ffCnt = " << ffCnt << endl;
    cout << "flCnt = " << flCnt << endl;
    cout << "fiCnt = " << fiCnt << endl;

    return 0;
}
 
5.14
//p5_14.cpp 
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main ()
{
    string temp;
    string strTemp = "\ 0"; // temporary storage statistics are words
    string strMax; // store the current highest number of consecutive words
    unsigned strCnt = 0, strTempCnt = 0; // strCnt current maximum number of consecutive, strTempCnt current statistics the number of consecutive words
    while(cin >> temp)
    {
        if(strTemp == "\0" )
        {
            strTemp = temp;
            ++strTempCnt;
        }
        else if(strTemp == temp)
        {
            ++strTempCnt;
        }
        else
        {
            if(strTempCnt >= strCnt )
            {
                strmax = strTemp;
                strCnt = strTempCnt;
            }
            strTemp = temp;
            strTempCnt = 1;
        }
        
    }
    if(strCnt < 2)
        cout << "have no word continuously appear!" << endl;
    else
    {
        cout << strMax << " "<< strCnt << " times " << endl;
    }

    return 0;
       
}
 
5.17
//p5_17.cpp
#include <iostream>
#include <vector>
using namespace std;
main int ()
{
 Vector <int> ivec1 = {0,. 1, 2};
 Vector <int> ivec2 = {0,. 1,. 1, 2,. 3,. 5,. 8};
  // int TEMP;     
 // the while ( cin >> temp) // read data like interactive, but that only the first to successfully read a while loop, while the second loop is only then skip  
 // ivec1.push_back (temp); // What is the solution, please let me know
 //while(cin >> temp)
 //    ivec2.push_back(temp);
 auto n = (ivec1.size() < ivec2.size()) ? ivec1.size() : ivec2.size();
 decltype(ivec1.size()) i = 0;
 for (; i < n; ++i)
 {
  if (ivec1[i] != ivec2[i])
   break;
 }
 bool isPrefix = (i == n) ? true : false;
 if (isPrefix)
  cout << "true" << endl;
 else
 {
  cout << "false" << endl;
 }
 return 0;
}
 
  

Guess you like

Origin www.cnblogs.com/xiaogaogao/p/11760553.html