C ++ primer exercises-Chapter 3rd and primer exercises-Chapter 3rd

Source: Internet
Author: User

C ++ primer exercises-Chapter 3rd and primer exercises-Chapter 3rd

URL: http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html.

[Exercise 2.11]
Write a program that requires the user to input two numbers-base and exponent to output the exponential result of the base number.

# Include <iostream> # include <math. h ># include <string> using namespace std; int main () {int base, exp; long result = 1; cout <"Enter the base number and index: "<endl; cin> base> exp; if (exp <0) {cout <" index cannot be negative! "<Endl; return-1 ;}for (int I = 1; I <= exp; I ++) result * = base; cout <base <"" <exp <"sub-party:" <result <endl; system ("PAUSE"); return 0 ;}

[Exercise 3.7]
Compile a program to read two string objects and test whether they are equal. If they are not equal, it indicates which of the two is larger. Then, the rewrite program tests whether their lengths are equal. If they are not equal, it indicates which of the two is longer.

# Include <iostream> # include <string> using namespace std; int main () {string str1, str2; cin> str1> str2; if (str1 = str2) cout <"str1 and str2 are equal" <endl; else cout <"str1 and str2 are not equal" <endl; system ("PAUSE"); return 0 ;}

[Exercise 3.8]

Compile a program, read multiple string objects from the standard input, and connect them to a larger string object. And output the connected string object. Next, rewrite the program to separate adjacent string objects after the connection with spaces.

# Include <iostream> # include <string> using namespace std; int main () {string str, ss; cout <"Enter the string: \ n "; while (cin> str) ss = ss + str; cout <"the connected string is:" <ss <endl; system ("PAUSE "); return 0 ;}

The rewritten program:

# Include <iostream> # include <string> using namespace std; int main () {string str, ss; cout <"Enter the string: \ n "; while (cin> str) ss = ss + ''+ str; cout <" the connected string is: "<ss <endl; system ("PAUSE"); return 0 ;}

[Exercise 3.10]

Compile a program to remove punctuation marks from the string object. The string entered into the program must contain punctuation marks. The output result is the string object after the punctuation marks are removed.

# Include <iostream> # include <string> # include <cctype> using namespace std; int main () {string str, ss; cout <"enter a string: \ n "; getline (cin, str); for (string: size_type I = 0; I! = Str. size (); ++ I) {if (! Ispunct (str [I]) ss + = str [I];} cout <"the connected string is:" <ss <endl; system ("PAUSE"); return 0 ;}

[Exercise 3.13]

Read a set of Integers to the vector object and calculate and output the sum of adjacent elements of each pair. If the number of read elements is odd, the system prompts that the last element has no sum and outputs its value.

# Include <iostream> # include <string> # include <vector> using namespace std; int main () {vector <int> vec; int n; while (cin> n) vec. push_back (n); if (! Vec. size () {cout <"no number! "<Endl; return-1 ;}for (vector <int >:: size_type I = 0; I <vec. size ()-1; I + = 2) {cout <vec [I] + vec [I + 1] <"\ t"; if (I + 1) % 6 = 0) cout <endl;} if (vec. size () % 2! = 0) cout <endl <"the last number is:" <vec [vec. size ()-1] <endl; system ("PAUSE"); return 0 ;}

[Exercise 3.14]
Read a piece of text into a vector object. Each word is stored as an element in the vector. Converts each word in a vector object to an uppercase letter. Output the converted elements in a vector object. Every eight words are output in one row.

# Include <iostream> # include <cctype> # include <string> # include <vector> using namespace std; void replace (string & s) // convert all lowercase characters in the string to uppercase {for (int I = 0; I <s. length (); ++ I) {if (islower (s [I]) s [I] = toupper (s [I]) ;}} int main () {int n; string str; vector <string> vec; n = 1; cout <"enter a piece of text: \ n"; while (cin> str) vec. push_back (str); for (vector <string >:: iterator I = vec. begin (); I! = Vec. end (); ++ I) {replace (* I); cout <(* I); if (n % 8 = 0) cout <endl; else cout <"; n ++;} system (" PAUSE "); return 0 ;}

[Exercise 3.18]

Write a program to create a vector object with 10 elements. Use the iterator to change each element value to twice the current value and output all elements of the vector.

#include <iostream>#include <vector>using namespace std;int main( ){    vector<int> vec(10,2);    for(vector<int>::iterator it=vec.begin(); it!=vec.end(); it++) {        *it=(*it)*2;        cout<<(*it)<<" ";    }    cout<<endl;    system("PAUSE");    return 0;}

 

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.