C++primeplus第五章编程练习

文章包含多个C++程序示例,涉及循环结构(如for和while)、关系表达式以及数组的使用。示例涵盖了计算两数之间整数之和、计算阶乘、累加输入值、模拟财务增长、统计销售数据以及存储和显示汽车信息等场景。
摘要由CSDN通过智能技术生成

文件尾条件需要整理

第五章循环和关系表达式

1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里先假设先输入较小的整数。

#include<iostream>

using namespace std;

int main() {

    int begin;

    int end;

    int sum = 0;

    cin >> begin;

    cin >> end;

    for (int i = begin; i <= end; i++) {

       sum += i;

    }

    cout << sum;

    return 0;

   

}

2.

#include<iostream>

#include<array>

using namespace std;

const int ArSize = 16;

int main() {

    array<long long, ArSize> factorials;

    factorials[1] = factorials[0] = 1LL;

    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;

    return 0;

}

3.

#include<iostream>

#include<array>

using namespace std;

int main() {

    int x;

    int sum = 0;

    while (cin >> x) {

       if (x == 0)

           break;

       sum += x;

       cout << sum << endl;;

    }

    return 0;

}

4.

#include<iostream>

#include<array>

using namespace std;

int main() {

    float x,y;

    float sum1 = 100;

    float sum2 = 100;

    int year = 1;

    while (sum2<=sum1) {

       sum1 += 10;

       sum2 += sum2 * 0.05;

       year++;

    }

    cout << year << endl;

    cout << sum1 << endl;

    cout << sum2 << endl;

    return 0;

}

5.

#include<iostream>

using namespace std;

int main() {

    const string Month[] = { "1","2","3","4","5","6","7","8","9","10","11","12" };

    int sell[12];

    int sum = 0;

    for (int i = 0; i < 12; i++) {

       cin >> sell[i];

    }

    for (int i = 0; i < 12; i++)

    {

       cout << Month[i] << ":" << sell[i] << endl;

       sum += sell[i];

    }

    cout << sum;

    return 0;

}

6.

#include<iostream>

using namespace std;

int main() {

    const string Month[] = { "1","2","3","4","5","6","7","8","9","10","11","12" };

    int sell[3][12];

    int sum = 0;

    for (int i = 0; i < 3; i++) {

       for(int j = 0;j<12;j++)

       cin >> sell[i][j];

    }

    for (int i = 0; i < 3; i++)

    {

       for (int j = 0; j < 12; j++) {

           cout << "第"<<i+1<<"年"<<Month[j] << "月:" << sell[i][j] << endl;

           sum += sell[i][j];

       }

    }

    cout <<"总量:" << sum;

    return 0;

}

7.

#include<iostream>

#include<string>

using namespace std;

int main() {

    int num;

    struct car {

       string corporation;

       int year;

    };

    cout << "How many cars do you wish to catalog?" << endl;

    cin >> num;

    cin.get();

    car* Car;

    Car = new car[num];

    for (int i = 0; i < num; i++) {

       cout << "Please enter the make" << endl;

       getline(cin, Car[i].corporation);

       cout << "Please enter the year made" << endl;

       cin >> Car[i].year;

       cin.get();

    }

    cout << "Here is your collection" << endl;

    for (int i = 0; i < num; i++) {

       cout << Car[i].year << " " << Car[i].corporation;

    }

    return 0;

}

Cin.get()

能在程序结束时输入一个字符,让程序停留在运行界面

8.

#include<iostream>

#include<string>

using namespace std;

int main() {

    int c = 0;

    char words[20];

    cout << "Enter words(to stop,type the word done):" << endl;

    while (strcmp("done", words) != 0)

    {

       c++;

       cin >> words;

       cin.get();

    }

    cout << "You entered a total of " << c-1 << " words." << endl;

    return 0;

}

9.

#include<iostream>

#include<string>

using namespace std;

int main() {

    int c = 0;

    string words;

    cout << "Enter words(to stop,type the word done):" << endl;

    while ("done"!= words!= 0)

    {

       c++;

       cin >> words;

       cin.get();

    }

    cout << "You entered a total of " << c-1 << " words." << endl;

    return 0;

}

10.

#include<iostream>

#include<string>

using namespace std;

int main() {

    int row;

    cout << "Enter number of rows: " << endl;

    cin >> row;

    for (int i = 0; i < row; i++) {

       for (int j = 0; j < row - i - 1; j++) {

           cout << ".";

       }

       for (int j = 0; j <= i; j++) {

           cout << "*";

       }

       cout << endl;

    }

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值