第五章 编程练习

编程练习1

#include <iostream>
using namespace std;

int main()
{
    int a,b;
    cout << "Please enter the start number: ";
    cin >> a;
    cout << "Please enter the end number: ";
    cin >> b;
    int sum = 0;
    for (int i = a; i<=b; i++)
        sum = sum + i;
    cout << " the sum of all integers between " << a
        << " and " << b << " is " << sum << endl;
    system("pause");
    return 0;
}

编程练习2

#include <iostream>
#include <array>

using namespace std;
const int ArSize = 101;
int main()
{
    array<long double, ArSize> factorials;
    factorials[1]  = factorials[0] = 1;
    for(int i = 2; i < ArSize; i++)
    {
        factorials[i] = i * factorials[i-1];
    }
    for(int i = 0; i < ArSize; i++)
    {
        cout << i << "! = " << factorials[i] << endl;
    }
    system("pause");
    return 0;
}

编程练习3

#include <iostream>

using namespace std;

int main()
{
    int i;
    int sum = 0;
    cout << "Please enter a number: ";
    cin >> i;
    while(i != 0)
    {
        sum = sum + i;
        cout << "Please enter another number: ";
        cin >> i;
    }
    cout << "The sum of input numbers is : " << sum << endl;
    system("pause");
    return 0;
}

编程练习4

#include <iostream>

using namespace std;

int main()
{
    float Daphne_Capital = 100;
    float Cleo_Capital = 100;
    float interest_fixed = 0.1;
    float interest_flex = 0.05;
    int year = 1;
    do {
        Daphne_Capital = Daphne_Capital + 100 * interest_fixed;
        Cleo_Capital = Cleo_Capital * (1 + interest_flex);
        year++;
    } 
    while (Daphne_Capital > Cleo_Capital);
    cout << "after " << year << " years, Cleo has more money than Daphne" << endl;
    cout << "Daphne has " << Daphne_Capital << " dollars" << endl;
    cout << "Cleo has " << Cleo_Capital << " dollars" << endl;
    system("pause");
    return 0;
}

编程练习5

#include <iostream>

using namespace std;

int main()
{
    char * month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul","Aug", "Sep", "Oct", "Nov", "Dec"};
    int sales_month;
    int sales_year = 0;
    for (int i = 0; i < 12;i++)
    {
        cout << "Enter the number of books sold in " << month[i] << " : " ;
        cin >> sales_month;
        sales_year = sales_year + sales_month;
    }

    cout << "This year we have sold " << sales_year << " books" << endl;
    system("pause");
    return 0;
}

编程练习6

#include <iostream>

using namespace std;

int main()
{
    int sales[3][12];
    int total_sales = 0;
    for (int year = 0; year < 3; year++)
    {
        int sales_year = 0;
        for (int month = 0; month < 12; month++)
        { 
            cout << " Enter the sales of year " << year+1 
                << " month " << month +1 << " :";
            cin >> sales[year][month];
            sales_year = sales_year + sales[year][month];
        }
        cout << "This year we have sold " << sales_year << " books" << endl;
        total_sales = total_sales + sales_year;
    }
    cout << "In total we have sold " << total_sales << " books" << endl;

    system("pause");
    return 0;
}

编程练习7
这个练习使用了一个很笨的方法,因为结构数组是用动态指针声明的,所以赋值后还要把指针调回原位。学到后面函数之后会有更好的方法

#include <iostream>
#include <string>

using namespace std;

struct car {
    string brand;
    int manufacture_year;
};

int main()
{
    int car_num;
    cout << "How many cars do you wish to catalog? ";
    cin>>car_num;
    car *car_arr = new car[car_num];
    for (int i = 0; i<car_num;i++,car_arr++)
    {
        cin.get();
        cout << "Car #" << i+1 <<":" << endl;
        cout << "Please enter the make: ";
        string name;
        getline(cin,name);
        //cin.get();
        cout << "Please enter the year made: ";
        int y;
        cin>>y;
        //cin.get();
        car_arr->brand = name;
        car_arr->manufacture_year = y;

    }
    for (int i =0; i<car_num;i++)
    {
        car_arr--;
    }
    cout << "Here is your collection:" << endl;
    for (int i=0; i<car_num;i++)
    {
        cout << car_arr[i].manufacture_year << "\t" << car_arr[i].brand << endl;
    }
    system("pause");
    return 0;
}

编程练习8

#include <iostream>

using namespace std;
const int ChSize = 100;

int main()
{
    char word[ChSize];
    int count = 0;
    cout << "Please enter words: (type \"done\" to stop)" << endl;
    do {
        cin>>word;
        if(!strcmp(word,"done"))
            break;
        else
            count++;
    } while(true);

    cout << "In total you have input " << count << " words!" << endl;
    system("pause");
    return 0;
}

编程练习9

#include <iostream>
#include <string>

using namespace std;


int main()
{
    string word;
    int count = 0;
    cout << "Please enter a word: ";
    getline(cin, word);
    while(word !="done")
    {
        count ++;
        cout << " Please enter another word: ";
        getline(cin, word);
    }

    cout << "In total you have input " << count << " words!" << endl;
    system("pause");
    return 0;
}

编程练习10

#include <iostream>
using namespace std;

int main()
{
    int row;
    cout << "Please input how many rows to display: ";
    cin >> row;
    for (int i=0; i<row; i++)
    {
        int star = i+1;
        int dot = row-i-1;
        for (int j=0; j<dot;j++)
            cout << " . ";
        for (int k=0;k<star;k++)
            cout << " * ";
        cout << endl;
    }
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值