• entries
61
131
• views
32334

Mathematics

192 views

I have recently discovered a great interest in math. And since we're just learning about Pythagoras at school, I thought it would be cool if I wrote a program for one of his theorems:

l^2 + l^2 = h^2

So I made a program for it. Here it is. If you find some design flaws or places where things could have been done more efficient, please let me know. Please note that the code is not commented [grin]

/* *  main.h *  pythagoras * *  Created by √oyvind on 10.02.06. *  Copyright 2006 __MyCompanyName__. All rights reserved. * */  /*TO BE ADDED *-Possibility to calculate legs instead of only hypotenuse *-save log file  *-comment code */#include #include #include #include using namespace std;struct SLEG{	float length;	float area;};struct SHypotenuse{	float length;	float area;};struct SMENU{	string m_sText;};	class CRATri //Right angled rectangle{	public:		SLEG LEG[1];		SHypotenuse H;  //hypotenuse				CRATri();				float GetLegLength(int) const;		float GetHypotenuseLength()const;		float GetLegArea(int)const;		float GetHypotenuseArea()const;				void CalculateHypotenuseArea();		void CalculateLegLength(int);		void CalculateHypotenuseLength();		void CalculateLegArea(int);		void SetLegLength(int, float);		void SetHypotenuseLength(float);		void SetLegArea(int, float);		void SetHypotenuseArea(float);			}RECT;CRATri::CRATri(){	H.area = ((H.length)*(H.length));	LEG[0].area = ((LEG[0].length)*(LEG[0].length));	LEG[1].area = ((LEG[1].length)*(LEG[1].length));}float CRATri::GetLegLength(int ID)const{	return(LEG[ID].length);}float CRATri::GetHypotenuseLength()const{	return(this->H.length);}float CRATri::GetLegArea(int ID) const{		return(this->LEG[ID].area);}float CRATri::GetHypotenuseArea()const{	return(this->H.area);}void CRATri::SetLegLength(int ID, float value){	this->LEG[ID].length = value;}void CRATri::SetHypotenuseLength(float value){	this->H.length = value;}void CRATri::SetLegArea(int ID, float value){	this->LEG[ID].area = value;}void CRATri::SetHypotenuseArea(float value){	this->H.area = value; }  void CRATri::CalculateHypotenuseArea() {	float temp;	temp = ((this->LEG[0].area)+(this->LEG[1].area));	SetHypotenuseArea(temp);}void CRATri::CalculateLegLength(int ID){	float temp;	switch(ID)	{		case 0:		temp = sqrt((this->H.area)-(this->LEG[1].area));		break;		case 1:		temp = sqrt((this->H.area)-(this->LEG[0].area));		break;	}	SetLegLength(ID, temp);}void CRATri::CalculateHypotenuseLength(){	float temp;	this->LEG[0].area = ((this->LEG[0].length)*(this->LEG[0].length));	this->LEG[1].area = ((this->LEG[1].length)*(this->LEG[1].length));	temp = sqrt((this->LEG[0].area)+(this->LEG[1].area));		this->SetHypotenuseLength(temp);}void CRATri::CalculateLegArea(int ID){	float temp;	switch(ID)	{		case 0:			temp = ((this->H.area)-(this->LEG[1].area));			break;		case 1:			temp = ((this->H.area)-(this->LEG[0].area));			break;	}	}class CPInput{	public:		float Leg1;		float Leg2;		float Hypo;		int GameEnd;		int MenuEnd;		int GameChoices;		SMENU MenuChoice[3]; //Four menu choices										int AskYesNo(string);			void AskLegLength(int);		void AskHypLength();			void Greet();		void PrintMenuChoice(int);		void Menu();		void SetUpGame();}INPUT;void CPInput::SetUpGame(){	this->MenuChoice[0].m_sText = "A hypotenuse of a triangle is the line opposite to the right angle. Illustrated below.\n\n |\ h \n |  \ y  \n |   \ po \n |     \ te \n |      \  nu \n |_______\  se\n";	this->MenuChoice[1].m_sText = "The legs of a triangle is the two lines opposite to the hypotenuse: \n l \n e \n g \n  l  e   g \n";	this->MenuChoice[2].m_sText = "Programmed by Zeptera in C++ 11th February 2006. Polished 12th February 2006. All Rights Reserved\n";}int CPInput::AskYesNo(string a){	int ans;	cout << a << "?"<	cin>>ans;	return ans;}				void CPInput::AskLegLength(int ID){	float ans;	cout << "Please write the length of leg "<<  (ID+1) <<"." << endl;	cin >> ans;	RECT.SetLegLength(ID, ans);}void CPInput::AskHypLength(){	float ans;	cout << "Please tell me what the length of the Hypotenuse is."<	cin >> ans;	RECT.SetHypotenuseLength(ans);}void CPInput::PrintMenuChoice(int ID){	cout << (this->MenuChoice[ID].m_sText) <}void CPInput::Greet(){	cout << "Welcome to the Pythagoras program!\nHere you can write in numbers \nand I'll calculate the opposite leg or hypotenuse for you!"<	while(MenuEnd != 1)	{		this->Menu();	}	}void CPInput::Menu(){	int input;	cout << "\t " << "Menu Selections: "<	cout << "1. " << "What is the hypotenuse of a triangle?" << endl;	cout << "2. " << "What is the legs of a triangle?" << endl;	cout << "3. " << "Start game. "<< endl;	cout << "4. " << "Credits " <	cin >> input;	switch(input)	{		case 1:			this->PrintMenuChoice(0);			break;		case 2:			this->PrintMenuChoice(1);			break;			case 3:			MenuEnd = 1;			break;		case 4:			this->PrintMenuChoice(2);			break;		default:			break;			}}		 int main() { 	INPUT.SetUpGame();	INPUT.Greet();	while(INPUT.GameEnd != 1)	{		INPUT.AskLegLength(0);		INPUT.AskLegLength(1);		RECT.CalculateHypotenuseLength();		INPUT.Hypo = RECT.GetHypotenuseLength();		cout <<"The length of the hypotenuse is "<< INPUT.Hypo<		INPUT.GameChoices = INPUT.AskYesNo("Do you want to run again? 1 for yes 0 for no ");		if(INPUT.GameChoices == 0)		{			INPUT.GameEnd = 1;		}else		{			INPUT.GameEnd = 0;		}	}}

But to be honest. This source post was motsly because I need a place to store my code so I can download it on my PC(I'm on a MAC OS X now). [smile]

Concerning my text RPG. It's pretty much dead. I need to start designing stuff or I won't be able to finish my projects. If anybody could point me to a free UML for the PC and MAC, or give me some alternatives, I would be most grateful. [smile]

Cheers,

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account