|
-
March 4th, 2011, 10:36 PM
#1
Classes help
I wasn't sure what to name the thread, but I have a few questiong. I've written a program to add, subtract, multiply, and divide for class. It all works well except I need to add a few odds and ends. I've added the reduction part of the code but it's not working. I also need to add in a way top automatically input a 1 if the user hits enter w/o entering a number.
The only thing left is I need to ensure the denom is not a 0 or negative. I was thinking something like
if(denom >= 0)
cout<<"please enter a valid denominator\n";
cin>>denom;
a.set_den(denom);
which works but I need to make it recursive or add in a check. I'm trying to keep my code from becoming bloated as well, so can I write that as a single code also w/o needing to have the code twice, once for each input of the denom. I'm listing my code below, thanks in advance.
//Define class Fraction Num\Denom int only, no float***
//Program operations +,-,*,/ to (overload)***
//When a user inputs a blank input build a constructor that enters "1"
//Research Euclidean Algorithm to find GCD for reduction.
//Class checks, no 0 or "-" in denominator.
//Minimal code in "main***
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
class Fraction
{
int num;
int den;
public:
//public function can access private function's and set variables
void set_num(int numpub)
{num=numpub;};
void set_den(int denpub)
{den=denpub;};
int get_num()
{return num;};
int get_den()
{return den;};
int gcd(int set_num, int set_den)
{
if(set_den == 0) return set_num;
else
{return gcd(set_den, set_num % set_den);};
}
Fraction(int a, int b)
{set_num(a); set_den(b);};
Fraction(){};
//Fraction x is the b input and the default w/ no x. is the a input
Fraction operator +(Fraction x)
{
Fraction result;
result.set_num(num*x.get_den()+den*x.get_num());
result.set_den(den*x.get_den());
return result;
}
Fraction operator -(Fraction x)
{
Fraction result;
result.set_num(num*x.get_den()-den*x.get_num());
result.set_den(den*x.get_den());
return result;
}
Fraction operator *(Fraction x)
{
Fraction result;
result.set_num(num*x.get_num());
result.set_den(den*x.get_den());
return result;
}
Fraction operator /(Fraction x)
{
Fraction result;
result.set_num(num*x.get_den());
result.set_den(den*x.get_num());
return result;
}
};
int main()
{
Fraction a;
Fraction b;
Fraction c;
Fraction out;
int numer;
int denom;
char oper;
cout<<"Please enter your FIRST Numerator and Denominator:\n";
cin>>numer >> denom;
a.set_num(numer); a.set_den(denom);
if(denom >= 0)
cout<<"please enter a valid denominator\n";
cin>>denom;
a.set_den(denom);
cout<<"Please enter the SECOND Numerator Denominator:\n";
cin>>numer >> denom;
b.set_num(numer); b.set_den(denom);
if(denom >= 0)
cout<<"please enter a valid denominator\n";
cin>>denom;
b.set_den(denom);
//operation, c is the new output after the operation is complete.
cout<<"Choose operation of '+,-,*,/'\n";
cin>>oper;
switch(oper)
{
case '+':c=a+b;
break;
case '-':c=a-b;
break;
case '*':c=a*b;
break;
case '/':c=a/b;
break;
default: cout<<"Invalid Operator!\n";
break;
}
cout<<"Success!\n"<<c.get_num()<<"/"<<c.get_den()<<endl;
return 0;
};
/*
int gcd(int set_num, int set_den)
{
if(set_den == 0) return set_num;
else
{return gcd(set_den, set_num % set_den);};
}
*/
Last edited by Belcross; March 6th, 2011 at 12:37 AM.
-
March 4th, 2011, 11:45 PM
#2
Re: Classes help
First order of business is to use code tags.
-
March 5th, 2011, 01:27 AM
#3
Re: Classes help
you say you want denominator greater than zero, but in your code you ask for just the opposite.
you need to do something like
Code:
int denom = 0;
while (denom <= 0)
{
cout << "Enter denom: ";
cin >> denom;
}
a.set_den(denom);
-
March 6th, 2011, 12:39 AM
#4
Re: Classes help
Ahhh ok I see what your saying. Does anyone know why it's not reducing or how to input a 1 automaticly on someone pressing "Enter" w/o inputing a number?
Also how do you use code tags?
-
March 6th, 2011, 08:00 AM
#5
Re: Classes help
 Originally Posted by Belcross
Ahhh ok I see what your saying. Does anyone know why it's not reducing or how to input a 1 automaticly on someone pressing "Enter" w/o inputing a number?
Also how do you use code tags?
[code]somecode[/code]
-
March 6th, 2011, 02:57 PM
#6
Re: Classes help
Hmmm, I did that and all it did was display them on the page.... oh well I might have typoed or something.
-
March 6th, 2011, 03:22 PM
#7
Re: Classes help
 Originally Posted by Belcross
Hmmm, I did that and all it did was display them on the page.... oh well I might have typoed or something.
try again
-
March 6th, 2011, 03:23 PM
#8
Re: Classes help
ok, lets try w/ the new code then 
Code:
//Define class Fraction Num\Denom int only, no float***
//Program operations +,-,*,/ to (overload)***
//When a user inputs a blank input build a constructor that enters "1"
//Research Euclidean Algorithm to find GCD for reduction.
//Class checks, no 0 or "-" in denominator.***
//Minimal code in "main***
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
class Fraction
{
int num;
int den;
public:
//public function can access private function's and set variables
void set_num(int numpub)
{num=numpub;};
void set_den(int denpub)
{den=denpub;};
int get_num()
{return num;};
int get_den()
{return den;};
Fraction(int a, int b)
{set_num(a); set_den(b);};
Fraction(){};
//Fraction x is the b input and the default w/ no x. is the a input
Fraction operator +(Fraction x)
{
Fraction result;
result.set_num(num*x.get_den()+den*x.get_num());
result.set_den(den*x.get_den());
return result;
}
Fraction operator -(Fraction x)
{
Fraction result;
result.set_num(num*x.get_den()-den*x.get_num());
result.set_den(den*x.get_den());
return result;
}
Fraction operator *(Fraction x)
{
Fraction result;
result.set_num(num*x.get_num());
result.set_den(den*x.get_den());
return result;
}
Fraction operator /(Fraction x)
{
Fraction result;
result.set_num(num*x.get_den());
result.set_den(den*x.get_num());
return result;
}
int gcd(int set_num, int set_den)
{
if(set_den == 0) return set_num;
else
{return gcd(set_den, set_num % set_den);};
}
};
int main()
{
Fraction a;
Fraction b;
Fraction c;
Fraction out;
int numer=0;
int denom=0;
char oper;
cout<<"Please enter your FIRST Numerator and Denominator:\n";
cin>>numer >> denom;
a.set_num(numer); a.set_den(denom);
while (denom <= 0)
{
cout << "Denominator may not be a 0 or negative number, please enter a valid denominator:\n";
cin >> denom;
}
a.set_den(denom);
cout<<"Please enter the SECOND Numerator and Denominator:\n";
cin>>numer >> denom;
b.set_num(numer); b.set_den(denom);
while (denom <= 0)
{
cout << "Denominator may not be a 0 or negative number, please enter a valid denominator:\n";
cin >> denom;
}
b.set_den(denom);
//operation, c is the new output after the operation is complete.
cout<<"Choose operation of '+,-,*,/'\n";
cin>>oper;
switch(oper)
{
case '+':c=a+b;
break;
case '-':c=a-b;
break;
case '*':c=a*b;
break;
case '/':c=a/b;
break;
default: cout<<"Invalid Operator!\n";
break;
}
cout<<"Success!\n"<<c.get_num()<<"/"<<c.get_den()<<endl;
//calculate
//display
return 0;
};
/*
int gcd(int set_num, int set_den)
{
if(set_den == 0) return set_num;
else
{return gcd(set_den, set_num % set_den);};
}
*/
-
March 6th, 2011, 04:52 PM
#9
Re: Classes help
 Originally Posted by Belcross
Ahhh ok I see what your saying. Does anyone know why it's not reducing
Use the debugger that comes with your compiler.
When you write a program, you are responsible for debugging it. You accomplish that by using your program's debugger and stepping through the program when it is running. You then get a chance to watch the variables and execution path of your program.
Regards,
Paul McKenzie
-
March 6th, 2011, 04:57 PM
#10
Re: Classes help
Code:
void set_num(int numpub)
{num=numpub;}; <- these
void set_den(int denpub)
{den=denpub;}; <- are pointless
Code:
void set_num(int numpub)
{num=numpub;}
void set_den(int denpub)
{den=denpub;}
-
March 6th, 2011, 08:03 PM
#11
Re: Classes help
I see, I didn't know that you could watch the program run like that.
I've noticed people seem to not like some of the ";" I've added, but it seems they don't make a diffrence if they are there or not in some cases, is this a point of being easier to read? I've only been at this for a short time so still learning.
-
March 6th, 2011, 09:20 PM
#12
Re: Classes help
 Originally Posted by Belcross
I see, I didn't know that you could watch the program run like that.
The debugger is a mandatory tool that you must learn to use. It basically lets you run the program one line at a time, under your control. Then you can see what values your variables are, what path your program is taking, set breakpoints (run the program full speed until it gets to a line you chose in your code) etc.
Unless the problem is so obvious that it can be spotted by just eyeballing the code, no one runs the computer in their head. If we were to answer your question, and the program is not trivial, then guess what tool we use? The debugger. So if we use this tool to answer your question, then it's a good idea for you to learn how to use this tool for yourself.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 6th, 2011 at 09:22 PM.
-
March 7th, 2011, 12:21 AM
#13
Re: Classes help
Not going to lie, I hit F8 and it complies and runs the .EXE, I didnt know the debugger was seperate. We use GNU 4.3 on Linux at school and we never used the debugger just tried to run it and see if it kicks back any errors. I realize the programs are small so maybe they just havnt taught that but seems like an oversite that's pretty big. I'll have to figure it out tonight or tomorrow.
-
March 7th, 2011, 05:34 AM
#14
Re: Classes help
 Originally Posted by Belcross
Not going to lie, I hit F8 and it complies and runs the .EXE, I didnt know the debugger was seperate. We use GNU 4.3 on Linux at school and we never used the debugger just tried to run it and see if it kicks back any errors.
The debugger that comes with GNU C++ is GDB. By itself, GDB is a command-line based debugger, but there are graphical user interfaces to GDB that makes using GDB very simple. Type in "gdb" from the Linux command-line and see what you get.
Another option is to get the free Visual Studio 2010 Express and when you are at home, use that to develop your programs. Then you have the Visual Studio debugger at your disposal, which is one of the best debuggers out there.
I realize the programs are small so maybe they just havnt taught that but seems like an oversite that's pretty big. I'll have to figure it out tonight or tomorrow.
Usually teachers do not mention debuggers to help you develop and understand your programs. Why is that? -- maybe tradition, as students way back when were supposed to go through their code "by hand" and figure out the problems, or use output statements to see what the value of the variables are. In this day and age, that is all automated by using interactive debuggers.
Regards,
Paul McKenzie
-
March 7th, 2011, 03:26 PM
#15
Re: Classes help
Well ATM I'm useing codeblocks on my windows machine, I tried Microsoft, but found I liked this better, I found it simpler to use. Still not figured the debugging out, it says it ran, but doesno't return anything at the end. Working on figuring it out now
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
|