第一章
1.9
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int sum = 0;
int i=50;
while (i <= 100)
{
sum += i;
i++;
}
cout << sum;
return 0;
}
输出:3825
1.10
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int i=10;
while (i >= 0)
{
cout << i << endl;
i--;
}
return 0;
}
1.11
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
cout << "输入两个整数" << endl;
int i, j;
cin >> i >> j;
if (i > j)
{
int m = j;
while (m <= i)
{
cout << m << endl;
m++;
}
}
else {
int m = i;
while (m <= j)
{
cout << m << endl;
m++;
}
}
return 0;
}
1.12
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int sum = 0;
for (int i = -100; i <= 100; ++i)
sum += i;
cout << sum << endl;
return 0;
}
sum = 0
1.16
从cin读取一组数,然后输出和。
文件结束符主要用于退出循环,输入一组数按enter进入一次循环。
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int value, sum;
sum = 0;
cout << "输入一组数" << endl;
for (; cin >> value;)
{
sum += value;
}
cout << sum << endl;
return 0;
}
1.20
#include<stdio.h>
#include<iostream>
#include"Sales_item.h"
using namespace std;
int main()
{
Sales_item book;
cout << "请输入销售记录" << endl;
//读入ISBN号、售出的册数以及销售价格
while (cin >> book)
{
//写入ISBN、售出的册数、总销售额和平均价格
cout << "ISBN、售出本数、销售额和平均售价为" << book << endl;
}
return 0;
}
1.21
#include<stdio.h>
#include<iostream>
#include"Sales_item.h"
using namespace std;
int main()
{
Sales_item book1,book2;
cout << "请输入两条ISBN相同的销售记录" << endl;
//读入ISBN号、售出的册数以及销售价格 输入两次按两次enter即可
cin >> book1 >> book2;
//写入ISBN、售出的册数、总销售额和平均价格
cout << "ISBN、售出本数、销售额和平均售价为" << book1+book2 << endl ;
return 0;
}
1.22
读取多个具有相同isbn的销售记录,输出所有记录的和
#include<stdio.h>
#include<iostream>
#include"Sales_item.h"
using namespace std;
int main()
{
Sales_item total,book;
cout << "请输入含有多条ISBN相同的销售记录" << endl;
//读入ISBN号、售出的册数以及销售价格
if (cin >> total)
{
while (cin >> book)
{
if (book.isbn() == total.isbn())
{
total += book;
}
else {
cout << "isbn不同" << endl;
return -1;
}
}
//写入ISBN、售出的册数、总销售额和平均价格
cout << "ISBN、售出本数、销售额和平均售价为" << total << endl ;
}
else {
cout << "没有数据" << endl;
return -1;
}
return 0;
}
1.23
读取多条销售记录,并统计每个ISBN有几条。
#include<stdio.h>
#include<iostream>
#include"Sales_item.h"
using namespace std;
int main()
{
Sales_item book1,book2;
int num = 1;//初始值是1
cout << "请输入若干销售记录" << endl;
//读入ISBN号、售出的册数以及销售价格
if (cin >> book1)
{
while (cin >> book2)
{
if (book1.isbn() == book2.isbn())
{
num++;
}
else {
cout << "isbn不同" << endl;
cout << book1.isbn() << "共有" << num << "条销售记录" << endl;
book1 = book2;//打印完进行重置
num = 1;//重新初始化num
}
}
//此处实际输出了拷贝给book1的book2数据
cout << book1.isbn() << "共有" << num << "条销售记录" << endl;
}
else {
cout << "没有数据" << endl;
return -1;
}
return 0;
}
1.24
数据文件重定向到1.23的程序中进行测试。
Windows中加.exe
1.25
书店程序:
打印警告信息使用了std::cerr
#include<stdio.h>
#include<iostream>
#include"Sales_item.h"
using namespace std;
int main()
{
Sales_item book;//保存下一条交易记录的变量
//读入第一条交易记录,并确保有数据可以处理
//读入ISBN号、售出的册数以及销售价格
if (cin >> book)
{
Sales_item trans; //保存和的变量
//读入并处理剩余交易记录
while (cin >> trans)
{
if (book.isbn() == trans.isbn())
{
book += trans;//更新总销售额
}
else {
//打印前一本书的结果
cout << book << endl;
book = trans;//book现在表示下一本书的销售额
}
}
//打印最后一本书的结果
cout << book << endl;
}
else {
//没有输入,警告
cerr << "没有数据?!" << endl;
return -1;//表示失败
}
return 0;
}