Friday, April 2, 2010

Program to Solve "System of Linear Equations" using Matrix Class Library

//Linear.cpp
//first save Matrix.h and Matrix.cpp files

#include "iostream.h"
#include "conio.h"
#include "stdlib.h"
#include "dos.h"
#include "string.h"
#include "fstream.h"
#include "matrix.h"

class menu
{
private:
short choice;

public:
menu() {}
friend void logo();
friend void main_menu();
void help();
int get_choice();
};

void logo()
{
cout << "\n\t\t **********************************" << endl;
cout << "\t\t * ---------------------- *" << endl;
cout << "\t\t * SYSTEM OF LINEAR EQUATIONS *" << endl;
cout << "\t\t * ---------------------- *" << endl;
cout << "\t\t **********************************" << endl << endl;
};

void main_menu()
{
cout << "\t ----------------------------------\n\n";
cout << "\t\t 1. START PROGRAM\n" << endl;
cout << "\t\t 2. CALCULATE FROM FILE\n" << endl;
cout << "\t\t 3. HELP\n" << endl;
cout << "\t\t 0. Exit\n" << endl;
cout << "\t\t ----------------------------------\n\n";
cout << "\t\t\t Enter Choice (0/1/2/3) : \n";
}

void menu :: help()
{
clrscr();
cout << "\t\t***************** HELP ****************\n\n";
cout << "This program solves Linear equations of the form ax + by + cz+............mn \n";
cout << "using Cramer's Rule.Enter Coefficient and Input matrix and the program will\n";
cout << "Calculate the Results for you.";
cout << "\n\n\n\t************* How to read the matrix from file ? *************\n\n";
cout << "1. First Save the Size of the Matrix in a file (Text file).\n";
cout << "2. Save the Coefficient Matrix after some line.\n";
cout << "3. Save the Input Matrix after some line.\n";
cout << "4. Every Data must be written after a Blank Space.\n";
cout << "5. In a Program Enter Filename with Extension.\n";
cout << "6. Hit Enter.\n";
cout << "\n\n\n\n\n\n\t\t\tPress any key to continue...";
getch();
}

int menu :: get_choice()
{
gotoxy(53,20);
cin >> choice;
return choice;
}

class linear
{
private:
unsigned short un; //Private Data

public:

float *input; //Pubilc Data

//Constructors

linear() {}
linear(int a)
{
un = a;
input = new float[un];
}

//Functions

int getsize()
{ return un; }

void solve_from_program();
void solve_from_file();
friend double solve(matrix ,float[],int);
void save_to_file();
~linear()
{delete input;}
};

double solve(matrix m,float f[],int pos)
{
matrix temp;
double d,d1;
temp = m;
d = determinant(temp);
if(d == 0)
{
cout << "\nNo Solution - Delta is Zero.";
exit(0);
}

int k = 0;
for(int i = 0;i < temp.row;i++)
for(int j = 0;j < temp.col;j++)
if(j == pos)
temp.mat[i][j] = f[k++];

else
temp.mat[i][j] = m.mat[i][j];

d1 = determinant(temp);
return (d1/d);

}

//Global Variables
char *fname;
float *coefValue,*inputValue,*output;
int unknowns;

void linear :: save_to_file()
{
//Create a file
ofstream destination;
cout << "\n\n\n";
cout.width(53);
cout << "Enter Filename With Extension : ";
cin >> fname;
destination.open(fname);
if(destination.fail())
{
cout << "\nError on Creating a File.";
getch();
exit(0);
}

//Write to file
destination << "Unknowns : " << unknowns << endl << endl; //Unknowns

destination << "Coefficient Matrix" << endl << endl;
for(int i = 0;i < unknowns * unknowns;i++) //Coefficeint Matrix
{
destination << coefValue[i] << ' ';
if((i+1) % unknowns == 0)
destination << endl;
}

destination << endl << endl;
destination << "Input Matrix" << endl << endl;
for(i = 0;i < unknowns;i++) //Input Matrix
destination << inputValue[i] << ' ';

destination << endl << endl;
destination << "Output Matrix" << endl << endl;
for(i = 0;i < unknowns;i++) //Output Matrix
destination << output[i] << '\t';

if(destination.good())
{
cout << "\n\n\n\n\n";
cout.width(53);
cout << "Data Saved Successfully.\n";
cout.width(54);
cout << "Press any key to continue...";
}

//Close File
destination.close();
}

void linear :: solve_from_program()
{
clrscr();
int u,as = 65;
char var[7],varTemp[7];
cout << "How Many Unknowns ? ";
cin >> u;
unknowns = u;
linear l(u); //Dynamic Constructor initialization
double d,value;
matrix coef(u,u),temp;

coefValue = new float[u * u];
inputValue = new float[u];
output = new float[u];

cout << "\nEnter Coefficient Matrix:\n";
cin >> coef;

int k = 0;
for(int i = 0;i < u;i++)
for(int j = 0;j < u;j++)
coefValue[k++] = coef.getvalue(i,j);

cout << "\nEnter Input Matrix:\n";
for(i = 0;i < l.getsize();i++)
{
cin >> l.input[i];
inputValue[i] = l.input[i];
}

temp = coef;
cout << endl << "Output Matrix\n\n";
for(i = 0;i < l.getsize();i++)
{
itoa((i+1),varTemp,10);
strcpy(var,"X");
strcat(var,varTemp);
value = solve(temp,l.input,i);
output[i] = value;
cout << var << " = " << value << endl;
as++;
}

cout << "\n\n\n\n\n";
cout.width(47);
cout << "Press S to Save..." << endl;
cout.width(57);
cout << "Press any other key to continue...";

char choice;
choice = getch();
if(choice == 's' || choice == 'S')
{
clrscr();
linear l;
l.save_to_file();
getch();
}

}

void linear :: solve_from_file()
{
clrscr();
ifstream source;
char filename[10],temp[10],var[7],varTemp[7];
unsigned short i,j,k = 0,tmp = 0,u,as = 65;
float value,*hold;
cout << "Enter Filename With Extension (must be a Notepad File): ";
cin >> filename;
source.open(filename);
if(source.fail())
{
cout << "\n\n\n\n\n";
cout.width(50);
cout << "Unable to Open File." << endl;
cout.width(54);
cout << "Press any key to continue...";
getch();
return;
}

cout << "\nHow Many Unknowns ? ";
source >> u;
cout << u << endl;
unknowns = u;
linear l(u);
matrix coef(u,u);
hold = new float[u * u];
coefValue = new float[u * u];
inputValue = new float[u];
output = new float[u];
j = 0;
float f;
for(i = 0;i < (u * u);i++)
{
source >> f;
hold[j++] = f;
source.ignore();
}

for(i = 0;i < u;i++)
for(j = 0;j < u;j++)
{
coefValue[k] = hold[k];
coef.putvalue(i,j,hold[k++]);
}

cout << "\nCoefficient Matrix:\n";
cout << coef;
tmp = 0;
while(u != tmp)
{
source >> f;
inputValue[tmp] = f;
l.input[tmp++] = f;
source.ignore();
}

cout << "\nInput Matrix\n";
for(i = 0;i < u;i++)
cout << l.input[i] << '\t';

cout << endl << "\nOutput Matrix\n\n";
for(i = 0;i < l.getsize();i++)
{
itoa((i+1),varTemp,10);
strcpy(var,"X");
strcat(var,varTemp);
value = solve(coef,l.input,i);
output[i] = value;
cout << var << " = " << value << endl;
as++;
}

cout << "\n\n\n\n\n";
cout.width(47);
cout << "Press S to Save..." << endl;
cout.width(57);
cout << "Press any other key to continue...";

char choice;
choice = getch();
if(choice == 's' || choice == 'S')
{
clrscr();
linear l;
l.save_to_file();
getch();
}

source.close();
}

int main()
{
menu m;
linear l;
do
{
clrscr();
logo();
main_menu();
short choice = m.get_choice();
switch(choice)
{
case 0:
clrscr();
cout << "\n\n\n\n\n\n\n\n\n\n";
cout.width(56);
cout << "System of Linear Equations.\n\n";
cout.width(56);
cout << "A Project by Ankit Pokhrel.\n\n";
cout.width(57);
cout << "Thank You for Using This Program.";
delay(1200);
return EXIT_SUCCESS;

case 1:
l.solve_from_program();
break;

case 2:
l.solve_from_file();
break;

case 3:
m.help();
break;

default:
cout << "\n\t\t\tPlease select appropriate option.";
delay(1000);
break;
}

}while(1);

}

No comments:

Post a Comment