This article address: http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html, reprint please indicate source address.
"Exercise 2.11"
Write a program that requires the user to enter two numbers-base and exponent (exponent)-to output the result of the exponent of the radix.
#include <iostream>#include<math.h>#include<string>using namespacestd;intMain () {int Base, exp; Longresult=1; cout<<"Please enter the base and index:"<<Endl; CIN>>Base>>exp; if(exp<0) {cout<<"the exponent cannot be negative! "<<Endl; return-1; } for(intI=1; I <= exp; i++) Result*=Base; cout<<Base<<"of the"<<exp<<"The second party is"<<result<<Endl; System ("PAUSE"); return 0;}
"Exercise 3.7"
Compile a program to read two string objects and test them for equality. If not equal, indicate which of the two is larger. Next, the rewrite program tests whether their lengths are equal and, if not equal, indicates which of the two is longer.
#include <iostream>#include<string>using namespacestd;intMain () {stringstr1, str2; CIN>>str1>>str2; if(str1 = =str2) cout<<"str1 is equal to str2"<<Endl; Elsecout<<"str1 and str2 are not equal"<<Endl; System ("PAUSE"); return 0;}
"Exercise 3.8"
A program that reads multiple string objects from a standard input and connects them to a larger string object. and outputs the concatenated string object. Next, rewrite the program, separating adjacent string objects with spaces after the connection.
#include <iostream>#include<string>using namespacestd;intMain () {stringSTR, SS; cout<<"Please enter a string: \ n"; while(cin>>str) SS= SS +str; cout<<"the string after the connection is:"<<ss<<Endl; System ("PAUSE"); return 0;}
Post-Rewrite program:
#include <iostream>#include<string>using namespacestd;intMain () {stringSTR, SS; cout<<"Please enter a string: \ n"; while(cin>>str) SS = ss + " + str; cout<<"the string after the connection is:"<<ss<<Endl; System ("PAUSE"); return 0;}
"Exercise 3.10"
Compile a program to remove punctuation from the string object. The string required to enter the program must contain punctuation, and the output is a string object after the punctuation is removed.
#include <iostream>#include<string>#include<cctype>using namespacestd;intMain () {stringSTR, SS; cout<<"Please 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 string after the connection is:"<<ss<<Endl; System ("PAUSE"); return 0;}
"Exercise 3.13"
Reads a set of integers to the vector object, computes and outputs the and of each pair of adjacent elements. If the number of elements being read is odd, the user is prompted that the last element is not summed and its value is output.
#include <iostream>#include<string>#include<vector>using namespacestd;intmain () {vector<int>VEC; intN; while(cin>>N) vec.push_back (n); if(!vec.size ()) {cout<<"No numbers!"<<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"
Reads a piece of text into a vector object, and each word is stored as an element in the vector. Converts each word in the vector object to uppercase. Outputs the converted elements in the vector object, with each eight words being one line of output.
#include <iostream>#include<cctype>#include<string>#include<vector>using namespacestd;voidReplacestring&s)//Converts all lowercase characters in a string to uppercase{ for(intI=0; I<s.length (); ++i) {if(Islower (S[i])) S[i]=ToUpper (S[i]); }}intMain () {intN; stringstr; Vector<string>VEC; N=1; cout<<"Please enter a 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; Elsecout<<" "; N++; } System ("PAUSE"); return 0;}
"Exercise 3.18"
Writes a program to create a vector object with 10 elements. Use iterators to change the value of each element to twice times the current value, outputting all elements of the vector.
#include <iostream>#include<vector>using namespacestd;intmain () {vector<int> Vec (Ten,2); for(vector<int>::iterator It=vec.begin (); It!=vec.end (); it++) { *it= (*it) *2; cout<< (*it) <<" "; } cout<<Endl; System ("PAUSE"); return 0;}
C++primer Exercises--3rd Chapter