首页 > 解决方案 > 为什么这个计算代码没有正确执行?

问题描述

我是 C++ 新手,我想制作一个非常低级别的迷你计算器,它可以使用数据结构技术计算电费。它是基于菜单的程序,可以每天从用户那里获取设备和小时数。我正在使用一个结构和两个堆栈,一个可以存储用户对设备的选择,另一个将存储每天消耗的小时数。

    #include<iostream>
const int size=20;
struct bill
{
    float arr[size];
    int top;
}app,hrs;

void push(bill *ps, float x);
float pop(bill *ps);
float calculation(bill *app , bill *hrs);

using namespace std;
int main()
{
    float hours=0.0, result=0.0;
    int c=1 , choice=0;
    app.top=-1;
    hrs.top=-1;

        cout<<"***********APPLIANCES******WATTS************\n";
        cout<<"1.Color TV\t150W\n";
        cout<<"2.Light Bulb\t60W\n";
        cout<<"3.Celling Fan\t50W\n";
        cout<<"4.Cloth dryer\t2500W\n";
        cout<<"5.Coffee Maker\t1100W\n";
        cout<<"6.Desktop Computer\t275W\n";
        cout<<"7.Electric Heater\t2500W\n";
        cout<<"8.Electric Kettle\t2100W\n";
        cout<<"9.Food Blender\t350W\n";
        cout<<"10.Refrigerator\t275W\n";
        cout<<"11.Hair Dryer\t2150W\n";
        cout<<"12.Air Conditioner\t2500W\n";
        cout<<"13.Iron\t1000W\n";
        cout<<"14.Laptop\t75W\n";
        cout<<"15.LED LightBulb\t8.5W\n";
        cout<<"16.Microwave\t1150W\n";
        cout<<"17.Oven\t2150W\n";
        cout<<"18.Smart Phone charger\t5.5W\n";
        cout<<"19.Vacuum Cleaner\t450W\n";
        cout<<"20.Washing Machine\t500W\n";


        while(c!=0)
        {
            cout<<"Enter 0 for exit and 1 to continue";
            cin>>c;
            if(c==0)
            break;
            else if(c==1)
            {
            cout<<"Enter your choice ";
            cin>>choice;
                push(&app, choice); 
        cout<<"Enter hours/per day used";
            cin>>hours;
            push(&hrs, hours);
        }
        else cout<<"Invalid input"; 
        }
        result=calculation(&app, &hrs);
        cout<<"Total Electricity Bill is : "<<result;
    return 0;
}

float calculation(bill *a, bill *h)
{
    float units=0.0, kWh=0.0, temp=0.0, cost=0.0;
    float w=0.0,total_w=0.0, total_h=0.0;
    for(int i=0; i<size; i++){
    if(temp=pop(&app) == 1)
    w=150;
    else if(temp=pop(&app) == 2)
    w=60;
    else if(temp=pop(&app) == 3)
    w=50;
    else if(temp=pop(&app) == 4)
    w=2500;
    else if(temp=pop(&app) == 5)
    w=1100;
    else if(temp=pop(&app) == 6)
    w=275;
    else if(temp=pop(&app) == 7)
    w=2500;
    else if(temp=pop(&app) == 8)
    w=2100;
    else if(temp=pop(&app) == 9)
    w=350;
    else if(temp=pop(&app) == 10)
    w=275;
    else if(temp=pop(&app) == 11)
    w=2150;
    else if(temp=pop(&app) == 12)
    w=2500;
    else if(temp=pop(&app) == 13)
    w=1000;
    else if(temp=pop(&app) == 14)
    w=75;
    else if(temp=pop(&app) == 15)
    w=8.5;
    else if(temp=pop(&app) == 16)
    w=1150;
    else if(temp=pop(&app) == 17)
    w=2150;
    else if(temp=pop(&app) == 18)
    w=5.5;
    else if(temp=pop(&app) == 19)
    w=450;
    else if(temp=pop(&app) == 20)
    w=500;
    else 
    cout<<"Invalid Input";
    total_w=total_w+w;
    total_h=total_h+pop(&hrs);
}
kWh=total_w*total_h*30;
units=kWh/1000;
cost=units*9;
return cost;
}

void push(bill *ps, float x)
{
    if(ps->top!=size-1)
    {
        ps->top=ps->top+1;
        ps->arr[ps->top]=x;
    }
}

float pop(bill *ps)
{
        return ps->arr[ps->top--];
}

它可能会产生运行时错误并且计算不正确

每当我们按 0 退出时,它都会给出错误的输出,因此我可以摆脱这个问题。

标签: c++calculatorexecution

解决方案


您的代码中存在三个问题,首先是您的函数中的for循环。calculation它必须从 0 到 top 而不是 size,因为 size 必须包含您未初始化的位置的原始值。考虑一下,您有一个 5 个数组,可以按以下方式说:

int a[5];
a[0]=2;
a[1]=4;

现在你说添加所有位置并给出如下结果:

result=a[0]+a[1]+a[2]+a[3]+a[4];

位置 0 和 1 分别有 2 和 4,但其余位置有垃圾。

代码中的第二个问题是pop重复 if 语句中的函数调用。为此,您可以在循环语句的开头执行以下操作:

top=pop(a);

a将不会通过app,因为您在 a 中通过了 app。

最后再添加一条语句作为检查语句或安全控制w=0;,因为在您的 else invalid 中正在打印并且 w 仍然具有其先前的值,因此无法提供正确的输出。

同样,即使 a 的选项无效,您的小时数也必须包含在总小时数中,因此要阻止这种情况发生,您必须使用变量 h 并且如果输入有效则必须为其赋值,否则它必须为零。因此,您的代码将如下所示:

 float calculation(bill *a, bill *h)
 {
     float units=0.0, kWh=0.0, temp=0.0, cost=0.0;
     float w=0.0,total_w=0.0, total_h=0.0,hr=0.0;
     for(int i=0; i<size; i++){
         temp=pop(a);
         w=0.0;
         hr=pop(h);
         if(temp == 1)
             w=150;
         else if(temp == 2)
             w=60;
         else if(temp == 3)
             w=50;
         else if(temp == 4)
             w=2500;
         else if(temp == 5)
             w=1100;
         else if(temp == 6)
             w=275;
         else if(temp == 7)
             w=2500;
         else if(temp == 8)
             w=2100;
         else if(temp == 9)
             w=350;
         else if(temp == 10)
             w=275;
         else if(temp == 11)
             w=2150;
         else if(temp == 12)
             w=2500;
         else if(temp == 13)
             w=1000;
         else if(temp == 14)
             w=75;
         else if(temp == 15)
             w=8.5;
         else if(temp == 16)
             w=1150;
         else if(temp == 17)
             w=2150;
         else if(temp == 18)
             w=5.5;
         else if(temp == 19)
             w=450;
         else if(temp == 20)
             w=500;
         else{
             cout<<"Invalid Input";
             hr=0;
         }
         total_w=total_w+w;
         total_h=total_h+hr;
      }
      kWh=total_w*total_h*30;
      units=kWh/1000;
      cost=units*9;
      return cost;
   }

推荐阅读