gpt4 book ai didi

c++ - 类不起作用的折旧函数

转载 作者:行者123 更新时间:2023-11-28 05:59:17 25 4
gpt4 key购买 nike

我正在编写一个程序,该程序在名为 Asset 的类中具有单倍和双倍折旧功能。我在双重折旧函数以及如何将其实现到我的类中时遇到了麻烦。此外,关于如何验证用户输入的任何建议(即没有负数,没有零,并且在询问字符时只有一个字符)?这是我的主文件、类和类头文件。

当我运行这个程序并尝试使用双重折旧函数时,它只使用直线折旧函数。它忽略了 Assets 类别中的双重功能。这是为什么?此外,在我的 main 中,当有人输入无效输入时,它会执行 if 循环,但随后会继续 main 的其余部分,我如何让它回到 do while 循环的顶部?

// Depreciation.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include "Asset.h"
#include <limits>

using namespace std;
using namespace System;
char choice;


int main()
{

double c;
double s;
double l;
int y = 0;
bool wrongdata = false;

//normal input task; welcome message, input of 3 fields and output features - with full data validation

cout << "Welcome to the Depreciation calculator!"<<endl;
do{

cout << "Please enter an Asset Cost: " << endl;
cin >> c;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Please input a correct value" << endl;
wrongdata = true;

}
if (c == 0)
{
cout << "Thanks for using my program!" << endl;
break;

}

cout<< "Please enter the Salvage Value: " << endl;
cin >> s;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please input a correct value" << endl;
wrongdata = true;
}
if (s == 0)
{
cout << "Thanks for using my program!"<<endl;
break;
}
cout << "Please enter the Asset life (in years): " << endl;
cin >> l;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please input a correct value" << endl;
wrongdata = true;
}
if (l == 0)
{

cout << "Thanks for using my program!" << endl;
break;
}

Asset a = Asset(c, s, l);
cout << "The annual depreciation is equal to " << a.getAnnualDep() << " for Straight-Line and \n" << a.getDDDep(y) << " for one year of Double Declining" << endl;

cout << "Would you like to see a depreciation schedule for Straight-Line, DDL, or Neither? (S/D/N)" << endl;
cin >> choice;
choice = toupper(choice);
if (choice = 'S')
{
cout << "Year Start Value Depreciation End Value" << endl;
for (int i = 1; i <= a.getorigLife(); i++)
{

cout << i << "\t\t" << a.getBegBal(i) << "\t\t" << a.getAnnualDep() << "\t\t" << a.getEndBal(i) << endl;
}

}
else if (choice = 'D')
{
cout << "Year Start Value Depreciation End Value" << endl;
for (int j = 1; j <= a.getorigLife(); j++)
{
cout << j << "\t\t" << a.getDDBBal(j) << "\t\t" << a.getDDDep(y) << "\t\t" << a.getDDEBal(j) << endl;
}

}
else if (choice = 'N')
{
cout << "Thanks for using my program" << endl;
break;
}
else
{
cout << "Could not understand your input";
wrongdata = true;
}

} while (wrongdata);

system("pause");
return 0;
}


//asset class
#include "stdafx.h"
#include "Asset.h"




Asset::Asset(double c, double s, int L)
{
ocost = c;
osalv = s;
oLife = L;
anndep = (c - s) / L;
double SLrate = (100.0 / L)/100;
DDanndep = (2 * SLrate) * c;
double DDrate = (2 * SLrate);


//declare arrays for starting and ending values
bbal = new double[oLife];
ebal = new double[oLife];
DDebal = new double[oLife];
DDbbal = new double [oLife];

//calculate straight line depreciation
bbal[0] = ocost;
DDbbal[0] = ocost;

for (int i = 0; i < oLife; i++)
{
if (i > 0)
{
bbal[i] = ebal[i - 1];
}

ebal[i] = bbal[i] - anndep;

}

//calculate double depreciation



for (int j = 0; j < oLife; j++)
{
if (j > 0)
{
DDbbal[j] = DDebal[j - 1];
}

DDebal[j] = DDbbal[j] - (DDbbal[j] * DDrate);
DDbbal = DDebal;


}



}

double Asset::getAnnualDep()
{

return anndep;
}
double Asset::getOrigCost()
{

return ocost;
}
double Asset::getOrigSalvage()
{
return osalv;
}
int Asset::getorigLife()
{
return oLife;
}
double Asset::getBegBal(int y)
{
return bbal[y - 1];
}
double Asset::getEndBal(int y)
{
return ebal[y - 1];

}
double Asset::getDDDep(int y)
{
return DDanndep;
}
double Asset::getDDBBal(int y)
{
return DDbbal[y - 1];
}
double Asset::getDDEBal(int y)
{
return DDebal[y - 1];
}

Asset::~Asset()
{
}

//class header
#pragma once
class Asset
{
public:
Asset(double Cost, double Salvage, int Life); //constructor
~Asset(void); //destructor

double getOrigCost(), getOrigSalvage(), getAnnualDep();
double getBegBal(int year), getEndBal(int year), getDDBBal(int year), getDDEBal(int year), getDDDep(int year);
int getorigLife();
char choice;

private:

double ocost, osalv, anndep, DDanndep;
int oLife;
double* bbal;
double* ebal;
double*DDebal;
double*DDbbal;


};

最佳答案

请注意,在 C++ 中,= 是赋值,== 是比较。

在像这样的 if 语句中:

if (choice = 'S')

您正在将“S”分配给 choice,然后检查它是否非零。

将其更改为:

if (choice == 'S')

关于c++ - 类不起作用的折旧函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589685/

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