CellPhone 发布的文章

#include<iostream>
using namespace std;
enum color { White, Black, Red, Brown, Green };
class Automobile
{
private:
    char *model;
    color c;
    int price;
    double mileage;
    static int countC;
public:
    Automobile(const char *model, color c, int price, double mileage);
    ~Automobile();
    void Assign(Automobile & p);
    void display();
    static int getCount();
    friend bool Mileage(Automobile &p);
};
Automobile::Automobile(const char *model, color c, int price, double mileage)
{
    this->model = new char[strlen(model) + 1];
    strcpy_s(this->model, strlen(model) + 1, model);
    this->c = c;
    this->price = price;
    this->mileage = mileage;
    ++countC;
    cout << "构造函数:" << model << "\n";
}
Automobile::~Automobile()
{
    cout << "析构函数:" << model << "\n";
    delete model;
    --countC;
}
void Automobile::Assign(Automobile & p)
{
    this->model = new char[strlen(p.model) + 1];
    strcpy_s(this->model, strlen(p.model) + 1, p.model);
    this->c = p.c;
    this->price = p.price;
    this->mileage = p.mileage;
}
int Automobile::getCount()
{
    return countC;
}
void Automobile::display()
{
    const char *cl=0;
    switch (c)
    {
    case 0:cl = "White"; break;
    case 1:cl = "Black"; break;
    case 2:cl = "Red"; break;
    case 3:cl = "Brown"; break;
    case 4:cl = "Green"; break;
    }
    cout << "\t该汽车型号:" << model << "\n";
    cout << "\t颜色:" << cl << "\n";
    cout << "\t价格:" << price << "\n";
    cout << "\t里程:" << mileage << "\n";
}
bool Mileage(Automobile &p)
{
    if (p.mileage >= 1000)
        return true;
    else
        return false;
}
int Automobile::countC = 0;
void main()
{
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    Automobile c1[3] = { Automobile("A1",White,100000,371.5),
                                   Automobile("A3",Red,150000,951.4),
                                      Automobile("A5",Black,200000,1024.1) };
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    Automobile *c2 = &c1[1];
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    Automobile *c3 = new Automobile("A7", Green, 25, 747);
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    c3->Assign(c1[2]);
    c3->display();
    cout <<"如果里程大于1000为1(真),否则为0(假):"<< Mileage(*c3) << "\n";
    delete c3;
    system("pause");
}

#include<iostream>
using namespace std;
class Rectangle 
{
private:
    double length;
    double width;
public:
    Rectangle(double l, double w)
    {
        this->length = l;
        this->width = w;
    }
    Rectangle(Rectangle & r)
    {
        cout << "拷贝构造函数:";
        length = r.length;
        width = r.width;
    }
    ~Rectangle() 
    {
        cout << "析构函数" << "\n";
    }
    void get();
    void input();
    double RArea();
    double RCircumference();
};
void Rectangle::input()
{
    double l, w;
    cout << "请分别输入矩形的长和宽:";
    cin >> l >> w;
    length = l;
    width = w;
}
void Rectangle::get()
{
    cout << "长为" << length << "\t宽为" << width << "的矩形\n";
}
double Rectangle::RArea()
{
    double area = 0;
    area = length * width;
    return area;
}
double Rectangle::RCircumference()
{
    double Circumference = 0;
    Circumference = 2 * (length + width);
    return Circumference;
}
void main()
{
    Rectangle r1(6.7, 8.6);
    Rectangle r2(12.13,13.14);
    r1.get();
    cout << "面积为:" << r1.RArea() << "\n";
    cout << "周长为:" << r1.RCircumference() << "\n";
    r2.input();
    r2.get();
    cout << "面积为:" << r2.RArea() << "\n";
    cout << "周长为:" << r2.RCircumference() << "\n";
    system("pause");
}

#include<iostream>
using namespace std;
class CDate
{
private:
    int Y, M, D;
public:
    CDate()
    {
        Y = 2018, M = 11, D = 21;
    }
    CDate(int y, int m, int d)
    {
        Y = y, M = m, D = d;
    }
    ~CDate()
    {
        cout << "析构函数\n";
    }
    void SetTime(int y, int m, int d);
    void print();
};

void CDate::SetTime(int y, int m, int d) //设置时间
{
    Y = y, M = m, D = d;
}
void CDate::print()//输出时间
{
    cout << Y << "年-" << M << "月-" << D << "日\n";
}
void main()
{
    CDate c1, c2(2000, 1, 1);
    c1.print();
    c2.SetTime(2018, 11, 11);
    c2.print();
    system("pause");
}

#include<iostream>
using namespace std;
//步骤一:
//定义结构体 
struct Student
{
    char name[20];  //姓名
    int num;   //学号
    float score;   //成绩
};
//步骤二:
//定义查找函数:
void Search(Student s1[], char name[20], int n)//查找该学生信息,n表示学生个数
{
    int flag = 0;  //flag=0表示未找到,flag=1表示找到该学生
    for (int i = 0; i < n; i++)
        if (strcmp(name, s1[i].name) == 0)  //用strcmp函数对字符串比较
        {
            flag = 1;// 表示找到该学生
            cout << "姓名:" << s1[i].name << "\t" << "学号:" << s1[i].num << "\t" << "分数:" << s1[i].score << "\n"; //输出该学生信息, .运算符输出
        }
    if (flag == 0)
    {
        cout << "不存在该学生信息" << endl;//不存在该学生信息
    }
}
int main()
{
    struct Student s1[] = {
        {"zhangsan",1,90},
        {"lisi",2,85},
        {"xiaoming",3,80},
        {"xiaohong",4,75},
        {"xiaogang",5,70}
    };   //定义Student结构体数组s1并初始化
    int length;   //学生个数
    char name[20];//存储要查找的学生姓名
    length = sizeof(s1) / sizeof(Student);
    cout<<"请输入要查找的学生姓名:"<< endl;
    cin >> name;        //输入学生姓名
    for (int n = 0; n < 5; n++)
    {
        Search(&s1[n], name, length);//调用查找函数Search输出学生信息
        break;//结束当前正在执行的for循环,防止重复输出函数
    }
    system("pause");
    return 0;
}

/*
程序名:指针
程序功能:指针的定义与相关操作,引用的定义与相关操作,结构体的定义和使用
程序目的:通过本程序理解并掌握指针引用与结构体*/
#include<iostream>
using namespace std;
int main()
{
    //指针数组的定义与使用
    char * data[3] = { "aaa","bbb","ccc" };//定义一个字符型二位指针数组(二位数组定义时可省略第一维的维数),并初始化元素
    cout << "&data[0][0]:" << &data[0][0] << " data[0]:" << data[0] << endl;//运用&运算符取data[0][0]的地址,用下标运算符取data[0]的地址
    cout << "&data[1][0]:" << &data[1][0] << " data[1]:" << data[1] << endl;//运用&运算符取data[1][0]的地址,用下标运算符取data[1]的地址
    cout << "&data[2][0]:" << &data[2][0] << " data[2]:" << data[2] << endl;// 运用&运算符取data[2][0]的地址,用下标运算符取data[2]的地址
    //数组指针的定义与使用
    int arr[4] = { 1,2,3,4 }; //定义一个数组并初始化全部元素
    int(*a_p)[4] = &arr; // 定义了一个行指针,并指向一个行数为4的arr数组首地址
    cout << "&arr:" << &arr << " arr:" << arr << endl;// 运用&运算符取arr首地址的存放地址,取arr的首地址
    cout << "a_p:" << a_p << " *a_p[0]:" << *a_p[0] << endl;// 取a_p的首地址,用指针运算符取指针指向的a_p数组第0列的地址

    //指针常量与常量指针
    int p = 0;
    const int pi = 3.14;  // 定义一个常量pi为3.14
    int * const cp = &p;  //定义一个指针常量cp并初始化(指向变量p)
    //cp=&p;//错误,指针cp的值不能改变
    const int *ct = &pi;  //定义一个常量指针ct并初始化(指向变量pi)
    //*ct=10;//错误,指针ct所指对象的值不能改变
    const int *const pcc = &pi; //定义一个常量指针pcc并初始化(指向变量pi)
    return 0;
}