5.9编程练习

  1. 第一题
    直接上代码:
#include <iostream>
using namespace std;


int main() {

    int a, b;
    cin >> a;
    cin >> b;
    int sum = 0;
    for(int i=a;i <= b;i++)
    {
        sum += i;
    }
    cout << sum;

    return 0;
}

运行结果:

2
9
44
  1. 第二题
#include <iostream>
#include <array>


const int ArSize = 101;
int main() {

    std::array<long double, ArSize> factorials;
    factorials[0] = factorials[1] = 1L;
    for(int i=2;i<ArSize;i++)
        factorials[i] = i * factorials[i - 1];
    for(int i=0;i<ArSize;i++)
        std::cout << i << "! = " << factorials[i] << std::endl;

    return 0;
}

运行结果:

92! = 1.24384e+142
93! = 1.15677e+144
94! = 1.08737e+146
95! = 1.033e+148
96! = 9.91678e+149
97! = 9.61928e+151
98! = 9.42689e+153
99! = 9.33262e+155
100! = 9.33262e+157

数量太多,只列举一些结果。

  1. 第三题
#include <iostream>


int main() {

    int a;
    int sum = 0;
    std::cin >> a;
    while (a != 0)
    {
        sum += a;
        std::cin >> a;
    }
    std::cout << sum;

    return 0;
}

运行结果:

5
6
7
2
0
20
  1. 第四题
#include <iostream>


const int money = 100;
const float Daphne = 0.1;
const float CLeo = 0.05;
int main() {

    float sum_d = 0.0;
    float sum_c = 0.0;
    float more;  // 盈利
    float now = money;  // 当前存款
    int year = 0;  // 年数
    while (sum_c <= sum_d)
    {
        sum_d += money * Daphne;

        more = now * CLeo;
        now += more;
        sum_c += more;
        year += 1;
    }
    std::cout << "year: " << year << std::endl;
    std::cout << "Daphne: " << sum_d << std::endl;
    std::cout << "CLeo: " << sum_c << std::endl;

    return 0;
}

运行结果:

year: 27
Daphne: 270
CLeo: 273.346
  1. 第五题
#include <iostream>
#include <string>


const int year = 12;
int main() {

    int month[year];
    // 使用char* 和 string 两种方法初始化
    const char* money[year] = {
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
    };
    std::string money_str[year] = {
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
    };
    // 基于范围的for循环
    for(int &x: month)
        std::cin >> x;
    for(int i=0;i<year;i++)
    {
        std::cout << money[i] << "销量为:" << month[i] << std::endl;
        std::cout << money_str[i] << "销量为:" << month[i] << std::endl;
    }

    return 0;
}

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
January销量为:1
January销量为:1
February销量为:2
February销量为:2
March销量为:3
March销量为:3
April销量为:4
April销量为:4
May销量为:5
May销量为:5
June销量为:6
June销量为:6
July销量为:7
July销量为:7
August销量为:8
August销量为:8
September销量为:9
September销量为:9
October销量为:10
October销量为:10
November销量为:11
November销量为:11
December销量为:12
December销量为:12
  1. 第六题
#include <iostream>
#include <string>


const int pre_year = 12;
const int all_year = 3;
int main() {

    int month[all_year][pre_year];
    int year[all_year] = {0};
    int all = 0;
    // 使用char* 和 string 两种方法初始化
    const char* money[pre_year] = {
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
    };
    std::string money_str[pre_year] = {
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
    };
    for(int i=0;i<all_year;i++)
    {
        for(int j=0;j<pre_year;j++)
        {
            std::cin >> month[i][j];
            year[i] += month[i][j];
        }
        all += year[i];
    }
    for(int i=0;i<all_year;i++)
    {
        std::cout << "第" << (i+1) << "年销量为:" << year[i] << std::endl;
    }
    std::cout << "总销量为:" << all << std::endl;
    return 0;
}

运行结果:
三年都是一样的值,数太多不放上去了。

9
10
11
121年销量为:782年销量为:783年销量为:78
总销量为:234
  1. 第七题
    由于geline会将换行符当作空字符串,因此在使用getline之前如果有数字或其他的输入将会产生换行符,因此需要使用cin.get()这种不带参数的方法去读取达到过滤的作用。
#include <iostream>
#include <string>
struct car
{
    std::string product;
    int year;
};


int main() {

    int cars;
    std::cout << "How many cars do you wish to catalog?";
    std::cin >> cars;
    std::cin.get();  // 捕捉换行符
    car *all_car = new car [cars];
    for(int i=0;i<cars;i++)
    {
        std::cout << "Car #" << (i+1) << ":" <<std::endl;
        std::cout << "Please enter the make:";
        getline(std::cin, all_car[i].product);
        std::cout << "Please enter the year made:";
        std::cin >> all_car[i].year;
        std::cin.get();  // 捕捉换行符
    }
    std::cout << "Here is your collection:" <<std::endl;
    for(int i=0;i<cars;i++)
    {
        std::cout << all_car[i].year << " " << all_car[i].product << std::endl;
    }
    delete [] all_car;

    return 0;
}

运行结果:

How many cars do you wish to catalog?2
Car #1:
Please enter the make:Hudson Hornet
Please enter the year made:1952
Car #2:
Please enter the make:Kaiser
Please enter the year made:1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
  1. 第八题
    对c风格字符串进行比较使用strcmp,相同返回0,不同返回1
#include <iostream>
#include <cstring>


int main() {

    char word[10];
    int num = 0;
    std::cout << "Enter words (to stop, type the word done):" << std::endl;
    std::cin >> word;
    while (strcmp(word, "done"))
    {
        num += 1;
        std::cin >> word;
    }
    std::cout << "You entered a total of " << num << " words." << std::endl;

    return 0;
}

运行结果:

Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
  1. 第九题
    对于string使用关系表达式比较
#include <iostream>
#include <string>


int main() {

    std::string word;
    int num = 0;
    std::cout << "Enter words (to stop, type the word done):" << std::endl;
    std::cin >> word;
    while (word != "done")
    {
        num += 1;
        std::cin >> word;
    }
    std::cout << "You entered a total of " << num << " words." << std::endl;

    return 0;
}

运行结果:

Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
  1. 第十题
#include <iostream>


int main() {

    int num_star;
    std::cout << "Enter number of rows:";
    std::cin >> num_star;
    for(int i=0;i<num_star;i++)
    {
        for(int j=0;j<(num_star-i-1);j++)
        {
            std::cout << ".";
        }
        for(int k=0;k<(i+1);k++)
        {
            std::cout << "*";
        }
        std::cout << std::endl;
    }

    return 0;
}

运行结果:

Enter number of rows:5
....*
...**
..***
.****
*****
原文地址:https://www.cnblogs.com/ycycn/p/14456838.html