• 汽车收费


    现在要开发一个系统,管理对多种汽车的收费工作。 给出下面的一个基类框架

    class Vehicle

    { protected:

    string NO;//编号

    public:

    virtual void display()=0;//输出应收费用

    }

    以Vehicle为基类,构建出Car、Truck和Bus三个类。

    Car的收费公式为: 载客数8+重量2

    Truck的收费公式为:重量*5

    Bus的收费公式为: 载客数*3

    生成上述类并编写主函数,要求主函数中有一个基类Vehicle指针数组,数组元素不超过10个。

    Vehicle *pv[10];

    主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数

    输入格式:每个测试用例占一行,每行给出汽车的基本信息,每一个为当前汽车的类型1为car,2为Truck,3为Bus。接下来为它的编号,接下来Car是载客数和重量,Truck给出重量,Bus给出载客数。最后一行为0,表示输入的结束。 要求输出各车的编号和收费。

    (PS:未使用 Vehicle *pv[10]; )

      1 #include<iostream>
      2 #include<string>
      3 using namespace std;
      4 
      5 class Vehicle
      6 { 
      7 protected:
      8     string NO;//编号
      9 public:
     10     virtual void display()=0;//输出应收费用
     11     virtual ~Vehicle(){};
     12 };
     13 
     14 class Car:public Vehicle
     15 {
     16     int number,weight;
     17 public:
     18     Car(){};
     19     friend istream& operator >> (istream &input,Car &C); //重载输入流
     20     void display();
     21 };
     22 
     23 istream& operator >> (istream &input,Car &C)
     24 {
     25     input>>C.NO>>C.number>>C.weight;
     26     return input;
     27 }
     28 
     29 void Car::display()
     30 {
     31     cout<<NO<<" "<<number*8+weight*2<<endl;
     32 }
     33 
     34 class Truck:public Vehicle
     35 {
     36     int weight;
     37 public:
     38     Truck(){};
     39     friend istream& operator >> (istream &input,Truck &T);
     40     void display();
     41 };
     42 
     43 istream& operator >> (istream &input,Truck &T)
     44 {
     45     input>>T.NO>>T.weight;
     46     return input;
     47 }
     48 
     49 void Truck::display()
     50 {
     51     cout<<NO<<" "<<weight*5<<endl;
     52 }
     53 
     54 class Bus:public Vehicle
     55 {    
     56     int number;
     57 public:
     58     Bus(){};
     59     friend istream& operator >> (istream &input,Bus &B);
     60     void display();
     61 };
     62 
     63 istream& operator >> (istream &input,Bus &B)
     64 {
     65     input>>B.NO>>B.number;
     66     return input;
     67 }
     68 
     69 void Bus::display()
     70 {
     71     cout<<NO<<" "<<number*3<<endl;
     72 }
     73 
     74 int main()
     75 {
     76     int flag;
     77     while(cin>>flag,flag)
     78     {
     79         switch(flag)
     80         {
     81         case 1:
     82             {
     83                 Car *p=new Car;
     84                 cin>>*p;
     85                 p->display();
     86                 delete p;
     87                 break;
     88             }
     89         case 2:
     90             {
     91                 Truck *p=new Truck;
     92                 cin>>*p;
     93                 p->display();
     94                 delete p;
     95                 break;
     96             }
     97         case 3:
     98             {
     99                 Bus *p=new Bus;
    100                 cin>>*p;
    101                 p->display();
    102                 delete p;
    103                 break;
    104             }
    105         }
    106     }
    107     return 0;
    108 }

     (使用 Vehicle *pv[10])

      1 #include<iostream>
      2 #include<string>
      3 using namespace std;
      4 
      5 class Vehicle
      6 {
      7 protected:
      8     string NO;//编号
      9 public:
     10     Vehicle() {};
     11     Vehicle(string N): NO(N) {};
     12     virtual void display() = 0; //输出应收费用
     13     virtual ~Vehicle() {};
     14 };
     15 
     16 class Car: public Vehicle
     17 {
     18     int number, weight;
     19 public:
     20     Car() {};
     21     Car(string N, int Num, int Wei): Vehicle(N), number(Num), weight(Wei) {};
     22     //friend istream& operator >> (istream &input,Car &C);
     23     void display();
     24 };
     25 /*
     26 istream& operator >> (istream &input,Car &C)
     27 {
     28     input>>C.NO>>C.number>>C.weight;
     29     return input;
     30 }
     31 */
     32 void Car::display()
     33 {
     34     cout << NO << " " << number * 8 + weight * 2 << endl;
     35 }
     36 
     37 class Truck: public Vehicle
     38 {
     39     int weight;
     40 public:
     41     Truck() {};
     42     Truck(string N, int W): Vehicle(N), weight(W) {};
     43     //friend istream& operator >> (istream &input,Truck &T);
     44     void display();
     45 };
     46 /*
     47 istream& operator >> (istream &input,Truck &T)
     48 {
     49     input>>T.NO>>T.weight;
     50     return input;
     51 }
     52 */
     53 void Truck::display()
     54 {
     55     cout << NO << " " << weight * 5 << endl;
     56 }
     57 
     58 class Bus: public Vehicle
     59 {
     60     int number;
     61 public:
     62     Bus() {};
     63     Bus(string N, int Num): Vehicle(N), number(Num) {};
     64     //friend istream& operator >> (istream &input,Bus &B);
     65     void display();
     66 };
     67 /*
     68 istream& operator >> (istream &input,Bus &B)
     69 {
     70     input>>B.NO>>B.number;
     71     return input;
     72 }
     73 */
     74 void Bus::display()
     75 {
     76     cout << NO << " " << number * 3 << endl;
     77 }
     78 
     79 int main()
     80 {
     81     int flag, count(0);
     82     Vehicle *pv[10];
     83 
     84     while(cin >> flag, flag)
     85     {
     86         switch(flag)
     87         {
     88             case 1:
     89                 {
     90                     string No;
     91                     int number, weight;
     92                     cin >> No >> number >> weight;
     93                     pv[count++] = new Car(No, number, weight);
     94                     pv[count - 1]->display();
     95                     break;
     96                 }
     97 
     98             case 2:
     99                 {
    100                     string No;
    101                     int weight;
    102                     cin >> No >> weight;
    103                     pv[count++] = new Truck(No, weight);
    104                     pv[count - 1]->display();
    105                     break;
    106                 }
    107 
    108             case 3:
    109                 {
    110                     string No;
    111                     int number;
    112                     cin >> No >> number;
    113                     pv[count++] = new Bus(No, number);
    114                     pv[count - 1]->display();
    115                     break;
    116                 }
    117         }
    118     }
    119 
    120     return 0;
    121 }
  • 相关阅读:
    HDU 1495 非常可乐
    ja
    Codeforces Good Bye 2016 E. New Year and Old Subsequence
    The 2019 Asia Nanchang First Round Online Programming Contest
    Educational Codeforces Round 72 (Rated for Div. 2)
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises)
    AtCoder Regular Contest 102
    AtCoder Regular Contest 103
    POJ1741 Tree(点分治)
    洛谷P2634 [国家集训队]聪聪可可(点分治)
  • 原文地址:https://www.cnblogs.com/wzzdeblog/p/10753493.html
Copyright © 2020-2023  润新知