C++Primer Fifth Edition Exercise Answers [Chapter One]

C++Primer Fifth Edition Exercise Answers [General List]: https://blog.csdn.net/Dust_Evc/article/details/114334124

1.1

 File types used in Visual C++ projects: https://docs.microsoft.com/zh-cn/cpp/build/reference/file-types-created-for-visual-cpp-projects?view=msvc-150

1.2

int main()
{
return -1;
}

1.3

#include <iostream>

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

1.4

#include <iostream>

int main()
{
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "The product of " << v1 << " and " << v2
				<< " is " << v1 * v2 << std::endl;
	return 0;
}

1.5

#include <iostream>

int main()
{
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "The product of ";
	std::cout << v1;
	std::cout << " and ";
	std::cout << v2;
	std::cout << " is ";
	std::cout << v1 * v2;
	std::cout << std::endl;
	return 0;
}

1.6

illegal.
**Error error C2143: syntax error: missing ";" (before "<<") **
fix: remove extra semicolon.

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

1.7

/*
*   注释在这里嵌套
*	/* 这里的嵌套是错的 */
*
*/

int main()
{
	return 0;
}

Error   E0169:    expected a declaration.

1.8

The third line compiles an error. The correction method is to add a quotation mark. std::cout << /* "*/" */";
output: /**/ */ /*

1.9

#include <iostream>

int main()
{
	int sum = 0, i = 50;

	while (i <= 100)
	{
		sum += i;
		++i;
	}

	std::cout << sum << std::endl;
	
	return 0;
}

1.10

#include <iostream>

int main()
{
	int i = 10;

	while (i >= 0)
	{
		std::cout << i << std::endl;
		--i;
	}

	return 0;
}

1.11

#include <iostream>

void  print_range(int lo, int hi)
{
	if (lo > hi)
	{
		print_range(hi, lo);
		return;
	}
	while (lo <= hi)
	{
		std::cout << lo << std::endl;
		++lo;
	}
}

int main()
{
	int low, high;
	std::cout << "please input two numbers : " << std::endl;
	std::cin >> low >> high;

	print_range(low, high);
	return 0;
}

1.12

From -100 to 100, the final value of sum is 0.

1.13

Exercise 1.9:

#include <iostream>

int main()
{
    int sum = 0;
    for (int i = 50; i <= 100; ++i) sum += i;
    std::cout << "the sum is: " << sum << std::endl;

    return 0;
}

Exercise 1.10:

#include <iostream>

int main()
{
    for (int i = 10; i >= 0; --i)
        std::cout << i << std::endl;
    return 0;
}

Exercise 1.11:

#include <iostream>

void  print_range(int lo, int hi)
{
	if (lo > hi)
	{
		print_range(hi, lo);
		return;
	}
	for (int i = lo; i <= hi; ++i)
	{
		std::cout << i << std::endl;
	}
}

int main()
{
	int low, high;
	std::cout << "please input two numbers : " << std::endl;
	std::cin >> low >> high;

	print_range(low, high);
	return 0;
}

1.14

1. In the for loop, the initialization bai and modification of the loop control variable are placed in the zhi part of the statement header. The form is more concise, and it is especially suitable for the situation where the number of loops is known.

2. In the while loop, the initialization of the loop control variable is generally placed before the while statement, and the modification of the loop control variable is generally placed in the loop body. The form is not as concise as the for statement, but it is more suitable for situations where the number of loops is not easy to predict ( Use a certain condition to control the loop).

3. The two forms have their own advantages, but they are equivalent in function and can be converted to each other.

1.15

Common compiler errors include syntax errors, type errors, and declaration errors.

1.16

#include <iostream>

int main()
{
	int sum = 0, value = 0;

	while (std::cin >> value)
	{
		sum += value;
	}

	std::cout << sum << std::endl;

	return 0;
}

1.18

#include <iostream>

int main()
{
	int currVal = 0, val = 0;

	if (std::cin >> currVal)
	{
		int cnt = 1;
		while (std::cin >> val)
		{
			if (val == currVal)
			{
				++cnt;
			}
			else
			{
				std::cout << currVal << " occurs " << cnt << " times" << std::endl;
				currVal = val;
				cnt = 1;
			}
		}
		std::cout << currVal << " occurs " << cnt << " times" << std::endl;
	}
	return 0;
}

1.19

#include <iostream>

void  print_range(int lo, int hi)
{
	if (lo > hi)
	{
		print_range(hi, lo);
		return;
	}
	while (lo <= hi)
	{
		std::cout << lo << std::endl;
		++lo;
	}
}

int main()
{
	int low, high;
	std::cout << "please input two numbers : " << std::endl;
	std::cin >> low >> high;

	print_range(low, high);
	return 0;
}

1.20

#include <iostream>
#include "Sales_item.h"

using std::cin;
using std::cout;
using std::endl;

int main()
{
	for (Sales_item item; cin >> item; cout << item << endl);
	return 0;
}

1.21


#include <iostream>
#include "Sales_item.h"
#include <stdexcept>
#include <string>

int main()
{
	Sales_item item1, item2;
	std::cin >> item1 >> item2;
	 
	if (item1.isbn() == item2.isbn()) 
	{
		std::cout << item1 + item2 << std::endl;
		return 0;  
	}
	else 
	{
		std::cerr << "Data must refer to same ISBN"
			<< std::endl;
		return -1;  
	}
}

1.22

#include <iostream>
#include "Sales_item.h"

using namespace std;
#define OK 0;

int main()
{
	Sales_item total;
	if (cin >> total)
	{
		Sales_item trans;
		while (cin >> trans)
		{
			if (total.isbn() == trans.isbn())
				total += trans;
			else
			{
				cout << "ISBN号为【" << total.isbn() << "】的书籍,总记录为:";
				cout << total << endl;
				total = trans;
			}
		}
		cout << "ISBN号为【" << total.isbn() << "】的书籍,总记录为:";
		cout << total << endl;
	}
	else
	{
		cerr << "No data?!" << endl;
		return -1;
	}

	return OK;
}

1.23

#include <iostream>
#include "Sales_item.h"

int main()
{
	Sales_item currItem, valItem;
	if (std::cin >> currItem)
	{
		int cnt = 1;
		while (std::cin >> valItem)
		{
			if (valItem.isbn() == currItem.isbn())
			{
				++cnt;
			}
			else
			{
				std::cout << currItem << " occurs " << cnt << " times " << std::endl;
				currItem = valItem;
				cnt = 1;
			}
		}
		std::cout << currItem << " occurs " << cnt << " times " << std::endl;
	}
	return 0;
}

1.24

The code is the same as above.

Guess you like

Origin blog.csdn.net/Dust_Evc/article/details/114280415