C++primer 7.1部分习题(一)

习题7.1

头文件 Sd.h

#pragma once
#ifndef SD_H_INCLUDED
#define SD_H_INCLUDED
#include<iostream>
#include<string>
class Sd {
	friend std::istream& operator>>(std::istream&, Sd&);
	friend std::ostream& operator<<(std::ostream&, const Sd&);
	//friend Sd operator +(const Sd&, const Sd&);
	friend bool operator ==(const Sd&,const Sd&);
public:
	Sd() = default;
	Sd(const std::string& book) :bookid(book) { }
	Sd(std::istream &is){is>>*this ;}

public:
	std::string isbn() const { return bookid; };
	Sd&  operator+=(const Sd&);

private:
	std::string bookid;  //书籍编号
	unsigned sold = 0;     //销售量
	double price = 0.;  //原始价格
	double realprice = 0.0;//实际价格
	double discount = 0.0;//折扣

};
inline bool operator ==(const Sd &lhs, const Sd &rhs) {   //哦,前面少了个const ,真是 yaleiyalei daze
	return lhs.sold == rhs.sold && lhs.realprice == rhs.realprice && lhs.price == rhs.price && lhs.isbn() == rhs.isbn();
}
Sd& Sd::operator+=(const Sd& rhs) {
	sold += rhs.sold;
	realprice = (rhs.price * rhs.sold + price * sold) / (rhs.sold + sold);
	if (price !=0 )
		discount = realprice / price;
	return *this;
}
std::istream& operator>>(std::istream& in, Sd& s)
{
	in >> s.bookid >> s.sold >> s.price >>s.realprice;
	if (in && s.price != 0)
		s.discount = s.realprice / s.price;
	else
		s = Sd();
	return in;
}
std::ostream& operator<<(std::ostream& out, const Sd& s) {
	out << s.isbn() <<" "<<s.sold<<" "<<s.realprice<<" "<<s.price<<std::endl;
	return out;
}

#endif // !SD_H_INCLUDED

源文件

#include <iostream>
#include"Sd.h"
using namespace std;

int main() {
	cout << "请输入交易记录(ISBN、销售量、原价、实际售价):" << endl;  //需要定义四个友元函数,输入,输出,加法,等号
	Sd total;
	if (cin >> total) {
		Sd trans;
		while (cin >> trans) {
			if (total.isbn() == trans.isbn())   //成员函数isbn,返回bookid,str
				total += trans;
			else
			{
				cout << total << endl;
				total == trans;
			}
		}
		cout << total << endl;
	}
	else {
		cerr << "No data?!" << endl;
		return -1;
	}
	return 0;
}

习题7.6

挺有意思的,学到了很多,c++确实很难学,但坚持学然后一定要注意一些细节,其实也没有那么难,至少先把一些简单的操作学会还是可以的。只要不放弃,学习的过程中也很容易发现C++的魅力。实在觉得没意思就放弃,然后尝试着去学Java。。。

#include<iostream>
#include<string>
using namespace std;
class person {
public:
	std::string name = " ";
	std::string address = " ";
	std::string  getname() const { return name; };   //const 的作用是修改隐式this 指针的类型 常量成员函数
	std::string getaddress() const { return address; };

	//建立两类read print 输入输出
	
	int printout() const {
		std::cout  <<name << address; return 0; }
	int readit() {
		std::cin >> name >> address;
		return 0;
	}
	person & add(const person& rhs) {
		name += ", " + rhs.name;
		address += ", " + rhs.address;
		return *this;
	}

};
//注意read,print 为非成员的接口函数,必放在外面才行
//注意看,这不就是普通的函数吗,这就是普通的函数啊,。。。。普通的函数但是他只能作用在我们定义的类,也是不普通的函数,所以叫做接口函数,专门为我们定义的类而定制的函数
//也可以算是类的一部分,这一点是比较特殊的,他在类的定义的外面,但是如果建立头文件的话可以作为头文件中的末尾,应用头文件就会得到这些接口函数,所以不要迷茫了!!!
std::istream& read(std::istream& is, person& item) {
		is >> item.name >> item.address;              
		return is;
	}
	std::ostream& print(std::ostream& out, const person& i) {
		out << i.name << i.address << endl;
		return out;
	}
	int addit(person& lhs, person& rhs) {
		lhs.name += " " + lhs.name;
		lhs.address += " " + lhs.address;
		return 0;
	}
	int reads(person& is) {
		cout << "enter name and address:  " << endl;
		cin >> is.name >> is.address;
		return 0;
	}
	int prints(person& is) {
		cout << is.name << is.address << endl;
		return 0;
	}

int main() {
	person test,test1;
	cin >> test.name >> test.address;
	cout << test.getname() << test.getaddress() << endl;
	test.printout();
	test.readit();
	cout << "  _______" << endl;
	read(cin, test1);
	cout << "  _______" << endl;
	print(cout, test1);
	test1.add(test);
	test1.printout();
	cout << "_______________" << endl;
	reads(test);
	prints(test);
	addit(test, test1);

	return 0;

}

习题7.8
read函数将类参数定义为普通的引用是因为read函数要修改参数的值,如果定义为常量肯定会出问题的,很简单的问题,而print将参数定义为常量,也可以不定义为常量的,但print是输出函数,不会改变参数的数值的,所以定义为常量也很合理。
习题7.10
条件部分的作用是先输入data1,后输入data2


(c++primer从第七章开始难度一下增加了很多,当初第七章前的内容是可以一天学一章的,现在感觉学习效率好低,还是我没认真学,还是之前学的时候难的地方直接跳过了才学得比较快,不管怎么样要认真学才能学好。)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值