5.9 编程练习
发布日期:2021-05-07 21:13:02 浏览次数:23 分类:精选文章

本文共 4152 字,大约阅读时间需要 13 分钟。

C++编程练习指南

1. 简单的循环结构

int main() {    using namespace std;    int a, b;    cout << "Please enter the first integer:";    cin >> a;    cout << "Please enter the second integer:";    cin >> b;    int s = 0;    while (a <= b) {        s += a;        ++a;    }    cout << "The sum of them is " << s << endl;    return 0;}

这个程序演示了如何使用简单的while循环来计算两个整数的和。当第一个数字超过第二个数字时,循环将自动结束。


2. 浮点数累加

int main() {    using namespace std;    float a;    cout << "Please enter the first number:";    cin >> a;    float s = 0;    while (a != 0) {        s += a;        cout << "The sum of numbers until now: " << s << endl;        cout << "Please enter the next number:";        cin >> a;    }    cout << "The sum of numbers until now: " << s << endl;    return 0;}

这个程序演示了如何使用float类型来累加浮点数。当输入的数值为零时,循环将自动结束。


3. 计算货币增长

int main() {    using namespace std;    const float a_D = 0.1; // 每年的增长率    const float a_C = 0.05; // 每年的利息率    float d = 100; // 目标金额    float c = 100; // 当前金额    int year = 0;    while (c <= d) {        c = (1 + a_C) * c; // 计算利息        d += (1 + a_D) * 100; // 更新目标金额        ++year;    }    cout << "The years are : " << year << endl;    return 0;}

这个程序用于计算达到特定金额所需的年数。每年都会增加一定的利息和本金增长。


4. 销售数据统计

int main() {    using namespace std;    const int Month = 12;    const string month[Month] = {        "January", "February", "March", "April", "May", "June",        "July", "August", "September", "October", "November", "December"    };    int sale[Month] = {0};    int s = 0;    cout << "Please enter the sales of 12 months.\n";    for (int i = 0; i < Month; ++i) {        cout << month[i] << ": ";        cin >> sale[i];        s += sale[i];    }    cout << "The sum of sales are : " << s << endl;    return 0;}

这个程序用于统计12个月的销售数据,并计算总销售额。


5. 多维度销售数据统计

int main() {    using namespace std;    const int Month = 12;    const string month[Month] = {        "January", "February", "March", "April", "May", "June",        "July", "August", "September", "October", "November", "December"    };    int sale[3][Month] = {0};    int sum = 0;    for (int j = 0; j < 3; ++j) {        int s = 0;        cout << j << " Please enter the sales of 12 months.\n";        for (int i = 0; i < Month; ++i) {            cout << month[i] << ": ";            cin >> sale[j][i];            s += sale[j][i];        }        cout << j << " The sum of sales are : " << s << endl;        sum += s;    }    cout << "The sum of sales of the 3 years are : " << sum << endl;    return 0;}

这个程序用于统计多年的销售数据,并分别计算每年的总销售额。


6. 车辆 catalog系统

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

这个程序是一个简单的车辆 catalog系统,允许用户输入多个车辆信息。


7. 字符串处理

int main() {    using namespace std;    cout << "Enter words (to stop, type the word done):\n";    string str;    int i = 0;    while (strcmp(str, "done")) {        cin >> str;        ++i;    }    cout << "You entered a total of " << i << " words.\n";    return 0;}

这个程序用于统计用户输入的单词数量,直到输入“done”为止。


8. 多功能字符串处理

int main() {    using namespace std;    string str;    cout << "Enter words (to stop, type the word done):\n";    int i = 0;    while (str != "done") {        cin >> str;        ++i;    }    cout << "You entered a total of " << i << " words.\n";    return 0;}

这个程序与上一个程序功能相同,但使用了不同的字符串比较方法。


9. 数字矩阵绘制

int main() {    using namespace std;    int num;    cout << "Enter number of rows:\n";    cin >> num;    for (int i = 0; i < num; ++i) {        for (int j = 0; j < num; ++j) {            if ((num - j - 1) > i) {                cout << ".";            } else {                cout << "*";            }        }        cout << endl;    }    return 0;}

这个程序用于绘制一个由星号和点组成的矩阵图形。

上一篇:算法工程师
下一篇:5.8 复习题

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月06日 07时51分33秒