// Tan Ming Hui A20EC0155 2021/01/17
// This program using pointer concept to calculate the total flooring cost of a house that has several rooms
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cmath>
# include <cstdlib>
using namespace std;

//function prototype
double *readRoomsize (int);
double *extractLinoInfo(char []);

int main ()
{
	// example Run
	char code [10];
	int room,i;
	double *z,*y,*x,*w,totallino=0,totalcost=0; // w hold the cost, x hold info of linoleum, y hold size of room, z hold the number of lino need
	cout << "What is the code of linoleum to use?: ";
	cin >> code;
	cout << "How many rooms to floor?: ";
	cin >> room;
	y=new double [room]; //dynamic array
	y=readRoomsize(room); // hold the size of each room
	cout << "\n\nThe flooring cost has been written into the output file";
	
	//dynamic array
	x= new double [4];
	z= new double [room];
	w= new double [room];
	
	x=extractLinoInfo (code); // hold the info of linoleum based on code enter
	
	for (i=0;i<room;i++)
	{
		z[i]=ceil ( *(y+i)/x[3] );
		w[i]=*(z+i)*x[2];
		totallino += z[i];
		totalcost += w[i];
	}

	//output file
	ofstream data;
	data.open("Linoleum.txt");	// open output file
	
	data << fixed << setprecision(2);
	data << "Linoleum Size: " << x[0] << " Feet x " << x[1] <<" Feet" <<endl;
	data << "Linoleum Price: RM "<< x[2] <<" per piece" << endl;
	
	data << endl <<endl << left;
	data <<setw(12)<< "Room No." << setw(20) << "Size (sqrt)"
		<< setw(30) << "Number of Linoleum (pieces)" 
		<< setw(9) << "Cost" << endl;
	for (i=0;i<room;i++)
	{
		data << setw(12) << (i+1);
		data << setw(20) << y[i];
		data << setw(30) << z[i];
		data << setw(3) << "RM"<<w[i];
		data << endl;
	}
	data<<"\n\nTotal Linoleum Required: " << totallino <<" Pieces"<< endl;
	data<<"Total Cost: RM "<< totalcost;
	data.close();	// close the file

	// free the dynamic memory and point to null
	delete []w;
	delete []y;
	delete []z;
	w=0,y=0,z=0;
	return 0;
}

double *readRoomsize(int r)
{
	int i;
	double *width, *length, *size;
	width = new double [r];
	length = new double [r];
	size =new double [r];
	cout << "\n\nEnter the width and length of each room: "<< endl;
	for (i=0;i<r;i++)
	{
		cout << "Room #" << (i+1) << " => ";
		cin >> width[i];
		cin >> length[i];
	}
	
	for (i=0;i<r;i++)
	{
		size[i]= width[i] *length[i];
	}

	return size;
	
	// free the dynamic memory and point to null
	delete [] width;
	delete [] length;
	delete [] size;
	width=0, length=0, size=0;
}

double *extractLinoInfo (char code[])
{
	double Lino_width,Lino_length,price,Lino_size,a=12.0,*x;
	for (int i=1;i<10;i++)
	{
		code[i]=code[i]-'0';
	}
	
	Lino_width =static_cast<double> ( code[1]*10+code[2])/a ; 
	Lino_length =static_cast<double>(code[3]*10+code[4])/a ;
	Lino_size = Lino_width * Lino_length;
	price = static_cast<double> (code[5] *10+code[6]+code[7]/10.0+code[8]/100.0);
	x= new double [4];
	x[0]=Lino_width;
	x[1]=Lino_length;
	x[2]=price;
	x[3]=Lino_size;

	return x;
	//free dynamic memory and point to null
	delete[]x;
	x=0;

}


