C++文件的存取

实例:

#include <iostream>
#include <fstream>
using namespace std;

double CalculateTax(double);

int main()
{
	char* FileNameIn = "Income.txt";
	char* FileNameOut = "Tax.txt";
	double Income, Tax;

	ifstream FileInput;//定义了名为FileInpu的输入型文件数据流
	ofstream FileOutput;//定义了名为FileOutput的输出型文件数据流
	FileInput.open(FileNameIn);
	FileOutput.open(FileNameOut);

	if (!FileInput)
	{
		cout << "文件:" << FileNameIn
			<< "打开失败!" << endl;
		exit(1);//要求操作系统立即中止此程序的执行
	}

	if (!FileOutput)
	{
		cout << "文件:" << FileNameOut
			<< "存档失败!" << endl;
		exit(1);
	}

	FileInput >> Income;
	Tax = CalculateTax(Income);
	cout << "要缴纳的综合所得税:"
		<< Tax << "元\n";
	FileOutput << "要缴纳的综合所得税:"
		<< Tax<<"元\n";
	FileInput.close();//关闭输入文件
	FileOutput.close();//关闭输出文件
	return 0;
}

double CalculateTax(double GIncome)
{
	double Tax;
	if (GIncome<0.0)
	{
		cout << "没有意义!\n";
	}
	else if (GIncome<330000.0)
	{ 
		Tax = GIncome*0.06;
	}
	else if (GIncome < 890000.0)
	{
		Tax = GIncome*0.13-23100;
	}
	else if (GIncome < 1780000.0)
	{
		Tax = GIncome*0.21-94300;
	}
	else if (GIncome < 3340000.0)
	{
		Tax = GIncome*0.3-254500;
	}
	else
		Tax = GIncome*0.4 - 588500;
	return Tax;
}

文件夹中含有:

C++文件的存取_第1张图片

输出结果:
C++文件的存取_第2张图片

你可能感兴趣的:(C++文件的存取)