2014-09-23 223 views
0

我目前正在采用C++类和我们的任务之一,由于某种原因我有一点麻烦。C++如果语句逻辑

要求用户自己购买的总金额在商店和 计算基于以下规则的优惠:

  • 如果他们花费了$ 50给他们10%的折扣
  • 如果他们花费了$ 75,折扣为15%
  • 如果他们花费了$ 100,折扣为25%
  • 最后,如果他们花费250美元或更多,他们将获得40%的折扣。
  • 显示购买金额,扣除折扣并显示结果。

这里是我迄今编写的代码:

#include <iostream> 
using namespace std; 

int main() { 

    double amount,discount=0.0; 
    cout<<"Enter the total amount of your purchase: $"; 
    cin>>amount; 
    cout<<endl; 

    if (amount<50) 
     cout<<"You do not recieve a discount"<<endl; 
    else if(amount<=50.0) 
    { 
     discount=0.1*amount; 
     cout<<"Discount: $"<<discount<<endl; 
     amount=amount*0.9; 
     cout<<"Total Amount: $"<<amount<<endl; 
     return 0; 
    } 
    else if(amount<=75.0) 
    { 
     discount=0.15*amount; 
     cout<<"Discount: $"<<discount<<endl; 
     amount=amount*0.85; 
     cout<<"Total Amount: $"<<amount<<endl; 
     return 0; 
    } 
    else if(amount<=100.0) 
    { 
     discount=0.25*amount; 
     cout<<"Discount: $"<<discount<<endl; 
     amount=amount*0.75; 
     cout<<"Total Amount: $"<<amount<<endl; 
     return 0; 
    } 
    else if(amount<=250.0) 
    { 
     discount=0.4*amount; 
     cout<<"Discount: $"<<discount<<endl; 
     amount=amount*0.6; 
     cout<<"Total Amount: $"<<amount<<endl; 
     return 0; 
    } 
    return 0; 
} 

我没有收到例如正确的数字,如果我输入74它会给我11.1 我觉得它是原因很简单,为什么我收到了错误的号码,但我不知道为什么

+0

如果缩进代码,逻辑将更容易遵循。 – OJFord 2014-09-23 00:04:27

+2

您需要反转逻辑。如果(amount> 250){}否则if(amount> 100){} ... else {}'。其他将指最低折扣。 – royhowie 2014-09-23 00:04:51

+2

您不需要在每个其他子句的末尾返回。 – 2014-09-23 00:12:43

回答

0
int main() { 

double amount,discount=0.0; 
cout<<"Enter the total amount of your purchase: $"; 
cin>>amount; 
cout<<endl; 

if (amount<50) 
cout<<"You do not recieve a discount"<<endl; 
else if(amount>=250.0)//250 or more 
{ 
    discount=0.4*amount; 
    cout<<"Discount: $"<<discount<<endl; 
    amount=amount*0.6; 
    cout<<"Total Amount: $"<<amount<<endl; 
    return 0; 
} 


else if(amount>=100.0) 
{ 
    discount=0.25*amount; 
    cout<<"Discount: $"<<discount<<endl; 
    amount=amount*0.75; 
    cout<<"Total Amount: $"<<amount<<endl; 
    return 0; 
} 

else if(amount>=75.0) 
{ 
    discount=0.15*amount; 
    cout<<"Discount: $"<<discount<<endl; 
    amount=amount*0.85; 
    cout<<"Total Amount: $"<<amount<<endl; 
    return 0; 
} 
else if(amount>=50.0) 
{ 
    discount=0.1*amount; 
    cout<<"Discount: $"<<discount<<endl; 
    amount=amount*0.9; 
    cout<<"Total Amount: $"<<amount<<endl; 
    return 0; 
} 

return 0; 
} 
4

让我们来看看你的问题情况之一:

else if(amount<=50.0) 

10%的折扣适用于花费至少50美元的人 - 上述测试将限制花费最多50美元的人(我们暂时忽略花费少于50美元的人已经被过滤掉由之前的if)。

要创建一个if子句决定一个人应该有10%的折扣,你需要的数学模型:

$50.00 <= amount < $75.00 

那表情自然会转化为下面的C代码:

else if (50.00 <= amount && amount < 75.00) 

如果您愿意,可以省略表达式的第一部分 - 在此else子句之前由if处理:

if (amount < 50.00) 
    cout<<"You do not receive a discount"<<endl; 
else if (amount < 75.00) 
    // etc... 
+0

谢谢你指出这些问题。非常感谢 – Brendon 2014-09-23 00:35:33

4

您的一组条件与问题陈述不匹配。当您要求74时,这将与(amount <= 75)相匹配,这将会应用错误的折扣。

你正在寻找的范围实际上是这些:

if (amount < 50) { 
    // No Discount 
} 
else if (amount < 75) { 
    // 10% Discount 
} 
else if (amount < 100) { 
    // 15% Discount 
} 
else if (amount < 250) { 
    // 25% Discount 
} 
else { 
    // 40% Discount 
} 
-1

floatdouble变量不应该与==<=>=,你应该使用小量进行比较。http://www.parashift.com/c++-faq/floating-point-arith.html

对我来说,你的if条件应该如下所示。

if (0 > amount) 
{ 
    // ERROR 
    return; 
} 
else if (50.0 > amount) 
{ 
    // No discount 
    return; 
} 
else if (75.0 > amount) 
{ 
    // 10% discount 
    return; 
} 
else if (100.0 > amount) 
{ 
    // 15% discount 
    return; 
} 
else if (250.0 > amount) 
{ 
    // 25% discount 
    return; 
} 
else 
{ 
    // 40% discount 
    return; 
} 
+2

100%与这个错误无关,并且100%是不必要的。引用你的链接:“绝对不要使用'=='来比较两个浮点数。”但OP不使用'=='来比较两个浮点数。 – 2014-09-23 00:29:22

+0

是的,他没有使用'==',但他正在使用'<='比较'double'。这是正确的举动吗?其次,我的if-else语句与我现在在帖子上方看到的相同(+4 btw)。第三,@ user3363156写了他的if-else声明在我看来是错误的,所以我写了一个。我在我的代码中纠正了例如'if(amount <50)'...... else else(amount <= 50.0)'.... @ user3363156每个折扣的范围都是错误的,在我的代码中有什么更正。我认为你的评论是不公平的。 – Dakorn 2014-09-23 14:15:45

+1

是的。浮点数的工作(或多或少)如'<, <=, > =,>'所预期的那样。你说得对,你的代码是正确和好的。 – 2014-09-23 16:37:50