c++ Primer 第一章:开始 练习答案记录

c++ Primer 第一章:开始 练习答案记录

下面的练习都别忘记加上这两语句,并且要将Sales_item.h放入到原文件处

#include<iostream>     //包含IO输入流和输出流,比如cin和cout
#include "Sales_item.h"

练习1.2返回错误值尝试

int main()
{
	return -1;         
}

一个使用IO库的程序演示

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;         
	return 0;
}

练习1.3 编写程序,打印Hello World

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

练习1.4 用*将两个数相乘,打印两个数的积

int main()
{
	std::cout << "请输入第一个乘数" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1;
	std::cout << "请输入第二个乘数" << std::endl;
	std::cin >> v2;
	std::cout << "相乘结果为: " << v1*v2<<std::endl;
	return 0;
}

练习1.6 解释下面程序片段是否合法

int main()
{
	int v1 = 0, v2 = 0;
	std::cin << v1 << v2;
	std::cout << "The sum of " << v1;           //没有用endl结束
	<< "and" << v2;
	<< "is" << v1 + v2 << std::endl;
}

练习1.7 编译一个包含不正确的嵌套注释的程序,观察编译器返回的错误信息

int main()
{
	
	int a = 0;      //warning C4138: 在注释外找到“/”
	
	return 0;
	
}

练习1.9 编写程序,使用while循环将50到100的整数相加

int main()
{
	int i = 50, sum = 0;
	while (i <= 100) {
		sum += i;
		++i;
	}
	std::cout << "使用while循环将50到100的整数相加:" << sum<<std::endl;
}

练习1.10 使用递减运算符在循环中按递减顺序打印出10到0之间的整数

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

练习1.11 编写程序,提升用户输入两个整数,打印出这两个整数所指定的范围内的所有整数

int main()
{
	int i, j;
	std::cout << "输入两个整数,打印出这两个整数所指定的范围内的所有整数" << std::endl;
	std::cout << "请输入第一个整数:" << std::endl;
	std::cin >> i;
	std::cout << "请输入第二个整数:" << std::endl;
	std::cin >> j;
	while (i >=j) {
		std::cout << j;
		++j;
	}
	while (i <= j) {
		std::cout << i;
		++i;
	}
	std::cout << std::endl;
}

练习1.12 求sum终值

int main()
{
	int sum = 0;
	for (int m = -100; m <= 100; ++m) {
		sum += m;
	}
	std::cout << sum << std::endl;
	return 0;
}

练习1.16

int main()
{
	int sum = 0,value = 0;                  //使用ctrl+z可以终止while
	while (std::cin >> value) {
		if (value == 0)
			break;
		sum += value;
	}
	std::cout << "Sum is :" << sum << std::endl;
	return 0;
}

练习1.17 输入范例

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

读写Sales_item,练习1.20

int main()
{
	Sales_item book;
	std::cin >> book;
	std::cout << book << std::endl;
	return 0;
}

练习1.21 两个相同Sales_item对象输出他们的和

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

练习1.22 读取多个具有相同ISBN的销售记录,输出所有记录的和

int main()
{
	Sales_item item,sum;
	std::cin >> sum;
	while (std::cin >> item) {
		sum += item;
	}
	std::cout << sum << std::endl;
}

P20页 范例,检查两个对象是否有相同的isbn,有则相加

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.23与1.24 读取多条销售记录,并统计每个ISBN(每本书)有几条销售记录

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

P21页 书店程序

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;
		return -1;
	}
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Is_LiuYiZheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值