20240607考试(1.打印车的信息 2.打印一维二维三维坐标 3.book.txt文件(待修改) 4.交换数字数组)

1.打印车的信息

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

class Car
{
public:
    Car(){}
    ~Car(){}

    void setInfo(string b,string c,int p)
    {
       brand=b;
       color=c;
       price=p;
    }

    string getBrand()
    {
        return brand;
    }
    string getColor()
    {
        return color;
    }
    int getPrice()
    {
        return price;
    }
    void showInfo()
    {
//        cout << "This is a " << color << " " << brand << " car costs " ;
        cout << "This is a " << color << " " << brand << " car costs "<<price << " CNY."  << endl;
      // cout << "This is a " << getColor() << " " << getBrand() << " car that costs " << getPrice() << " CNY." << endl;
    }
private:
    string brand;
    string color;
    int price;
};

int main()
{
    string brand, color;
    int price;
    cin >> brand >> color >> price;
    Car car;
    car.setInfo(brand, color, price);
    car.showInfo();
    return 0;
}


2.打印一维二维三维坐标



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

class Point
{
public:
    virtual void show(){
    }
};

class Point1D : public Point
{
public:
    int a;
    Point1D(){}
    ~Point1D(){}
    void set(int a)
    {
        this->a =a;
    }
    void show()
    {
       cout << "This is a one-dimensional point (" << a << ")." << endl;
    }
};

class Point2D : public Point
{
public:
    int a;
    int b;
    Point2D(){}
    ~Point2D(){}

    void set(int a,int b)
    {
        this->a=a;
        this->b=b;
    }
    void show()
    {
        cout << "This is a two-dimensional point ("<<  a << "," << b << ")." << endl;
    }
};

class Point3D : public Point
{
public:
    int a;
    int b;
    int c;

    Point3D(){}
    ~Point3D(){}

    void set(int a,int b,int c)
    {
        this->a=a;
        this->b=b;
        this->c=c;
    }
    void show()
    {
        cout << "This is a three-dimensional point (" << a << "," << b << ","  << c <<  ")." << endl;
    }
};

void ShowPoint(Point *p)
{
     p->show();
}

int main()
{
    int a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    Point1D p1;
    Point2D p2;
    Point3D p3;
    p1.set(a);
    p2.set(b, c);
    p3.set(d, e, f);
    ShowPoint(&p1);
    ShowPoint(&p2);
    ShowPoint(&p3);
    return 0;
}

3.book.txt文件(待修改)

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


int TotalPriceInFile(char * file)
{
    fstream is;
    is.open(file,ios::in);
    string s;
    int n;
    int m;
    int total = 0;
    while(!is.eof())
    {
        is>>s>>n>>m;

        total += (n*m);

    }
    return total;
}
int main()
{
    char fileName[200] = "books.txt";
    int total = TotalPriceInFile(fileName);
    cout << "The total price is " << total << "." << endl;
    return 0;
}

4.交换数字数组


#include <iostream>
using namespace std;

class Array
{
    friend ostream& operator<< (ostream &  out,const Array & a)
    {
        for(int i=0;i<a.m;i++)
        {
            out << a.data[i] << " ";
        }
        return out;
    }
     friend istream& operator>> (istream &  in,const Array & a)
    {
        for(int i=0;i<a.m;i++)
        {
            in >>a.data[i];
        }
        return in;
    }
public:
    int m;
    double * data;
    Array()
    {

    }
    ~Array()
    {
        delete [] data;
    }

    void setLength(int l)
    {
        m=l;
        data=new double [m];
    }

    void swap(Array& a)
    {
        Array a1;
        a1.setLength(m);
        for(int i=0;i<a1.m;i++)
        {
            a1.data[i]=data[i];
        }
        //delete [] data;

        this->setLength(a.m);
        for(int i=0;i<a.m;i++)
        {
            this->data[i]=a.data[i];
        }
        //delete [] a.data;
        a.setLength(a1.m);
        for(int i=0;i<a1.m;i++)
        {
            a.data[i]=a1.data[i];
        }
    }
};

int main()
{
    int m, n;
    cin >> m >> n;
    Array a1, a2;
    a1.setLength(m);
    a2.setLength(n);
    cin >> a1;
    cin >> a2;
    a1.swap(a2);
    cout << a1 << endl;
    cout << a2 << endl;
    return 0;
}

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值