CS50-HALF 练习题 - 我该如何解决我的代码运行正常但未产生所需输出的问题?

问题描述 投票:0回答:2

CS50 练习题半 -

背景

假设您和朋友在外面的餐馆吃饭,并且想平摊账单。您可能需要在账单到达(含税)之前预计您所欠的金额。在此问题中,您将完成一个函数,根据账单金额、税费和小费来计算每个人所欠的金额。

实施细节

您的函数应使用输入参数

bill
tax
tip
来计算最终金额。但是,由于这些值是百分比,因此您必须做一些工作才能将它们转换为更合适的格式以用于您的计算。

最终应付金额,应在计算小费之前将税费添加到账单中。最后,您将退回全额金额的一半,包括账单金额、税费和小费。

思考问题

  • 你认为为什么 C 中有不同的数据类型?

如何测试您的代码

您的程序应按照以下示例运行。

half/ $ ./half
Bill before tax and tip: 12.50
Sale Tax Percent: 8.875
Tip percent: 20
You will owe $8.17 each!
half/ $ ./half
Bill before tax and tip: 23.50
Sale Tax Percent: 7  
Tip percent: 15
You will owe $14.46 each!
half/ $ ./half
Bill before tax and tip: 100
Sale Tax Percent: 6.25
Tip percent: 18
You will owe $62.69 each!

这是我写的代码-

// Calculate your half of a restaurant bill
// Data types, operations, type casting, return value

#include <cs50.h>
#include <stdio.h>

float half(float bill, float tax, int tip);

int main(void)
{
    float bill_amount = get_float("Bill before tax and tip: ");
    float tax_percent = get_float("Sale Tax Percent: ");
    int tip_percent = get_int("Tip percent: ");

    printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
{
    float total = (bill + (bill * (tax / 100)) + (bill * (tip * .01))) / 2;
    return ((int)(total * 100.0 + 0.5) / 100.0);
}

这个问题所要做的就是完成已经给出的一半函数。我完成了该函数,看不出有什么问题,但它没有给出所需的输出。每个所需的输出都是错误的。

例如问题输出中给出的上述三个示例 -

1-

You will owe $8.05 each!

2-

You will owe $14.34 each!

3-

You will owe $62.13 each!

这是所需的输出和我得到的 - https://submit.cs50.io/check50/8bbdc3f80441100b5f1a4e842ef6efe1b7f443fd

## cs50/labs/2023/x/half

### :) half.c exists

日志

checking that half.c exists...

### :) half.c compiles

日志

running clang half.c -o half -std=c11 -ggdb -lm -lcs50...

### :( Bill of $50, with 10% tax and 20% tip, creates output of $33.00

原因

expected "33.00", not "You will owe $..."

日志

running ./half...  
sending input 50...  
sending input 10...  
sending input 20...  
checking for output "33.00"...  
**Expected Output:**  
33\.00**Actual Output:**  
You will owe $32.50 each!

### :( Bill of $50, with 12.5% tax and 20% tip, creates output of $33.75

原因

expected "33.75", not "You will owe $..."

日志

running ./half...  
sending input 50...  
sending input 12.5...  
sending input 20...  
checking for output "33.75"...

预期输出:

33\.75**Actual Output:**  
You will owe $33.13 each!

### :( Bill of $100, with 12.5% tax and 15% tip, creates output of $64.69

原因

expected "64.69", not "You will owe $..."

日志

running ./half...  
sending input 100...  
sending input 12.5...  
sending input 15...  
checking for output "64.69"...

预期输出:

64\.69

实际产量:

You will owe $63.75 each!

enter image description here

c cs50
2个回答
1
投票

简单地将一长篇难以阅读的方程分成更小的部分:

float half(float bill, float tax, int tip)
{
    float total = bill * ( 1 + tax/100.0);
    total = total * (1 + tip/ 100.0);
    return total /2;
}

是不是更容易阅读和理解?编译器不关心。

让调试时的生活更轻松,无需用户输入 - 只需创建一些测试用例。这会节省你很多时间

float half(float bill, float tax, int tip);

int main(void)
{
    printf("You will owe $%.2f each!\n", half(50.0f, 10.0f, 20.0f));
    printf("You will owe $%.2f each!\n", half(50.0f, 12.5f, 20.0f));
    printf("You will owe $%.2f each!\n", half(100.0f, 12.5f, 15.0f));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
{
    float total = bill * ( 1 + tax/100.0);
    total = total * (1 + tip/ 100.0);
    return total /2;
}

https://godbolt.org/z/fzvo8e5zq


0
投票

我通过将其分解来修复它,

float bt = (bill + (bill * (tax / 100)));
float total = (bt +(bt * (tip / 100))) / 2;
return total;

问题是账单和税金的总和没有乘以小费金额。您在等式中使用了原始账单金额,导致金额较小。我知道这大约晚了一年,但对于未来的程序员来说。

© www.soinside.com 2019 - 2024. All rights reserved.