第一章

#include <iostream>
int main()
{
std::cout << "Hello, World" << std::endl;
return 0;
}

1.9while实现50-100和

#include <iostream>
using namespace std;
int main()
{
        int sum = 0;
        int i = 50;
        while(i <= 100)
        {
                sum += sum + i;
        }
        cout << sum;
}

 

 

 

 

1.10实现10-0整数

#include <iostream>
using namespace std;
int main()
{
int i = 10;
while(i >= 0)
{
cout << i << endl;
i--;
}
return 0;
}

 

 

 

1.13 for循环

#include <iostream>
using namespace std;
int main()
{
        int sum = 0;
        for(int i = 50; i < 101; i++)
        {
                sum += i;
                cout << i  << "  " << sum << endl;
        }
        return 0;
}

 

 

#include <iostream>
using namespace std;
int main()
{
        int v1 = 0, v2 = 0;
        cout << "enter two numbers" << endl;
        cin >> v1 >> v2;
        int sum = 0;
        for(int i = v1; i <= v2; i++)
        {
                sum += i;
                cout << i  << "  " << sum << endl;
        }
        return 0;
}

 

1.16 cin一组数据

#include <iostream>
using namespace std;
int main()
{
        int value = 0;
        int sum = 0;
        cout << "enter one group data: " << endl;
        while (cin >> value)
        {
                sum = sum + value;
        }

        cout << sum << endl;

        return 0;
}

 

 

 

1.20

#include <iostream>
#include "Sales_item.h"
int main()
{
        Sales_item item1, item2;
        std::cin >> item1 >> item2;
        std::cout << item1 + item2 << std::endl;
        return 0;
}

 

 

 

1.22编写程序,读取多个具有相同 ISBN 的销售记录,输出所有记录的和

 

#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
        Sales_item item, sum_item;
        cout << "enter some groups of data which ISBN is same." << endl;
        while(cin >> item)
        {
                sum_item += item;
        }
        std::cout << "The data sum is " <<  sum_item << std::endl;
        return 0;
}

 

 

 

1.24输入表示多个 ISBN 的多条销售记录来测试上一个程序,每个 ISBN 的记录应该聚在一起。

#include <iostream>
#include "Sales_item.h"

int main()
{
    Sales_item total;
    if (std::cin >> total){
        Sales_item trans;
        while (std::cin >> trans){
            if (total.isbn() == trans.isbn()) {
                total += trans;
            }
            else {
                std::cout << total << std::endl;
                total = trans;
            }
        }
        std::cout << total << std::endl;
    }
    else {
        std::cerr << "No data?!" << std::endl;
        return -1;
    }
    return 0;
}

 

posted @ 2021-03-29 16:32  11YS  阅读(73)  评论(0编辑  收藏  举报