第5章 练习题

5.1

空语句就是一个单独的分号;在程序的某个地方语法上需要一条语句但是逻辑上不需要,此时应该使用空语句

5.2

用花括号括起来的语句序列,在程序的某个地方语法上需要一条语句,但逻辑上需要多条语句时用

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 没有初始化
string::iterator iter = s.begin();
while(iter != s.end())
{}
b ) 不需要if 语句
 
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 -- 改写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;  //加分号
else
  ival1 = ival2 = 0;
b )
if (ival1 < minval)
{
  minval = ival;
  occurs = 1;  
}
c ) 
if (int ival = get_value())
  cout << "ival = " << ival << endl;
else
  cout << "ival = 0\n";
d )
if (ival == 0)
  ival = get_value();
 
一些题比较简单就不浪费时间了
 
5.9
//p5_9.cpp -- 使用if统计元音字母
#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";      //临时存储正在统计的单词
    string strMax;              //存储当前连续出现次数最多的单词
    unsigned strCnt = 0, strTempCnt = 0;    //strCnt当前连续出现最多次数,strTempCnt当前统计单词连续出现次数
    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;
int main()
{
 vector<int> ivec1 = { 0, 1, 2 };
 vector<int> ivec2 = { 0, 1, 1, 2, 3, 5, 8 };
  //int temp;     
 //while(cin >> temp)  //想通过交互式的读取数据,但是每次只有第一个while循环能够成功读取,第二个while循环就只接跳过了  
 //    ivec1.push_back(temp); //有什么解决方案,请不吝赐教
 //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;
}
 
  

猜你喜欢

转载自www.cnblogs.com/xiaogaogao/p/11760553.html