新建项目,选择Qt console Application项。参照c++primer5书本中2.6.2编写程序如下:
#include <QCoreApplication>
#include "sales_data.h"
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/*************************************/
sales_data data1,data2;
//读入data1和data2的代码
double price = 0;
std::cin >> data1.bookNo >>data1.units_sold >> price;
data1.revenue = data1.units_sold*price;
std::cin >> data2.bookNo >>data2.units_sold >> price;
data2.revenue = data2.units_sold*price;
//检查data1和data2的isbn是否相同的代码
if(data1.bookNo == data2.bookNo){
unsigned totalCnt = data1.units_sold +data2.units_sold;
double totalRevene = data1.revenue + data2.revenue;
//输出:ISBN,总销售量,总销售额,平均价格
std::cout << data1.bookNo << " " << totalCnt << " " << totalRevene << " ";
if(totalCnt!=0)
std::cout<<totalRevene/totalCnt<<std::endl;
else
std::cout<<"no sales"<<std::endl;
return 0;//标示成功
}
else{
std::cerr << "Data must refer to the same ISBN"
<<std::endl;
return -1;//标示失败
}
/*************************************/
return a.exec();
}
输出如下: