c++实验报告

实验报告一

#include <iostream>
using namespace std;
class Stock
{
private:
    int Number;
    float Max,Min,Begin,End;
public:
    Stock();
    Stock(int n,float ma,float mi,float b,float e);
    Stock(const Stock &p);
    ~Stock();
    void Set_Stock(int n,float ma,float mi,float b,float e);
    float Get_End();
    void Show_Stock();

};

Stock::Stock(int n, float ma,float mi,float b,float e)
{
    this->Number = n;
    this->Max = ma;
    this->Min = mi;
    this->Begin = b;
    this->End = e;

    cout<< "构造函数使用中……"<< endl;
}
Stock::Stock()
{
    this->Number = 0;
    this->Max = 0;
    this->Min = 0;
    this->Begin = 0;
    this->End = 0;

    cout<< "不带参数的构造函数使用中……"<< endl;
}
Stock::Stock(const Stock&p)
{
    this->Number = p.Number;
    this->Max = p.Max;
    this->Min = p.Min;
    this->Begin = p.Begin;
    this->End = p.End;

    cout<<"特殊的构造函数之拷贝构造函数使用中……"<<endl;
}
Stock::~Stock()
{
    cout<<"析构函数使用中……ByeBye!"<<endl;
}
float Stock::Get_End()
{
    return End;
}
void Stock::Set_Stock(int n, float ma,float mi,float b,float e)
{
    this->Number = n;
    this->Max = ma;
    this->Min = mi;
    this->Begin = b;
    this->End = e;
}
void Stock::Show_Stock()
{
    cout<<"交易日:"<<Number<<"  ";
    cout<<"最高价:"<<Max<<"  ";
    cout<<"最低价:"<<Min<<"  ";
    cout<<"开盘价:"<<Begin<<"  ";
    cout<<"收盘价:"<<End<<endl;
}
void showUp(Stock &stock1,Stock &stock2)
{
    float up=(stock2.Get_End()-stock1.Get_End())/stock2.Get_End();
    cout<<"涨幅为:"<<up*100<<"%"<<endl;
}
int main()
{
    Stock s1(1, 4.20,4.00,4.10,4.20);
    Stock s2(2,4.40,4.00,4.20,4.40);
    showUp(s1,s2);
    Stock s3(3,4.60,4.00,4.30,4.60);
    Stock s0();
    Stock s4(s3);

    Stock ob[100];
    for(int i=0;i<100;i++){
        ob[i].Show_Stock();
    }

    return 0;
}

实验报告二

#include <iostream>
using namespace std;
const int N=7;
class Stock
{
private:
    int Number;
    float Max,Min,Begin,End;
    static int count;
public:
    Stock() {count++;};
    Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock();
    void Set_Stock(const Stock &p);
    friend int ifup(const Stock&p);
    void Show_Stock();
    float Get_End();
    int Show_Count();
};
int Stock::count = 0;
Stock::Stock(int n, float ma,float mi,float b,float e) {
    Number = n;
    Max = ma;
    Min = mi;
    Begin = b;
    End = e;
    count++;
}
void Stock::Set_Stock(int n, float ma,float mi,float b,float e)
{
    Number = n;
    Max = ma;
    Min = mi;
    Begin = b;
    End = e;
}
float Stock::Get_End(){ return End;}
void Stock::Set_Stock()
{
    cout<<"第几天:";
    cin>>Number;
    cout<<"最高值:";
    cin>>Max;
    cout<<"最低值:";
    cin>>Min;
    cout<<"开始值:";
    cin>>Begin;
    cout<<"结束值:";
    cin>>End;
}
void Stock::Set_Stock(const Stock &p)
{
    this->Number = count;
    this->Max = p.Max;
    this->Min = p.Min;
    this->Begin = p.Begin;
    this->End = p.End;
}
int ifup(const Stock &p)
{
    if(p.End>p.Begin)
        return 1;
    else
        return 0;
}
int Stock::Show_Count()
{
    cout <<"\n"<<"一共进行了"<< count <<"天的股票统计"<< endl;
}
void Stock::Show_Stock()
{
    cout<<"第"<<Number<<"天的股票信息:"<<"\t";
    cout<<"最大值"<<Max<<"\t";
    cout<<"最小值"<<Min<<"\t";
    cout<<"初始值"<<Begin<<"\t";
    cout<<"结束值"<<End<<endl;
}
int main()
{
    int i;
    Stock s[N];
    Stock *p;
    for (i=0,p=s;i<N-1;i++,p++)
    {
        p->Set_Stock();
    }
    p->Set_Stock(s[5]);
    p->Show_Stock();
    for (i=0,p=s;i<N;i++,p++)
    {
        if(ifup(*p)==1)
        {
            cout<<"第"<<i+1<<"天当天涨了"<<endl;
        }else{
            cout<<"第"<<i+1<<"天当天没涨"<<endl;
        }
        p->Show_Stock();
    }
    for (i=1,p=s+1;i<N;i++,p++)
    cout<<"\n"<<"第"<<i+1<<"天较前一天涨幅为"<<(p->Get_End()-(p-1)->Get_End())/(p-1)->Get_End()*100<<"%"<<endl;

    s[1].Show_Count();
    return 0;
}

实验报告三

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

class Furniture
{
private:
    string num;
protected:
    double price;
public:
    Furniture(string num)
    {
        this->num = num;
        this->price = 0;
    }
    void set_num(string num)
    {
        this->num = num;
    }
    void set_price(double price)
    {
        this->price = price;
    }
    string get_num()
    {
        return num;
    }
    double get_price()
    {
        return price;
    }
};

class Sofa:public Furniture
{
private:
    double weight;
public:
    Sofa(string num,double weight):Furniture(num)
    {
        this->weight = weight;
    }
    double figure_price()
    {
        price = weight*10;
        return price;
    }
    void showSofa()
    {
        cout<<"沙发的编号为"<<this->get_num()<<endl;
        cout<<"沙发的价格为"<<this->figure_price()<<endl;
        cout<<"---------------------------------------"<<endl;
    }
};

class EuroSofa:public Sofa
{
private:
    int material;
    double volume;
public:
    EuroSofa(string num,double weight,int material,double volume):Sofa(num,weight)
    {
        this->material = material;
        this->volume = volume;
    }
    double figure_price()
    {
        switch(material)
        {
            case 1:
                price = volume*100;

                break;
            case 2:
                price = volume*50;

                break;
            case 3:
                price = volume*20;

                break;
            default:
                price = volume*10;

                break;
        }
        return price;
    }
    void showEuroSofa()
    {
        cout<<"欧洲沙发的编号为"<<this->get_num()<<endl;
        cout<<"欧洲沙发的材质为"<<this->material<<endl;
        cout<<"欧洲沙发的体积为"<<this->volume<<endl;
        cout<<"欧洲沙发的价格为"<<this->figure_price()<<endl;
        cout<<"---------------------------------------"<<endl;
    }
};

class Toy
{
private:
    string num;
    int material;
    double volume;
protected:
    double price;
public:
    Toy(string num,int material,double volume)
    {
       this->material = material;
       this->num = num;
       this->price = 0;
       this->volume = volume;
    }
    double figure_price()
    {
        switch(material)
        {
            case 1:
                price = volume*50;
                break;
            case 2:
                price = volume*30;
                break;
            case 3:
                price = volume*5;
                break;
            default:
                price = volume;
                break;
        }
        return price;
    }
    void set_num(string num)
    {
        this->num = num;
    }
    void set_price(double price)
    {
        this->price = price;
    }
    int get_material()
    {
        return material;
    }
    double get_volume()
    {
        return volume;
    }
    string get_num()
    {
        return num;
    }
    double get_price()
    {
        return price;
    }
    void showToy()
    {
        cout<<"玩具的编号为"<<this->num<<endl;
        cout<<"玩具的材质为"<<this->material<<endl;
        cout<<"玩具的体积为"<<this->volume<<endl;
        cout<<"玩具的价格为"<<this->figure_price()<<endl;
        cout<<"---------------------------------------"<<endl;
    }
};

class EuroToy:public Toy,public EuroSofa
{
public:
    EuroToy(string num,double weight,int material,double volume):Toy(num,material,volume),EuroSofa(num,weight,material,volume){}
    double figure_price()
    {
        int material = Toy::get_material();
        switch(material)
        {
            case 1:
                Toy::price = get_volume()*200;
                break;
            case 2:
                Toy::price = get_volume()*100;
                break;
            case 3:
                Toy::price = get_volume()*50;
                break;
            default:
                Toy::price = get_volume()*10;
                break;
        }
        return Toy::price;
    }
    void showEuroToy()
    {
        cout<<"欧洲玩具的编号为"<<Toy::get_num()<<endl;
        cout<<"欧洲玩具的材质为"<<Toy::get_material()<<endl;
        cout<<"欧洲玩具的体积为"<<Toy::get_volume()<<endl;
        cout<<"欧洲玩具的价格为"<<this->figure_price()<<endl;
        cout<<"---------------------------------------"<<endl;
    }
};


int main()
{
    Sofa sofa("sofa",100);
    sofa.showSofa();

    EuroSofa eurosofa("eurosofa",100,1,100);
    eurosofa.showEuroSofa();

    Toy toy("toy",1,10);
    toy.showToy();

    EuroToy eurotoy("eurotoy",100,1,100);
    eurotoy.showEuroToy();
}

实验报告四

简单版

#include <iostream>
#include <cstdlib>
//相对于C语言,C++在引包时通常比同作用的C语言包开头多一个C
using namespace std;

int getGCD(int a,int b)
{
    if(a%b==0)
    {
        return b;
    }
    return getGCD(b,a%b);
}

class Rational
{
private:
    int num,den;
public:
    Rational(int num=1,int den=1);
    void Standardize();


    //运算符重载中参数表内使用引用是为了避免在重载函数内创建临时的类
    //而函数使用时,形参是通过浅拷贝函数创建出来的
    //而重载函数内参数表大都是类,而类内大概率还有指针类的变量
    //当函数结束时,临时变量析构,可能导致实参内的指针变量悬空。
    Rational operator+(Rational &a);
    Rational operator-(Rational &a);
    Rational operator*(Rational &a);
    Rational operator/(Rational &a);

	//类似于“=”和“<<”类型的运算符重载,返回值都是使用引用来代替
	//因为在时候类似运算符时通常是连环使用,并且需要用到自身
	//“=”结果使用应用同时参数表内有const
	//使运算结果赋值给左值本身而不是它产生的一个临时变量
	//对引用赋值,右值必须是确定的值(常量)。
	//cout"<<"使用引用,通常输出不是一段,当输出完后返回同一个输出本身,这使得这一段输出被同一个cout<<输出,从而满足连续输出
    Rational& operator=(const Rational &a);
    friend istream& operator>>(istream& in,Rational &r);
    friend ostream& operator<<(ostream& out,Rational& d);
};

void Rational::Standardize()
{
    if(den<0)
    {
        den=-den;
        num=-num;
    }
}

Rational::Rational(int p,int q):num(p),den(q)
{
    if(den==0)
    {
        cerr<<"A Zero denominator is invalid"<<endl;
        exit(1);//非正常退出
    }
}

istream& operator>>(istream& in,Rational& r)
{
    in>>r.num;
    in>>r.den;
    if(r.den==0)
    {
        cerr<<"A Zero denominator is invalid\n";
        exit(1);
    }
    r.Standardize();
    return in;
}
ostream& operator<<(ostream& out,Rational& d)
{
    out<<d.num;
    out<<"/";
    out<<d.den;
    return out;
}

Rational Rational::operator+(Rational &a)
{
    Rational r;
    r.num = this->num*a.den + a.num*this->den;
    r.den = this->den*a.den;

    int max = r.num>r.den?r.num:r.den;
    int min = r.num>r.den?r.den:r.num;
    int x = getGCD(max,min);

    r.num = r.num/x;
    r.den = r.den/x;
    return r;
}

Rational Rational::operator-(Rational &a)
{
    Rational r;
    r.num = this->num*a.den - a.num*this->den;
    r.den = this->den*a.den;

    int max = r.num>r.den?r.num:r.den;
    int min = r.num>r.den?r.den:r.num;
    int x = getGCD(max,min);

    r.num = r.num/x;
    r.den = r.den/x;
    return r;
}

Rational Rational::operator*(Rational &a)
{
    Rational r;
    r.num = this->num*a.num;
    r.den = this->den*a.den;

    int max = r.num>r.den?r.num:r.den;
    int min = r.num>r.den?r.den:r.num;
    int x = getGCD(max,min);

    r.num = r.num/x;
    r.den = r.den/x;
    return r;
}

Rational Rational::operator/(Rational &a)
{
    Rational r;
    r.num = this->num*a.den;
    r.den = this->den*a.num;

    int max = r.num>r.den?r.num:r.den;
    int min = r.num>r.den?r.den:r.num;
    int x = getGCD(max,min);

    r.num = r.num/x;
    r.den = r.den/x;
    return r;
}

Rational& Rational::operator=(const Rational &a)
{
    if(this == &a)return *this;
    this->den = a.den;
    this->num = a.num;
    return *this;
}

int main()
{
Rational test1(5,6);
Rational test2(3,5);
cout<<test1<<endl;
cout<<test2<<endl;
Rational temp = test1+test2;
cout<<temp<<endl;
temp = test1-test2;
cout<<temp<<endl;
temp = test1*test2;
cout<<temp<<endl;
temp = test1/test2;
cout<<temp<<endl;
return 0;
}


复杂版

#include <iostream>
#include <cstdlib>

using namespace std;

class Complex
{
private:
    double real,image;
public:
    Complex(){};
    Complex(double real,double image);
    Complex(double x);
    Complex& operator*(Complex &com);//****
    double operator[](int x);//****
    Complex& operator++();//****
    Complex& operator++(int);//****
    Complex& operator+(double x);//****
    Complex& operator+(Complex c);//****
    //Complex operator()(double x);
    friend istream& operator>>(istream& in,Complex &c);//****
    friend ostream& operator<<(ostream& out,Complex&c);//****
};


Complex::Complex(double real,double image)
{
    this->image = image;
    this->real = real;
}

Complex::Complex(double x)          //代替
{
    this->image = x;
    this->real = 0;
}


Complex& Complex::operator+(double x)
{
    Complex *temp;
    temp->real = this->real+x;
    temp->image = this->image;
    return *temp;
}

Complex& Complex::operator+(Complex c)
{
    Complex *temp;
    temp->real = this->real+c.real;
    temp->image = this->image+c.image;
    return *temp;
}

Complex& Complex::operator*(Complex &com)          //*
{
    Complex *temp;
    temp->real = com.real*this->real - com.image*this->image;
    temp->image = com.image*this->real + com.real*this->image;
    return *temp;
}

/*Complex Complex::operator()(double x)     //()
{
    Complex c(0,x);
    return c;
}*/

double Complex::operator[](int x)            //[]
{
    if(x==0){
        return this->real;
    }else if(x==1){
        return this->image;
    }else{
        cout<<"[]内的数字错误,应为0/1";
        exit(1);
    }
}

Complex& Complex::operator++()      //前置 ++
{
    real++;
    return *this;
}

Complex& Complex::operator++(int) //后置 ++
{
	Complex *temp;
	temp->image = this->image;
	temp->real = this->real;
	real++;
	return *temp;
}


istream& operator>>(istream& in,Complex& r)     //>>
{
    in>>r.real>>r.image;
    return in;
}
ostream& operator<<(ostream& out,Complex& d)    //<<
{
    out<<d.real;
    if(d.image>0){
        out<<"+"<<d.image<<"i";
    }else{
        out<<d.image<<"i";
    }
    return out;
}



int main()
{
    Complex c1(2.0, 3.5), c2(5, 8.5), c3, c4;
    cin>>c3>>c4;
    cout<<c1*c2++*c3<<endl<<c4<<endl;
    cout<<++(Complex)c2[0]<<endl;
    cout<<c1+c3+4.7;
    return 0;
}








实验报告五

#include <bits/stdc++.h>
using namespace std;
fstream file;
const double esp = 1e-8;

class Complex
{
private:
    double real,image;
public:
    Complex(double real=0.0,double image=0.0);                  //实现构造函数
    void display(){    //输出复数
            cout<<real;
            if(image>0) cout<<"+";
            if(image!=0) cout<<image<<"i\n";
        }
    friend istream& operator>>(fstream& in,Complex &com);
    friend ostream& operator<<(ostream& out,Complex &com);
    Complex operator* (Complex &com);                   //复数相乘
    Complex& operator*= (const Complex &com);           //复数的*=
    bool operator== (Complex &com);                      //复数的比较
    //friend fstream& operator>>(fstream& in,Complex &com);          //实现对文件的输入
    friend fstream& operator<<(fstream& out,Complex &com);
};
Complex::Complex(double real,double image)
{
        this->real = real;
        this->image = image;
};
ostream& operator<<(ostream& out,Complex &com)
{
    cout<<com.real;
    if(com.image>=0){
        cout<<"+";
    }
    cout<<com.image<<"i";
}
istream& operator>>(fstream& in,Complex &com)
//fstream& operator>>(fstream& in,Complex& com)
{
    in>>com.real>>com.image;   //从文件获取复数对象
}

fstream& operator<<(fstream& out,Complex& com)
{
    out<<com.real;    //输出数据到文件
    if(com.image>0) out<<"+";
    out<<com.image<<"i\n";
}

Complex Complex::operator* (Complex &com)
{
    Complex c;
    c.real = com.real*this->real - com.image*this->image;
    c.image = com.image*this->real + com.real*this->image;
    return c;
}

Complex& Complex::operator*=(const Complex &com)
{
    Complex c;
    c.real = com.real*this->real - com.image*this->image;
    c.image = com.image*this->real + com.real*this->image;
    this->real = c.real;
    this->image = c.image;
    return *this;
}

bool Complex::operator==(Complex &com)
{
    double imagex = this->image-com.image;
    double realx = this->real-com.real;
    if(fabs(imagex) <= esp && fabs(realx) <= esp){
        return true;
    }else{
        return false;
    }

}

int main()
{
    Complex *cs[]={
        new Complex(2.0,3.5),
        new Complex(1,3.6),
        new Complex(14,5),
        new Complex(3,0.1),
    };






    Complex r1 = *cs[0];
    //cout<<r1<<endl;
    for(int i=1;i<sizeof(*cs);i++){
        r1 *= *cs[i];
    }
    cout<<"r1:"<<r1<<endl;


    Complex r2 = (*cs[0])*(*cs[1])*(*cs[2])*(*cs[3]);
    cout<<"r2:"<<r2<<endl;

    if(r1==r2) cout<<"r1和r2相等"<<endl;  //判断两个复数是否相等
    else cout<<"r1和r2不相等"<<endl;







    file.open("c:\\test\\data.txt",ios::in | ios::out | ios::binary | ios::trunc);  //打开文件data.txt
    for(int i=0;i<4;i++){
        file.write((const char*)&(*cs[i]),sizeof(*cs[i]));
    }
    Complex a,b,c;
    Complex d(1.4,50);
    file.seekp(sizeof(Complex)*2);
    file.read((char*)&a,sizeof(a));
    file.read((char*)&b,sizeof(b));
    c = a*b;
    cout<<"--------"<<a;
    cout<<"--------"<<b;
    cout<<"--------"<<c;
    file.seekp(sizeof(Complex)*2);
    file.write((const char*)&(d),sizeof(d));
    file.seekp(sizeof(Complex)*4);
    file.write((const char*)&(c),sizeof(c));





    Complex x;
    for(int i=0;i<5;i++){
        file.seekp(sizeof(Complex)*i);
        file.read((char*)&a,sizeof(a));
        cout<<endl<<"      "<<a;
    }



    file<<(*cs[0]);  //输出数据
    file<<(*cs[1]);
    file<<(d);
    file<<(*cs[3]);
    file<<c;
    return 0;
}

文件的输入输出

text类文件

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
    fstream fout1("data.text",ios::app);

    if(!fout1)
    {
        cout<<"Cannot open output file.\n";
        exit(1);
    }

    fout1<<"\"abs.\"\n";
    fout1.close();

    fstream fin1("data.text",ios::in);

    if(!fin1)
    {
        cout<<"Cannot open input file.\n";
        exit(1);
    }

    char str[80];
    while(fin1)
    {
        fin1.getline(str,80);
        cout<<str<<endl;
    }
    fin1.close();
    return 0;
}

二进制文件

二进制文件的读写1

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
    ofstream fout1("data.text",ios::binary);

    if(!fout1)
    {
        cout<<"Cannot open output file.\n";
        exit(1);
    }

    char a[] = {"aaaaa"};
    for(int i=0;i<3;i++)
    {
        fout1.put(a[i]);
    }
    fout1.close();

    ifstream fin1("data.text",ios::binary);

    if(!fin1)
    {
        cout<<"Cannot open input file.\n";
        exit(1);
    }

    char x;
    while(fin1.get(x))
    {
        cout<<x;
    }

    fin1.close();

    return 0;
}

二进制文件的读写2

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
    ofstream fout1("data.text",ios::binary|ios::out);

    if(!fout1)
    {
        cout<<"Cannot open output file.\n";
        exit(1);
    }

    char a[] = {"aaaaa"};
    char b[6];
    for(int i=0;i<6;i++)
    {
        fout1.write((char*)&a[i],sizeof(a));
    }
    fout1.close();

    ifstream fin1("data.text",ios::binary|ios::in);

    if(!fin1)
    {
        cout<<"Cannot open input file.\n";
        exit(1);
    }

    fin1.read((char*)&b,sizeof(b));
    cout<<b<<endl;
    fout1.close();

    return 0;
}

二进制数据文件的随机读写

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

struct List
{
    char sourse[15];
    int score;
};

int main()
{
    List List3[3] = {{"Computer",100},{"Mathematics",99},{"English",98}};
    List lis;


    fstream fout("data.text",ios::binary|ios::out);
    if(!fout)
    {
        cout<<"gg"<<endl;
        exit(1);        //abort();
    }
    for(int i=0;i<3;i++)
    {
        fout.write((char*)&List3[i],sizeof(List));
    }
    fout.close();


    fstream fin("data.text",ios::binary|ios::in);
    if(!fin)
    {
        cout<<"gg"<<endl;
        exit(1);        //abort();
    }

    fin.seekp(sizeof(List)*2);
    fin.read((char*)&lis,sizeof(List));
    cout<<lis.sourse<<"\t"<<lis.score<<endl;

    fin.close();

    return 0;
}

实验报告六

#include <bits/stdc++.h>
using namespace std;
fstream file;

ofstream out("C:\\test\\frist.txt",ios::out);
ifstream fin("C:\\test\\data.txt",ios::in);    //输入流对象

class MyException //异常类
{
private:
    char message[100]; //异常信息
public:
    static int count;   //用于记录当前类创建的对象个数
    MyException(int a){count++;}                      //构造函数用于初始化数据
    MyException(){count++;}
};
int MyException::count = 0;



void func1(int a) throw(int, MyException)
{
	if (a == 0)
	{
		cout<<"a="<<a<<" before throw"<<endl;
           throw a;
		cout<<"a="<<a<<" after throw"<<endl;
	}
	else if (a < 0)
	{
		cout<<"MyException(a)="<<a<<" before throw"<<endl;
           throw MyException(a);
		cout<<"MyException(a)="<<a<<" after throw"<<endl;
	}
}



class Student
{
private:
    double ave;
    int sNo; // 学号
    char sName[20]; // 姓名
    char *sClass; // 班级
    int scores[3]; // 成绩
public:
    Student();
    Student(int sNo,char *sName,char *sClass,int scores[3]);
    Student(Student& stu);
    ~Student();
    void display();
    double getAve();
    void setStudent(int sNo,char *sName,char *sClass,int scores[3]);
    bool is();
    void put();
};

Student::Student()
{
    sNo = 0;
    strcpy(sName,"王铁头");
    sClass = new char[10];
    strcpy(sClass,"计科1902");
    for(int i=0;i<3;i++)
    scores[i] = 0;
}

Student::Student(int sNo,char *sName,char *sClass,int scores[3])
{
    this->sNo = sNo;
    strcpy(this->sName,sName);
    this->sClass = new char[strlen(sClass)+1];
    strcpy(this->sClass,sClass);
    for(int i=0;i<3;i++)
    this->scores[i] = scores[i];
}

Student::Student(Student& stu)
{
    this->sNo = stu.sNo;
    strcpy(this->sName,stu.sName);
    this->sClass = new char[strlen(stu.sClass)+1];
    strcpy(this->sClass,stu.sClass);
    for(int i=0;i<3;i++)
    this->scores[i] = stu.scores[i];
}

Student::~Student()
{
    //delete []sName;
    //delete []sClass;
    //delete []scores;
}

void Student::setStudent(int sNo,char *sName,char *sClass,int scores[3])
{
    double sum=0;
    this->sNo = sNo;
    strcpy(this->sName,sName);
    this->sClass = new char[strlen(sClass)+1];
    strcpy(this->sClass,sClass);
    for(int i=0;i<3;i++)
    {
        this->scores[i] = scores[i];
        sum += scores[i];
    }
    ave = sum/3;
}

void Student::display()
{
    cout<<"学号:"<<sNo<<" "<<"姓名:"<<sName<<" "<<"班级:"<<sClass<<" "<<"成绩:"<<scores[0]<<"  "<<scores[1]<<"  "<<scores[2]<<endl;
}

bool Student::is()
{
    if(scores[0]>=70 && scores[1]>=70 && scores[2]>=70 && ave>=85)
    {
        return true;
    }else{
        return false;
    }
}

void Student::put()
{
    out<<sNo<<" "<<sName<<" "<<sClass<<" "<<scores[0]<<" "<<scores[1]<<" "<<scores[2]<<endl;
}

double Student::getAve()
{
    return (scores[0]+scores[1]+scores[2])/3.0;
}

int main()
{
    /*cout<<"请输入五个数字进行测试 :"<<endl;
    for(int i=0;i<5;i++)
    {
        int a;
        cin>>a;
        try
        {
            func1(a);
        }
        catch (int e)
        {
            cout<<" MyException::count="<<MyException::count<<endl;
        }
        catch (MyException e)
        {
            cout<<" MyException::count="<<MyException::count<<endl;
        }
        catch (...)
        {
            cout<<" MyException::count="<<MyException::count<<endl;
        }
    }*/

    cout<<"\n学生奖学金管理程序"<<endl;

    Student st[6];
    for(int i=0;i<6;i++){
        int sNo,scores[3];
        char sName[30],sClass[30];
        fin>>sNo>>sName>>sClass;
        fin>>scores[0]>>scores[1]>>scores[2];       //从文件读入数据
        st[i].setStudent(sNo,sName,sClass,scores);
    }
    cout<<"展示输入数据:"<<endl;
    for(int i=0;i<6;i++){
        st[i].display();
        if(st[i].is()){
            st[i].put();    //输出数据到文件
        }
    }

    fin.close();
    out.close();    //关闭文件
    return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了方便,把代码放在Word里面了,每次上机实验的题目代码都在。 第一次: 对如下多项式编写类定义: + + +…+ 其中,n为多项式的次数。完成如下功能: (1) 可存储任意大的多项式(提示:可用动态数组实现)。 (2) 定义构造函数、析构函数、拷贝构造函数。 (3) 包含一个static成员存储定义的多项式的数量。 (4) 定义一个成员函数输出多项式。(可参照-x^4-6x^3+5格式输出) (5) 定义一个成员函数计算多项式的值。 (6) 写main函数测试类的功能。 (7) 采用多文件实现。 考虑:哪些成员函数可以声明为const. 第二次: (8) 重载“+”运算符,实现两个多项式相加。 (9) 重载“-”运算符,实现两个多项式相减。 (10) 重载“*”运算符,实现两个多项式相乘。 (11) 重载“=”运算符,实现两个多项式的赋值运算。 考虑:把其中某个运算符重载为友元函数。 第三次: C++的一般编译器都定义和封装了字符串功能,请模仿定义string类的实现,可以实现并支持如下功能: (1)string s = “吉林大学”; (2)string t = s; (3)string m; m = t; (4)m.legnth() 函数测量字符串的长度 (5)m.cat(string const &)连接字符串 第四次: 我公司为仪器生产企业,目前生产摄像机和行车记录仪两种产品,分别销售给用户。 摄像机包含摄像、图像质量设定、编码算法等属性。 将摄像机增加相应芯片(具有操作菜单、自动拍摄、车速传感器、源代码等功能)后,形成一个行车记录仪。 要求: 设计摄像机类,并请根据下列不同的功能要求,采用不同的继承方式,设计行车记录仪类,并添加测试代码,体验不同继承方式下的成员访问属性。(类设计时可根据需要自行添加数据成员和其他成员函数。) (1) 行车记录仪的芯片可以使用摄像机的摄像、图像质量设定功能。 行车记录仪用户可以操作行车记录仪的操作菜单和摄像机的摄像功能。 (2)行车记录仪的芯片可以使用摄像机的拍摄、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单。 (3) 行车记录仪的芯片可以使用摄像机的拍摄、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单 同时其他公司购买行车记录仪,因该公司也用于销售,不得泄露其全部内容 课后: (1)采用组合方式设计行车记录仪类,增加相应测试代码,体验继承和组合的关系。 (2)分别为继承和组合方式下为各类添加构造函数、析构函数,增加相应测试代码,体验对象的初始化和构造顺序。 (3)将摄像机类和行车记录仪类功能相近的函数(如拍摄、编码等功能函数)设为同名函数,增加相应测试代码,体验同名函数覆盖。 (4)为我公司建立一个多态的产品类层次结构,使用抽象类,测试时,创建一个基类指针的容器,通过基类指针调用虚函数,体验多态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值