C++基础(2)2016.6.13

-------------------Sales_item.h-------------
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>

class Sales_item
{
public:
 Sales_item(const std::string &book) :isbn(book), units_sold(0), revenue(0.0){}
 Sales_item(std::istream &is){ is >> *this; }
 friend std::istream& operator>>(std::istream &, Sales_item &);
 friend std::ostream& operator<<(std::ostream &, const Sales_item &);
public:
 Sales_item & operator+=(const Sales_item&);
public:
 double avg_price() const;
 bool same_isbn(const Sales_item &rhs)const
 {
  return isbn == rhs.isbn;
 }
 Sales_item() :units_sold(0), revenue(0.0){}
public:
 std::string isbn;
 unsigned units_sold;
 double revenue;
};
using std::istream;
using std::ostream;
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.same_isbn(rhs);
}
inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
 return !(lhs == rhs);
}
inline Sales_item & Sales_item::operator +=(const Sales_item &rhs)
{
 units_sold += rhs.units_sold;
 revenue += rhs.revenue;
 return *this;
}
inline Sales_item operator+(const Sales_item &lhs, const Sales_item &rhs)
{
 Sales_item ret(lhs);
 ret += rhs;
 return ret;
}
inline istream& operator>>(istream &in, Sales_item &s)
{
 double price;
 in >> s.isbn >> s.units_sold >> price;
 if (in)
  s.revenue = s.units_sold * price;
 else
  s = Sales_item();
 return in;
}
inline ostream& operator<<(ostream &out, const Sales_item &s)
{
 out << s.isbn << "\t" << s.units_sold << "\t" << s.revenue << "\t" << s.avg_price();
 return out;
}
inline double Sales_item::avg_price() const
{
 if (units_sold)
  return revenue / units_sold;
 else
  return 0;
}
#endif



------------source.cpp----------

#include <iostream>
#include "Sales_item.h"
int main(){
 /*Sales_item book;
 std::cin >> book;
 std::cout << book << std::endl;
 return 0;*/
 /*Sales_item item1, item2;
 std::cin >> item1 >> item2;
 std::cout << item1 + item2 << std::endl;
 return 0;*/
 /*Sales_item item1, item2;
 std::cin >> item1 >> item2;
 if (item1.same_isbn(item2)){
  std::cout << item1 + item2 << std::endl;
  return 0;
 }
 else{
  std::cerr << "Data must refer to same ISBN" << std::endl;
  return -1;
 }*/
 //declare variables to hold running sum and data for the next record
 Sales_item total, trans;
 //is there data to process?
 if (std::cin>>total){
  //if so, read the transaction records
  while (std::cin>>trans){
   if (total.same_isbn(trans)){
    //match,update the running total
    //total = total + trans;
    //加入用复合赋值操作符会如何
    total += trans;
   }
   else{
    //no match,print and assign(重置) to total
    std::cout << total << std::endl;
    total = trans;
   }
  }
  //用于最后一次的输出
  std::cout << total << std::endl;
 }
 else{
  std::cout << "no data" << std::endl;
  return -1;
    }
 return 0;
}


第一部分 基本语言
第二章 变量和基本类型:
2.1 基本内置类型
2.1.1 整型
char 8bit    wchar_t 16bit(中文、日文)
short(半个机器字(word, 4 byte)长) int long  带符号(signed,表示正负数) 不带符号(unsigned, 大于等于0的数) short,int,long都默认为带符号数
2.1.2 浮点型
float(32bit,只能保证6位有效数字) double(64bit,可以满足10位有效数字) long double(3到4个字)

注意:char一般用于存储字符而不用于计算,int和long的选用要根据程序的具体细节而定,double类型一般是比较合适的。

2.2 字面值常量
2.2.1 整型字面值规则
20---十进制   024(八进制,前面是零0) 0x14(十六进制)      这3个数都表示十进制数20   1024LU----表示无符号数长整型
2.2.2 浮点字面值规则
1E-3F
2.2.3 布尔字面值 ture false 字符型字面值 'a'
2.2.4 非打印字符的转义序列 \n
2.2.5 字符串字面值 "hello world"----正如存在宽字符字面值L'a',同样存在宽字符串字面值L"a wide string literal"
2.2.6 字符串字面值的连接
std::cout << "a multi-line "
  "string literal "
  "using concatenation"
  << std::endl;
2.2.7 多行字面值

2.3 变量
int value = 2;
 int pow = 10;
 int result = 1;
 for (int cnt = 0; cnt != pow; cnt++)
  result *= value;
 std::cout << value
  << " raised to the power of "
  << pow << ":\n"
  << result << std::endl;
2.3.1 什么是变量
变量提供了程序可以操作的有名字的存储区。
2.3.2 变量名
C++关键字
变量命名习惯student_loan





















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值