C++ Primer Plus(第6版) 中文版 第五章编程练习

测试时分开测试

#include <iostream>
#include <array>
#include <cstring>
using namespace std;

int main() {
//    //1
//    int i, j;
//    int sum = 0;
//    cin >> i >> j; // 读取两个整数

//    // 使用for循环遍历从i到j(包括i和j)的所有整数
//    for(int k = i; k <= j; k++) { // 使用新变量k进行迭代
//        sum += k; // 将每个数字加到sum上
//    }

//    cout << sum; // 输出总和

//    //2
//    array<long double, 101> factorials; // 使用long double以支持大数的阶乘计算
//    factorials.at(0) = factorials.at(1) = 1; // 初始化阶乘的基础情况

//    // 计算阶乘
//    for(int i = 2; i < 101; i++) {
//        factorials.at(i) = i * factorials.at(i - 1);
//    }

//    // 打印阶乘结果
//    for(int i = 0; i < 101; i++) {
//        cout << i << "! = " << factorials.at(i) << endl;
//    }
//    //3
//    int sum = 0, num;

//    cout << "请输入一系列整数(输入0时程序结束):" << endl;

//    while (cin >> num) {
//        if (num == 0) {
//            cout << "程序结束。" << endl;
//            return 0; // 或者 break; 如果你不想立即结束整个程序,但想跳出循环
//        }

//        sum += num; // sum = sum + num 的简写形式
//        cout << "到目前为止输入总和为:" << sum << endl;
//    }
//    //4
//    int years = 0;
//    float Daphne= 100;
//    float Cleo = 100;
//    while(Daphne>=Cleo)
//    {
//        Daphne = Daphne + 100*0.1;
//        Cleo = Cleo+0.05*Cleo;
//        years++;
//    }
//    cout<<years<<"年后Cleo超过Daphne"<<endl;
//    cout<<"Daphne总存款:"<< Daphne<<endl;
//    cout<<"Cleo总存款:"<< Cleo<<endl;
//    //5
//    string month[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
//    int monthNumer[12]= {0};
//    int sum = 0;
//    cout<<"请输入十二个月的销售数量:"<<endl;
//    for(int i =0;i<12;i++)
//    {
//        cin>>monthNumer[i];
//        cout<<month[i]<<"销售本数:"<<monthNumer[i]<<endl;
//        sum+=monthNumer[i];
//    }
//    cout<<"本年度销售总数:"<<sum;

    //6
//    int monthNumer[3][12] = {0};
//    string month[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
//    int sum = 0;

//    cout << "请输入三年每个月的销售数量:" << endl;

//    for (int i = 0; i < 3; i++) {
//        int yearsNumber = 0; // 每年的销售总数初始化为0

//        for (int j = 0; j < 12; j++) {
//            cin >> monthNumer[i][j];
//            cout << "第 " << i + 1 << " 年 " << month[j] << " 销售本数:" << monthNumer[i][j] << endl;
//            sum += monthNumer[i][j]; // 更新总销售数量
//            yearsNumber += monthNumer[i][j]; // 更新当前年份的销售总数
//        }

//        cout << "第 " << i + 1 << " 年销售总数:" << yearsNumber << endl;
//    }

//    cout << "三年销售总数:" << sum;

 //   //7
//    struct car
//    {
//        string manufacturer;
//        int year;
//    };
//    int number;
//    cout<<"How many cars do you wish to catalog?";
//    cin>>number;
//    cin.get();
//    car *cars = new car[number];
//    for(int i = 0;i<number;i++)
//    {
//         cout<<"Car #"<<i+1<<endl;
//         cout<<"Please enter the make:";
//         getline(cin,cars[i].manufacturer);
//         cout<<"Please enter the year made:";
//         cin>>cars[i].year;
//         cin.get();

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

//    for(int i = 0;i<number;i++)
//    {
//        cout<<cars[i].year<<" "<< cars[i].manufacturer<<endl;

//    }
//    //8
//    char arr[1024] = {0};
//    int count = 0;
//    cout<<"Enter words (to stop,type the word done):"<<endl;
//    while (strcmp(arr,"done")) {

//        cin>>arr;
//        count++;
//    }
//    cout<<"You entered a total of "<<count-1 <<" words";


//    //9
//    string str;
//    int count = 0;
//    cout<<"Enter words (to stop,type the word done):"<<endl;
//    while (str!="done") {

//        cin>>str;
//        count++;
//    }
//    cout<<"You entered a total of "<<count-1 <<" words";
    //10
    
    int count;
    cout<<"Enter number of rows: ";
    cin>>count;
    //第五章暂时未学分支语句,这里使用for循环完成
    for(int j = 1;j<=count;j++)
    {

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

            cout<<".";

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

            cout<<"*";

        }
        cout<<endl;
    }
/*    
    //使用ifelse
    int count;
    cout << "Enter number of rows: ";
    cin >> count;

    for (int j = 1; j <= count; j++) {
        for (int i = 0; i < count; i++) {
            if (i < count - j) {
                cout << ".";
            } else {
                cout << "*";
            }
        }
        cout << endl;
    }
*/
     return 0;

}

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值