24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1546  |  回复: 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的回帖

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的回帖

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的回帖

cooooldog

铁杆木虫 (著名写手)

ส็็็

这么清楚的练习题作业,
ส็็็็็็็็็็็็็็็็็็็็
5楼2014-05-24 07:28:09
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhaowei8686

新虫 (小有名气)

引用回帖:
4楼: Originally posted by libralibra at 2014-05-23 23:40:43
只能帮你到这里了,复制进一个后缀为cpp的文件运行一下,根据需要再改改,添加一些需要的函数
#include <iostream>
#include <string>

using namespace std;

class Car
{
private:
    int m ...

谢谢了,没学过C++,还有些迷糊,
6楼2014-05-24 08:22:15
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

fontomas

新虫 (初入文坛)

你可以看一下C++抽象类、抽象函数的相关定义
把“汽车”这个抽象概念独立设计一个抽象类,包含“汽车”这个概念应有的一些属性和行为(类似于相关参数和操作功能),然后利用多态,根据各品牌汽车的不同特性,设计相对应的子类,实现需要的功能,多一种车就是多添加一个子类。
7楼2014-05-27 19:35:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zhaowei8686 的主题更新
信息提示
请填处理意见