注册 登录
编程论坛 C++教室

怎么样修改?

haiyungood 发布于 2008-10-22 17:29, 650 次点击
停车场三小时内的最少收费2。00美元,超过三小时每增加一小时需要收取0。5美元的附加费,24小时之内的最多收费是10美元,假设没有车子一次停车超过24小时。编写一个程序,计算并显示昨天的天个客户各自的停车费用,输出使用下面的格式:
     car                hour               charge
     1                  1.50                2.00
     2                  4.0                 2.50
     3                  24.0                10.00
    Total               29.5                14.50
下面是我写的程序:(和要求不一样,请各位帮忙看看是哪里错了)
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{float h,h1,h2,h3,ch,ch1,ch2,ch3,t1,t2;
    cout<<"Enter hour:  ";
    cin>>h1>>h2>>h3;
    h=h1,h2,h3;
    if(h>0&&h<=3)
    {ch=2.00;
    cout<<ch<<endl;
    }
    else if(h>3&&h<=19)
    {
        ch=0.5*h+0.5;
        cout<<ch<<endl;
    }
    else
    {ch=10.00;
    cout<<ch<<endl;
    }
    if(h=h1)
    {ch1=ch;
    }
    else if(h=h2)
    {ch2=ch;
    }
    else
    {ch3=ch;
    }
    t1=h1+h2+h3;
    t2=ch1+ch2+ch3;
    cout<<setiosflags(ios::fixed)<<setprecision(2);
    cout<<"\t"<<"car"<<"\t\t"<<"hour"<<"\t\t"<<"charge"<<endl;
    cout<<"\t"<<1<<"\t\t"<<h1<<"\t\t"<<ch1<<endl;
    cout<<"\t"<<2<<"\t\t"<<h2<<"\t\t"<<ch2<<endl;
    cout<<"\t"<<3<<"\t\t"<<h3<<"\t\t"<<ch3<<endl;
    cout<<"\t"<<"Total"<<"\t\t"<<t1<<"\t\t"<<t2<<endl;
    return 0;
}
和要求不一样请各位帮忙看看是哪里错了。谢谢了。
3 回复
#2
blueboy820062008-10-22 18:17
挺有想法的...
h=h1,h2,h3;
你想过这句是什么效果吗?
#3
keanbin2008-10-22 18:46
帮你改了一下,你看下!!
#include <iostream.h>
#include<iomanip.h>

int main()
{
    float t1=0;
    float t2=0;
    float h[3],ch[3];
    for(int i=0;i<3;i++)
    {
        cout<<"Enter hour:  ";
        cin>>h[i];
        if(h[i]<=0||h[i]>24)
        {
            cout<<"Hour is error!";
        }
        else if(h[i]<=3)
        {
            ch[i]=2.00;
//    cout<<ch<<endl;
        }
        else if(h[i]<=19)
        {        
            ch[i]=0.5*h[i]+0.5;
//        cout<<ch<<endl;
        }
        else
        {
            ch[i]=10.00;
//    cout<<ch<<endl;
        }
        t1+=h[i];
        t2+=ch[i];
    }
 /*   if(h=h1)
    {ch1=ch;
    }
    else if(h=h2)
    {ch2=ch;
    }
    else
    {ch3=ch;
    } */
//    t1=h1+h2+h3;
//    t2=ch1+ch2+ch3;
    cout<<setiosflags(ios::fixed)<<setprecision(2);
    cout<<"\t"<<"car"<<"\t\t"<<"hour"<<"\t\t"<<"charge"<<endl;
    for(i=0;i<3;i++)
    {
        cout<<"\t"<<1<<"\t\t"<<h[i]<<"\t\t"<<ch[i]<<endl;
    }
    cout<<"\t"<<"Total"<<"\t\t"<<t1<<"\t\t"<<t2<<endl;
    return 0;
}
运行的结果在附件上:
#4
haiyungood2008-10-22 22:14
谢谢了,现在终于懂了,发现自己学的很差。以后一定要努力了。上面还缺一句:"using namespace std;".在有就是题意是,超过三小时,每增加一小时收取0。5美元,不到一小时的按一小时算。这个是取整函数,该如何往上加了?
1