c++day11

作者: __method__ | 来源:发表于2021-02-21 21:26 被阅读0次

结构体

数组只能存一种类型, 结构体可以存不同类型

#include <iostream>
#include <cstring>
using namespace std;
// 定义了一个结构体
struct student{
    int num;
    char name[20];
    float score[4];
};
// 结构体可以作为函数的参数
void print(student stud){
    cout << "num = " << stud.num<< endl;
    cout << "name = " << stud.name<< endl;
    cout << "math score = " << stud.score[0]<< endl;
    cout << "chinese score = " << stud.score[1]<< endl;
    cout << "music score = " << stud.score[2]<< endl;
    cout << "english score = " << stud.score[3]<< endl;
}
int main()
{
    student stud;
    stud.num = 2033;
//    strcpy(stud.name, "Mrs dong");
    cin>>stud.name;
    stud.score[0] = 100;
    stud.score[1] = 99;
    stud.score[2] = 98;
    stud.score[3] = 98;
    print(stud);

}

结构体数组

#include <iostream>
#include <cstring>
// 结构数组
using namespace std;
// 定义了一个结构体
struct student{
    int num;
    char name[20];
    float score;
};
// 结构体输入 函数  返回值是一个结构体
student Input( )
{
    student stud;

    cout<<"please input number name and score:";
    cin>>stud.num>>stud.name>>stud.score;
    return stud;
}

void Output(student stud)
{
    cout<<"number=  "<<stud.num<<'\n'<<"name=  "<<stud.name<<'\n'
        <<"score=  "<<stud.score<<endl;
}
// 结构体可以作为函数的参数

int main()
{
    student studs[3];
    for (int i = 0; i < 3; ++i) {
        studs[i] = Input();
    }
    for (int i = 0; i < 3 ; ++i) {
        Output(studs[i]);
    }


}

结构体数组作为函数的参数

#include <iostream>
#include <cstring>
// 结构数组作为函数的参数
using namespace std;
// 定义了一个结构体
struct stud{
    int num;
    char name[20];
    int age;
    char sex;
    int score;
};
// 结构数组作为函数的参数
float average(stud studs[], int n){
    // 求平均成绩
    float  avg = 0;
    for (int i = 0; i < n; ++i) {
       avg += studs[i].score;
    }
    avg = avg/n;
    return avg;
}

int main()
{
    stud studs[4]={ {020110101, "Wu", 19, 'M', 80},
                    {020110102, "Li", 18, 'F', 95},
                    {020110103, "Zhang", 18, 'F', 78},
                    {020110104, "Zhao", 20, 'M', 88} };
    float  avg = average(studs, 4);
    cout<< "avg = "<<avg<<endl;


}

共同体/联合体

占用字节数不同的类型变量, 从同一地址开始, 某一时刻只有一个变量有效

#include <iostream>
// 取出一个整数的4个字节
using namespace std;

int main()
{
    // 节省空间  特殊应用
    union  {
        int i;
        char c[4];// 整数占4个字节
    } a;
    cout<< "input a number";
    cin>>a.i;
    cout<<a.i +  "int number four bytes is";
    for (int k = 3; k >=0 ; k--) {
        cout<<(int) a.c[k]<<"\t";
    }

}

枚举

#include <iostream>
#include <iomanip>
// 枚举
using namespace std;
// i, j , k 是不同颜色的球  查看共有多少种组合  i != j != k
enum color {red, yellow, blue, white, black, purple};
// 枚举类型作为函数的参数
void print(enum color c){
    switch (c) {
        case  red:    cout<<setw(10)<<"red";     break;
        case  yellow: cout<<setw(10)<<"yellow";  break;
        case  blue:   cout<<setw(10)<<"blue";    break;
        case  white:  cout<<setw(10)<<"white";   break;
        case  black:  cout<<setw(10)<<"black";   break;
        case  purple: cout<<setw(10)<<"purple";  break;
        default: break;
    }
}

int main()
{
    int count = 0;
     color i, j , k; // i, j , k是枚举类型
     // red purple 分别是头和尾部
     for(i = red; i<=purple; i = color(int(i) + 1)){
         for(j = red; j<=purple; j = color(int(j) + 1)){
             if(i!=j){
                 for(k = red; k<=purple; k = color(int(k) + 1)){
                     if ((k!=i)&&(k!=j)){
                         cout<< ++count<<"\t";
                         print(i);
                         print(j);
                         print(k);
                         cout<<endl;

                     }
                 }
             }
         }

     }

}

相关文章

  • c++day11

    结构体 数组只能存一种类型, 结构体可以存不同类型 结构体数组 结构体数组作为函数的参数 共同体/联合体 占用字节...

网友评论

      本文标题:c++day11

      本文链接:https://www.haomeiwen.com/subject/efxcfltx.html