2018年11月

#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;
}

#include<iostream>
using namespace std;
struct teacher
{
    char name[20];
    int teachage;
    float pay;
};
int main()
{
    int x, y;
    cout << "请输入教师职工的信息\n";
    struct teacher th[5];               //定义结构体数组
    for (x = 0; x < 5; x++)
    {
        cout << "请输入第" << x+1 << "位教师职工的姓名:";
        cin >> th[x].name;
        cout << "请输入第" << x+1 << "位教师职工的教龄:";
        cin >> th[x].teachage;
        cout << "请输入第" << x+1 << "位教师职工的工资总额:";
        cin >> th[x].pay;
        cout << "\n";
    }
    cout << "这5位教师职工的信息如下:\n";
    cout << "姓名\t" << "教龄\t" << "工资总额\n";
    for (y = 0; y < 5; y++)            //输出结构体数组里的元素
    {
        cout << th[y].name << "\t" << th[y].teachage << "\t" << th[y].pay << "\n";
    }
    system("pause");
    return 0;
}