24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 1686  |  回复: 6
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

zhaowei8686

新虫 (小有名气)

[求助] 求助一个C++的程序,十万火急 已有1人参与

题目是:
The Car Company

A car company sells different car models. During the purchase order, the customer can choose between different types of engines, body chapes (coupé, sedan, estate), body colors, etc...

Can you create a C++ class architecture that will provide a car object, corresponding to our needs ?

Every car has some typical functions to read mileage, fuel gauge or number of seats, start the engine, turn right, etc... Feel free to implement some you think relevant to exploit the C++ object model.

Example of function calls to implement:
Car* myCar = CarCompany.getCar( paremeters );
fuel_type = myCar.getFuleType();
km = myCar.getMileAge();
(etc...)

本人没有学过C++,求高手帮助
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
zhaowei8686(jjdg代发): 金币+5, 辛苦了 2014-05-24 01:25:55
zhaowei8686: 金币+25, ★★★★★最佳答案 2014-05-24 08:21:27
引用回帖:
3楼: Originally posted by zhaowei8686 at 2014-05-23 21:23:13
一家汽车公司销售不同类型的汽车。在采购时,客户可以选择不同类型的发动机,车体(比如双门跑车,轿车,房车等),车身颜色等等..

你可以创建一个C + +类的架构,根据我们的需求,来提供一个汽车对象。

每 ...

只能帮你到这里了,复制进一个后缀为cpp的文件运行一下,根据需要再改改,添加一些需要的函数
CODE:
#include <iostream>
#include <string>

using namespace std;

class Car
{
private:
    int mileage;
    int fuel;   // fuel gauge: 0-Diesel, 1-Petrol
    int seat;
    double engine;
    int type;   // body type: 0-Coupé, 1-Sedan, 2-Estate
    int color;  // body color: 0-Red, 1-Orange, 2-Yellow, 3-Green, 4-Blue, 5-Purple, 6-Magenta
    int direction; // directions: 0-North, 1-East, 2-South, 3-West
    int status; // status: 0-OFF, 1-ON

    static string types[3];
    static string dirs[4];
    static string colors[7];
public:
    Car(double e, int t, int c, int m=0, int f=0, int n=4, int d=0, int s=0) : mileage(m), fuel(f), seat(n),
    engine(e), type(t), color(c), direction(d), status(s) { }

    int getMileAge(void) { return this->mileage; }
    string getFuleType(void) { return this->fuel?"Petrol":"Diesel"; }
    int getSeats(void) { return this->seat; }

    double getEngine(void) { return this->engine; }
    string getType(void) { return this->types[this->type]; }
    string getColor(void) { return this->colors[this->color]; }
    void setEngine(double e) { this->engine = e; }
    void setType(int t) { this->type = t; }
    void setColor(int c) { this->color = c; }

    void turnRight(void) { cout << "Turning right..." << endl; this->direction++; this->direction %= 4; }
    void turnLeft(void) { cout << "Turning left..." << endl; this->direction = this->direction==0?3:this->direction-1;}
    string getDirection(void) { return this->dirs[this->direction]; }

    string getStatus(void) { return this->status?"ON":"OFF"; }
    void startEngine(void) { if (!this->status) { cout << "Starting engine..." << endl; } this->status = 1; }
    void stopEngine(void) { if (this->status) { cout << "Stop engine..." << endl; }this->status = 0; }

    void printInfo(void)
    {
        cout << "\nCar information: " << endl
        << "\tEngine size:\t" << this->getEngine() << " L" << endl
        << "\tType:\t\t" << this->getType() << endl
        << "\tColor:\t\t" << this->getColor() << endl
        << "\tMileage:\t" << this->getMileAge() << " mile(s)" << endl
        << "\tFuel:\t\t" << this->getFuleType() << endl
        << "\tSeats:\t\t" << this->getSeats() << endl
        << "\n\tDirection:\t" << this->getDirection() << endl
        << "\tEngine is:\t" << this->getStatus() << endl;
    }
};

string Car::dirs[] = {"North","East","South","West"};
string Car::types[] = {"Coupé", "Sedan","Estate"};
string Car::colors[] = {"Red","Orange","Yellow","Green","Blue","Purple","Magenta"};

class Company
{
public:
    Car* getCar(double engine, int type, int color)
    { return new Car(engine,type,color); }
};

int main()
{
    Company CarCompany = Company();
    Car* myCar = CarCompany.getCar(1.6,0,1);
    myCar->printInfo();

    myCar->startEngine();
    myCar->printInfo();

    myCar->turnRight();
    myCar->printInfo();

    myCar->turnRight();
    myCar->printInfo();

    myCar->turnRight();
    myCar->printInfo();

    myCar->turnRight();
    myCar->printInfo();

    myCar->turnLeft();
    myCar->printInfo();

    myCar->stopEngine();
    myCar->printInfo();
    return 0;
}

运行结果
CODE:

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      North
        Engine is:      OFF
Starting engine...

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      North
        Engine is:      ON
Turning right...

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      East
        Engine is:      ON
Turning right...

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      South
        Engine is:      ON
Turning right...

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      West
        Engine is:      ON
Turning right...

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      North
        Engine is:      ON
Turning left...

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      West
        Engine is:      ON
Stop engine...

Car information:
        Engine size:    1.6 L
        Type:           Coupé
        Color:          Orange
        Mileage:        0 mile(s)
        Fuel:           Diesel
        Seats:          4

        Direction:      West
        Engine is:      OFF

Process returned 0 (0x0)   execution time : 0.062 s
Press any key to continue.

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
4楼2014-05-23 23:40:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 7 个回答

q515949148

铜虫 (小有名气)

jjdg: lz在14分钟后已翻译,请你继续 2014-05-24 01:26:38
能稍微翻译一下么?鄙人英语水平实在太差。但翻译出来还是会帮助你的

[ 发自小木虫客户端 ]
2楼2014-05-23 21:09:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhaowei8686

新虫 (小有名气)

引用回帖:
2楼: Originally posted by q515949148 at 2014-05-23 21:09:11
能稍微翻译一下么?鄙人英语水平实在太差。但翻译出来还是会帮助你的

一家汽车公司销售不同类型的汽车。在采购时,客户可以选择不同类型的发动机,车体(比如双门跑车,轿车,房车等),车身颜色等等..

你可以创建一个C + +类的架构,根据我们的需求,来提供一个汽车对象。

每辆汽车都有一些典型的函数来读取行驶里程,燃油表或座位数,启动发动机,右转等。随便来实现一些你认为相关的利用了C + +对象模型。

基本上就是这样子的,出题的是一个法国的姐妹,大概意思就是这样了。
3楼2014-05-23 21:23:13
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cooooldog

铁杆木虫 (著名写手)

ส็็็

这么清楚的练习题作业,
ส็็็็็็็็็็็็็็็็็็็็
5楼2014-05-24 07:28:09
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 0856求调剂 +9 zhn03 2026-03-25 10/500 2026-03-28 12:28 by zllcz
[考研] 283求调剂 +7 A child 2026-03-28 7/350 2026-03-28 12:05 by zllcz
[考研] 材料求调剂一志愿哈工大324 +7 闫旭东 2026-03-28 9/450 2026-03-28 08:51 by Xu de nuo
[考研] 311求调剂 +8 lin0039 2026-03-26 8/400 2026-03-28 08:00 by Iveryant
[考研] 086502化学工程342求调剂 +6 阿姨复古不过 2026-03-27 6/300 2026-03-28 07:06 by wangy0907
[考研] 331环境科学与工程求调剂 +3 熠然好运气 2026-03-27 3/150 2026-03-28 04:11 by fmesaito
[考研] 275求调剂 +10 Micky11223 2026-03-25 13/650 2026-03-27 22:42 by Micky11223
[考研] 265求调剂 +8 小木虫085600 2026-03-27 8/400 2026-03-27 22:16 by 无际的草原
[有机交流] 高温高压反应求助 10+4 chibby 2026-03-25 4/200 2026-03-27 21:08 by BT20230424
[考研] 0703一志愿9,初试成绩:338,四六级已过,有科研经历,求调剂! +3 Zuhui0306 2026-03-25 3/150 2026-03-27 14:09 by shangxh
[考研] 281求调剂 +3 亚克西good 2026-03-26 5/250 2026-03-26 19:48 by 不吃魚的貓
[考研] 一志愿 南京邮电大学 288分 材料考研 求调剂 +3 jl0720 2026-03-26 3/150 2026-03-26 13:39 by zzll406
[考研] 085600 材料与化工 329分求调剂 +9 Mr. Z 2026-03-25 9/450 2026-03-26 10:36 by baoball
[考研] 309求调剂 +4 gajsj 2026-03-25 5/250 2026-03-26 00:27 by Dyhoer
[考研] 334分 一志愿武理-080500 材料求调剂 +4 李李不服输 2026-03-25 4/200 2026-03-25 21:26 by 星空星月
[考研] 282求调剂 +3 wcq131415 2026-03-24 3/150 2026-03-25 12:16 by userper
[考研] 311求调剂 +3 冬十三 2026-03-24 3/150 2026-03-24 21:31 by peike
[考研] 化工专硕求调剂 +3 question挽风 2026-03-24 3/150 2026-03-24 18:48 by jhhcooi
[考研] 材料专硕331求调剂 +4 鲜当牛 2026-03-24 4/200 2026-03-24 15:58 by JourneyLucky
[考研] 一志愿南大,0703化学,分数336,求调剂 +3 收到VS 2026-03-21 3/150 2026-03-21 18:42 by 学员8dgXkO
信息提示
请填处理意见