C++考试

第一题:

#include <iostream>

using namespace std;

int main()
{
    const int n=20;
    int a[n];
    int *p[n];
    int i;

    for(i=0;i<n;i++)
        a[i]=i+1;
     //以下为需要写的
    for(i=0;i<n;i++)
    {
    	p[i]=&a[i];
        *p[i]+=10;
    }

    for(i=0;i<n;i++)
    {
        if(i%10==0)
            cout<<endl;
        cout<<*p[i]<<" ";
    }

    return 0;
}

第二题:

#include <iostream>
#include <string>
using namespace std;

string str[5],k;

void Sort(string *p)
{
    int i,j;
    for(i=0;i<5;i++)
    {
        for(j=4;j>=i+1;j--)
        {
            if(p[j-1]>p[j])
            {
                k=p[j];
                p[j]=p[j-1];
                p[j-1]=k;
            }
        }
    }
}

int main()
{
    int i;
    for(i=0;i<5;i++)
        cin>>str[i];
    Sort(str);
    for(i=0;i<5;i++)
        cout<<str[i]<<endl;
    return 0;
}

第三题:

#include <iostream>
#include <string>

using namespace std;

#define PAI 3.14

class Ring
{
public:
    Ring(double r1,double r2):radius1(r1),radius2(r2){}
    double get_area();
private:
    double radius1;
    double radius2;
};

double Ring::get_area()
{
    return PAI*(radius1-radius2)*(radius1-radius2);
}

int main()
{
    int i;
    double r1,r2;
    for(i=0;i<3;i++)
    {
        cin>>r1>>r2;
        Ring c(r1,r2);
        cout<<c.get_area()<<endl;
    }
    return 0;
}

第四题:

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

class String
{
public:
    String(const char *str = NULL);             // 普通构造函数
    String(const String &other);                // 拷贝构造函数
    ~String(void);                             // 析构函数
    String & operate =(const String &other);    // 赋值函数
private:
    char *m_data;                               // 用于保存字符串
};

// String的普通构造函数

String::String(const char *str)
{
    if(str==NULL)
    {
        m_data = new char[1];
        *m_data = '\0';
    }
    else
    {
        int length = strlen(str);
        m_data = new char[length+1];
        strcpy(m_data, str);
    }
}

// String的析构函数

String::~String(void)
{
    delete [] m_data;
}

// 拷贝构造函数

String::String(const String &other)
{
    int length = strlen(other.m_data);
    m_data = new char[length+1];
    strcpy(m_data, other.m_data);
}

// 赋值函数

String & String::operate =(const String &other)
{
    if(this == &other)
        return *this;

    delete [] m_data;

    int length = strlen(other.m_data);
    m_data = new char[length+1];
    strcpy(m_data, other.m_data);

    return *this;

}
int main()
{
    return 0;
}

第五题:

#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    string name;//姓名
    string sno;//学号
    double math;
    double chinese;
    double english;
    double total;
};

void max(Student *p)
{
    int i,maxTotal=0,k;
    for(i=0;i<10;i++)
    {
        if(p[i].total>maxTotal)
        {
            k=i;
            maxTotal=p[i].total;
        }
    }
    cout<<p[k].sno<<endl;
}

int main()
{
    int i=0;
    Student stu[10];

    for(i=0;i<10;i++)
    {
        cin>>stu[i].name>>stu[i].sno>>stu[i].math
        >>stu[i].chinese>>stu[i].english>>stu[i].total;
    }
    max(stu);

    return 0;
}

第六题:

#include <iostream>
#include <string>
using namespace std;

class Date;
class Weather
{
public:
    Weather(string a,string b);
    friend void display(const Date &a,const Weather &b);
private:
    string wea;
    string tem;
};

Weather::Weather(string a,string b):wea(a),tem(b){}

class Date
{
public:
    Date(int a,int b,int c);
    friend void display(const Date &a,const Weather &b);
private:
    int year;
    int month;
    int day;
};

Date::Date(int a,int b,int c):year(a),month(b),day(c){}

void display(const Date &a,const Weather &b)
{
    cout<<b.tem<<" "<<b.wea<<endl;
    cout<<a.year<<" "<<a.month<<" "<<a.day<<endl;
}

int main()
{
    Weather t("晴朗","45度");
    Date r(2019,4,24);

    display(r,t);

    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务