循环,为总值添加数字

时间:2017-07-02 14:43:14

标签: c++ loops for-loop

我在完成家庭作业时遇到了麻烦。我在程序上的所有功能都非常有用,但for循环增加了升级的价值。

作业如下:

  

从用户选择汽车后,一些引擎升级。这些升级可以从0(无升级)到5最大升级。你也需要一个很好的循环。

     

每级升级100美元加上升级费用降低

     

示例:1级升级为100美元   示例:2级升级为100美元+ 200美元= 300美元   示例:3级升级为100美元+ 200美元+ 300美元= 600美元   等等等等等等等等等等等等......

这是我到目前为止的代码:

#include <iostream>
#include <iomanip>

int main () {
 int quoteNum = 0;
 int carChoice = 0;
 double carCost = 0;
 double upgradeCost = 0;
 int upgradeChoice = 0;
 int engineLevel = 0;
 double totalCost = 0;
 std::cout << std::fixed << std::showpoint << std::setprecision(2);
do {
    std::cout << "Welcome to Bob's used car lot and chop shop!\n";
    std::cout << "As you can see we have quite a few to choose from!\n";
    std::cout << "Which one would you like?: \n";
    std::cout << "[1] 2005 Volkswagen Beetle ($8,000)\n";
    std::cout << "[2] 2008 Pontiac G6 ($8,581)\n";
    std::cout << "[3] 2004 Chevy S-10 ($10,500)\n";
    std::cout << "[4] 2016 Jeep Patriot ($15,209)\n";
    std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)\n";
    std::cin >> carChoice;
} while (carChoice <= 0 || carChoice > 5);
switch (carChoice) {
    case 1:
        carCost = 8000.00;
        break;
    case 2:
        carCost = 8581.00;
        break;
    case 3:
        carCost = 10500.00;
        break;
    case 4:
        carCost = 15209.00;
        break;
    case 5:
        carCost = 24390.00;
        break;
}
  do {
      std::cout << "Quote:{" << quoteNum++ << "} ";
      std::cout << " Car($" << carCost << ")";
      std::cout << " E(" << engineLevel << ")";
      std::cout << " Upgrades($" << upgradeCost << ")" << std::endl;
      std::cout << "Do you want to upgrade your car?\n";
      std::cout << "[-/+1] Downgrade / Upgrade Engine\n";
      std::cout << "[   2] Clear all upgrades\n";
      std::cout << "[   3] Reset car\n";
      std::cout << "[   4] Buy Car!!!\n";
      std::cout << "What would you like to do?: \n";
      std::cin >> upgradeChoice;
      if (upgradeChoice == -1) {
          engineLevel--;
          upgradeCost--;
      }
      if (upgradeChoice == 1) {
          for (upgradeCost = 0; upgradeCost <= 5; upgradeCost++) {
              upgradeCost = 100.00;
          }
      }
      if (upgradeChoice == 2) {
          engineLevel = 0;
          upgradeCost = 0.00;
      }
      if (upgradeChoice == 3) {
          std::cout << "Welcome to Bob's used car lot and chop shop!\n";
          std::cout << "As you can see we have quite a few to choose "
                  "from!\n";
          std::cout << "Which one would you like?: \n";
          std::cout << "[1] 2005 Volkswagen Beetle ($8,000)\n";
          std::cout << "[2] 2008 Pontiac G6 ($8,581)\n";
          std::cout << "[3] 2004 Chevy S-10 ($10,500)\n";
          std::cout << "[4] 2016 Jeep Patriot ($15,209)\n";
          std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)\n";
          std::cin >> carChoice;
          std::cin.ignore();
          switch (carChoice) {
              case 1:
                  carCost = 8000.00;
                  break;
              case 2:
                  carCost = 8581.00;
                  break;
              case 3:
                  carCost = 10500.00;
                  break;
              case 4:
                  carCost = 15209.00;
                  break;
              case 5:
                  carCost = 24390.00;
                  break;
          }
      }
      if (upgradeChoice == 4) {
          totalCost = carCost + upgradeCost;
          std::cout << "Bill: Car($" << carCost << ")";
          std::cout << " Upgrades($" << upgradeCost << ")";
          std::cout << " Total ($" << totalCost << ")\n";
          std::cout << "Press ENTER to continue";
          std::cin.ignore();
          std::cin.get();
          return 0;
      }
  } while (upgradeChoice >= -1 || upgradeChoice <= 4 && upgradeChoice != 0);
 }

你们有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我使用类解决了这个任务,我建议你开始学习面向对象的编程,因为它是编程的一个非常重要的部分,即使你没有在学校开始学习它。它使您的开发更容易透视。

对于这个项目,我使用C ++ 11和GNU C / C ++编译器。

虽然它可能包含错误或传统错误,但抱歉!

包含的标题:

#include <iostream>
#include <string> 
#include <vector>
#include <sstream> // std::stringstream

<强>助手:

using USHORT = unsigned short;

template <typename T>
std::string convert_string(const T value) { 
    std::stringstream stream;
    stream << value;
    return stream.str();
}

// note: use std::to_string instead of convert_string, if your compiler supports it

代表经销商汽车的汽车类:

class Car {
    USHORT year, engine_level;
    std::string name;
    float price, upgrade_cost;

public:
    Car(USHORT year, std::string name, float price) {
        this->year = year;
        this->name = name;
        this->price = price;

        engine_level = 1;
        upgrade_cost = 0.0;
    }

    std::string to_string() { return convert_string(year) + " " + name + " " + " ($" + convert_string(price) + ")"; }

    friend std::ostream& operator<<(std::ostream& os, Car car) { 
        os << car.to_string() << std::endl;  
        return os;
    }

    std::string get_engine_level() { return "E(" + convert_string(engine_level) + ")"; }
    std::string get_price() { return "Car($" + convert_string(price) + ")"; }
    std::string get_upgrade_cost() { return "Upgrades($" + convert_string(upgrade_cost) + ")"; }

    void downgrade() {
        if(engine_level > 1) {
            engine_level--;
            upgrade_cost -= (100 + (engine_level * 100));
        } else {
            std::cout << "You can't downgrade your car from level 1!" << std::endl;
        }
    }

    void upgrade() { 
        engine_level++;
        upgrade_cost += (100 + (engine_level * 100)); 
    }

    void clear_upgrades() { engine_level = 1; upgrade_cost = 0.0; }

    std::string get_total_cost() { return "Total ($" + convert_string(price + upgrade_cost) + ")"; }
};

代表经销商和研讨会的班级:

class CarDealerAndWorkshop {
    std::vector<Car> cars_for_sale;

public:
    CarDealerAndWorkshop(std::vector<Car> cars_for_sale) { this->cars_for_sale = cars_for_sale; }

    void welcome_message() {
        std::cout <<
            "Welcome to Bob's used car lot and chop shop!\n"
            "As you can see we have quite a few to choose from!\n"
            "Which one would you like?: ";
    }

    void upgrade_message() {
        std::cout << 
            "Do you want to upgrade your car?\n"
            "[-/+1] Downgrade / Upgrade Engine\n"
            "[   2] Clear all upgrades\n"
            "[   3] Reset car\n"
            "[   4] Buy Car!!!\n"
            "What would you like to do?: ";
    }

    void show_cars() {
        int counter = 1;
        for (auto car : cars_for_sale) {
            std::cout << "[" << counter++ << "] ";
            std::cout << car;
        }
    }

    unsigned number_of_cars() { return cars_for_sale.size(); }

    bool validate(const USHORT number) { return number <= number_of_cars() && number >= 0; }

    Car* get_car_by_num(const USHORT num) { return &cars_for_sale[num]; }

    void buy(const int number) { cars_for_sale.erase(cars_for_sale.begin() + number); }

    bool are_there_cars() { 
        if(number_of_cars() == 0) {
            std::cout << "We are very sorry, but we have ran out of cars!\n";
            return false;
        }
        return true;
    }
};

代表客户的类:

class Customer {
    Car* car_choice;
    std::string name;

public:
    Customer(std::string name) { this->name = name; }
    void set_car_choice(Car* car) { car_choice = car; }
    Car* get_car_choice() { return car_choice; }
};

最后是客户端代码:

int main () {
    CarDealerAndWorkshop Bobs_Dealership({
        {2005, "Volkswagen Beetle", 8000},
        {2008, "Pontiac G6", 8581},
        {2004, "Chevy S-10", 10500},
        {2016, "Jeep Patriot", 15209},
        {2012, "Jeep Wrangler Sport", 24390}
    });

    Customer customer("Bill");

    Bobs_Dealership.welcome_message();

    bool is_quit_dealer = false;

    while(!is_quit_dealer) {

        if(!Bobs_Dealership.are_there_cars())
            is_quit_dealer = true;

        USHORT car_choice;

        do {
            Bobs_Dealership.show_cars();
            std::cin >> car_choice;
        } while (!Bobs_Dealership.validate(car_choice-1));

        customer.set_car_choice(Bobs_Dealership.get_car_by_num(car_choice-1));

        unsigned quote_num = 0;
        bool is_quit_workshop = false;
        short upgrade_choice;

        while(!is_quit_workshop) {

            std::cout << "Quote:{" << quote_num++ << "} "              << std::endl;
            std::cout << customer.get_car_choice()->get_price()        << std::endl;
            std::cout << customer.get_car_choice()->get_engine_level() << std::endl;
            std::cout << customer.get_car_choice()->get_upgrade_cost() << std::endl;

            Bobs_Dealership.upgrade_message();
            std::cin >> upgrade_choice;

            switch(upgrade_choice) {
                case -1: customer.get_car_choice()->downgrade();      break;
                case  1: customer.get_car_choice()->upgrade();        break;
                case  2: customer.get_car_choice()->clear_upgrades(); break;
                case  3: is_quit_workshop = true; break;
                case  4: 
                    std::cout << customer.get_car_choice()->get_price() << std::endl;
                    std::cout << customer.get_car_choice()->get_upgrade_cost() << std::endl;
                    std::cout << customer.get_car_choice()->get_total_cost() << std::endl;
                    std::cout << "Press ENTER to continue" << std::endl;
                    std::cin.sync();
                    std::cin.get();
                    Bobs_Dealership.buy(car_choice-1);
                    is_quit_workshop = true;
            }
        }
    }

    return 0;
}