| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4018 人关注过本帖
标题:C++primer第五版第一章的书店程序中的销售记录怎么输入
只看楼主 加入收藏
柠檬师弟
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-9-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
C++primer第五版第一章的书店程序中的销售记录怎么输入
代码如下:
#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;
    return -1;
    }
    return 0;
    }
程序运行没问题,但是输入销售记录的时候就有问题了,我输入的是
0-201-78345-X 3 20.00
0-201-78345-X 4 25.00
0-201-78345-X 5 22.50
0-201-70353-X 4 25.00
然后再按回车键想输入就直接输出结果了,而且最后一种ISBN没有显示出来,我想知道当ISBN变化了之后要怎么才能继续输入。难道只能输入一个ISBN么
搜索更多相关主题的帖子: include return 记录 
2016-09-20 17:06
qq826647235
Rank: 2
等 级:论坛游民
帖 子:37
专家分:10
注 册:2016-5-4
收藏
得分:10 
首先。你的程序编译就不过不去,括号都不匹配。
另外程序是能做到ISBN变化后继续输入的。
第15行 total=trans; 就是换到了下一本书的销售额。
不过这个程序只能在相同ISBN的书是连续输入的时候才是正确的。不然会出现相同ISBN的书分几次计算的情况
2016-09-21 23:03
柠檬师弟
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-9-20
收藏
得分:0 
回复 2楼 qq826647235
理论上确实能做到isbn变化还能输入,然而在调试窗口中输入的时候就是不行,我也不知道为什么
2016-09-26 18:56
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:10 
第一,你给的代码不完整,别人想要运行测试你的代码还需要一个Sles_item.h。   这个头文件以后一定要记得提供
正好我也在学这本书,所以有这个头文件.
程序代码:
/*

 * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.

 * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the

 * copyright and warranty notices given in that book:

 *

 * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."

 *

 *

 * "The authors and publisher have taken care in the preparation of this book,

 * but make no expressed or implied warranty of any kind and assume no

 * responsibility for errors or omissions. No liability is assumed for

 * incidental or consequential damages in connection with or arising out of the

 * use of the information or programs contained herein."

 *

 * Permission is granted for this code to be used for educational purposes in

 * association with the book, given proper citation if and when posted or

 * reproduced.Any commercial use of this code requires the explicit written

 * permission of the publisher, Addison-Wesley Professional, a division of

 * Pearson Education, Inc. Send your request for permission, stating clearly

 * what code you would like to use, and in what specific way, to the following

 * address:

 *

 *     Pearson Education, Inc.

 *     Rights and Permissions Department

 *     One Lake Street

 *     Upper Saddle River, NJ  07458

 *     Fax: (201) 236-3290
*/

/* This file defines the Sales_item class used in chapter 1.

 * The code used in this file will be explained in

 * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)

 * Readers shouldn't try to understand the code in this file

 * until they have read those chapters.
*/

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
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:
    // constructors are explained in section 7.1.4, pages 262 - 265
    // default constructor needed to initialize members of built-in type
    Sales_item() = default;
    Sales_item(const std::string &book): bookNo(book) { }
    Sales_item(std::istream &is) { is >> *this; }
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
   
    // operations on Sales_item objects
    std::string isbn() const { return bookNo; }
    double avg_price() const;
// private members as before
private:
    std::string bookNo;      // implicitly initialized to the empty string
    unsigned units_sold = 0; // explicitly initialized
    double revenue = 0.0;
};

// used in chapter 10
inline bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }

// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    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); // != defined in terms of operator==
}

// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

// assumes that both objects refer to the same ISBN
Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs)
{
    Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
    ret += rhs;           // add in the contents of (|rhs|)
    return ret;           // return (|ret|) by value
}

std::istream& operator>>(std::istream& in, Sales_item& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else
        s = Sales_item();  // input failed: reset object to default state
    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





φ(゜▽゜*)♪
2016-09-27 21:13
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:0 
亲测无误。截图为证(途中红色箭头所指两行为程序的输出,其他行是我的输入)
程序代码:
#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())//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;
}
图片附件: 游客没有浏览图片的权限,请 登录注册

另外,以后尽量把代码放到专门的代码框里去,这样不会破坏掉原来的结构对称。美观大方,也更易于其他人查看


φ(゜▽゜*)♪
2016-09-27 21:17
柠檬师弟
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-9-20
收藏
得分:0 
回复 5楼 书生牛犊
谢谢了,我之前以为不能继续输入了,还有那个成员函数的括号也确实忘了,嘿嘿,现在明白了,真的感谢!
2016-10-07 20:03
快速回复:C++primer第五版第一章的书店程序中的销售记录怎么输入
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036715 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved