gpt4 book ai didi

c++ - 使用派生类的程序的错误输出。不为心之光

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

注意:如果这个程序看起来很痛苦,我深表歉意。这是我的第一个使用派生类的程序。我计划在完成我想要的操作后将其提交给代码审查,这样我就可以收到有关如何更好地设计它的提示。欢迎在这里提供这些提示,但我写这篇文章的主要目的是让它正常工作。

该程序处理银行账户的取款和存款,然后应用利率。

问题:出于某种原因,在处理类类型为“checkingAccount”的帐户时,checkingBalance 的值似乎卡住了,并且在循环的其余部分没有更新为新值。这很奇怪,因为同一个循环用于处理另一个派生类“savingsAccount”并且它工作正常并且“checkingAccount”类型的帐户之一甚至似乎已被正确处理所以我完全不知道发生了什么。

如果有人能帮我解决这个问题,我将不胜感激。请不要犹豫,让我指定任何内容,我会尽快回复。

正在读取的数据:

enter image description here

输出: enter image description here

我怀疑问题出在的代码:

//This loop is exited early:

for (int j = 0; j < transactionNum; j++)
{
inFile >> transactionTypeTemp >> amountTemp;
inFile.get(discard);
ba.applyTransaction(accountType, transactionTypeTemp, amountTemp, j);
}

//when something in here occurs?
// c = j from loop

double checkingAccount:: applyTransaction(char transactionTypeTemp, int amountTemp, int c, double checkingBalance)
{
if (transactionTypeTemp == 'D')
{
checkingBalance = checkingBalance + amountTemp;
}
else if (transactionTypeTemp == 'W')
{
if (checkingBalance < amountTemp)
{
cout << "error: transaction number " << c + 1 << " never occured due to insufficent funds." << endl;
}
else
{
checkingBalance = checkingBalance - amountTemp;
if(checkingBalance < minimumBalance) //if last transaction brought the balance below minimum balance
{
checkingBalance = (checkingBalance - serviceCharge); //apply service charge
}
}
}

return checkingBalance;
}

整个程序:

//header file
#include <iostream>
#include <fstream>


using namespace std;

class bankAccount
{
public:
bankAccount();
void setAccountInfo(int accountNumTemp, double balanceTemp);
void prePrint(char accountType);
void applyTransaction(char accountType, char transactionTypeTemp, int amountTemp, int j);
void applyInterest(char accountType);
void postPrint();

private:
int accountNumber;
double balance;
};

class checkingAccount: public bankAccount
{
public:
void prePrint(int accountNumber, char accountType, double checkingBalance);
checkingAccount();
double applyTransaction(char transactionTypeTemp, int amountTemp, int c, double checkingBalance);
double applyInterest(double checkingBalance);

private:
float interestRate;
int minimumBalance;
float serviceCharge;

};

class savingsAccount: public bankAccount
{
public:
void prePrint(int savingsAccountNumber, char accountType, double savingsBalance);
savingsAccount();
double applyTransaction(char transactionTypeTemp, int amountTemp, int c, double savingsBalance);
double applyInterest(double checkingBalance);

private:
float interestRate;
};

//class implementation .cpp file

bankAccount:: bankAccount()
{
accountNumber = 0;
balance = 0;
}

void bankAccount:: setAccountInfo(int accountNumTemp, double balanceTemp)
{
accountNumber = accountNumTemp;
balance = balanceTemp;
}

void bankAccount:: prePrint(char accountType)
{
if(accountType == 'C')
{
int checkingAccountNumber = accountNumber;
double checkingBalance = balance;
checkingAccount ca;
ca.prePrint(checkingAccountNumber, accountType, checkingBalance);
}
else if (accountType == 'S')
{
int savingsAccountNumber = accountNumber;
double savingsBalance = balance;
savingsAccount sa;
sa.prePrint(savingsAccountNumber, accountType, savingsBalance);
}


}

void bankAccount:: applyTransaction(char accountType, char transactionTypeTemp, int amountTemp, int j)
{
int c;
c = j;
double checkingBalance, savingsBalance;
checkingAccount ca;
savingsAccount sa;

if (accountType == 'C')
{
checkingBalance = balance;
balance = ca.applyTransaction(transactionTypeTemp, amountTemp, c, checkingBalance);
}
else if (accountType == 'S')
{
savingsBalance = balance;
balance = sa.applyTransaction(transactionTypeTemp, amountTemp, c, savingsBalance);
}

}

void bankAccount:: applyInterest(char accountType)
{
double checkingBalance, savingsBalance;
checkingAccount ca;
savingsAccount sa;

if (accountType == 'C')
{
checkingBalance = balance;
balance = ca.applyInterest(checkingBalance);
}
else if (accountType == 'S')
{
savingsBalance = balance;
balance = sa.applyInterest(savingsBalance);
}


}

void bankAccount:: postPrint()
{
cout << "Balance after processing: " << balance << endl;
}

checkingAccount:: checkingAccount()
{
interestRate = .02;
minimumBalance = 500;
serviceCharge = 20;
}

void checkingAccount:: prePrint(int checkingAccountNumber, char accountType, double checkingBalance)
{
cout << "Account Number:" << checkingAccountNumber << " account type:" << accountType << " Starting Balance:" << checkingBalance << endl;
}

double checkingAccount:: applyTransaction(char transactionTypeTemp, int amountTemp, int c, double checkingBalance)
{
if (transactionTypeTemp == 'D')
{
checkingBalance = checkingBalance + amountTemp;
}
else if (transactionTypeTemp == 'W')
{
if (checkingBalance < amountTemp)
{
cout << "error: transaction number " << c + 1 << " never occured due to insufficent funds." << endl;
}
else
{
checkingBalance = checkingBalance - amountTemp;
if(checkingBalance < minimumBalance) //if last transaction brought the balance below minimum balance
{
checkingBalance = (checkingBalance - serviceCharge); //apply service charge
}
}
}

return checkingBalance;
}

double checkingAccount:: applyInterest(double checkingBalance)
{
checkingBalance = (checkingBalance + (checkingBalance * interestRate));
return checkingBalance;
}
savingsAccount:: savingsAccount()
{
interestRate = .04;
}


void savingsAccount:: prePrint(int savingsAccountNumber, char accountType, double savingsBalance)
{
cout << "Account Number:" << savingsAccountNumber << " account type:" << accountType << " Starting Balance:" << savingsBalance << endl;
}

double savingsAccount:: applyTransaction(char transactionTypeTemp, int amountTemp, int c, double savingsBalance)
{
if (transactionTypeTemp == 'D')
{
savingsBalance = savingsBalance + amountTemp;
}
else if (transactionTypeTemp == 'W')
{
if (savingsBalance < amountTemp)
{
cout << "error: transaction number" << c + 1 << " never occured due to insufficent funds." << endl;
}
else
{
savingsBalance = savingsBalance - amountTemp; //apply transaction
}
}

return savingsBalance;
}

double savingsAccount:: applyInterest(double savingsBalance)
{
savingsBalance = (savingsBalance + (savingsBalance * interestRate));
return savingsBalance;
}

//main .cpp file
int main()
{
ifstream inFile;
int numberOfAccounts, accountNumTemp, transactionNum, amountTemp;
double balanceTemp;
char discard, accountType, transactionTypeTemp;
bankAccount ba;

cout << "Processing account data..." << endl;

inFile.open("Bank.txt");

if (!inFile)
{
for (int a = 0; a < 20; a++)
cout << endl;
cout << "Cannot open the input file."
<< endl;
return 1;
}

inFile >> numberOfAccounts;
inFile.get(discard);

for (int i = 0; i < numberOfAccounts; i++)
{
inFile >> accountNumTemp >> accountType >> balanceTemp >> transactionNum;
inFile.get(discard);
ba.setAccountInfo(accountNumTemp, balanceTemp);
ba.prePrint(accountType);

for (int j = 0; j < transactionNum; j++)
{
inFile >> transactionTypeTemp >> amountTemp;
inFile.get(discard);
ba.applyTransaction(accountType, transactionTypeTemp, amountTemp, j);
}
ba.applyInterest(accountType);
ba.postPrint();
}


inFile.close();

return 0;
}

这里是复制粘贴形式的输入,这样其他人就不必输入了:

6
35231 C 500 3
W 250
W 200
W 100
46728 S 2700 2
D 250
W 100
87324 C 500 3
D 300
W 100
W 150
79873 S 800 0
89932 C 3000 2
W 1000
W 1500
98322 C 750 6
D 50
W 75
W 100
W 25
W 30
W 75

最佳答案

嗯...快速编译并运行您的数据,我得到以下输出:

Processing account data...
Account Number:35231 account type:C Starting Balance:500
error: transaction number 3 never occured due to insufficent funds.
Balance after processing: 10.2
Account Number:46728 account type:S Starting Balance:2700
Balance after processing: 2964
Account Number:87234 account type:C Starting Balance:500
Balance after processing: 561
Account Number:79873 account type:S Starting Balance:800
Balance after processing: 832
Account Number:89832 account type:C Starting Balance:3000
Balance after processing: 510
Account Number:98322 account type:C Starting Balance:750
Balance after processing: 484.5

在我看来,其中大部分内容都相当合理。在我的脑海中快速添加,“资金不足”对于第一个帐户似乎是合理的,因为您给它的初始余额为 500,而提款总计为 550。

就我所知道的来说,我认为我不会像您那样编写代码,但是我得到的输出似乎没有几乎与您展示的问题相同。 可能来自未定义的行为(例如,使用未初始化的变量),但我没有花足够的时间查看代码来确定。就我个人而言,我认为我宁愿将时间花在不同的设计上,也不愿花在寻找代码中可能存在的错误上。

编辑:就重新设计代码而言,当您需要在两个派生类中实现不同的代码时,您希望使用虚函数,并在每个派生类中适本地实现它。我没有在每个成员函数中将银行账户创建为局部变量,并在每个成员函数中创建一个新的银行账户对象,而是在读取定义账户的数据时创建一个账户对象,然后将该账户对象用于该对象上的所有交易。

您是否真的需要基类和派生类,这似乎也引发了相当大的疑问。我没有仔细看,但在我看来,这两种帐户的行为几乎(如果不是完全)相同。我看到的唯一区别是储蓄账户不允许取负数,而支票账户允许此类取款,但会收取服务费。尽管您没有对其进行建模,但如果支票账户余额低于某个点,我很确定他们也会开始退回支票。

在这种情况下,在我看来,您可以将其全部建模为一种类型的帐户,但有两个最低级别,以及与违反每个级别相关的费用。第一级意味着允许交易,但要收取费用。第二种表示不允许交易。

对于储蓄账户,水平和费用都设置为 0.0 美元。对于支票账户,“收费”级别为 0.0 美元,“拒绝交易”级别为(比如说)-50 美元。两者的费用都是 50 美元。

关于c++ - 使用派生类的程序的错误输出。不为心之光,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5773031/

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