第一章 很简单,最基础的介绍
有两个小知识点:
○endl有什么作用?
一是换行;二是将与设备关联的缓冲区中的内容刷新到设备中,缓冲刷新操作可以保证到目前为止程序所产生的所有输入都真正写入输出流中,而不是仅仅停留在内存中等待写入流。
○for和while的区别?
for:循环控制变量的初始化和修改都放在语句头部分,形式较简洁,且特别适用于循环次数已知的情况。
while:循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)
Sales_item.h的代码
#include<iostream>
#include"Sales_item.h"
using namespace std;
void test_1_9() {
int sum = 0, val = 50;
while (val <= 100)
{
sum += val;
++val;
}
std::cout << "sum=" << sum << std::endl;
}
void test_1_10() {
int val = 10;
while (val >= 0)
{
std::cout << val << " ";
--val;
}
std::cout << std::endl;
}
void test_1_11() {
int a, b;
std::cout << "请输入两个整数:" << std::endl;
std::cin >> a >> b;
while (a <= b)
{
std::cout << a << " ";
++a;
}
std::cout << endl;
}
void example_1_4_3() {
int sum = 0, value = 0;
while (std::cin>>value)
{
sum += value;
}
std::cout << "sum=" << sum << std::endl;
}
void example_1_4_4() {
//统计 在输入中 。每个值连续出现了多少次
int cur_val=0, val=0;
if (std::cin >> cur_val)
{
int cur_count = 1;
while (std::cin >> val)
{
if (val == cur_val)
++cur_count;
else
{
std::cout << cur_val << " " << cur_count << (cur_count == 1 ? "time" : "times") << std::endl;
cur_val = val;
cur_count = 1;
}
}
std::cout << cur_val << " " << cur_count << (cur_count == 1 ? "time" : "times") << std::endl;
}
}
void test_1_20() {
Sales_item book;
std::cin >> book;
std::cout << book << std::endl;
}
void test_1_21() {
Sales_item book1,book2;
std::cin >> book1>>book2;
std::cout << book1+book2 << std::endl;
}
void test_1_22() {
Sales_item sum_book, book;
std::cin >> sum_book;
while (std::cin >> book)
{
sum_book += book;
}
std::cout << sum_book << std::endl;
}
void test_1_24() {
Sales_item cur_book, book;
if (std::cin >> cur_book)
{
int cur_count=1;
while (std::cin >> book)
{
if (cur_book.isbn() == book.isbn())
{
cur_book += book;
++cur_count;
}
else
{
std::cout << cur_book.isbn() << "的记录出现了" << cur_count << "次" << " ";
std::cout << cur_book << std::endl;
cur_book = book;
cur_count = 1;
}
}
std::cout << cur_book.isbn() << "的记录出现了" << cur_count << "次" << " ";
std::cout << cur_book << std::endl;
}
else {
std::cerr << "No Data?" << std::endl;
}
}
int main()
{
//test_1_9();
//test_1_10();
//test_1_11();
//example_1_4_3();
//example_1_4_4();
//test_1_20();
//test_1_21();
//test_1_22();
//test_1_24();
return 0;
}