[C ++ Primer notes] Chapter 1 exercises

Chapter 1 exercises

Environment: CLion + MinGW

1.1 Check the compiler used the documentation to determine that it uses file naming conventions, compile and run the main program of the second page

slightly

1.2 rewrite program, it returns -1. Return value of -1 is generally identified as a program error. Recompile and run your program, observe how your system identifies the main processing errors returned

int main(){
    return -1;
}
g++ main.cpp

a.exe

echo %ERRORLEVEL%
-1

Command-line operating results, view the status get -1

1.3 Write a program that prints on standard output Hello, World

#include <iostream>
int main(){
	std::cout << "Hello, World" << std::endl;
	return 0;
}

Our 1.4 + operator uses a wig to the two numbers together, programming a multiplication operator * to print product of two numbers

#include <iostream>
int main(){
    int a = 0, b = 0;
    std::cin >> a >> b;
    std::cout << "The product of " << a << " and " << b
        << " is " << a * b << std::endl;
    return 0;
}

1.5 We will all output operations on a long statement, rewrite the program will print the operation of each of the operands in a separate statement

slightly

1.6 explain the following program fragment is legitimate, if not legally be how to fix

std::cout << "The sum of " << v1;
		  << " and " << v2;
		  << " is " << v1 + v2 << std::endl;

Illegal, may be at the beginning of three rows plus a second std::cout, constitute three statements, or removing the first two lines at the end of a semicolon, a synthesized statement

Compile a 1.7 contains an incorrect nested comments, an error message is returned to observe the compiler

int main(){
/*
 */* incorrect comment*/
 *
 */
	return 0;
}
In function 'int main()':
error: 'incorrect' was not declared in this scope
  */* incorrect comment*/

Since delimiter nested pairs such that incorrect comment statement is considered outside Notes

1.8 points out that the following output statements are legal

std::cout << "/*";	
std::cout << "*/";	
std::cout << /*"*/" */;
std::cout << /*"*/" /* "/*" */; 

(1) legal, printing / *

(2) legal, printing * /

(3) Illegal, equivalent tostd::cout << "*/;

(4) legal, equivalent to std::cout << " /* ";printing / *

1.9 programming, while loop integer adding 50 to 100

#include <iostream>
int main(){
	int i = 50, sum = 0;
	while(i <= 100){
		sum += i;
		i += 1;
	}
	std::cout << sum << std::endl;
	return 0;
}

In addition to 1.10 ++ operator operand value increases beyond 1, there is a decrement operator (-) to achieve a reduced value. Decreasing programming, using the operator to print in descending order of an integer in a loop 10-0

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

1.11 Write a program that prompts the user for two integers, print out all integers within two integers specified range

#include <iostream>
int main(){
    int low, high;
    std::cout << "Enter two numbers: ";
    std::cin >> low >> high;
    if(low > high){		// 若用户按降序输入, 则调换两个变量的值
        int tmp = low;
        low = high;
        hight = tmp;
    }
    int i = low;
    while(i <= high){
        std::cout << i << " ";
        i ++;
    }
    std::cout << std::endl;
    return 0;
}

1.12 The following cycle has been completed for what function? What is the sum of the final value?

int sum = 0;
for(int i = -100; i <= 100; ++i)
	sum += i;

Of [-100, 100] between the integer sum, sum of the final value of 0

1.13 using a for loop to redo all the exercises in section 1.4.1

slightly

1.14 Comparison for and while loops, two forms of what strengths and weaknesses?

Number of cycles is used for loop is known, more simple and easy to understand, the unknown number of cycles or a specific condition control cycle is used while, more flexible

1.16 programming, reading a set of numbers from CIN, and outputs

#include <iostream>
int main(){
	int val = 0, sum = 0;
	while(std::cin >> val){
		sum += val;
	}
	std::cout << sum << std::endl;
	return 0;
}

1.17 If all input values ​​are equal, this section of the program will output what? If there are no duplicate values, the output will be like?

#include <iostream>
int main(){
    int val = 0, cur = 0, cnt = 1;
    if(std::cin >> val){
        while(std::cin >> cur){
            if(cur == val){
                cnt += 1;
            }else{
                std::cout << val << " occurs " << cnt <<" times." << std::endl;
                val = cur;      // reset val, count
                cnt = 1;
            }
        }
        std::cout << val << " occurs " << cnt <<" times." << std::endl;
    }
    return 0;
}

Like all input values

1 1 1 1 1 1
^D
1 occurs 6 times.

No repeat value

1 2 3 4 5
1 occurs 1 times.
2 occurs 1 times.
3 occurs 1 times.
4 occurs 1 times.
^D
5 occurs 1 times.

1.18 Ibid.

1.19 Procedures Section 1.4.1 modified to practice written so that it can handle the user input a first number is smaller than the second number of cases

1.11 Exercise program already has this feature

1.20 copy of the official website of the header file Sales_item.h, and use it to write a program that reads a book set sales records, each record will be printed to the standard output

# include <iostream>
# include "Sales_item.h"
using namespace std;
int main()
{
    Sales_item book;
    while(cin >> book){
        cout << book << endl;
    }
    return 0;
}

1.21 programming, reading the same two ISBN Sales_item objects, and their outputs

# include <iostream>
# include "Sales_item.h"
using namespace std;
int main()
{
    Sales_item book1, book2;
    cin >> book1 >> book2;
    if(book1.isbn() == book2.isbn()){
        cout << book1 + book2 << endl;
        return 0;
    }else{
        cout << "The books' ISBNs must be the same." << endl;
        return -1;
    }
}

1.22 programming, reading the same plurality ISBN Sales_item objects, and the output thereof

# include <iostream>
# include "Sales_item.h"
using namespace std;
int main()
{
    Sales_item book, total;
    if(cin >> total){
    	while(cin >> book){
    		if(book.isbn() == total.isbn()){
                total += book;
            }else{
                cout << "The books' ISBNs must be the same." << endl;
                return -1;
            }
    	}
        cout << total << endl;
        return 0;
    }else{
        cout << "No data." << endl;
       	return -1;
    }
}

1.23 Write a program that reads a number of sales records and statistics for each ISBN there are several sales records

# include <iostream>
# include "Sales_item.h"
using namespace std;
int main()
{
     Sales_item total;
     if(cin >> total){
         Sales_item currBook;
         while(cin >> currBook){
             if(currBook.isbn() == total.isbn())
                 total += currBook;
             else{
                 cout << total << endl;
                 total = currBook;
             }
         }
         cout << total << endl;
         return 0;
     }else{
        cerr << "No data" << endl;
        return -1;
     }
}
Published 27 original articles · won praise 0 · Views 389

Guess you like

Origin blog.csdn.net/young_cr7/article/details/105119824