Website Design United States, Website Design California, Website Designing United States, Website Designing California

Simultaneous Linear Equation Solver in MFC - C++

This program solves a simultaneous linear equation which is in the following format:
ax + by = c
dx + ey = f

/*

Main Dialog: IDD_DIALOG1
Format of Edit Controls and Static controls
[IDC_EDIT_A1]x + [IDC_EDIT_B1]y = [IDC_EDIT_C1]
[IDC_EDIT_A2]x + [IDC_EDIT_B2]y = [IDC_EDIT_C2]
"Solve" Command Button : IDC_BUTTON_SOLVE
"Reset" Command Button : IDC_BUTTON_RESET
Solution 'x' Edit Control = IDC_EDIT_RX
Solution 'y' Edit Control = IDC_EDIT_RY

Menu:
About : IDABOUT
Exit : IDOK

About Dialog Box:[IDD_DIALOG2]
"OK" Command Button : IDOK

 

This code is written by Sanchit Karve. If you want the compiled version of this code send me a message through programmers-corner.com or email me at born2c0de@hotmail.com

Click Project->Settings and select "Use MFC in a Shared DLL" for this program to work.

*/

#include <afxwin.h>
#include "resource.h"

class dlgabout:public CDialog
{
public:
dlgabout():CDialog(IDD_DIALOG2)
{
}
};

class dlgmain:public CDialog
{
private:
float a1,a2,b1,b2,c1,c2,x,y;
CString strA1,strA2,strB1,strB2,strC1,strC2;

public:
dlgmain():CDialog(IDD_DIALOG1)
{
a1=a2=b1=b2=c1=c2=x=y=NULL;
}

void about()
{
dlgabout a;
a.DoModal();
}

void reset()
{
SetDlgItemText(IDC_EDIT_A1,"");
SetDlgItemText(IDC_EDIT_A2,"");
SetDlgItemText(IDC_EDIT_B1,"");
SetDlgItemText(IDC_EDIT_B2,"");
SetDlgItemText(IDC_EDIT_C1,"");
SetDlgItemText(IDC_EDIT_C2,"");
SetDlgItemText(IDC_EDIT_RX,"");
SetDlgItemText(IDC_EDIT_RY,"");
}

void GetData()
{
GetDlgItemText(IDC_EDIT_A1,strA1);
a1=atof((LPCTSTR)strA1);
GetDlgItemText(IDC_EDIT_A2,strA2);
a2=atof((LPCTSTR)strA2);
GetDlgItemText(IDC_EDIT_B1,strB1);
b1=atof((LPCTSTR)strB1);
GetDlgItemText(IDC_EDIT_B2,strB2);
b2=atof((LPCTSTR)strB2);
GetDlgItemText(IDC_EDIT_C1,strC1);
c1=atof((LPCTSTR)strC1);
c1*=-1;
GetDlgItemText(IDC_EDIT_C2,strC2);
c2=atof((LPCTSTR)strC2);
c2*=-1;

}

void solve()
{
GetData();
if((a1/b1)==(a2/b2))
{
MessageBox("Lines are parallel.Equation has no answer","Error",MB_ICONERROR);
}
else
{
//x = ((B2 * C1) / B1 - C2) / (A2 - (B2 * A1) / B1)
//y = (-1 * A1 * x) / B1 - C1 / B1
x=((b2*c1)/b1-c2)/(a2-(b2*a1)/b1);
y=(-1*a1*x)/b1-c1/b1;
char tmp[10];
sprintf(tmp,"%.3f",x);
SetDlgItemText(IDC_EDIT_RX,tmp);
sprintf(tmp,"%.3f",y);
SetDlgItemText(IDC_EDIT_RY,tmp);
}
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(dlgmain,CDialog)
ON_COMMAND(IDABOUT,about)
ON_COMMAND(IDC_BUTTON_RESET,reset)
ON_COMMAND(IDC_BUTTON_SOLVE,solve)
END_MESSAGE_MAP()

class App:public CWinApp
{
public:
int InitInstance()
{
dlgmain d;
d.DoModal();
return 1;
}
};

App obj1;

WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com