tags:

views:

347

answers:

2

I know my quiestion is silly but I'm not good in Math:

I'm writing a program:

I want to count the consumption of water and electricity and phone calls. I know how i want it but i dont know how to translate it to C# statments

1- I created the method decimal getElect(Unit kw)

I want the consumption to be counted as the following:-

-----KW-------------------------Price---

The first 100 kw-------------5---------

The second 100 kw-------10--------

The third 100 kw-----------20--------

More than three------------50--------


I will explaine more, for example if the total kw=250 the way to count them is

the cost of the first 100 = 5*100

the cost of the second 100=10*100

the cost of 50 =50*20

total cost will be = 500+1000+1000=2500

==================================================================================

2- I created the method decimal getWater(unit m3)

I want the consumption to be counted as the following:-

-----M3-------------------------Price---

0 ....100---------------------5---------

100 ....200-----------------10--------

200....300------------------20--------

More than that---------------50--------


I will explaine more, for example if M3=150

the cost =150*10=1500

=====================================================================================

+6  A: 

These should work for you...

public decimal getElect(decimal kw)
{
//a stores unit count @ 5
//b stores unit count @ 10
//c stores unit count @ 20
//d stores unit count @ 50


    decimal a = 0, b = 0, c = 0, d = 0;

    a = (kw > 100) ? 100 : kw ;
    b = (kw > 200) ? 100 : ((kw - 100) > 0 ? kw - 100 : 0);
    c = (kw > 300) ? 100 : ((kw - 200) > 0 ? kw - 200 : 0);
    d = ((kw - 300) > 0) ? kw - 300 : 0;

    return (a * 5) + (b * 10) + (c * 20) + (d * 50);
}

public decimal getWater(decimal m3)
{
    int multip = 5;
    if (m3 > 100) multip = 10;
    if (m3 > 200) multip = 20;
    if (m3 > 300) multip = 50;

    return m3 * multip;
}
Eoin Campbell
What a hideous piece of code.
David Arno
Eoin Campbell
thanx a lot man that was it YOUR ARE THE MAN !!!
Arin S. Rizk
Hardly... its fugly code... You'd be as well off using this as an example and if necessary taking a pen and paper to figure out whats goign on.
Eoin Campbell
Eoin I hate mathematical problems It scared me to death , so please show some sympathy ---- anyway you seems to be a good person take care my friendagain thank for helping
Arin S. Rizk
@Eoin, you had me worried there for a bit. Glad to see it was written horribly because it was a "can you do my homework for me" question. Respect and an upvote for you! :)
David Arno
+1  A: 
// returns zero if x is negative, returns x if x is positive
int z(int x)
{
     return (x > 0) ? x : 0;
}

// returns zero if x is negative, returns 1 if x is positive
int s(int x)
{
     return (x > 0) ? 1 : 0;
}

// we'll pay at least 5 for each kw plus at least 5 for each kw above 100 etc.
int getElect(int kw)
{
    return 5 * kw + 5 * z(kw - 100) + 10 * z(kw - 200) + 30 * z(kw - 300);
}

// we'll pay at least 5 for each m3 plus at least 5 for each m3 if m3 is above 100 etc.
int getWater(int m3)
{
    return 5 * m3 + 5 * m3 * s(m3 - 100) + 10 * m3 * s(m3 - 200) + 30 * m3 * s(m3 - 300);
}
Alexander Prokofyev