C++ primer 5 -- 第一章习题答案
1.9
#include <stdio.h>
#include <iostream>
#include <math.h>
int main()
{
int sum = 0, vec = 50;
while (vec <= 100 )
{
sum += vec;
++vec;
}
std::cout << sum << std::endl;
return 0;
}
1.10
int main()
{
int sum = 0, vec = 10;
while (vec >= 0 )
{
std::cout << vec << std::endl;
--vec;
}
return 0;
}
1.11
int main()
{
int vec1 = 0, vec2 = 0, vec= 0;
std::cout << "please input two integer, eg 1, 4" << std::endl;
std::cin >> vec1 >> vec2;
while ( vec1 <= vec2 )
{
std::cout << vec1 << std::endl;
++vec1;
}
return 0;
}
1.12
将-100到100的所有整数相加, sum=0
1.13
int main()
{
int sum = 0;
for(int i = 50; i <= 100; i++)
{
sum += i;
}
std::cout << sum << std::endl;
return 0;
}
int main()
{
for(int i = 10; i >= 0; i--)
{
std::cout << i << std::endl;
}
return 0;
}
int main()
{
int vec1 = 0, vec2 = 0;
std::cout << "please input two integer" << std::endl;
std:: cin >> vec1 >> vec2;
for(int i = vec1; i <= vec2; i++)
{
std::cout << i << std::endl;
}
//std::cout << sum << std::endl;
return 0;
}
1.17
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int vec1 = 0, vec2 = 0, vec= 0;
std::cout << "please input two integer, eg 1, 4" << std::endl;
std::cin >> vec1 >> vec2;
if( vec2 >= vec1)
{
while(vec1 <= vec2)
{
cout << vec1 << std::endl;
++vec1;
}
}
else
{
while(vec2 <= vec1)
{
cout << vec2 << endl;
++vec2;
}
}
return 0;
}
1.20
#include <stdio.h>
#include <iostream>
#include <math.h>
#include "Sales_item.h"
using namespace std;
int main()
{
Sales_item item1, item2;
cin >> item1 >> item2;
cout << item1 << item2 <<endl;
return 0;
}
最简单g++ main.cpp 编译即可,自定义输入文件<add_item>以及输出文件, 使用文件重定向:
$ ./a.out <add_item>myoutput
1.21
int main()
{
Sales_item item1, item2;
cout <<"please input" << endl;
cin >> item1 >> item2;
if( item1.isbn() == item2.isbn() )
{
cout << item1 + item2 <<endl;
}
else
{
cerr <<"Data must refer to same ISBN" << endl;
}
return 0;
}
1.22
int main()
{
Sales_item item1, item2;
cout <<"please input" << endl;
cin >> item1;
while( cin >> item2)
{
if( item2.isbn() == item1.isbn() )
{
item1 += item2;
}
else
{
cerr <<"Data must refer to same ISBN or end " << endl;
}
}
cout << item1 << endl;
return 0;
}
1.23
与eg.1.4.4相同, 只不过将输入整数类型转换为Sales_item 类型。
int main()
{
Sales_item item1, item2;
cout <<"please input" << endl;
if( cin >> item1 )
{
int cnt = 1;
while( cin >> item2)
{
if( item2.isbn() == item1.isbn() )
{
++cnt;
}
else
{
cout << item1 << "occurs" << cnt << " times " << endl;
item1 = item2;
cnt = 1;
}
}
cout << item1 << "occurs" << cnt << " times " << endl;
}
return 0;
}
1.25
int main()
{
Sales_item total; // variable to hold data for the next transaction
// read the first transaction and ensure that there are data to process
if (std::cin >> total) {
Sales_item trans; // variable to hold the running sum
// read and process the remaining transactions
while (std::cin >> trans) {
// if we're still processing the same book
if (total.isbn() == trans.isbn())
total += trans; // update the running total
else {
// print results for the previous book
std::cout << total << std::endl;
total = trans; // total now refers to the next book
}
}
std::cout << total << std::endl; // print the last transaction
} else {
// no input! warn the user
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
return 0;
}