gpt4 book ai didi

c++ - 无法计算 C++ 中的总数

转载 作者:行者123 更新时间:2023-11-28 06:52:52 28 4
gpt4 key购买 nike

我一辈子都弄不明白如何计算程序的某个部分。我只需要一些帮助,告诉你如何得出正确的总数。

在文件中

111111 米 303-333-3333 333 550 0.9 720-777-7777 501 444 0.4

222222 U 303-444-5555 555 55 3.3

333333 H 720-888-9999 1888 888 3.3 720-999-1111 999 1050 1.8 720-222-2222 800 800 2.1

444444 升 720-123-4567 88 10 0

使用文本文件中的内容并了解这一点..

每部手机的电话费(定义如下)

对电话费征收 8.1% 的州税和地方税注意:8.1% 将存储为 0.081

每部手机 1.44 美元的普遍服务费

L = Low usage plan (no minutes included) 
M = Mid usage plan (500 talk minutes and 500 texts included)
H = High usage plan (1000 talk minutes and 1000 texts and 2 GB data included)
U = Unlimited plan

Phone charges are as follows for each plan:
Plan Base Fee Charge for minutes Charge for texts Charge for data

L* 4.99 0.10 per minute 0.10 per text Data not available

M 49.99 0.08 per minute over 500 0.08 per text over 500 $2.88 per 500 MB over 500 MB

H 74.99 0.07 per minute over 1000 0.07 per text over 1000 $1.78 per 500 MB over 2 GB

U 99.99 No charge No charge No charge

我需要通过计算以某种方式得到这些结果

111111 Mid usage 2 $ 118.48 

222222 Unlimited 1 $ 109.53

333333 High usage 3 $ 326.19

444444 Low usage 1 $ 17.43

当月总计 571.63 美元

我只需要帮助,主要是关于存档的第一个和第三个帐户。到目前为止,我只有这个(用于中高计划费用)..

float midPlanCharge (int& min, int& texts, float& data)
{
float total;


// double check!!!!!!!!!!!!!!!!!!!
if ( min <= BASE_MID_MIN && texts <= BASE_MID_TEXT && data <= BASE_MID_DATA )
{
total = MID_FEE;
}
else if ( min > BASE_MID_MIN && texts <= BASE_MID_TEXT && data <= BASE_MID_DATA )
{
total = MID_FEE + ( (min - BASE_MID_MIN) * MID_MIN );
}
else if ( min <= BASE_MID_MIN && texts > BASE_MID_TEXT && data <= BASE_MID_DATA )
{
total = MID_FEE + ( ( texts - BASE_MID_TEXT) * MID_TEXT);
}
else if ( min <= BASE_MID_MIN && texts <= BASE_MID_TEXT && data > BASE_MID_DATA )
{
total = MID_FEE + ( (( data - BASE_MID_DATA) * 1000 ) * MID_DATA);
}
else if ( min > BASE_MID_MIN && texts <= BASE_MID_TEXT && data > BASE_MID_DATA )
{
total = MID_FEE + (( min - BASE_MID_MIN) * MID_MIN) + ( (( data - BASE_MID_DATA)*1000) * MID_DATA);
}
else if ( min > BASE_MID_MIN && texts > BASE_MID_TEXT && data <= BASE_MID_DATA )
{
total = MID_FEE + ((min - BASE_MID_MIN) * MID_MIN) + (( texts - BASE_MID_TEXT) * MID_TEXT);
}
else if ( min <= BASE_MID_MIN && texts > BASE_MID_TEXT && data > BASE_MID_DATA )
{
total = MID_FEE + ((texts - BASE_MID_TEXT) * MID_TEXT) + (((data - BASE_MID_DATA)*1000) * MID_DATA);
}
else {
total = MID_FEE + ((min - BASE_MID_MIN) * MID_MIN) + ((texts - BASE_MID_TEXT) * MID_TEXT) + (((data - BASE_MID_DATA)*1000) * MID_DATA);
}


return total;
}

float highPlanCharge (int& min, int& texts, float& data)
{
float total;

if ( min <= BASE_HIGH_MIN && texts <= BASE_HIGH_TEXT && data <= BASE_HIGH_DATA )
{
total = HIGH_FEE;
}
else if ( min > BASE_HIGH_MIN && texts <= BASE_HIGH_TEXT && data <= BASE_HIGH_DATA )
{
total = HIGH_FEE + ( (min - BASE_HIGH_MIN) * HIGH_MIN );
}
else if ( min <= BASE_HIGH_MIN && texts > BASE_HIGH_TEXT && data <= BASE_HIGH_DATA )
{
total = HIGH_FEE + ( ( texts - BASE_HIGH_TEXT) * HIGH_TEXT);
}
else if ( min <= BASE_HIGH_MIN && texts <= BASE_HIGH_TEXT && data > BASE_HIGH_DATA )
{
total = HIGH_FEE + ((( data - BASE_HIGH_DATA) * 1000) * HIGH_DATA);
}
else if ( min > BASE_HIGH_MIN && texts <= BASE_HIGH_TEXT && data > BASE_HIGH_DATA )
{
total = HIGH_FEE + (( min - BASE_HIGH_MIN) * HIGH_MIN) + ((( data - BASE_HIGH_DATA)*1000) * HIGH_DATA);
}
else if ( min > BASE_HIGH_MIN && texts > BASE_HIGH_TEXT && data <= BASE_HIGH_DATA )
{
total = HIGH_FEE + ((min - BASE_HIGH_MIN) * HIGH_MIN) + (( texts - BASE_HIGH_TEXT) * HIGH_TEXT);
}
else if ( min <= BASE_HIGH_MIN && texts > BASE_HIGH_TEXT && data > BASE_HIGH_DATA )
{
total = HIGH_FEE + ((texts - BASE_HIGH_TEXT) * HIGH_TEXT) + (((data - BASE_HIGH_DATA)*1000) * HIGH_DATA);
}
else {
total = HIGH_FEE + ((min - BASE_HIGH_MIN) * HIGH_MIN) + ((texts - BASE_HIGH_TEXT) * HIGH_TEXT) + (((data - BASE_HIGH_DATA)*1000) * HIGH_DATA);
}
return total;
}

最佳答案

(1) 不确定您是否被告知要使用 float ,但由于内部舍入,使用 float 很难赚钱。最好使用整数并以美分计算,必要时只除以 100 并乘以 100 - 通常是在需要打印值时。

(2) 我只是以你的中档为例,它似乎有效,如果无效,它应该足够接近,你可以纠正。

(3) 你被卡住的两个 (1 & 3) 有多个手机,所以你可能在一开始就解析了它。我这里只是模拟了一下。这不会让您免费回家,但希望能减少困惑 - 其中一些似乎是您自己造成的。

#include <iostream>
#include <cmath>

using namespace std;

const float SERVICE_FEE = 1.44;

const float MID_FEE = 49.99;
const int BASE_MID_MIN = 500;
const float MID_MIN = 0.08;
const int BASE_MID_TEXT = 500;
const float MID_TEXT = 0.08;
const int BASE_MID_DATA = 500;
const float MID_DATA = 2.88;

float midPlanCharge (int min, int texts, float data)
{
float total = MID_FEE;

data *= 1000;

if (min > BASE_MID_MIN)
total += (min - BASE_MID_MIN) * MID_MIN;

if (texts > BASE_MID_TEXT)
total += (texts - BASE_MID_TEXT) * MID_TEXT;

if (data > BASE_MID_DATA)
total += roundf((data - BASE_MID_DATA) / BASE_MID_DATA) * MID_DATA;

return total;
}

int main(int argc, char *argv[])
{
float accTotal = 0.0;

accTotal += midPlanCharge(333, 550, 0.9);
accTotal += midPlanCharge(501, 444, 0.4);

accTotal += (accTotal * 0.081); //tax
accTotal += SERVICE_FEE * 2; //service fee on 2 phones

cout << "Total = " << accTotal << endl;

return(0);
}

输出:总计 = 118.482

关于c++ - 无法计算 C++ 中的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23588976/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com