Skip to content

Latest commit

 

History

History
399 lines (310 loc) · 8.99 KB

README.md

File metadata and controls

399 lines (310 loc) · 8.99 KB

Chapter 1. Getting started

Exercise 1.1

Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the main program from page 2.

  • Windows

windows

Exercise 1.2

Exercise 1.2: Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main.

-Windows

Windows

Exercise 1.3

Write a program to print Hello, World on the standard output.

#include <iostream>

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

Exercise 1.4

Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.

#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;
}

Exercise 1.5

We wrote the output in one large statement. Rewrite the program to use a separate statement to print each operand.

#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;
}

Exercise 1.6

Explain whether the following program fragment is legal.

不合法,后面的<<运算符没有运算对象。应该去掉换行处的分号。

Exercise 1.7

Compile a program that has incorrectly nested comments.

#include <iostream>

int main(int argc, char *argv[])
{
    /*注释
    /*注释
     */
    *注释已经结束
    */
    int a = 1;
    return 0;
}

错误信息: ex1_7

Exercise 1.8

Indicate which, if any, of the following output statements are legal:

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

After you’ve predicted what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.

编译结果: ex1_8 改正:

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

输出:

/**/ */ /*

Exercise 1.12

What does the following for loop do? What is the final value of sum?

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

从-100加到100,结果等于0

Exercise 1.13

Rewrite the exercises from 1.4.1 (p. 13) using for loops.

Ex1.9:

#include <iostream>

int main(int argc, char *argv[])
{
	int sum = 0;
	for (int i = 50; i <= 100; ++i)
		sum += i;
	std::cout << sum << std::endl;
	return 0;
}

Ex1.10

#include <iostream>

int main(int argc, char *argv[])
{
	int sum = 0;
	for (int i = 10; i >= 0; --i)
		std::cout << i << ' ';
	std::cout << std::endl;
	return 0;
}

Ex1.11

#include <iostream>
int main(int argc, char *argv[])
{
	int num1, num2, k;
	std::cin >> num1 >> num2;
	k = abs(num1 - num2);
	for (int i = 0; i <= k; ++i)
	{
		std::cout << num1 << ' ';
		num1 += (num1 < num2 ? 1 : -1);
	}
	return 0;
}

Exercise 1.14

Compare and contrast the loops that used a for with those using a while. Are there advantages or disadvantages to using either form?

  • while的特点
    • 判断的条件明显
    • 不需要计算多少步
  • for的特点
    • 判断更为简洁
    • 步数清晰

Exercise 1.15

Write programs that contain the common errors discussed in the box on page 16. Familiarize yourself with the messages the compiler generates.

Exercise 1.16

Write your own version of a program that prints the sum of a set of integers read from cin.

#include <iostream>

int main(int argc, char *argv[])
{
	int sum = 0, value = 0;
	while (std::cin >> value)
		sum += value;
	std::cout << "Sum is " << sum << std::endl;
	return 0;
}

Exercise 1.17

What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values?

如果全部相等那么程序一直处于输入状态,如果不相等输入一个显示一个。

Exercise 1.18

Compile and run the program from this section giving it only equal values as input. Run it again giving it values in which no number is repeated.

ex1_18

Exercise 1.19

Revise the program you wrote for the exercises in § 1.4.1 (p. 13) that printed a range of numbers so that it handles input in which the first number is smaller than the second.

ex1_11.cpp

Exercise 1.20

http://www.informit.com/title/032174113 contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.

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

int main(int argc, char *argv[])
{
	Sales_item book;
	while (std::cin >> book)
		std::cout << book << std::endl;
	return 0;
}

结果

ex1_20

Exercise 1.21

Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.

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

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

Exercise 1.22

Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read.

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

int main(int argc, char *argv[])
{
	Sales_item curritem, item;
	if (std::cin >> curritem)
	{
		while (std::cin >> item)
		{
			if (curritem.isbn() == item.isbn())
				curritem += item;
			else
			{
				std::cout << curritem << std::endl;
				curritem = item;
			}
		}
		std::cout << curritem << std::endl;
	}
	else
		std::cerr << "No data?" << std::endl;
	return 0;
}

Exercise 1.23

Write a program that reads several transactions and counts how many transactions occur for each ISBN.

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

int main(int argc, char *argv[])
{
	Sales_item curritem, item;
	int cnt = 1;
	if (std::cin >> curritem)
	{
		while (std::cin >> item)
		{
			if (item.isbn() == curritem.isbn())
				++cnt;
			else
			{
				std::cout << curritem.isbn() << " occurs " << cnt << " times" << std::endl;
				curritem = item;
			}
		}
		std::cout << curritem.isbn() << " occurs " << cnt << " times" << std::endl;
	}
	else
	{
		std::cerr << "Nodata?" << std::endl;
		return -1;
	}
	return 0;
}

Exercise 1.24

Test the previous program by giving multiple transactions representing multiple ISBNs. The records for each ISBN should be grouped together.

重定向输入infile文件

ex1_24

Exercise 1.25

Using the Sales_item.h header from the Web site, compile and execute the bookstore program presented in this section.

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

int main(int argc, char *argv[])
{
	Sales_item total;
	if (std::cin >> total)
	{
		Sales_item trans;
		while (std::cin >> trans)
		{
			if (total.isbn() == trans.isbn())
				total += trans;
			else
			{
				std::cout << total << std::endl;
				total = trans;
			}
		}
		std::cout << total << std::endl;
	}
	else
	{
		std::cerr << "Nodata?!" << std::endl;
		return -1;
	}
	return 0;
}

ex1_25