C++primer (fifth edition) Fifth chapter summary (including some exercises source code and parsing) statement

Source: Internet
Author: User

This chapter explains some basic statements, the C language basis will find roughly the same! There are, of course, some subtle differences;

Conditional statements:

A:If statements

The error point of the C language is the same: The conditional statement is written in an assignment statement, and the compiler has a great impact on the implementation of the function, which requires special attention.

Example: if (max=1), this should be if (max==1), such a mistake seems not to be committed, often for various reasons always appear, but when you know the easy wrong place, you will be very easy to modify!

B:switch statements

We are also familiar with, familiar with how to know how to use flexible; if the following two about the switch statement and break statement code, can quickly see the output, it is understood that we need to use this feature flexibly!

Input: Huananligongdxue

Code Listing 1:

#include <iostream>
using namespace Std;
int main ()
{
unsigned acnt=0,ecnt=0,icnt=0,ocnt=0,ucnt=0;
Char ch;
cout<< "Please enter characters, press CTRL + Z" <<endl;
while (CIN&GT;&GT;CH)
{


Switch (CH)
{


Case ' a ':
++acnt;
Break
Case ' E ':
++ecnt;
Break
Case ' I ':
++icnt;
Break
Case ' O ':
++ocnt;
Break
Case ' U ':
++ucnt;
Break
}
}
cout<< "Number of vowel a:\t" <<acnt<< ' \ n ' << "number of vowel e:\t" <<ecnt<< ' \ n ' < < "number of vowel i:\t" <<icnt<< ' \ n '
<< "Number of vowel o:\t" <<ocnt<< ' \ n ' << "number of vowel u:\t" <<ucnt<<endl;
return 0;
}

Code Listing 2:

#include <iostream>
using namespace Std;
int main ()
{
unsigned acnt=0,ecnt=0,icnt=0,ocnt=0,ucnt=0;
Char ch;
cout<< "Please enter characters, press CTRL + Z" <<endl;
while (CIN>>CH)
{


Switch (CH)
{


Case ' a ':
++acnt;


Case ' E ':
++ecnt;


Case ' I ':
++icnt;


Case ' O ':
++ocnt;


Case ' U ':
++ucnt;

}
}
cout<< "Number of vowel a:\t" <<acnt<< ' \ n ' << "number of vowel e:\t" <<ecnt<< ' \ n ' < < "number of vowel i:\t" <<icnt<< ' \ n '
<< "Number of vowel o:\t" <<ocnt<< ' \ n ' << "number of vowel u:\t" <<ucnt<<endl;
return 0;
}

We soon came to the conclusion that the first is obviously that the input character will increase the corresponding number of characters, then jump out of the loop; Code 2 because there is no break out, it will be executed to continue the following statement, so that the result is definitely ascending sequence, from top to bottom are superimposed;

The results are as follows:

Code 1 Results:

Code 2 results:

Continue statement: Interrupts the current iteration, but resumes execution of the loop;


Exercises 5.41 and 5.20 explain together:

The code is as follows:

#include <iostream>

using namespace Std;

int main ()
{
String ch,sw,mch;
int max=1,b=1;
cout<< "Input test word:" <<endl;

while (CIN&GT;&GT;CH)
{


if (sw==ch)
{
++b;


}


Else
{
if (B>max)
{
Max=b;
MCH=SW;
}
Else
B=1;
}
Sw=ch;

}
if (max==1)
cout<< "Word does not appear continuously \ n" <<endl;
Else
cout<< "word" <<mch<< "maximum number of consecutive times" <<max<<endl;

return 0;
}

Results:


Above is 5.14 of the program, 5.2 is to judge it, b=2, the end of the loop! Although the above solves 5.14 of the problem, just insufficient is the boundary question consideration is not very thoughtful, if there are several identical longest words, also can only print one longest! Need to improve!!!

The application of codeblocks may have some compiler compatibility problems!










C++primer (fifth edition) Fifth chapter summary (including some exercises source code and parsing) statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.