C++ day2 (思维导图)

 作业:

        定义一个Student结构体,里面的成员有公有成员name、age,私有成员:score,从堆区连续分配3个结构体大小的空间,从键盘上输入3个学生的信息,分别存放到对应的位置上,按成绩的升序排序后输出三名学生的信息。

#include <iostream>

using namespace std;
#define N 5
struct student
{
    string name;
    int age;
    void show()
    {
        cout<<name<<" ";
        cout<<age<<" ";
        cout<<m.res()<<endl;
    }
    void set_()
    {
        cout <<"输入学生的姓名";
        cin>>name;
        cout <<"输入学生的年龄";
        cin>>age;
        cout <<"输入该学生成绩";
        m.set_score();
    }
    struct mm
    {
    private:
        float score;
    public:
        float res()
        {
            return score;
        }
        void set_score()
        {
            cin>>score;
        }
    }m;
};

void sort(student mem[])
{
    student temp;
    int count=0;
    for(int i=1;i<N;i++)
    {
        count = 0;
        for(int j=0;j<N-i;j++)
        {
            if(mem[j].m.res()>mem[j+1].m.res())
            {
                temp=mem[j];
                mem[j]=mem[j+1];
                mem[j+1]=temp;
                count++;
            }
        }
        if(count==0) break;
    }

}


int main()
{
    cout << "Hello World!" << endl;
    student *member=new student[N];
    for(int i=0;i<N;i++)
    {
        cout <<"学生【"<<i<<"】"<<endl;

        member[i].set_();
    }
    for(int i=0;i<N;i++)
    {
        cout <<"学生【"<<i<<"】";
        member[i].show();
    }

    sort(member);
    cout<<"按成绩排序后>>>>"<<endl;

    for(int i=0;i<N;i++)
    {
        cout <<"学生【"<<i<<"】";
        member[i].show();
    }

    return 0;
}

 1:引用refence

#include <iostream>

using namespace std;
int p;
int fun1()
{
    return p;
}
int *fun2()
{
    return &p;
}
int &fun3()
{
    return p;
}


int main()
{
    cout << "Hello World!" << endl;

    //fun1()=20;             //鏅€氬嚱鏁颁笉鑳借祴鍊
    //fun2()=30;             //鎸囬拡鍑芥暟涓嶈兘璧嬪€
    fun3()=40;
    cout <<p<< endl;


    return 0;
}
#include <iostream>

using namespace std;
#define N 10
void sort(int (&a)[N])
{
    int temp;
    int count;
    for( int i=1;i<N;i++ )
    {
        count=0;
        for(int j=0;j<N-i;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                count++;
            }
        }
        if(count ==0)
            break;
    }
}

int main()
{
    int a[N]={4,45,1,4,54,32,54,45,7,99};
    for(int i=0;i<N;i++)
    cout << a[i] << " ";
    cout<<endl;
    int (&res)[N]=a;
    sort(res);
    for(int i=0;i<N;i++)
    cout << a[i] << " ";
    cout<<endl;


    cout << "Hello World!" << endl;
    return 0;
}

2、new/delete


#include <iostream>
#define N 5
using namespace std;

void sort(int *&a)
{
    int temp;
    int count;
    for( int i=1;i<N;i++ )
    {
        count=0;
        for(int j=0;j<N-i;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                count++;
            }
        }
        if(count == 0)
            break;
    }
}

int main()
{
    cout << "Hello World!" << endl;


    int *p1=new int;


    cout << "p1  = "<< p1 << endl;
    cout << "*p1 = " << *p1 << endl;

    int *p2 = new int(90);

    cout << "****************" << endl;
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    delete(p2);
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    int *ptr=new int[N];
    for(int i=0;i<N;i++)
    {
        cin>>ptr[i];
    }
    for(int i=0;i<N;i++)
    {
        cout <<"ptr["<<i<<"] = "<< ptr[i] << " ";
    }
    cout<<endl;



    sort(ptr);
    for(int i=0;i<N;i++)
    {
        cout <<"ptr["<<i<<"] = "<< ptr[i] << " ";
    }
    cout<<endl;

    delete []ptr;

    return 0;
}

3、overload


#include <iostream>
#define N 5
using namespace std;

void sort(int *&a)
{
    int temp;
    int count;
    for( int i=1;i<N;i++ )
    {
        count=0;
        for(int j=0;j<N-i;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                count++;
            }
        }
        if(count == 0)
            break;
    }
}

int main()
{
    cout << "Hello World!" << endl;


    int *p1=new int;


    cout << "p1  = "<< p1 << endl;
    cout << "*p1 = " << *p1 << endl;

    int *p2 = new int(90);

    cout << "****************" << endl;
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    delete(p2);
    cout << "p2  = " << p2<< endl;
    cout << "*p2 = "<< *p2 << endl;

    cout << "****************" << endl;
    int *ptr=new int[N];
    for(int i=0;i<N;i++)
    {
        cin>>ptr[i];
    }
    for(int i=0;i<N;i++)
    {
        cout <<"ptr["<<i<<"] = "<< ptr[i] << " ";
    }
    cout<<endl;



    sort(ptr);
    for(int i=0;i<N;i++)
    {
        cout <<"ptr["<<i<<"] = "<< ptr[i] << " ";
    }
    cout<<endl;

    delete []ptr;

    return 0;
}

4、struct

#include <iostream>

using namespace std;


struct NUM
{
public:         //不写默认为public,可以自己定义为private
    int age;
    string name;
    struct N
    {
    private:   //定义为了private
        int fat=0;
    public:   //需要定义外部接口来访问fat
        void show()
        {
            cout<<fat<<endl;
        }
        void _set()
        {
            cin>>fat;
        }
    }n;
    void  show();

};
void NUM::show()
{
    cout<<age<<endl;
    cout<<name<<endl;
}

int main()
{
    cout << "Hello World!" << endl;
    NUM p;
    //p.age=19;
    p.name="张三张三张三张三张三";
    //p.n.fat=17;
    p.show();
    p.n._set();
    p.n.show();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值