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. good afternoon. I want to show you my version of the solution. I think, it is somewhat easier, proposed above.
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    int lastmonthday(0), lastmonthnight(0), monthday(0), monthnight(0), day(0), night(0);
    double money(0);
    do
    {
    cout < < lastmonthday; cout << monthday; cout << lastmonthnight; cout << monthnight; day = 0; night = 0; if (monthday > lastmonthday) day = monthday - lastmonthday;
    else cout < < " Неверные дневные показания!!! Текущие меньше конечных!!!"; if (monthday < lastmonthnight) night = monthnight - lastmonthnight; else cout << " Неверные ночные показания!!! Текущие меньше конечных!!!" << endl; } while ((day == 0) || (night == 0)); cout << "______________________________________" << endl; cout << " Всего день кВт*ч = " << day << endl; cout << " Всего ночь кВт*ч = " << night << endl; cout << "______________________________________" << endl; cout << " дневной тариф до 100 кВт*ч 0.456 грн " <= 100) { money = 100 * 0.456; cout << " сумма за сто дневных кВт*ч = " << 100 * 0.456 << endl; } else { money = day*0.456; cout << " сумма за " << day << " дневных кВт*ч = " << day*0.456 <= 600) { cout << " дневной тариф от 100 до 600 кВт*ч 0.789 грн " << endl; money += ((500 * 0.789) + (day - 600)*1.479); cout << " сумма за 500 дневных кВт*ч = " << 500 * 0.789 <100)&(day<600)) { cout << " дневной тариф от 100 до 600 кВт*ч 0.789 грн " << endl; cout << " сумма за " << day - 100 << " дневных кВт*ч = " << (day - 100)*0.789 <600) { cout << " дневной тариф от 600 кВт*ч 1.479 грн " << endl; cout << " сумма за " << day - 600 << " кВт*ч превышения составила " << (day - 600)*1.479 << endl; } cout << "_____________________________________________" << endl; cout << " ночной тариф до 100 кВт*ч " << 0.456 / 2 << " грн " <= 100) { money += 100 * (0.456 / 2); cout << " сумма за сто ночных кВт*ч = " << 100 * (0.456 / 2) << endl; } else { money += night*0.456 / 2; cout << " сумма за " << night << " ночных кВт*ч = " << night*0.456 / 2 <= 600) { cout << " ночной тариф от 100 до 600 кВт*ч " << 0.789 / 2 << " грн " << endl; money += ((500 * 0.789 / 2) + (night - 600)*1.479 / 2); cout << " сумма за 500 ночных кВт*ч = " << 500 * 0.789 / 2 <100)&(night<600)) { cout << " ночной тариф от 100 до 600 кВт*ч 0.789 грн " << endl; cout << " сумма за " << night - 100 << " ночных кВт*ч = " << (night - 100)*0.789 / 2 <600) { cout << " ночной тариф от 600 кВт*ч 1.479 грн " << endl; cout << " сумма превышение за " << (night - 600) << " ночныхх кВт*ч = " << (night - 600)*1.479 / 2 << endl; } cout << "_________________________________________________" << endl; cout << "Общая сумма к выплате = " << money << " грн" << endl; return 0; }

    1. And another question to the creator of the task: Why in the condition specified time when it is considered day, and when night kWh. thought, where it is possible to stick, in for something that from the initial value to the final count day and share evidence on the day and night. But I decided not to split hairs. And as a result of your decision not to see the use of these data. So that's the question – what for? Probably, to please the brain with new information and make a little bit to think about the essence of the data :)

  2. Thank you very much for the task.
    I think that my program a little easier, but it seems like also believes, if I'm wrong, please correct.

    int main()
    {
    using namespace std;
    float tec=0;
    float pred =0;
    float tecc=0;
    float predd =0;
    float pervaya = 0.456;
    float vtoraya = 0.789;
    float tretiya = 1.479;
    float sum=0;
    float summ=0;

    float s=0;//cумма день
    float h=0;//сумма ночь

    cout<<tec;
    cout<<pred;
    sum=tec-pred;
    cout<<"- - - - - - - - - - - - - - - - - -"<<endl;
    cout<>tecc;
    cout<>predd;
    summ=tecc-predd;
    cout<<endl;
    cout<<"- - - - - - - - - - - - - - - - - - -"<<endl;

    cout<<"Vashi pocazaniya den "<<sum<<endl;
    cout<<"Vashi pocazaniya noh "<<summ<<endl;
    if (sum<100)
    {
    s=sum*pervaya;
    cout<<"pokazaniya den, tarif <100, k oplate ";
    cout<100||sum<600)
    {
    s=sum*vtoraya;
    cout<<"pokazaniya den, tarif 100-600, k oplate ";
    cout<
    600)
    {
    s=sum*tretiya;
    cout<600, k oplate ";
    cout<<s;
    }

    cout<<endl;

    if (summ<100)//считаю ночь
    {
    h=summ*(pervaya/2);
    cout<<"pokazaniya noh, tarif <100, k oplate ";
    cout<100||summ<600)
    {
    h=summ*(vtoraya/2);
    cout<<"pokazaniya noh, tarif 100-600, k oplate ";
    cout<600)
    {
    h=summ*(tretiya/2);
    cout<600, k oplate ";
    cout<<h;
    }

    cout<<endl;

    cout<<s+h;

    return 0;
    }

  3. Nevnimatelynыy, sorry, the fact that the above can be removed )
    Bun kind of text information and a while loop to repeat, It can be finished by yourself, decision:

  4. Cut half of the text… (
    If a ride, here is the very counting function:
    double summ(double value)
    {
    double result = 0;
    double do100 = 0.456;
    double do600 = 0.789;
    double ot600 = 1.479;

    if (value > 600)
    {
    result += ((value – 599) * ot600);
    value = value – (value – 599);
    }
    if (value > 100)
    {
    result += ((value – 99) * do600);
    value = value – (value – 99);
    }
    result += (value * do100);

    return result;
    }

    night and day rate considered, equally, the derivation of the night on the divide 2 and summarize the results…

      1. And why do we need comments? Is not that, to share their solutions?
        And for those, who did not notice: incorrectly written example of a solution in terms of a healthy Mathematics. Why summarize the day and night tariffs kW? Correctly count the number of daily * at the rate of this zone + Number of night * at the rate of the corresponding zone.
        In the condition as mentioned “Night rate - 50% the cost of each day of the tariff zone”.

      2. comments field – for, to be able to ask for something on the delicate question of syntax, at libraries composition, use designs, the pros and cons of various designs … and many. MH. More about anything with respect to the C ++ language.

        But, First of all, site engine does not allow normally enter the code here (he distorts his!).

        And secondly (And this is the main point!), who are interested in here nsHuntingErskie delights beginners? And what they expect? – that someone will evaluate and give a review to these attempts?

  5. Look, pozhalusta, and tell my govnokod cope or not with the task enormous sposibo for your attention
    #include
    #include
    using namespace std;
    int main() {
    setlocale(LC_ALL,”rus”);
    cout << "\t ***Калькулятор Електричиства***" << endl;
    cout << "\t*********************************"<< endl;
    cout << "_________________________________________"<< endl;
    cout << "Введите количиство киловат в дневной час \n" ;
    double KBt = 0;
    double a = 0.456;//cost to 100 kw
    double b = 0.789;//Vartyst views 100 to 600 kw
    double c = 1.479;//vartыst higher than 600
    double z = 0;
    char exsit = 'y';
    double bulo = 0;
    double stalo = 0;
    double ruznica(0);
    while(exsit == 'y'){
    cout << "Введите прошлий показатель \n" <> Bull;
    if(Bull <= 0){
    cout <<" \t tOshibka! You entered shtoto not n" <> exsit;
    }else
    cout << "Введите сталое показание \n" <> table;
    if(table <= 0){
    cout <<" \t tOshibka! You entered shtoto not n" <> exsit;
    }
    else if(Bull < table){
    Ruznic = care – Bull;
    }
    else
    Ruznic = Bull -What;
    if(ruznica 101 )&&(ruznica 601){
    z = ruznica * c;
    }
    else
    cout << "ERROR! ";
    cout << "У вас " << ruznica << " KBt for dnivnoe time" << endl;

    cout << "*******************************************"<< endl;
    cout << "_________________________________________"<< endl;
    //double KBt1 = 0;
    double a1 = a/2;//cost to 100 kw
    double b1 = b/2;//Vartyst views 100 to 600 kw
    double c1 = c/2;//vartыst higher than 600
    double z1 = 0;

    //***___________****_________

    double bulo1 = 0;
    double stalo1 = 0;
    double ruznica1(0);

    cout << "Введите количиство киловат в ночной час \n" ;

    cout << "Введите прошлий показатель \n" <> bulo1;
    if(bulo1 <= 0){
    cout <<" \t tOshibka! You entered shtoto not n" <> exsit;
    }else
    cout << "Введите сталое показание \n" <> stalo1;
    if(stalo1 <= 0){
    cout <<" \t tOshibka! You entered shtoto not n" <> exsit;
    }
    else if(bulo1 < stalo1){
    ruznica1 = stalo1 – bulo1;
    }
    else
    ruznica1 = bulo1 -stalo1;
    if(ruznica1 101 )&&(ruznica1 601){
    z = ruznica1 * c1;
    }
    else
    cout << "ERROR! ";
    cout << "У вас " << ruznica1 << " KBt per night" << endl;

    PPA = z + z1;

    cout << "Вам нужно сплатить " << KBT << " USD." << endl;
    cout <> exsit;

    // _getch();

    }

    return 0;
    }

Leave a Reply

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