C ++ primer exercises-Chapter 1st and primer exercises-Chapter 1st

Source: Internet
Author: User

C ++ primer exercises-Chapter 1st and primer exercises-Chapter 1st

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

[Exercise 1.3]

Compile a program and print "Hello, World" on the standard output ".

#include <iostream>using namespace std;int main(){    cout<<"Hello, World\n";    return 0;}

[Exercise 1.4]
Our program uses the built-in addition operator "+" to generate the sum of two numbers. Write a program and use the multiplication operator "*" to generate the product of two numbers.

#include <iostream>using namespace std;int main( ){    cout<<"Enter two numbers: "<<endl;    int v1, v2;    cin>>v1>>v2;    cout<<"The product of"<<v1<<" and "<<v2<<" is "<<v1 * v2<<endl;    system("PAUSE");    return 0;}

[Exercise 1.5]
Our program uses a long output statement. Rewrite the program and print each operand using a separate statement.

#include <iostream>using namespace std;int main( ){    cout<<"Enter two numbers: "<<endl;    int v1, v2;    cin>>v1>>v2;    cout<<"The sum of";    cout<<v1;    cout<<" and ";    cout<<v2;    cout<<" is ";    cout<<v1 + v2;    cout<<endl;    system("PAUSE");    return 0;}

[Exercise 1.10]
Use for loop programming to calculate from 50 ~ The sum of all natural numbers of 100. Then rewrite the program with a while loop.
Use for loop:

#include <iostream>using namespace std;int main( ){    int i, sum=0;    for(i=50; i<=100; ++i)        sum+=i;    cout<<"sum of 50 to 100 is "<<sum<<endl;    system("PAUSE");    return 0;}

Use a while loop:

#include <iostream>using namespace std;int main( ){    int i, sum;    i=50;    sum=0;    while(i++<=100)        sum+=i;    cout<<"sum of 50 to 100 is "<<sum<<endl;    system("PAUSE");    return

[Exercise 1.11]
Use while LOOP programming to output 10 ~ A natural number that is 0 in descending order. Then rewrite the program with a for loop.

Use a while loop:

#include <iostream>using namespace std;int main( ){    int i=10;    while(i>=0)        cout<<i--<<endl;    return 0;}

Use for loop:

#include <iostream>using namespace std;int main( ){    for(int i=10; i >=0; --i)        cout<<i<<endl;
  return 0;}

[Exercise 1.16]
Write a program to output the large numbers of user input.

# Include <iostream> using namespace std; int main () {int a, B; cout <"enter two numbers:"; cin> a> B; cout <"a large number is:" <(a> = B )? A: B) <endl; system ("PAUSE"); return 0 ;}

[Exercise 1.17]
Write a program, requiring the user to enter a group of numbers. The output information indicates the number of negative numbers.

# Include <iostream> using namespace std; int main () {int num, a; num = 0; while (cin> a) if (a <0) num ++; cout <"the number of input negative numbers is:" <num <endl;
Return 0 ;}

[Exercise 1.18]
Write a program, prompting the user to enter two workers to write each number in the two data ranges to the standard output.

# Include <iostream> using namespace std; void print (int a, int B) {a ++; while (a <B) cout <a ++ <""; cout <endl ;}int main () {int a, B, I; cout <"enter two numbers:"; cin >>a> B; if (a <B) print (a, B); else print (B, );
Return 0 ;}

[Exercise 1.21]
The book supporting the website (http://www.awprofessional.com/cpp_primer) Chapter 1 of the Code directory has Sales _ item. h source file. Copy the file to your working directory. Write a program that cyclically traverses the sales transactions of a group of books and reads the transactions into each transaction segment to write the transactions to the standard output.

# Include <iostream> # include "Sales_item.h" using namespace std; int main () {Sales_item book; cout <"input transaction:" <endl; while (cin> book) {cout <"number of books sold, total revenue, and average price:" <endl; cout <book <endl ;} system ("PAUSE"); return 0 ;}

Add the Sales _ item. h source file:

# Ifndef SALESITEM_H # define SALESITEM_H // Definition of Sales_item class and related functions goes here # include <iostream> # include <string> class Sales_item {friend bool operator = (const Sales_item &, const Sales_item &); // other members as beforepublic: // added constructors to initialize from a string or an istream Sales_item (const std: string & book): isbn (book ), units_sold (0), revenue (0.0) {} Sale S_item (std: istream & is) {is> * this;} friend std: istream & operator> (std: istream &, Sales_item &); friend std:: ostream & operator <(std: ostream &, const Sales_item &); public: // operations on Sales_item objects // member binary operator: left-hand operand bound to implicit this pointer Sales_item & operator + = (const Sales_item &); // other members as before public: // operations on Sales_item objec Ts double avg_price () const; bool same_isbn (const Sales_item & rhs) const {return isbn = rhs. isbn;} // default constructor needed to initialize members of built-in type Sales_item (): units_sold (0), revenue (0.0) {}// private members as beforeprivate: std:: string isbn; unsigned units_sold; double revenue ;}; // nonmember binary operator: must declare a parameter for each operandSales_item operator + (Const Sales_item &, const Sales_item &); inline bool operator = (const Sales_item & lhs, const Sales_item & rhs) {// must be made a friend of Sales_item return lhs. units_sold = rhs. units_sold & lhs. revenue = rhs. revenue & lhs. same_isbn (rhs);} inline bool operator! = (Const Sales_item & lhs, const Sales_item & rhs) {return! (Lhs = rhs );//! = Defined in terms of operator ==} using std: istream; using std: ostream; // assumes that both objects refer to the same isbninlineSales_item & Sales_item :: operator + = (const Sales_item & rhs) {units_sold + = rhs. units_sold; revenue + = rhs. revenue; return * this;} // assumes that both objects refer to the same isbninlineSales_item operator + (const Sales_item & lhs, const Sales_item & rhs) {Sales_item ret (lhs ); // copy lhs into a local object that we'll return ret + = rhs; // add in the contents of rhs return ret; // return ret by value} inlineistream & operator> (istream & in, Sales_item & s) {double price; in> s. isbn> s. units_sold> price; // check that the inputs succeeded if (in) s. revenue = s. units_sold * price; else s = Sales_item (); // input failed: reset object to default state return in;} inlineostream & operator <(ostream & out, const Sales_item & s) {out <s. isbn <"\ t" <s. units_sold <"\ t" <s. revenue <"\ t" <s. avg_price (); return out;} inlinedouble Sales_item: avg_price () const {if (units_sold) return revenue/units_sold; else return 0;} # endifView Code

[Exercise 1.22]
Write a program and read two Sales_item object operators with the same ISBN to generate their sums.

# Include <iostream> # include "Sales_item.h" using namespace std; int main () {Sales_item trans1, trans2; cout <"read transaction:" <endl; cin> trans1> trans2; if (trans1.same _ isbn (trans2) cout <"the two transactions have the same ISBN and are: "<endl <trans1 + trans2; else cout <" the two transactions do not have the same ISBN "; return 0 ;}

[Exercise 1.23]
Write a program, read several transactions with the same ISBN, and output the sum of all read transactions.

# Include <iostream> # include "Sales_item.h" using namespace std; int main () {Sales_item sum, trans; cout <"read transaction:" <endl; if (cin> sum) {while (cin> trans) {if (sum. same_isbn (trans) sum + = trans; else {cout <"different ISBN! "<Endl; return-1 ;}} else {cout <" no data! "<Endl; return-1;} return 0 ;}

[Exercise 1.24]
Write a program and read several different transactions. For each newly read transaction, determine whether its ISBN is the same as the ISBN of the previous transaction, and write down the total number of transactions of each ISBN.

Test the program by specifying multiple transactions. These transactions must represent multiple different ISBN, but each ISBN record should be in the same group.

# Include <iostream> # include <map> # include <string> # include "Sales_item.h" using namespace std; int main () {Sales_item trans; cout <"read transaction: "<endl; cin> trans; map <string, int> count; count [trans. getIsbn ()] ++; while (cin> trans) {++ count [trans. getIsbn ()];} map <string, int >:: iterator it; for (it = count. begin (); it! = Count. end (); it ++) cout <it-> first <":" <it-> second <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.