C++If语句逻辑

C++ If statement logic

本文关键字:语句 C++If      更新时间:2023-10-16

我目前正在上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我觉得这是我打错号码的一个简单原因,但我不确定

的原因

让我们看看您的一个问题案例:

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)

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

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

您的条件集与问题语句不匹配。当您请求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
}
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;
}

floatdouble变量不应与==<=>=进行比较,应使用epsilon。http://www.parashift.com/c++-faq/foating-point-arite.html

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

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;
}