【发布时间】:2014-02-26 02:43:50
【问题描述】:
//Purpose: To calculate the price to pay an author
//Programmer: Brandon C Ballard
//Last Updated: 2/20/2014
#include <iostream>
#include <cstdlib>
using namespace std;
//function prototype
float TotalPay(int numWords, char Level);
int main()
{
float TotalPay;
int numWords;
char Level;
int amtToPay;
cout << "Please enter number of words: ";
cin >> numWords;
cout << endl;
cout << "Please enter a Level, A,B, or C: ";
cin >> Level; cout << endl << endl;
//calculate price per word
if (numWords < 7500)
amtToPay = numWords * .08;
else if (numWords >= 7500 && numWords < 8000)
amtToPay = 600;
else if (numWords >= 8000 && numWords < 17500)
amtToPay = numWords * .075;
else if (numWords >= 17500 && numWords < 19000)
amtToPay = 1313;
else
amtToPay = numWords *.07;
//calculate the Level of the author
if (Level == 'C' or Level == 'c')
Level = 1;
else if (Level == 'B' or Level == 'b')
Level = 1.25;
else if (Level == 'A' or Level == 'a')
Level = 1.75;
TotalPay = amtToPay * Level;
cout << "Length of Story (words): "; cout << numWords; cout << endl << endl;
cout << "Amount Due Author: "; cout << "$"; cout << TotalPay; cout << endl << endl << endl;
system("PAUSE");
return 0;
}//end main function
我的导师希望我编写一个程序,该程序可以计算向向杂志提交文章的作者支付的金额。支付作者的金额取决于文章中的字数。它是这样工作的......
-如果长度(以字为单位)小于 7,500:作者每字获得 0.08 美元的报酬。
-如果长度为 7,500 到 8,000:作者获得固定的 600 美元报酬。
-如果长度为 8,000 到 17,500:作者每字获得 0.075 美元的报酬。
-如果长度为 17,500 到 19,000:作者获得固定的 1313 美元报酬。
-如果长度大于 19,000:作者每字获得 0.08 美元的报酬。
另外:作者分为三个不同的“级别”(A、B 和 C)。 “C”级作者(新作者)将根据上述信息获得报酬。 “B”级作者(知名作家)的报酬是 C 级作者的 1.25 倍。 “A”级作者(摇滚明星)的报酬是 C 级作者的 1.75 倍。
数学:基本上,我编写了程序,以便它首先计算支付给作者的金额(amtToPay)。然后,它计算 (Level) 等于什么。那么 (TotalPay) 就是 (amtToPay) 乘以 (Level)。
我的问题:除了计算作者级别的部分之外,一切都很好。例如,如果我将作者输入为“A”级,他的报酬应该是 C 级作者的 1.75 倍。因此,它应该将 (amtToPay) 乘以 1.75,但实际所做的是将其乘以“1”并忽略“.75”。
我是编程新手,我知道可能还有很多其他方法可以编写这个。但请尽量帮助我。谢谢。
【问题讨论】:
-
你从来没有定义过的函数原型,然后用同名的正则变量怎么办?
-
您通常应该只使用整数并跟踪小数位...例如 rateInMil = 75 而不是 .075...然后您可以毫无意外地使用 int 数学。
-
Level的重用也很恶心......而且它会截断双精度值 1-1.99999 是相同的