Untitled

mail@pastecode.io avatar
unknown
c_cpp
19 days ago
979 B
1
Indexable
Never
#include <iostream>
#include <fstream>

using namespace std;

bool readData(const string& fileName, int& a, int& b, int& c)
{
	ifstream inputFile(fileName);

	if (!inputFile)
	{
		cerr << "Nie mozna otworzyc pliku " << fileName << '!' << endl;
		return false;
	}

	inputFile >> a >> b >> c;
	inputFile.close();

	return true;
}

bool saveData(const string& fileName, double pole)
{
	ofstream outputFile(fileName);

	if (!outputFile)
	{
		cerr << "Nie mozna otworzyc pliku " << fileName << '!' << endl;
		return false;
	}

	outputFile << pole << '\n';
	outputFile.close();

	return true;
}

double calculateArea(int a, int b, int c)
{
	return 0.5 * (a + b) * c;
}

int main()
{
	int a, b, c;

	if (!readData("dane.txt", a, b, c))
	{
		return -1;
	}

	double area = calculateArea(a, b, c);

	if (!saveData("pole.txt", area))
	{
		return -1;
	}

	cout << "Pole trapezu zapisane do pliku pole.txt" << endl;

	return 0;

}
Leave a Comment