xj自用hahahahhah

实验1-1

#include <iostream>
#include <iomanip>
using namespace std;
class Date
{public:
    Date(){
        year=0;month=0;day=0;
    }
    void set_date(void);
    void show_date(void);
 private:
    int year;
    int month;
    int day;
}; 

void Date::set_date(void)          
{   cin>>year;
    cin>>month;
    cin>>day;
}

void Date::show_date(void)         
{   cout<<setw(4)<<right<<setfill('0')<<year<<"-"<<setw(2)<<right<<setfill('0')<<month<<"-"<<setw(2)<<right<<setfill('0')<<day<<endl; }

int main()
{    Date d;
    d.set_date(); 
    d.show_date(); 
    return 0;  
}

实验1-2

#include <iostream>
#include <iomanip>
using namespace std;
class Date
{public:
    Date(){
        year=0;month=0;day=0;
    }
    void set_date(void)
    {
        cin>>year;
        cin>>month;
        cin>>day;
    }
    void show_date(void)
    {
       cout<<setw(4)<<right<<setfill('0')<<year<<"-"<<setw(2)<<right<<setfill('0')<<month<<"-"<<setw(2)<<right<<setfill('0')<<day<<endl;
    }
    void pd(){
        if(year>0 && month>0 && month<13 && day>0 && day<32){
            if(month>=8 && month%2==0 && day<=31){
                show_date();
            }else if(month>=8 && month%2==1 && day<=30){
                show_date();
            }else if(month==2 && day<=28){
                show_date();
            }else if(month<=7 && month%2==0 && day<=30 && month !=2){
                show_date();
            }else if(month<=7 && month%2==1 && day<=31 && month !=2){
                show_date();
            }else{
                cout<<"输入有错,请重新输入!"<<endl;
            }
        }else{
            cout<<"输入有错,请重新输入!"<<endl;
        }
    }
 private:
    int year;
    int month;
    int day;
}; 

int main()
{    Date d;
    d.set_date();
    d.pd();
    return 0;  
}

实验1-3

#include <iostream>
#include "teacher.cpp"  
using namespace std;
int main ()
{    Teacher tea1,tea2;              
    tea1.set_info(); 
    tea2.set_info();
    tea1.show();  
    tea2.show();           
    return 0; 

#include "teacher.h" 
#include <iostream>
#include <iomanip>
using namespace std;
void Teacher::set_info(){
            cin>>id;
            cin>>name;
            cin>>course;
            cin>>location;
        }
void Teacher::show(){
    if(id>0 && id<50000 && location>='a' && location <='z'){
        cout<<"id:"<<setw(5)<<right<<setfill('0')<<id<<", "<<"name:"<<name<<", "<<"course:"<<course<<", "<<"location:"<<location<<endl;
    }else{
        cout<<"输入有错,请重新输入!"<<endl;
    }
}

#include <iostream>
using namespace std;
class Teacher{
    public:

        void set_info();
        void show();
    private:    
        int id;
        string name;
        string course;
        char location;
};
实验1-4

#include "ellipsoid.cpp"
using namespace std;
int main()
{Ellipsoid ell1,ell2,ell3;
 ell1.get_value();    //输入椭球的长宽高
 ell2.get_value(); 
 ell3.get_value();
 ell1.volume();        //计算椭球的体积
 ell2.volume();
 ell3.volume();
 ell1.display();    //输出椭球的体积
 ell2.display();
 ell3.display();
 return 0;
}

#include <iostream>
using namespace std;
class Ellipsoid {
    public:
        void get_value();
        void volume();
        void display();
    private:
        int x;
        int y;
        int z;
        double s;
};

#include "ellipsoid.h"
#include <iostream>
#include <iomanip>
using namespace std;
void Ellipsoid::get_value(){//输入 
    cin>>x;
    cin>>y;
    cin>>z;
}

void Ellipsoid::volume(){//计算 
    s=4/3*3.14*x*y*z;
}

void Ellipsoid::display(){//输出 
    if(x>0 && y>0 && z>0){
        cout<<fixed<<setprecision(2)<<s<<endl;
    }else {
        cout<<"输入有错,请重新输入!"<<endl;
    }
}

实验2-1

#include <iostream>
using namespace std;
class Wallet{
    public:
        void set(){
            cin>>a>>b>>c;
        }
        
        Wallet operator+(Wallet &w2);
        Wallet operator-(Wallet &w2);
        Wallet operator*(Wallet &w2);
        Wallet operator/(Wallet &w2);

        void show(){
            cout<<"("<<a<<","<<b<<","<<c<<")" <<endl;
        }
        
    private:
        double a;
        double b;
        double c;
    
};

Wallet Wallet::operator+(Wallet &w2){
    Wallet w;
    w.a=a+w2.a;
    w.b=b+w2.b;
    w.c=c+w2.c;
    return w; 
}
Wallet Wallet::operator-(Wallet &w2){
    Wallet w;
    w.a=a-w2.a;
    w.b=b-w2.b;
    w.c=c-w2.c;
    return w; 
}
Wallet Wallet::operator*(Wallet &w2){
    Wallet w;
    w.a=a*w2.a;
    w.b=b*w2.b;
    w.c=c*w2.c;
    return w; 
}
Wallet Wallet::operator/(Wallet &w2){
    Wallet w;
    w.a=a/w2.a;
    w.b=b/w2.b;
    w.c=c/w2.c;
    return w; 
}
int main(){
    Wallet w1,w2,w3,w4,w5,w6;
    w1.set();
    w2.set();
    w3=w1+w2;
    w4=w1-w2;
    w5=w1*w2;
    w6=w1/w2;
    cout<< "w1+w2 " <<w3.show() ;
    cout<< "w1-w2 " ; w4.show() ;
    cout<< "w1*w2 " ; w5.show() ;
    cout<< "w1/w2 " ; w6.show() ;
}
实验2-2

#include <iostream>
using namespace std;
class Wallet{
    public:
        void input(){
            cin>>a>>b>>c;
        }
        Wallet operator-(Wallet &w2);
        
        friend Wallet operator-(float &i,Wallet &w2);
        friend Wallet operator-(Wallet &w2,float &i);
        
        void display(){
            cout<<"("<<a<<","<<b<<","<<c<<")" <<endl;
        }
    private:
        float a;
        float b;
        float c;
    
};


Wallet operator-(float &i,Wallet &w2){
    Wallet  w;
    w.a=i-w2.a;
    w.b=w2.b;
    w.c=w2.c;
    return w; 
}

Wallet Wallet::operator-(Wallet &w2){
    Wallet w;
    w.a=a-w2.a;
    w.b=b-w2.b;
    w.c=c-w2.c;
    return w; 
}

Wallet operator-(Wallet &w2,float &i){
    Wallet  w;
    w.a=w2.a-i;
    w.b=w2.b;
    w.c=w2.c;
    return w; 
}

int main()
{Wallet w1,w2,w3;
 float rmb;
 w1.input();
 w2.input();
 cin>>rmb;
 w3=w1-w2;
 cout<<"w1-w2=";
 w3.display();
 w3=rmb-w1;
 cout<<rmb<<"-w1=";
 w3.display();
 w3=w1-rmb;
 cout<<"w1-"<<rmb<<"=";
 w3.display();
 return 0;
}
实验2-3

#include <iostream>
using namespace std;
class Matrix{
    public: 
        void input();
        void display();
        Matrix operator*(Matrix &t);
    private:
        double mat[3][3];
    
};

void Matrix::input()                 //定义输入数据函数
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            cin >> mat[i][j];
}

void Matrix::display()               //定义输出数据函数
{
    for (int i = 0; i < 3; i++){
    
        for (int j = 0; j < 3; j++)
            cout << mat[i][j]<<" ";
        cout<<endl;    
    }        
}

Matrix Matrix::operator*(Matrix &t){
    Matrix s;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            s.mat[i][j]=mat[i][0]*t.mat[0][j]+mat[i][1]*t.mat[1][j]+mat[i][2]*t.mat[2][j];
            
    return s;        
}

int main()
{Matrix a,b,c;
float x = 0;
 a.input();                  //输入矩阵A
 b.input();                  //输入矩阵B
 cout<<"Matrix a:"<<endl;
 a.display();
 cout<<endl<<"Matrix b:"<<endl;
 b.display();
 c=a*b;                  //用重载运算符“*”实现矩阵乘法
 cout<<endl<<"Matrix c = Matrix a * Matrix b :"<<endl;
 c.display();
 return 0;
}
实验3-1

#include "Teacher1.cpp"
int main()
{Teacher1 te;
 te.get_value();
 te.show_1();
 return 0;
}

#include "Teacher.h"
#include <string>
#include <iostream>
using namespace std;
void Teacher::_get_value(){
    cin>>id>>name>>department>>sex;
}

void Teacher::show(){
    cout<<" id: "<<id<<endl;
    cout<<" name: "<<name<<endl;
    cout<<" department: "<<department<<endl;
    cout<<" sex: "<<sex<<endl;
}
/*Teacher.h*/
#pragma once
#include <string>
#include <iostream>
using namespace std;
//声明基类
class Teacher
{
public: 
    void _get_value();
    void show();
private : 
    int id;
    string name;
    char department;  //a-z英文字符
    char sex;
};

#include "Teacher1.h"
#include <string>
#include <iostream>
using namespace std;
void Teacher1::get_value(){
    _get_value();
    cin>>age>>course>>office;
}

void Teacher1::show_1(){
    show();
    cout<<" age: "<<age<<endl;
    cout<<" course: "<<course<<endl;
    cout<<" office: "<<office<<endl;
}
/*Teacher1.h*/
#pragma once
#include "Teacher.cpp"
class Teacher1: public Teacher //以public方式声明派生类Teacher1
{
public:
    void get_value();
    void show_1();
private: 
    int age;
    string course;
    string office;
};

实验3-2

/*main.cpp*/
#include "Teacher1.cpp"
int main()
{Teacher1 te(1011,"wangping",'a','m',24,"math", "jjl3");
 //te.get_value();
 te.show_1();
 return 0;
}

/*Teacher.cpp*/
#include "Teacher.h"
#pragma once 
#include <string>
#include <iostream>
using namespace std;
Teacher::Teacher(int i,string n,char d, char s){
    id=i;
    name=n;
    department=d;
    sex=s;
}

void Teacher::_get_value(){
    cin >>id>>name>>department>>sex;
}

void Teacher::show( ){
    cout<<" id: "<<id<<endl;
    cout<<" name: "<<name<<endl;
    cout<<" department: "<<department<<endl;
    cout<<" sex: "<<sex<<endl;
}
/*Teacher.h*/
#pragma once 
#include <string>
#include <iostream>
using namespace std;
// 声明基类
class Teacher
{public: 
Teacher(int i,string n,char d, char s);
void _get_value();
void show();
private : 
int id;
string name;
char department; //a-z字符
char sex;
};

/*Teacher1.cpp*/
#include "Teacher1.h"
#include "Teacher.cpp"//头哥里要.h 
#pragma once 
#include <string>
#include <iostream>
using namespace std;
Teacher1::Teacher1(int i,string n,char d, char s,int a,string c,string o):Teacher(i,n,d,s){
    age=a;
    course=c;
    office=o;
}

void Teacher1::get_value(){
    _get_value();
    cin>>age>>office;
}

void Teacher1::show_1( ){
    show( );
    cout<<" age: "<<age<<endl;
    cout<<" course: "<<course<<endl;
    cout<<" office: "<<office<<endl;
}

/*Teacher1.h*/
#pragma once
#include "Teacher.h"
class Teacher1: protected Teacher 
{public:

Teacher1(int i=1001,string n="",char d='a', char s='f',int a=18,string c="",string o="");
/*构造函数中提供了五个默认参数,分别给id、name、department、sex、age、
course、office成员赋值*/
void get_value();
void show_1( );
private: 
int age;
string course;
string office;
};
实验3-3

•&Dancy€•yuan 22:51:04
 /*Association.cpp*/ 
 #include <iostream>
 #include <string>
 #include"Association.h"
 void AssociationMember::show()
{     
cout<<"id:"<<AssociationMember::num<<endl<<"name:"<<AssociationMember::name<<endl<<"sex:"<<AssociationMember::sex<<endl<<"post:"<<post<<endl;
 }
AssociationMember::AssociationMember(int nu,string nam,char s,string p)
 {
     num=nu;
     name=nam;
     sex=s;
     post=p;
 }

•&Dancy€•yuan 22:51:04
 /*Association.h*/
  #include <iostream>
 #include <string>
using namespace std;
 class AssociationMember
 {
     public:
     AssociationMember(int nu,string nam,char s,string p);
     void show();
     protected:
     int num;
     string name;
     char sex;
     string post;
 };

•&Dancy€•yuan 22:51:05
/*Student.cpp*/
 #include <iostream>
 #include <string>
 #include"Student.h"
void Student::show()
{ cout<<"id:"<<Student::num<<endl<<"name:"<<Student::name<<endl<<"sex:"<<Student::sex<<association_name<<endl;
}
Student::Student(int nu,string nam,char s,string ass)
{
    num=nu;
    name=nam;
    sex=s;
    association_name=ass;
}

•&Dancy€•yuan 22:51:05
/*Student.h*/ 
 #include <iostream>
 #include <string>
 using namespace std;
class Student
{
    public:
    Student(int nu,string nam,char s,string ass);
    void show();
    protected:
    int num;
    string name;
    char sex;
    string association_name;
};

•&Dancy€•yuan 22:51:05
  /*sa.cpp,存放student_association类中成员函数的类外定义*/
  #include <iostream>
  #include"sa.h"
  using namespace std;
  void Student_Association::display()
  {
cout<<"id:"<<Student::num<<endl<<"name:"<<Student::name<<endl<<"sex:"<<Student::sex<<endl<<"association_name:"<<association_name<<endl<<"post:"<<post<<endl<<"activities_num:"<<activities_num<<endl;
  }
  Student_Association::Student_Association(int nu,string nam,char s,string ass,string p,int n):Student(nu,nam,s,ass),AssociationMember(nu,nam,s,p)
  {
      activities_num=n;
  }

•&Dancy€•yuan 22:51:05
 /*sa.h,存放student_association类的定义*/ 
 #include<iostream>
 #include<string>
 #include"Association.h"
 #include"Student.h"
 using namespace std;
 class Student_Association:public AssociationMember,public Student
 {public:
     Student_Association(int nu,string nam,char s,string ass,string p,int n);
     void display();
private:
    int num;
    string name;
    char sex;
    int activities_num;
 };
实验4

#include<iostream>
#include<fstream>
using namespace std;
//fun1函数从键盘输入10个数,将10个数存放到f1.dat,将10个数两两相加的结果存放入f2.dat
void func1()
{
    int i = 0;
    float a[10];
    float temp;
    ofstream inFile1("file1.dat", ios::out);

    for (int i = 0; i < 10; i++) {
        cin >> a[i];
        inFile1 << a[i]<<" ";
    }
    inFile1.close();
    i = 0;
    ofstream inFile2("file2.dat", ios::out);
    while (i < 9) {
        temp = a[i] + a[i + 1];
        inFile2 << temp<<" ";
        i = i + 2;
    }
    inFile2.close();
}
//从f1.dat读入4个数,然后两两相减的结果存放到f2.dat文件原有数据的后面
void func2()
{
    ifstream inFile1("file1.dat", ios::in);

    float b[4];
    float temp;
    int i = 0;
    for (i = 0; i < 4; i++)
        inFile1 >> b[i];
    inFile1.close();
    ofstream inFile2("file2.dat", ios::app);
    i = 0;
    while (i < 3)
    {
        temp = b[i] - b[i + 1];
        inFile2 << temp;
        i = i + 2;
    }
    inFile2.close();
}
//从f2.dat中读入7个数,将它们按从大到小的顺序存放到f2.dat 
void func3()
{
    int m, n;
    int i = 0;
    float c[7];
    float temp;
    ifstream inFile1("file2.dat", ios::in);
    for (i = 0; i < 7; i++)
        inFile1 >> c[i];
    inFile1.close();
    //冒泡排序
    for (m = 0; m <6; m++)//n个数的数列总共扫描n-1次
    {
        for (n = 0; n < 6- m ; n++)//每一趟扫描到a[n-i-2]与a[n-i-1]比较为止结束
        {
            if (c[n] < c[n + 1])//后一位数比前一位数小的话,就交换两个数的位置(升序)
            {
                temp = c[n + 1];
                c[n + 1] = c[n];
                c[n] = temp;
            }
        }
    }
    ofstream inFile2("file2.dat", ios::out);
    for (i = 0; i < 7; i++)
    {
        inFile2 << c[i];
        cout << c[i] << " ";
    }
    inFile2.close();
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值