C++Primer学习笔记+练习答案-第一章

有错欢迎指出

笔记部分

1.1 编写⼀个简单的 C++程序
每个 C++程序都包含⼀个或多个函数 (functi0n), 其中⼀个必须命名为 main。 操作系统通过调⽤ main 来运⾏ C++程序。

int main()
{
    return 0;
}

main 函数的返回类型必须为 int,一般返回0代表成功,非0返回值的含义由系统定义, 通常⽤来指出错误类型。
1.2 初识输入输出
cin:标准输入(standard input)
cout:标准输出(standard output)
cerr通常用于输出警告和错误信息,标准错误(standard error)
clog一般用于输入一般性消息
输出运算符:<<
输入运算符:>>
1.3 注释
单行注释://

界定符对注释:
1.4 控制流
形式:
while(condition)
statement

练习答案

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.
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.

#include<iostream>
int main(){
	return -1;
} 	

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,v2;
	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,v2;
	std::cin>>v1>>v2;
	std::cout<<"The product of "<<v1<<" and "
	        <<v2<<" is "<<(v1*v2)<<std::endl;
	return 0;
	
}


Exercise 1.6: Explain whether the following program fragment is legal.

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

If the program is legal, what does it do? If the program is not legal, why not? How would you fix it?

不合法,可删除第一第二行的分号或者在这两行前面加上“std::cout”。
一种修改方式

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

Exercise 1.7: Compile a program that has incorrectly nested comments

#include<iostream>
int main(){
  /* /**/ */
  return 0;  
}

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.
第一行和第二行不会报错,分别输出“/*”和“**/”
第三行报错,等价于

std::cout << " */;

可改为

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

第四个不会报错,输出“/*”。
Exercise 1.9: Write a program that uses a while to sum the numbers from 50 to 100.

#include<iostream>
int main(){
	int va1=50,sum=0;
	while(va1<=100){
		sum+=va1;
		va1++;
	}
	std::cout<<"The sum of numbers from 50 to 100 is "<<sum<<std::endl;
	return 0;
}

输出结果: The sum of numbers from 50 to 100 is 3825

Exercise 1.10: In addition to the ++ operator that adds 1 to its operand, there is a decrement operator (–) that subtracts 1. Use the decrement operator to write a while that prints the numbers from ten down to zero.

#include<iostream>
int main(){
	int va1=10,sum=0;
	while(va1>=1){
		sum+=va1;
		va1--;
	}
	std::cout<<"the result is "<<sum<<std::endl;
}

Exercise 1.11: Write a program that prompts the user for two integers.Print each number in the range specified by those two integers.

#include<iostream>
int main(){
	std::cout<<"Enter two numbers"<<std::endl;
	int v1,v2;
	std::cin>>v1>>v2;
	while(v1<=v2){//默认v1<v2 
		std::cout<<v1<<" ";
		v1++;
	}
	return 0;
}

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+(-99)+(-98)+…98+99+100,
结果是0。

Exercise 1.13: Rewrite the exercises from § 1.4.1 (p. 13) using for loops.

#include<iostream>
int main(){
	int sum=0;
	for(int i=1;i<=10;++i)
	    sum+=i;
	std::cout<<sum<<std::endl;
	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?
1、在for循环中,循环控制变量的初始化和修改都放在语句头部分,形式较简洁,且特别适用于循环次数已知的情况。
2、在while循环中,循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)。
3、两种形式各有优点,但它们在功能上是等价的,可以相互转换。

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 v1,sum=0;
	std::cout<<"Enter numbers"<<std::endl;
	while(std::cin>>v1){
		sum+=v1;
	}
	std::cout<<"the sum of them 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.

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.

#include<iostream>
int main(){
	int v1,v2;
	std::cout<<"Input two numbers"<<std::endl;
	std::cin>>v1>>v2;
	if(v1>v2){
		int t;
		t=v1;
		v1=v2;
		v2=t;
	}
	while(v1<=v2){
		std::cout<<v1<<" ";
		v1++;
	}
	return 0;
}

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()
{
    Sales_item item;
    while (std::cin>>item)
        std::cout << item<<std::endl;
    return 0;
}

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()
{
    Sales_item item1,item2;
    std::cout<<"input two items that have the same isbn"<<std::endl;
    std::cin>>item1>>item2;
    std::cout<<"the sum is "<<item1+item2<<std::endl;
    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()
{
    Sales_item item,sitem;
    std::cin>>sitem;
    while(std::cin>>item){
    	sitem+=item;
	}
	std::cout<<sitem<<std::endl;
}

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()
{
    Sales_item curItem,item;
    if (std::cin >> curItem) {
        int cnt = 1; 
        while (std::cin >> item) { 
        if (item == curItem) 
             ++cnt; 
        else {
              std::cout << curItem << " occurs " << cnt << " times" << std::endl;
              curItem = item; 
              cnt = 1; 
	        }
	   } 
        std::cout << curItem << " occurs " << cnt << " times" << std::endl;
    }
    else{
    	std::cout<<"these's no input of sales item"<<std::endl;
	}
         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.

Exercise 1.25: Using the Sales_item.h header from the Web site, compile and execute the bookstore program presented in this section.
Sales_item 下载地址:http://www.informit.com/title/032174113
(第一章的代码里)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值