第五章 循环和关系表达式编程题

1.

#include <iostream>

int min()
{
    using namespace std;
    int x, y;
    cout << "Enter two numbers: \n";
    cin >>x;
    cin >>y;
    int sum = 0;
    for(int i = x; i <= y; ++i)
        sum += i;
    cout << "The sum is " << sum << endl;
    return 0;
}

2.

#include <iostream>

int main()
{
    using namespace std;
    double money_d = 100, money_c = 100;
    int year = 0;
    double total_d = 100, total_c = 100;

    do
    {
        total_d = total_d + money_d *0.1;
        total_c = money_c + money_c * 0.05;
        money_c = total_c;
        year++;
        cout << year << " year: " << "d have " << total_d << ";c have " << total_c << endl;
    }while (total_d >= total_c);
    cout << year << " years" << endl;

    return 0;
}

3.

#include <iostream>

int main()
{
    using namespace std;
    double money_d = 100, money_c = 100;
    int year = 0;
    double total_d = 100, total_c = 100;

    do
    {
        total_d = total_d + money_d *0.1;
        total_c = money_c + money_c * 0.05;
        money_c = total_c;
        year++;
        cout << year << " year: " << "d have " << total_d << ";c have " << total_c << endl;
    }while (total_d >= total_c);
    cout << year << " years" << endl;

    return 0;
}

4.

#include <iostream>
int main()
{
    using namespace std;
    char *month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    int number[12];
    int total = 0;
    for (int i = 0;i < 12; ++i)
    {
        cout << "Enter the number of " << month[i] << ": " << endl;
        cin >> number[i];
        total += number[i];
    }

    cout << "The total number is " << total << endl;
}

5.

#include <iostream>
int main()
{
    using namespace std;
    char *month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    char *year[3]= {"the first year" , "the next year", "the third year"};
    int number[3][12];
    int total = 0;

    for (int i = 0; i < 3; ++i)
    {
        cout << "Enter the number of " << year[i] << ": " << endl;
        for (int j = 0; j < 12; ++j) {
            cout << month[j] << ": " << endl;
            cin >> number[i][j];
            total += number[i][j];
        }
    }
    cout << "The total number is " << total << endl;
}

6.

#include <iostream>
#include <string>
using namespace std;
struct car{
    string name;
    int year;
};
int main()
{
    cout<<"How many cars do you wish to catalog? ";
    int num;
    (cin>>num).get();
    car* ps=new car[num];
    for(int i=0;i<num;++i)
    {
        cout<<"Car #"<<i+1<<":\n";
        cout<<"Please enter the make: ";
        getline(cin,ps[i].name);
        cout<<"Please enter the year made: ";
        (cin>>ps[i].year).get();
    }
    cout<<"Here is your collection:\n";
    for(int i=0;i<num;++i)
        cout<<ps[i].year<<" "<<ps[i].name<<endl;
    delete [] ps;
    return 0;
}

这题是参考网上的代码,字符串的输入还是没怎么掌握。

7.

#include <iostream>
#include <string>
int main()
{
    using namespace std;
    cout << "Enter words (to stop, type the word done):";
    char *word;
    cin >>word;
    int sum = 0;
    while (strcmp(word, "done"))
    {
        cin >> word;
        sum++;
    }
    cout << "You enter a total of " << sum << " words" << endl;
    return 0;
}

8.

#include <iostream>
#include <string>
int main()
{
    using namespace std;
    cout << "Enter words (to stop, type the word done):";
    string word;
    cin >> word;
    int sum = 0;
    while (word != "done")
    {
        cin >> word;
        sum++;
    }
    cout << "You enter a total of " << sum << " words" << endl;
    return 0;
}

9.

#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter number of rows: ";
    int row;
    cin >> row;
    for (int i = 1; i <= row; ++i)
    {
        for (int j = 1; j <= 5-i; ++j)
            cout << ".";

        for (int j= 1; j <= i; ++j)
            cout << "*";
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值