The basics of programming in c++ for beginners

A task: two-tariff electricity meter

A task: write a program to calculate the cost of electricity on the testimony of two-tariff electricity meter. There is a two-tariff electricity meter. Readings are taken at the end of the month. Data for calculating – the difference between the readings of the previous month and the current (day and night tariff.) Initial data:

  • daily rate (with 7-00 am to 23-00);
  • night charge (with 23-00 to 7-00 morning );
  • The first tariff zone – to 100 kWh time: 0.456 UAH
  • The second tariff zone – from 100 to 600 kWh time: 0.789 UAH
  • A third tariff zone – over 600 kWh time: 1.479 UAH
  • night charge – 50% of the value of each daily tariff zone

19 thoughts on “A task: two-tariff electricity meter

  1. well yes, comments not for code, and “sample” from above the standard to which all should be equal …
    Uh-huh, a bunch of variables nafig unnecessary. There was no separate conclusion on tariffs in the problem statement. If you had to make a receipt – this one, but getting the total amount is completely different.
    But it doesn't matter.
    You, excuse me, code check, before pouring ? The final numbers in the screenshot – obvious delirium. Checked by an elementary calculator.

    To 100
    Day 45,6 + night 11,4 = 57
    После 100
    Day 39,45

    Total 96,45.

    1. I agree. Already on 94 line of code appears some doubt about using the formula, what is it…

  2. #include

    const double ZONEONE = 0.456; // 600 kWt
    const int MINKWT = 100;
    const int MAXKWT = 600;

    struct ClientValue
    {
    int oldDate = 0;
    int newDate = 0;
    int resultDate = 0;
    double totalPrice = 0;
    };
    struct DayOrNight
    {
    ClientValue day;
    ClientValue night;
    };

    void kWtPerMounth(const int& newDate, const int& oldDate, int& result);
    void CinOldValue(int& oldValue);
    void CinNewValue(int& newValue, const int& oldValue);
    void CalculateDay(const int& resultPerMounth, double& totalPrice);
    void CalculateNight(const int& resultPerMounth, double& totalPrice);
    void Show(const DayOrNight& clientDate);

    int main()
    {
    std::cout << "Enter value of multi-tariff (multi-zone) means of accounting.\n" << "Please enter a old day value: \n";
    DayOrNight clientDate;

    CinOldValue(clientDate.day.oldDate);
    std::cout << "Please enter a new day value: \n";
    CinNewValue(clientDate.day.newDate, clientDate.day.oldDate);
    std::cout << "Please enter a old night value: \n";
    CinOldValue(clientDate.night.oldDate);
    std::cout <> oldValue;
    while (oldValue <= 0)
    {
    std::cout << "Value < 0.\n" <> oldValue;
    }
    }
    void CinNewValue(int& newValue, const int& oldValue)
    {
    std::cin >> newValue;
    while (newValue <= oldValue)
    {
    std::cout << "New value cant be > newValue;
    }
    }
    void CalculateDay(const int& resultPerMounth, double& totalPrice)
    {
    if (resultPerMounth MINKWT && resultPerMounth < MAXKWT)
    totalPrice = resultPerMounth * ZONETWO;
    else
    totalPrice = resultPerMounth * ZONETHREE;
    }
    void CalculateNight(const int& resultPerMounth, double& totalPrice)
    {
    if (resultPerMounth MINKWT && resultPerMounth < MAXKWT)
    totalPrice = resultPerMounth * ZONETWO / 2;
    else
    totalPrice = resultPerMounth * ZONETHREE / 2;
    }
    void Show(const DayOrNight& clientDate)
    {
    std::cout << "Your old date for day is: " << clientDate.day.oldDate << " kWt/month.\n";
    std::cout << "Your new date for day is: " << clientDate.day.newDate << " kWt/month.\n";
    std::cout << "Your old date for nigth is: " << clientDate.night.oldDate << " kWt/month.\n";
    std::cout << "Your new date for nigth is: " << clientDate.night.newDate << " kWt/month.\n";
    std::cout << "Your month result for day is: " << clientDate.day.resultDate << " kWt/month.\n";
    std::cout << "Your month result for nigth is: " << clientDate.night.resultDate << " kWt/month.\n";
    std::cout << "Your may pay for day is: " << clientDate.day.totalPrice << " UAH.\n";
    std::cout << "Your may pay for nigth is: " << clientDate.night.totalPrice << " UAH.\n";
    }

  3. Я посмотрел решение после того как написал свой код и ужаснулся. Автор сам то разберется в логике своей программы через год? Зачем столько условий и переменных?
    Вот мой код(извините что без поясняющих комментариев)

    #include
    using namespace std;
    double tarzone1(int wattday, int wattnight)
    {
    double night = (0.456 / 2)* wattnight;
    double day = wattday * 0.456;
    double summ = night + day;
    return summ;
    }
    double tarzone2(int wattday, int wattnight)
    {
    double night = (0.789 / 2) * wattnight;
    double day = wattday * 0.789;
    double summ = night + day;
    return summ;
    }
    double tarzone3(int wattday, int wattnight)
    {
    double night = (1.479 / 2) * wattnight;
    double day = wattday * 1.479;
    double summ = night + day;
    return summ;
    }
    int main()
    {
    setlocale(LC_ALL, “RU”);
    int wattday;
    int wattnight;
    cout <> wattday;
    cout <> wattnight;
    double summ;
    if ((wattday + wattnight) < 100)
    {
    summ = tarzone1(wattday, wattnight);
    }
    if ((wattday + wattnight) || (wattday + wattnight) 600)
    summ = tarzone3(wattday, wattnight);
    cout << "К оплате " << summ << endl;
    cout << "До свидания" << endl;
    return 0;
    }

  4. #include
    using namespace std;

    void logic(int chose, int chose2, int chose3, float t1, float t2, float t3) {
    if (chose == 1) {
    if (chose2 == 1)
    cout << chose3*t1 << endl;
    if (chose2 == 2)
    cout << chose3*t2 << endl;
    if (chose2 == 3)
    cout << chose3*t3 << endl;
    if (chose2 != 1 || chose3 != 2 || chose3 != 3)
    cout << "Не верная тарифная зона, попробуйте ещё раз" << endl;
    }
    if (chose == 2) {
    if (chose2 == 1)
    cout << chose3*(t1/2) << endl;
    if (chose2 == 2)
    cout << chose3*(t2/2) << endl;
    if (chose2 == 3)
    cout << chose3*(t3/2) << endl;
    if (chose2 != 1 || chose3 != 2 || chose3 != 3)
    cout << "Не верная тарифная зона, попробуйте ещё раз" << endl;
    }
    }

    void main() {
    int chose, chose2, chose3;
    float t1 = 0.456, t2 = 0.789, t3 = 1.479;

    do {
    do {
    cout << "тариф дневной или ночной? 1 – дневной 2 – ночной" <> chose;

    if (chose != 1 || chose != 2) {
    cout << "ошибка, выберите только 1 или 2" << endl;
    }
    }while(chose != 1 || chose !=2);
    cout << "Какая тарифная зона? выберите от 1-3" << endl << endl;
    cout << "1. Первая тарифная зона до 100 кВт*ч = 0.456 UAH" << endl;
    cout << "2. Вторая тарифная зона от 100 to 600 кВт*ч = 0.789 UAH" << endl;
    cout << "3. Третья тарифная зона от 600 кВт*ч = 1.479 UAH" <> chose2;

    cout << "На сколько киловат эннергии вы хотите рассчитать?" <> chose3;

    logic(chose, chose2, chose3, t1, t2, t3);

    }while(chose != 3);

    }

    1. Вот переделал на нормальный, но всё равно код так себе, недоделка, делал быстро. Например нету выхода из программы.

      #include
      using namespace std;

      void logic(int chose, int chose2, int chose3, float t1, float t2, float t3) {
      if (chose == 1) {
      if (chose2 == 1 || chose2 == 2 || chose2 == 3)
      if (chose2 == 1)
      cout << chose3*t1 << endl;
      if (chose2 == 2)
      cout << chose3*t2 << endl;
      if (chose2 == 3)
      cout << chose3*t3 << endl;
      }
      if (chose == 2) {
      if (chose2 == 1 || chose2 == 2 || chose2 == 3)
      if (chose2 == 1)
      cout << chose3*(t1/2) << endl;
      if (chose2 == 2)
      cout << chose3*(t2/2) << endl;
      if (chose2 == 3)
      cout << chose3*(t3/2) << endl;
      }
      }

      int main() {
      int chose, chose2, chose3;
      float t1 = 0.456, t2 = 0.789, t3 = 1.479;

      do {
      do {
      cout << "тариф дневной или ночной? 1 – дневной 2 – ночной" <> chose;

      if (chose 2) {
      cout << "ошибка, выберите только 1 или 2" << endl;
      }
      }while(chose 2);
      do {
      cout << "Какая тарифная зона? выберите от 1-3" << endl << endl;
      cout << "1. Первая тарифная зона до 100 кВт*ч = 0.456 UAH" << endl;
      cout << "2. Вторая тарифная зона от 100 to 600 кВт*ч = 0.789 UAH" << endl;
      cout << "3. Третья тарифная зона от 600 кВт*ч = 1.479 UAH" <> chose2;
      }while(chose2 3);
      cout << "На сколько киловат эннергии вы хотите рассчитать?" <> chose3;
      if (chose3 chose3)
      return 0;
      else
      logic(chose, chose2, chose3, t1, t2, t3);
      cout << endl;

      }while(chose != 3);

      return 0;
      }

  5. Все написано как можно проще для самого начинающего!

    #include

    float Part(float& pd, float& pn, float& d, float& n)
    {
    float result = 0;
    if ((d – pd) >= (n – pn))
    {
    result = (n – pn) / ((n – pn) + (d – pd));
    }
    else if ((d – pd) < (n – pn))
    {
    result = (d – pd) / ((n – pn) + (d – pd));
    }
    std::cout << result << " = result part " <= (NightPNight))
    {
    day = Kw – (Kw * part);
    night = Kw – day;
    }
    else if ((DayPDay) <= (NightPNight))
    {
    night = Kw – (Kw * part);
    day = Kwnight;
    }
    return (day * Price) + (night * (Price / 2));
    }

    int main()
    {
    float f = 0.456, s = 0.789, t = 1.479;
    float PastDay(0), PastNight(0);
    std::cout <> PastDay >> PastNight;
    float Day(0), Night(0);
    std::cout << std::endl <> Day >> Night;
    std::cout < 0)
    {
    float result (0);
    float under_600 (0);
    float under_100 (0);
    float under_0 (0);
    if (Kwt > 600)
    {
    result = Kwt – 600;
    under_600 = Calculate_Parts(part, t, result, PastDay, PastNight, Day, Night);
    std::cout << under_600 < 600″ < 100 && Kwt <= 600)
    {
    result = Kwt – 100;
    under_100 = Calculate_Parts(part, s, result, PastDay, PastNight, Day, Night);
    std::cout << under_100 < 100 до 600″ << std::endl;
    Kwt = 100;
    }
    if (Kwt <= 100)
    {
    under_0 = Calculate_Parts(part, f, Kwt, PastDay, PastNight, Day, Night);
    std::cout << under_0 < 0″ << std::endl;
    }
    std::cout << "Total = " << under_0 + under_100 + under_600 << std::endl;
    }
    else std::cout << "0 kwt = 0! ";
    return 0;

    }

Leave a Reply

Your email address will not be published. Required fields are marked *