提交 1fb73641 编写于 作者: J jzplp

2.42-3.1

上级 9bb2973f
#include<iostream>
#include<string>
#include "Sales_data.h"
int main()
{
Sales_data book;
double price;
std::cin >> book.ISBN >> book.amount >> price;
book.totalPrice = price * book.amount;
std::cout << book.ISBN << ' ' << book.amount << ' ' << book.totalPrice;
if(book.amount > 0)
std::cout << ' ' << book.totalPrice / book.amount << std::endl;
else
std::cout << ' ' << "No Sales!" << std::endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
int main()
{
Sales_data book1, book2;
double price;
std::cin >> book1.ISBN >> book1.amount >> price;
book1.totalPrice = price * book1.amount;
std::cin >> book2.ISBN >> book2.amount >> price;
book2.totalPrice = price * book2.amount;
if(book1.ISBN != book2.ISBN)
{
std::cerr << "Not Same Books!" <<std::endl;
return -1;
}
book1.amount += book2.amount;
book1.totalPrice += book2.totalPrice;
std::cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
std::cout << ' ' << book1.totalPrice / book1.amount << std::endl;
else
std::cout << ' ' << "No Sales!" << std::endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
int main()
{
Sales_data book1, book2;
double price;
if(!(std::cin >> book1.ISBN >> book1.amount >> price))
return -1;
book1.totalPrice = price * book1.amount;
while (std::cin >> book2.ISBN >> book2.amount >> price)
{
book2.totalPrice = price * book2.amount;
if(book1.ISBN != book2.ISBN)
{
std::cerr << "Not Same Books!" <<std::endl;
return -1;
}
book1.amount += book2.amount;
book1.totalPrice += book2.totalPrice;
}
std::cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
std::cout << ' ' << book1.totalPrice / book1.amount << std::endl;
else
std::cout << ' ' << "No Sales!" << std::endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
int main()
{
Sales_data book1, book2;
double price;
int num = 0;
if(!(std::cin >> book1.ISBN >> book1.amount >> price))
return -1;
book1.totalPrice = price * book1.amount;
num++;
while (std::cin >> book2.ISBN >> book2.amount >> price)
{
book2.totalPrice = price * book2.amount;
if(book1.ISBN == book2.ISBN)
{
book1.amount += book2.amount;
book1.totalPrice += book2.totalPrice;
num++;
}
else
{
std::cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
std::cout << ' ' << book1.totalPrice / book1.amount << std::endl;
else
std::cout << ' ' << "No Sales!" << std::endl;
std::cout << "num = " << num << std::endl;
num = 1;
book1 = book2;
}
}
std::cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
std::cout << ' ' << book1.totalPrice / book1.amount << std::endl;
else
std::cout << ' ' << "No Sales!" << std::endl;
std::cout << "num = " << num << std::endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
int main()
{
Sales_data total;
double price;
if(std::cin >> total.ISBN >> total.amount >> price)
{
total.totalPrice = price * total.amount;
Sales_data trans;
while (std::cin >> trans.ISBN >> trans.amount >> price)
{
trans.totalPrice = price * trans.amount;
if(total.ISBN == trans.ISBN)
{
total.amount += trans.amount;
total.totalPrice += trans.totalPrice;
}
else
{
std::cout << total.ISBN << ' ' << total.amount << ' ' << total.totalPrice;
if(total.amount > 0)
std::cout << ' ' << total.totalPrice / total.amount << std::endl;
else
std::cout << ' ' << "No Sales!" << std::endl;
total = trans;
}
}
std::cout << total.ISBN << ' ' << total.amount << ' ' << total.totalPrice;
if(total.amount > 0)
std::cout << ' ' << total.totalPrice / total.amount << std::endl;
else
std::cout << ' ' << "No Sales!" << std::endl;
} else {
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include<string>
struct Sales_data
{
std::string ISBN;
double totalPrice;
int amount;
};
#endif
......@@ -277,4 +277,12 @@ decltype(j) b = i;
[重写1.21程序代码](2.41/2.41_1.21.cpp)
[重写1.22程序代码](2.41/2.41_1.22.cpp)
[重写1.23程序代码](2.41/2.41_1.23.cpp)
[重写1.25程序代码](2.41/2.41_1.25.cpp)
\ No newline at end of file
[重写1.25程序代码](2.41/2.41_1.25.cpp)
* **练习2.42**
[Sales_data.h程序代码](2.42/Sales_data.h)
[重写1.20程序代码](2.42/2.42_1.20.cpp)
[重写1.21程序代码](2.42/2.42_1.21.cpp)
[重写1.22程序代码](2.42/2.42_1.22.cpp)
[重写1.23程序代码](2.42/2.42_1.23.cpp)
[重写1.25程序代码](2.42/2.42_1.25.cpp)
\ No newline at end of file
#include<iostream>
using std::cout;
using std::endl;
int main()
{
int val = 10;
while(val >= 0)
{
cout<<val<<" ";
val--;
}
cout<<endl;
return 0;
}
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int v1,v2,temp;
cin >> v1 >> v2;
if(v1 > v2)
{
temp = v1;
v1 = v2;
v2 = temp;
}
while(v2 >= v1)
{
cout << v1 << " ";
v1++;
}
cout << endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
Sales_data book;
double price;
cin >> book.ISBN >> book.amount >> price;
book.totalPrice = price * book.amount;
cout << book.ISBN << ' ' << book.amount << ' ' << book.totalPrice;
if(book.amount > 0)
cout << ' ' << book.totalPrice / book.amount << endl;
else
cout << ' ' << "No Sales!" << endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
int main()
{
Sales_data book1, book2;
double price;
cin >> book1.ISBN >> book1.amount >> price;
book1.totalPrice = price * book1.amount;
cin >> book2.ISBN >> book2.amount >> price;
book2.totalPrice = price * book2.amount;
if(book1.ISBN != book2.ISBN)
{
cerr << "Not Same Books!" << endl;
return -1;
}
book1.amount += book2.amount;
book1.totalPrice += book2.totalPrice;
cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
cout << ' ' << book1.totalPrice / book1.amount << endl;
else
cout << ' ' << "No Sales!" << endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
int main()
{
Sales_data book1, book2;
double price;
if(!(cin >> book1.ISBN >> book1.amount >> price))
return -1;
book1.totalPrice = price * book1.amount;
while (cin >> book2.ISBN >> book2.amount >> price)
{
book2.totalPrice = price * book2.amount;
if(book1.ISBN != book2.ISBN)
{
cerr << "Not Same Books!" <<endl;
return -1;
}
book1.amount += book2.amount;
book1.totalPrice += book2.totalPrice;
}
cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
cout << ' ' << book1.totalPrice / book1.amount << endl;
else
cout << ' ' << "No Sales!" << endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
Sales_data book1, book2;
double price;
int num = 0;
if(!(cin >> book1.ISBN >> book1.amount >> price))
return -1;
book1.totalPrice = price * book1.amount;
num++;
while (cin >> book2.ISBN >> book2.amount >> price)
{
book2.totalPrice = price * book2.amount;
if(book1.ISBN == book2.ISBN)
{
book1.amount += book2.amount;
book1.totalPrice += book2.totalPrice;
num++;
}
else
{
cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
cout << ' ' << book1.totalPrice / book1.amount << endl;
else
cout << ' ' << "No Sales!" << endl;
cout << "num = " << num << endl;
num = 1;
book1 = book2;
}
}
cout << book1.ISBN << ' ' << book1.amount << ' ' << book1.totalPrice;
if(book1.amount > 0)
cout << ' ' << book1.totalPrice / book1.amount << endl;
else
cout << ' ' << "No Sales!" << endl;
cout << "num = " << num << endl;
return 0;
}
#include<iostream>
#include<string>
#include "Sales_data.h"
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
int main()
{
Sales_data total;
double price;
if(cin >> total.ISBN >> total.amount >> price)
{
total.totalPrice = price * total.amount;
Sales_data trans;
while (cin >> trans.ISBN >> trans.amount >> price)
{
trans.totalPrice = price * trans.amount;
if(total.ISBN == trans.ISBN)
{
total.amount += trans.amount;
total.totalPrice += trans.totalPrice;
}
else
{
cout << total.ISBN << ' ' << total.amount << ' ' << total.totalPrice;
if(total.amount > 0)
cout << ' ' << total.totalPrice / total.amount << endl;
else
cout << ' ' << "No Sales!" << endl;
total = trans;
}
}
cout << total.ISBN << ' ' << total.amount << ' ' << total.totalPrice;
if(total.amount > 0)
cout << ' ' << total.totalPrice / total.amount << endl;
else
cout << ' ' << "No Sales!" << endl;
} else {
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
#include<iostream>
using std::cout;
using std::endl;
int main()
{
int sum = 0,val = 50;
while(val <= 100)
{
sum += val;
val++;
}
cout<<sum<<endl;
return 0;
}
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include<string>
struct Sales_data
{
std::string ISBN;
double totalPrice;
int amount;
};
#endif
* **练习3.1**
[重写1.9程序代码](3.1/3.1_1.9.cpp)
[重写1.10程序代码](3.1/3.1_1.10.cpp)
[重写1.11程序代码](3.1/3.1_1.11.cpp)
[重写1.20程序代码](3.1/3.1_1.20.cpp)
[重写1.21程序代码](3.1/3.1_1.21.cpp)
[重写1.22程序代码](3.1/3.1_1.22.cpp)
[重写1.23程序代码](3.1/3.1_1.23.cpp)
[重写1.25程序代码](3.1/3.1_1.25.cpp)
\ No newline at end of file
......@@ -7,4 +7,5 @@ C++ Primer 第五版中文版 练习题答案代码(个人所做)
## 答案目录
* [第一章 练习题答案](Chapter-1/chapter-1-answer.md)
* [第二章 练习题答案](Chapter-2/chapter-2-answer.md)
\ No newline at end of file
* [第二章 练习题答案](Chapter-2/chapter-2-answer.md)
* [第三章 练习题答案](Chapter-3/chapter-3-answer.md)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册