3.1 使用恰当的using 声明重做 1.4.1节和2.6.2节的练习
1.4.1
#include <iostream>
using std::cout;
using std::endl;
int main(){
int sum = 0;
for(int val = 50; val <=100; ++val){
sum += val;
}
cout << "sum of 50 to 100 inclusive is "
<< sum << endl;
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
int main(){
for(int val = 10; val >= 0; --val){
cout << "val = " << val << endl;
}
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main(){
cout << "Enter two number: " << endl;
int v = 0, v1 = 0;
std::cin >> v >> v1;
if(v <= v1){
for(; v <= v1; ++v){
cout << v << endl;
}
}
else{
for(; v1 <= v; ++v1){
cout << v1 << endl;
}
}
}
2.6.2
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sale_Data
{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
// for(Sales_Data sd; cin >> sd; cout << sd << endl);
Sales_Data book;
double price;
cin >> book.bookNo >> book.units_sold >> price >> endl;
book.revenue = book.units_sold * price;
cout << book.bookNo << " " << book.units_sold << " " << book.revenue << " " << price << endl;
return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::cerr;
struct Sales_Data{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
Sales_Data book, book2;
double price;
cin >> book.bookNo >> book.units_sold >> price >> endl;
cin >> book2.bookNo >> book2.units_sold >> price >> endl;
if(book.bookNo == book2.bookNo){
cout << book.units_sold + book2.units_sold << endl;
}else{
cerr << "输入有误" << endl;
}
return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sales_Data{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
Sales_Data total_book;
double total_price;
if(cin >> total_book.bookNo >> total_book.units_sold >> total_price){
// int cnt = 1;
total_book.revenue = total_book.units_sold * total_price;
Sales_Data book;
double book_price;
while(cin >> book.bookNo >> book.units_sold >> book_price){
book.revenue = book.units_sold * book_price;
if(total_book.bookNo == book.bookNo){
total_book.units_sold += book.units_sold;
total_book.revenue += book.revenue;
// ++cnt;
}else{
cout << total_book.bookNo << " " << total_book.units_sold << " " << total_book.revenue << total_price << endl;
// std::cout << total_book.bookNo << "has " << cnt << "recordings!";
total_book.bookNo = book.bookNo;
total_price = book_price;
total_book.units_sold = book.units_sold;
total_book.revenue = book.revenue;
}
}
cout << total_book.bookNo << " " << total_book.units_sold << " " << total_book.revenue << total_price << endl;
// std::cout << total_book.bookNo << "has " << cnt << "recordings!";
}
else{
cout << " No Data" << endl;
return -1;
}
return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sales_Data{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(){
Sales_Data total_book;
double total_price;
if(cin >> total_book.bookNo >> total_book.units_sold >> total_price){
int cnt = 1;
total_book.revenue = total_book.units_sold * total_price;
Sales_Data book;
double book_price;
while(cin >> book.bookNo >> book.units_sold >> book_price){
book.revenue = book.units_sold * book_price;
if(total_book.bookNo == book.bookNo){
// total_book.units_sold += book.units_sold;
// total_book.revenue += book.revenue;
++cnt;
}else{
cout << total_book.bookNo << "has " << cnt << "recordings!" << endl;
total_book.bookNo = book.bookNo;
total_price = book_price;
total_book.units_sold = book.units_sold;
total_book.revenue = book.revenue;
}
}
cout << total_book.bookNo << "has " << cnt << "recordings!" << endl;
}
return 0;
}
3.2 编写一段程序从标准输入中一次读入一行,然后修改该程序使其一次读入一个词
一次一行
int main(