C++ primer 第一章代码记录

//	page : 5

#include<iostream>

int main()
{
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "The sum of " << v1 << " and " 
		<< v2 << " is " << v1 + v2 << std::endl;
	system("pause");
	return 0;
}
//	page : 10

#include<iostream>

int main()
{
	int sum = 0, val = 1;
	//只要Val的值小于等于10,while循环就会持续进行
	while (val <= 10)
	{
		sum += val;
		++val;
	}
	std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
	system("pause");
	return 0;
}
//	page : 11

#include<iostream>

int main()
{
	int sum = 0;
	//从1加到10
	for (int val = 1; val <= 10; ++val)
		sum += val;
	std::cout << "Sum of 1 to 10 is " << sum << std::endl;
	system("pause");
	return 0;
}
//	page : 13

#include<iostream>

int main()
{
	int sum = 0, value = 0;
	//读取数据直到遇到文件尾,计算所有读入的值的和
	while (std::cin >> value)//win系统的文件结束符尾ctrl+d
		sum += value;
	std::cout << "Sum is: " << sum << std::endl;
	system("pause");
	return 0;
}
//	page : 18

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

int main()
{
	Sales_item book;
	
	std::cin >> book;
	std::cout << book << std::endl;
	system("pause");
	return 0;
}
//	page : 19

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

int main()
{
	Sales_item item1,item2;
	
	std::cin >> item1 >> item2;
	std::cout << item1 + item2 << std::endl;

	system("pause");
	return 0;
}
//	page : 20

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

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

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

int main()
{
	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 << "No data?!" << std::endl;
		system("pause");
		return -1;
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值