【C++】课后题作业 & 几种遍历字符串数组的方式

p120 7
ref:https://blog.csdn.net/weixin_42597789/article/details/123329323

#include<iostream>
using namespace std;

class Student{
public:
    Student(int n, float s):num(n), score(s){}
    void  change(int n, float s)const {
        num = n;
        score = s;
    }
private:
   mutable  int num;
   mutable  float score;
};

int main(){
    const Student s1(10,78.5);
    s1.change(100,100.1);
    return 1;
}

改成指针的方式:

    const Student s1(10,78.5);
    s1.change(100,100.1);
    Student stud(101,78.5);
    Student *p = &stud;
    p->change(1,2);
// 在界面上输入数字的月份(1-12),输出对应月份的英文单词的全称,如果输入的数字不合法,给出提示。
#include<iostream>
#include<string>
#include <cstring>
using namespace std;

int main(){
    int month;
    cout << "请输入月份:"; cin >> month;
    cout << "month=" << month << endl;
    if (month == 1){
        cout << "January \n";
    }else if(month == 2){
        cout << "February \n";
    }else if(month == 3){
        cout << "March \n";
    }else if(month == 4){
        cout << "April \n";
    }else if(month == 5){
        cout << "May \n";
    }else if(month == 6){
        cout << "June \n";
    }else if(month == 7){
        cout << "July \n";
    }else if(month == 8){
        cout << " August\n";
    }else if(month == 9){
        cout << " September\n";
    }else if (month == 10)
        cout << "October\n";
    else if (month == 11)
        cout << "November\n";
    else if (month == 12)
        cout << "December\n";
    else
        cout << "输入的数字不正确。\n";

    switch (month)
    {
    case 1:
        cout << "January \n";
        break;
    case 2:
        cout << "February \n";
        break;
    case 3:
        cout << "March \n";
        break;
    case 4:
        cout << "April \n";
        break;
    case 5:
        cout << "May \n";
        break;
    case 6:
        cout << "June \n";
        break;
    case 7:
        cout << "July \n";
        break;
    case 8:
        cout << "August \n";
        break;
    case 9:
        cout << "October \n";
        break;
    case 10:
        cout << "September\n";
        break;
    case 11:
        cout << "November \n";
        break;
    case 12:
        cout << "December \n";
        break;
    
    default:
        cout << "输入的数字不正确。\n";
        break;
    }
    // 方式一:必须指定第二维的大小,且应大于等于数组最长字符串的长度
    char montharr[][10] = { "January","February","March","April","May",
        "June","July","August","September","October","November","December" }; 
    string montharr1[12] = { "January","February","March","April","May",
        "June","July","August","September","October","November","December" }; 
    if (month>=1 && month<=12)
        cout << "======111" << montharr[month-1] << endl;
    else
        cout << "输入的数字不正确。\n";

    if (month>=1 && month<=12){
        cout << "======222" << montharr1[month-1] << endl;
    }else{
        cout << "输入的数字不正确。\n";
    }
    char *str_arr[] = { "January","February","March","April","May",
        "June","July","August","September","October","November","December" }; 


    int size = sizeof(str_arr)/sizeof(str_arr[0]); // 获取数组大小

	printf("000000000-----------------%s\n", *(str_arr+month-1));
	
        
    return 1;
}

几种遍历字符串数组的方式!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值