How to run the bookstore program on C++ primer with VSCode

dev can't be used, vs also has some problems or vscode is easy to use, concise and convenient

创建一个文件夹,添加头文件和cpp (头文件名字一定要对)insert image description here
examples from the book

Exercise 1.20

On the website http://www.informit.com/title/032174113, the code directory for Chapter 1 includes the header files Sales_item.h. Copy it into your own working directory and write a program that reads a set of book sales records and prints each to standard output.
insert image description here
下载这个 然后解压 找到 这个头文件
insert image description here
或者直接 复制我的内容加到那个头文件里面
insert image description here

#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>
 
class Sales_item {
    
    
	friend std::istream& operator>>(std::istream&, Sales_item&);
	friend std::ostream& operator<<(std::ostream&, const Sales_item&);
	friend bool operator<(const Sales_item&, const Sales_item&);
	friend bool
		operator==(const Sales_item&, const Sales_item&);
public:

#if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
	Sales_item() = default;
#else
	Sales_item() : units_sold(0), revenue(0.0) {
    
     }
#endif
	Sales_item(const std::string &book) :
		bookNo(book), units_sold(0), revenue(0.0) {
    
     }
	Sales_item(std::istream &is) {
    
     is >> *this; }
public:

	Sales_item& operator+=(const Sales_item&);
 
	std::string isbn() const {
    
     return bookNo; }
	double avg_price() const;

private:
	std::string bookNo; 
#ifdef IN_CLASS_INITS
	unsigned units_sold = 0; 
	double revenue = 0.0;
#else
	unsigned units_sold;
	double revenue;
#endif
};

inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{
    
    
	return lhs.isbn() == rhs.isbn();
}

Sales_item operator+(const Sales_item&, const Sales_item&);
 
inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    
    

	return lhs.units_sold == rhs.units_sold &&
		lhs.revenue == rhs.revenue &&
		lhs.isbn() == rhs.isbn();
}
 
inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    
    
	return !(lhs == rhs);
}

Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
    
    
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}
 

Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{
    
    
	Sales_item ret(lhs);
	ret += rhs; 
	return ret; 
}
 
std::istream&
operator>>(std::istream& in, Sales_item& s)
{
    
    
	double price;
	in >> s.bookNo >> s.units_sold >> price;

	if (in)
		s.revenue = s.units_sold * price;
	else
		s = Sales_item();
	return in;
}
 
std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
    
    
	out << s.isbn() << " " << s.units_sold << " "
		<< s.revenue << " " << s.avg_price();
	return out;
}
 
double Sales_item::avg_price() const
{
    
    
	if (units_sold)
		return revenue / units_sold;
	else
		return 0;
}
#endif

Don't worry, just compile and save
insert image description here

main.cpp

#include <iostream>
#include "Sales_item.h"
int main()
{
    
    
	Sales_item book;
	std::cout << "请输入销售记录:" 
		<< std::endl;
	while (std::cin >> book) {
    
    
		std::cout << "ISBN、售出本数和平均售价为 " 
			<< book << std::endl;
	}
	return 0;
}

If the input: 0-201-70353-X 4 24.99
the output is: 0-201-70353-X 4 99.96 24.99

insert image description here
完成!!

Guess you like

Origin blog.csdn.net/weixin_49486457/article/details/124190165