0

该程序基本上应该从文件中读取数据,然后根据其内容处理该数据。这有点像一家模拟餐饮公司,变量是成人人数、儿童人数、膳食类型(豪华或标准)、一天类型(周末[是或否]、初始押金等以及附加费、税金) , total 等在 CalcData 函数中计算,具体取决于该数据是什么(即如果是豪华餐(D 或 S),价格是 25.80 美元,而不是 21.75 美元(标准),如果是周末(是或否),附加费加到总账单中,并根据总金额给予折扣)。

虽然我认为我在我的函数中过度使用了引用,但程序在没有错误检查部分的情况下运行良好(即检查输入是否有效 - 成人/儿童/初始存款的数量没有负数,除了 S/ 没有其他字母D 和/或 Y/N 等)。我最初使用了一个返回 bool 的“isValid”函数和一个“outputErrorFile”函数,并在 main 中使用 if/else - 如果数据无效,则输出到错误文件,如果它不是无效的,然后只是将其输出到“帐单”文本文件。我将两者结合在一个“checkValid”函数中。我认为做同样的事情,所以没有必要有两个单独的功能。

现在它正在将所有内容输出到错误文件(具体来说,布尔变量“valid”在我的所有数据上始终为假)。我确定我在里面做了一些愚蠢的事情。我并不真正关心输出到控制台的内容,只关心输出到文本文件的内容...感谢您的关注。

谢谢。

输入文件(成人、儿童、豪华或标准餐、周末(Y/N)、初始存款):

10 0 SY 100.00

27 3 岁 57.50

125 17 DN 0.00

4 0 序列号 25.00

0 25 SY 23.75

250 43 DN 500.00

0 0 DN 0.0

10 0 里亚尔 10.00

17 3 博士 15.00

5 0 元 275.00

-3 10 元 20.00

14 -1 SN 30.00

20 3 岁 -10.00

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void getData(int &, int &, char &, char &, float &);
void checkValid(int &, int &, char &, char &, float &, bool &);
void calcData(int, int, char, char, float, float &, float &, float &, float &);
void sendData(int, int, char, char, float, float &, float &, float &, float &);

ifstream inFile;
ofstream outFile("Billing_Statement.txt");
ofstream error_Report("Error_Report.txt");

//Declare the tax rate and weekend surcharge as constants.

const float taxRate = 0.18;
const float weekendSurcharge = .07;

int main()
{

bool valid = true;
float mealCost;
float totalTax;
float totalSurcharge;
float discountAmount;
int numAdults;
int numChildren;
char mealType;
char dayType;
float depositAmount;

cout << "\nThis program will calculate data for a catering company " << endl;

outFile << " Adults " << "Children  " << "Meal " << " Weekend " << setw(9) << "Deposit "
<< setw(6) << "Tax" << setw(11) << "Surcharge" << setw(10) << "Discount" << setw(12) <<
"Meal Cost" << endl;
error_Report << " Adults " << "Children  " << "Meal " << " Weekend " << setw(9) <<  
"Deposit " << endl;

inFile.open("file.txt");

if (!inFile) {
cout << "nError: File could not be opened. ";
exit(1);
}

while (!inFile.eof()) {

getData(numAdults, numChildren, mealType, dayType, depositAmount);
checkValid(numAdults, numChildren, mealType, dayType, depositAmount, valid);

if (valid == true)
{
    calcData(numAdults, numChildren, mealType, dayType, depositAmount, totalTax,   
totalSurcharge, discountAmount, mealCost);
    sendData(numAdults, numChildren, mealType, dayType, depositAmount, mealCost,
totalTax, totalSurcharge, discountAmount);
}}

cout << "\nA copy of this has created for your convenience in the file, 
\"Billing_Statement.txt \"" << endl;

inFile.close();
outFile.close();
error_Report.close();

return 0;

}

void getData(int &numAdults, int &numChildren, char &mealType, char &dayType, float 
&depositAmount)
{
inFile >> numAdults >> numChildren >> mealType >> dayType >> depositAmount;
}

void checkValid(int &numAdults, int &numChildren, char &mealType, char &dayType, float
&depositAmount, bool & valid)
{

if (numAdults < 0 || numChildren < 0)
valid = false;
else if (mealType != 'D' || mealType != 'S')
valid = false;
else if (dayType != 'Y' || dayType != 'N')
valid = false;
else if (depositAmount < 0)
valid = false;

else
valid = true;

if (valid == false) {

error_Report << setw(7) << numAdults << setw(9) << numChildren << setw(6) << mealType <<
setw(9) << dayType << setw(9) << right << depositAmount << setw(8) << endl;
}
}

void calcData(int numAdults, int numChildren, char mealType, char dayType, float   
depositAmount, float &totalTax, float &totalSurcharge, float &discountAmount, float 
&mealCost)
{

if (mealType == 'S') {

mealCost = ((numAdults * 21.75) + (numChildren * (21.75 * .60)));
totalTax = mealCost * taxRate;
mealCost += taxRate;

if (dayType == 'Y') {
    totalSurcharge = mealCost * weekendSurcharge;
    mealCost += totalSurcharge;
}}

else {

mealCost = ((numAdults * 25.80) + (numChildren * (25.80 * .60)));
totalTax = mealCost * taxRate;
mealCost += taxRate;

if (dayType == 'Y') {
    totalSurcharge = mealCost * weekendSurcharge;
    mealCost += totalSurcharge;
    }
}

if (mealCost < 100) {

discountAmount = .015 * mealCost;
mealCost -= discountAmount;
}

else if (mealCost >= 100 && mealCost < 400) {

discountAmount = .025 * mealCost;
mealCost -= discountAmount;
}

else if (mealCost >= 400) {

discountAmount = .035 * mealCost;
mealCost -= discountAmount;
}
}

void sendData(int numAdults, int numChildren, char mealType, char dayType, float 
depositAmount, float &mealCost, float &totalTax, float &totalSurcharge, float 
&discountAmount)
{
outFile << fixed << showpoint << setprecision(2);
outFile << setw(7) << numAdults << setw(9) << numChildren << setw(6) << mealType << 
setw(9) << dayType << setw(9) << right << depositAmount << setw(8) << totalTax << 
setw(10) << totalSurcharge << setw(10) << right << discountAmount << setw(12) << right 
<< mealCost << endl;
}
4

1 回答 1

2

看来您检查类型,例如

mealType != 'D' || mealType != 'S'

将始终屈服true,因此valid始终设置为false. 你可能是说

!(mealType == 'D' || mealType == 'S')

或用布尔逻辑重写

mealType != 'D' && mealType != 'S'

顺便说一句,您的程序中还有其他问题。例如,我最讨厌的一个问题是:使用file.eof()控制输入循环总是错误的!您将处理最后一行两次,或者如果输入格式错误,则以无限循环结束。尝试读取输入是否成功,您始终需要检查!流不可能提前知道您将尝试阅读什么以及这是否会成功。

于 2013-11-30T23:12:25.973 回答