c++Primer学习笔记之四:第一章课后习题

// ex1.3
#include <iostream>
int main()
{
	std::cout<<"Hello,World"<<std::endl;
	return 0;
}
//ex1.4
#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;
}

贴个编译的图片,运行环境是在win7下,gcc version 4.8.1(gcc)

//ex1.5
#include <iostream>
int main()
{
	std::cout<<"Enter two numbers:"<<std::endl;
	int v1,v2;
	std::cin>>v1>>v2;
	std::cout<<"The sum of ";
	std::cout<<v1;
	std::cout<<" and ";
	std::cout<<v2;
	std::cout<<" is ";
	std::cout<<v1+v2;
	std::cout<<std::endl;
	return 0;
}
//ex1.6
/*
*不合法
*第一句:std::cout << "The sum of " << v1;合法
*第二句:输出操作符“<<”没有左操作数,输入的右操作数没有存放地址。
*后面也是一样的。
*/
ex1.7和ex1.8可以自己编译下试试,自己了解下自己机器的错误信息代码,有利于以后自己调试程序。

//ex1.9
#include <iostream>
// 输出-100到100的累加和
int main()
{
	int sum = 0;
	for (int i = -100; i <= 100; ++i)
		sum += i;
	std::cout<<"Sum of -100 to 100 inclusive is "
			<< sum <<std::endl;
	return 0;
}	
//ex1.10
#include <iostream>
int main()
{
	int sum = 0;
	for (int i = 50; i <= 100; ++i)
		sum += i;
	std::cout<<"Sum of 50 to 100 inclusive is "
			<< sum <<std::endl;
	return 0;
}
//ex1.11
#include <iostream>
int main()
{
	int sum = 0,i=10;
	while (i >= 0){
		std::cout<<i<<" ";
		--i;
	}
	std::cout<<std::endl;
	return 0;
}
//ex1.16
#include <iostream>
int main()
{
	std::cout<<"Enter two numbers:"<<std::endl;
	int v1,v2;
	std::cin>>v1>>v2;
	int lower,upper;
	if (v1 <= v2){
		upper = v2;
	}else {
		upper = v1;
	}
	std::cout<<"The upper is "<<upper<<std::endl;
	return 0;
}
//ex1.17
#include <iostream>
int main()
{
	std::cout<<"Enter 5 numbers:"<<std::endl;
	int v1,v2,v3,v4,v5;
	std::cin>>v1>>v2>>v3>>v4>>v5;
	int num = 0;
	if (v1 < 0){
		num += 1;
	}
	if (v2 < 0){
		num += 1;
	}
	if (v3 < 0){
		num += 1;
	}
	if (v4 < 0){
		num += 1;
	}
	if (v5 < 0){
		num += 1;
	}
	std::cout<<"The numbers of negative is "<<num<<std::endl;
	return 0;
}
//ex1.18
#include <iostream>
int main()
{
	std::cout<<"Enter two numbers. \n"
			<<"We will write out all the numbers between this two numbers"
			<<std::endl;
	int v1,v2,lower,upper;
	std::cin>>v1>>v2;
	if (v1 <= v2){
		lower = v1;
		upper = v2;
	}else {
		lower = v2;
		upper = v1;
	}
	for (int i = lower;i <= upper;++i){
		std::cout<<i<<" ";
	}
	std::cout<<std::endl;
	return 0;		
}
//ex1.19
#include <iostream>
int main()
{
	std::cout<<"Enter two numbers. \n"
			<<"We will write out all the numbers between this two numbers"
			<<std::endl;
	int v1,v2,lower,upper;
	std::cin>>v1>>v2;
	if (v1 <= v2){
		lower = v1;
		upper = v2;
	}else {
		lower = v2;
		upper = v1;
	}
	for (int i = lower;i <= upper;++i){
		std::cout<<i<<" ";
		if ((i-lower)%10 == 0)
			std::cout<<"\n";
	}
	std::cout<<std::endl;
	return 0;		
}


<pre name="code" class="cpp">//ex1.20
#include <iostream>
int main()
{
	int v1 = 3,v2 = 5,sum = 0;
	for (v1;v1<=v2;v1++)
		sum += v1;
	std::cout<<"Sum of 3 to 5 inclusive is "
			<< sum <<std::endl;
	return 0;
}
 

下面的程序中需要的头文件

<pre name="code" class="cpp">#include "Sales/Sales_item.h"
 
 

下载地址:http://www.ituring.com.cn/book/656

点击其中的随书下载。

//ex1.21
#include <iostream>
#include "Sales/Sales_item.h"
int main()
{
	Sales_item item1;
	std::cout<<"Please write a sales item"<<std::endl;
	std::cin>>item1;
	std::cout<<item1<<std::endl;
	return 0;
}

//ex1.22
#include <iostream>
#include "Sales/Sales_item.h"
int main()
{
	Sales_item item1,item2;
	std::cout<<"Please write two sales item"<<std::endl;
	std::cin>>item1>>item2;
	std::cout<<item1+item2<<std::endl;
	return 0;
}
//ex1.23
#include <iostream>
#include "Sales/Sales_item.h"
int main()
{
	Sales_item item,sum_item;
	std::cout<<"Please write a ISBN sales item"<<std::endl;
	std::cin>>sum_item;
	std::cout<<"Please write some sales items which has the same ISBN \n"
			<<"you can end of your items with ctrl+z(windows),ctrl+d(linux)"
			<<std::endl;
	while (std::cin>>item)
		sum_item += item;
	std::cout<<"The sum of all the sales items are \n"
			<<sum_item<<std::endl;
	return 0;
}
//ex1.24
#include <iostream>
#include "Sales/Sales_item.h"
int main()
{
	Sales_item item,total_item;
	std::cout<<"Please write a ISBN sales item"<<std::endl;
	std::cin>>total_item;
	// 这里简单的实现对相同的isbn的叠加,需要注意的是如果isbn相同,最好将他们放在一起输入。
	std::cout<<"Please write some sales. When the ISBNs that you write is same please write them together.\n"
			<<"you can end of your items with ctrl+z(windows),ctrl+d(linux)"
			<<std::endl;
	while (std::cin>>item)
		if (total_item.same_isbn(item))
			total_item += item;
		else {
			std::cout<<total_item<<std::endl;
			total_item = item;
		}
	std::cout<<total_item<<std::endl;
	return 0;
}
这边也给个贴图吧


例子中,所有输出的比较长的就是求和以后的输出,也就是输出流中的数据

比较短的一些数据是输入流中的数据

//ex1.25

/*
*this is a c++ program
*author: wgh
*time: 2014.08.24
*/

#include <iostream>
#include "Sales/Sales_item.h"
int main()
{
	// declare variables to hold running sum and data for the next record
	Sales_item total,trans;
	if (std::cin >> total){
		while (std::cin>>trans)
			if (total.same_isbn(trans))
				total = total + trans;
			else {
				std::cout<<total<<std::endl;
				total = trans;
			}
		//remember to print last record
		std::cout<<total<<std::endl;
	}else{
		std::cout<<"No data?!"<<std::endl;
		return -1;  //indicate failure
	}
	return 0;
}

//ex1.26

/*这个是真的不太了解
*不过我在ex1.24中自己写的时候就用了“+=”操作符,好像没什么错误
*我也不知道这样到底是不是可以,有知道的大神,欢迎在留言板留言
*/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值