⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex3_1.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>

class Accumulator
{
	private:
		float value;
	public:
		Accumulator (float v = 0);
		void Add (float x);
		void Print (void) const;
};

Accumulator::Accumulator (float v) : value(v)
{}

void Accumulator::Add (float x)
{
	value += x;
}

void Accumulator::Print (void) const
{
	// print in fixed format with exactly two decimal places
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << value << endl;
}

void main(void)
{
	float startbalance;
	float transaction = -1;
	
	cout << "Enter the starting balance: ";
	cin >> startbalance;
	
	Accumulator balance(startbalance), credit, debit;
	
	cout << "Enter transactions stopping on a 0" << endl;
	for ( ; ; )
	{	
		cin >> transaction;
		if (transaction == 0)
			break;
		else if (transaction < 0)
			debit.Add(-transaction);
		else
			credit.Add(transaction);
		
		balance.Add(transaction);
	}
	
	cout << "Balance is ";
	balance.Print();
	cout << "Credit is ";
	credit.Print();
	cout << "Debit is ";
	debit.Print();
}
/*
<Run>

Enter the starting balance: 250
Enter transactions stopping on a 0
25.00
-10.50
35.50
70.00
-100.00
0
Balance is 270.00
Credit is 130.50
Debit is 110.50
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -