-
November 6th, 2012, 04:29 AM
#1
[Begineer] Template Issue!
Greeting CodeGuru World.
I am C++ beginner programmer. I just learned most of the fundamentals of C++. This is my neither any college project nor any home assignment. This is what i decided to work out on what i have learned from c++ fundamentals book. The Chapter which i was most influenced by "Template" because it is widely used in STL. So, in this project i decided to implement template on fraction class. But, i got a bit confusion about "Class Template". Here i will post my entire source code of my personal small project of "Four Function Calculator"[Took 20-25 min to write source code]
I decided to put template on class fraction because user may enter floating point or Integers. So, i decided to work on class fraction which will operate on both variable types.
Code:
/*A project on building four function calculator which operate on three types of number
i.e. long integers, decimal/floating numbers & fractional numbers */
#include <iostream>
#include <stdlib.h>
using namespace std;
class numbers
{
private:
long fNumb;
char oper;
long sNumb;
long result;
public:
numbers() : fNumb(0),sNumb(0) {}
numbers(long f, long s) : fNumb(f),sNumb(s) {}
void get_numb();
void numb_display()const;
};
class decimal
{
private:
double fNumb;
char oper;
double sNumb;
double result;
public:
decimal(): fNumb(0.0),sNumb(0.0){}
decimal(double f, double s) : fNumb(f),sNumb(s) {}
void get_decimal();
void dec_display()const;
};
template<class type>
class fraction
{
private:
type numenator;
char c;
type dinominator;
public:
fraction():numenator(1),dinominator(1){}
fraction(type n, type d) : numenator(n),dinominator(d){}
void get_frac();
void frac_display()const;
void low_term();
fraction operator +(fraction&);
fraction operator -(fraction&);
fraction operator *(fraction&);
fraction operator /(fraction&);
};
//-----------------------------------------------------------------------------
void numbers::get_numb()
{
while(true)
{
cout << "\nEnter 1st Number: ";
cin >> fNumb;
if(cin.good())
{
cin.ignore(10,'\n');
break;
}
cin.clear();
cout << "Incorrect Input, Try Again!";
cin.ignore(10,'\n');
}
while(true)
{
cout << "\nEnter 2nd Number: ";
cin >> sNumb;
if(cin.good())
{
cin.ignore(10,'\n');
break;
}
cin.clear();
cout << "Incorrect Input, Try Again!";
cin.ignore(10,'\n');
}
cout << "**Operation Menu, Make your choice**"
<< "\nEnter '+' for Addition:"
<< "\nEnter '-' for subtraction:"
<< "\nEnter '*' for multiplication:"
<< "\nEnter '/' for division:";
cin >> oper;
switch(oper)
{
case '+' : result = fNumb+sNumb; break;
case '-' : result = fNumb-sNumb; break;
case '*' : result = fNumb*sNumb; break;
case '/' : result = fNumb/sNumb; break;
}
}
void numbers::numb_display()const
{cout << result;}
//-----------------------------------------------------------------------------
void decimal::get_decimal()
{
while(true)
{
cout << "\nEnter 1st Decimal Number: ";
cin >> fNumb;
if(cin.good())
{
cin.ignore(10,'\n');
break;
}
cin.clear();
cout << "Incorrect Input, Try Again!";
cin.ignore(10,'\n');
}
while(true)
{
cout << "\nEnter 2nd Decimal Number: ";
cin >> sNumb;
if(cin.good())
{
cin.ignore(10,'\n');
break;
}
cin.clear();
cout << "Incorrect Input, Try Again!";
cin.ignore(10,'\n');
}
cout << "**Operation Menu, Make your choice**"
<< "\nEnter '+' for Addition:"
<< "\nEnter '-' for subtraction:"
<< "\nEnter '*' for multiplication:"
<< "\nEnter '/' for division:";
cin >> oper;
switch(oper)
{
case '+' : result = fNumb+sNumb; break;
case '-' : result = fNumb-sNumb; break;
case '*' : result = fNumb*sNumb; break;
case '/' : result = fNumb/sNumb; break;
}
}
void decimal::dec_display()const
{cout << result;}
//-----------------------------------------------------------------------------
void fraction::get_frac()
{
cout << "Enter fraction in (N/D) format: ";
cin >> numenator >> c >> dinominator;
}
void fraction::frac_display()const
{cout << numenator << c << dinominator;}
fraction fraction :: operator +(fraction& f1)
{
type nu, di;
nu = numenator+f1.numenator;
di = dinominator + f1.dinominator;
return fraction(nu, di);
}
fraction fraction :: operator -(fraction& f1)
{
type nu, di;
nu = numenator-f1.numenator;
di = dinominator - f1.dinominator;
return fraction(nu, di);
}
fraction fraction :: operator *(fraction& f1)
{
type nu, di;
nu = numenator*f1.numenator;
di = dinominator * f1.dinominator;
return fraction(nu, di);
}
fraction fraction :: operator /(fraction& f1)
{
type nu, di;
nu = numenator*f1.dinominator;
di = dinominator * f1.numenator;
return fraction(nu, di);
}
void fraction::low_term()
{
type tnum, tden, temp, gcd;
tnum = labs(numenator);
tden = labs(dinominator);
if(tden==0 )
{
cout << "Illegal fraction: division by 0"; exit(1);
}
else if( tnum==0 )
{
numenator=0; dinominator = 1; return;
}
while(tnum != 0)
{
if(tnum < tden)
{
temp=tnum; tnum=tden; tden=temp;
}
tnum = tnum - tden;
}
gcd = tden;
numenator = numenator/ gcd;
dinominator = dinominator / gcd;
}
int main()
{
char choice, ch, oper;
numbers n1;
decimal d1;
fraction f1,f2, result;
do
{
cout <<"\n******MAIN MENU******\n"
<<"**Choose Operation type**"
<<"\nPress'n' for calculating long numbers:"
<<"\nPress'd' for calculating decimal numbers:"
<<"\nPress'f' for calculating fractional numbers:"
<<"\nPress'x' for exit::";
cin >> choice;
switch(choice)
{
case 'n' : n1.get_numb();n1.numb_display(); break;
case 'd' : d1.get_decimal(); d1.dec_display(); break;
case 'f' : f1.get_frac(); f2.get_frac();
cout << "**Operation Menu, Make your choice**"
<< "\nEnter '+' for Addition:"
<< "\nEnter '-' for subtraction:"
<< "\nEnter '*' for multiplication:"
<< "\nEnter '/' for division:";
cin >> oper;
switch(oper)
{
case '+' : result = f1+f2; break;
case '-' : result = f1-f2; break;
case '*' : result = f1*f2; break;
case '/' : result = f1/f2; break;
}
result.low_term();
result.frac_display();
case 'x' : exit(1);
default : cout <<"\nUnknown KeY!";
}
cout <<"\nDo another calculation? (y/n)"; cin >> ch;
}while(ch!='n');
return 0;
}
I already mentioned, I am just a beginner programmer. I haven't post any comment of each line because there is no any complex method used. I guess it might be a one of the simplest program of C++. I guess i messed a bit in class fraction because my compiler signal most of the error from there.
Awaiting any suggestion:
Regards
Basanta
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|