visual2015
2015-07-01 04:58
采纳率: 80%
浏览 1.9k

关于书店的一个小程序,c++写的。.大家帮忙看看怎么运行不了???

代码可以在这儿粘贴
#include
#include
using namespace std;
class Sales_data
{
friend std::istream& operator>>(std::istream&, Sales_data&);
friend std::ostream& operator<<(std::ostream&, const Sales_data&);
friend bool operator < (const Sales_data&,const Sales_data&);
friend bool operator ==(const Sales_data&, const Sales_data&);
public:
Sales_data(){ default; }
Sales_data(const std::string &book) :bookNo(book){ }
Sales_data(std::istream &is) { is >> *this; }
public:
Sales_data& operator +=(const Sales_data&);
std::string isbn() const{ return bookNo; }
private:
std::string bookNo;
unsigned units_sold = 0;
double sellingprice = 0.0;
double saleprice = 0.0;
double discount = 0.0;
};
inline bool comparaIsbn(const Sales_data &lhs, const Sales_data &rhs)
{return lhs.isbn() == rhs.isbn();}

Sales_data operator + (const Salea_data&, const Salea_data&);

inline bool operator==(const Sales_data&lhs, const Sales_data&rhs)
{
return lhs.units_sold == rhs.units_sold&&
lhs.sellingprice == rhs.sellingprice&&
lhs.saleprice == rhs.saleprice&&
lhs.isbn() == rhs.isbn();
}
inline bool operator!=(const Sales_data&lhs, const Sales_data&rhs)
{
return!(lhs == rhs);
}
Sales_data& Sales_data::operator+=(const Sales_data&rhs)
{
units_sold += rhs.units_sold;
saleprice = (rhs.saleprice*rhs.units_sold + saleprice*units_sold)
/ (rhs.units_sold + units_sold);
if (sellingprice != 0)
discount = saleprice / sellingprice;
return *this;
}
Sales_data operator+(const Sales_data&lhs, const Sales_data&rhs)
{
Sales_data ret(lhs);
ret += rhs;
return ret;
}
std::istream&operator>>(std::istream& in, sales_data& s)
{
in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;
if (in && s.sellingprice != 0)
s.discount = s.saleprice / s.sellingprice;
else
s = Sales_data();
return in;
}
std::ostream& operator<<(std::ostream&out, const Sales_data&s)
{
out << s.isbn() << "" << s.units_sold << ""
<< s.sellingprice << "" << s.saleprice << "" << s.discount;
return out;
}
int main()
{
Sales_data book;
std::cout << "请输入销售记录:" << std::endl;
while (std::cin >> book){
std::cout << "ISBN、售出本数、原始价格、实售价格、折扣为" << book <<
std::endl;
}
Sales_data trans1, trans2;
std::cout << "请输入两条ISBN相同的销售记录:" << std::endl;
std::cin >> trans1 >> trans2;
if (comparaIsbn(trans1, trans2))
std::cout << "汇总信息:ISBN、售出本数、原始价格、实售价格、折扣为"
<< trans1 + trans2 << std::endl;
else
std::cout << "两条销售记录的ISBN不同" << std::endl;
Sales_data total, trans;
std::cout << "请输入几条ISBN相同的销售记录:" << std::endl;
if (std::cin >> total){
while (std::cin>>trans)
if (compareIsbn(total, trans))
total += trans;
else{
std::cout << "当前书籍ISBN不同" << std::endl;
break;
}
std::cout << "有效汇总信息:ISBN、售出本数、原始价格、实售价格、折扣为"
<< total << std::endl;
}
else{
std::cout << "没有数据" << std::endl;
return -1;
}
int num = 1;
std::cout << "请输入若干销售记录:" << std::endl;
if (std:; cin >> trans1){
while (std::cin>>trans2)
if (comparaIsbn(trans1, trans2))
num++;
else{
std::cout << trans1.isbn() << "共有"
<< num << "条销售记录" << std::endl;
trans1 == trans2;
num = 1;
}
std::cout << trans1.isbn() << "共有"
<< num << "条销售记录" << std::endl;
}
else{
std::cout << "没有数据" << std::endl;
return -1;
}
return 0;
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • lniwn 2015-07-01 07:38
    已采纳
     #include <iostream>
    #include <sstream>
    
    using namespace std;
    class Sales_data
    {
        friend std::istream& operator>>(std::istream&, Sales_data&);
        friend std::ostream& operator<<(std::ostream&, const Sales_data&);
        friend bool operator < (const Sales_data&, const Sales_data&);
        friend bool operator ==(const Sales_data&, const Sales_data&);
    public:
        Sales_data() = default;
        Sales_data(const std::string &book) : bookNo(book) { }
        Sales_data(std::istream &is) { is >> *this; }
    public:
        Sales_data& operator +=(const Sales_data&);
        std::string isbn() const { return bookNo; }
    private:
        std::string bookNo;
        unsigned units_sold = 0;
        double sellingprice = 0.0;
        double saleprice = 0.0;
        double discount = 0.0;
    };
        inline bool comparaIsbn(const Sales_data &lhs, const Sales_data &rhs)
        {return lhs.isbn() == rhs.isbn();}
        Sales_data operator+ (const Sales_data& lhs, const Sales_data& rhs);
        inline bool operator==(const Sales_data& lhs, const Sales_data& rhs)
        {
            return lhs.units_sold == rhs.units_sold &&
                   lhs.sellingprice == rhs.sellingprice &&
                   lhs.saleprice == rhs.saleprice &&
                   lhs.isbn() == rhs.isbn();
        }
        inline bool operator!=(const Sales_data& lhs, const Sales_data& rhs)
        {
            return!(lhs == rhs);
        }
    
    Sales_data& Sales_data::operator+=(const Sales_data& rhs)
    {
        units_sold += rhs.units_sold;
        saleprice = (rhs.saleprice * rhs.units_sold + saleprice * units_sold)
                    / (rhs.units_sold + units_sold);
        if (sellingprice != 0)
            discount = saleprice / sellingprice;
        return *this;
    }
    Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs)
    {
        Sales_data ret(lhs);
        ret += rhs;
        return ret;
    }
    std::istream& operator>>(std::istream& in, Sales_data& s)
    {
        in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;
        if (in && s.sellingprice != 0)
            s.discount = s.saleprice / s.sellingprice;
        else
            s = Sales_data();
        return in;
    }
    std::ostream& operator<<(std::ostream&out, const Sales_data&s)
    {
        out << s.isbn() << "" << s.units_sold << ""
            << s.sellingprice << "" << s.saleprice << "" << s.discount;
        return out;
    }
    int main()
    {
        Sales_data book;
        std::cout << "请输入销售记录:" << std::endl;
        while (std::cin >> book) {
            std::cout << "ISBN、售出本数、原始价格、实售价格、折扣为" << book <<
                      std::endl;
        }
        Sales_data trans1, trans2;
        std::cout << "请输入两条ISBN相同的销售记录:" << std::endl;
        std::cin >> trans1 >> trans2;
        if (comparaIsbn(trans1, trans2))
            std::cout << "汇总信息:ISBN、售出本数、原始价格、实售价格、折扣为"
                      << trans1 + trans2 << std::endl;
        else
            std::cout << "两条销售记录的ISBN不同" << std::endl;
        Sales_data total, trans;
        std::cout << "请输入几条ISBN相同的销售记录:" << std::endl;
        if (std::cin >> total) {
            while (std::cin >> trans)
                if (comparaIsbn(total, trans))
                    total += trans;
                else {
                    std::cout << "当前书籍ISBN不同" << std::endl;
                    break;
                }
            std::cout << "有效汇总信息:ISBN、售出本数、原始价格、实售价格、折扣为"
                      << total << std::endl;
        }
        else {
            std::cout << "没有数据" << std::endl;
            return -1;
        }
        int num = 1;
        std::cout << "请输入若干销售记录:" << std::endl;
        if (std:: cin >> trans1) {
            while (std::cin >> trans2)
                if (comparaIsbn(trans1, trans2))
                    num++;
                else {
                    std::cout << trans1.isbn() << "共有"
                              << num << "条销售记录" << std::endl;
                    trans1 == trans2;
                    num = 1;
                }
            std::cout << trans1.isbn() << "共有"
                      << num << "条销售记录" << std::endl;
        }
        else {
            std::cout << "没有数据" << std::endl;
            return -1;
        }
        return 0;
    }
    
    

    把格式简单整理了下,都是些很低级的错误,单词拼错什么的,楼主连这点耐心都没有,还是不要写代码了。

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题