Click here to Skip to main content
16,004,833 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: c free() function Pin
Sean Wcisel17-Mar-07 21:56
Sean Wcisel17-Mar-07 21:56 
AnswerRe: c free() function Pin
Stephen Hewitt18-Mar-07 0:19
Stephen Hewitt18-Mar-07 0:19 
AnswerRe: c free() function Pin
prasad_som18-Mar-07 1:34
prasad_som18-Mar-07 1:34 
AnswerRe: c free() function Pin
ThatsAlok18-Mar-07 20:39
ThatsAlok18-Mar-07 20:39 
QuestionKeep the value of an array slot from changing Pin
Sean Wcisel17-Mar-07 20:03
Sean Wcisel17-Mar-07 20:03 
AnswerRe: Keep the value of an array slot from changing Pin
Michael Dunn18-Mar-07 8:22
sitebuilderMichael Dunn18-Mar-07 8:22 
GeneralRe: Keep the value of an array slot from changing Pin
Sean Wcisel18-Mar-07 8:45
Sean Wcisel18-Mar-07 8:45 
QuestionI need help in creating a fraction calculator Pin
dragon53217-Mar-07 18:52
dragon53217-Mar-07 18:52 
Hi, I am a total beginner - first time using this forum.

I need to write a fraction calculator program that can do +,-,*,and /. The output format required is like this:
> 1/2 + 3/4
> 5/4

>2 1/2 - 3/4
>1 3/4

Essentially, I need to ask the user to input a valid expression, and be able to output the result on the screen. Notice that I have to be able to do mixed fractions. This is a huge challenge for me. I don't know how to extract key data from user's inputs.

This is an example given in class - but my professor said that we can't have the program menu driven. It means I can't ask the user numerator and denominator separately, for example. A good method/strategy to write this program is appreciated.

Note: something with me posting header files - so the ones really needed: stdlib.h, iostream, math.h, conio.h, string

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <conio.h>
#include <time.h>
#include <string>

using namespace std;

class Fraction
{
public:
Fraction()
{
num = 0;
denom = 1;
}

Fraction(int a, int b) : num(a), denom(b)
{
int div = gcd(num, denom);
num /= div;
denom /= div;
}

Fraction(const Fraction &x)
{
num = x.num;
denom = x.denom;
}

Fraction operator =(const Fraction &x)
{
num = x.num;
denom = x.denom;
return Fraction(num,denom);
}

friend ostream& operator <<(ostream& out, const Fraction &x);
friend istream& operator >>(istream& in, Fraction &x);
friend Fraction operator +(const Fraction &x, const Fraction &y);
friend Fraction operator -(const Fraction &x, const Fraction &y);
friend Fraction operator *(const Fraction &x, const Fraction &y);
friend Fraction operator /(const Fraction &x, const Fraction &y);

private:
int num, denom;

int gcd(int p, int q)
{
return (q==0)? p : gcd(q, p%q);
}
};

Fraction operator +(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.denom + y.num*x.denom;
denom = x.denom * y.denom;
return Fraction(num,denom);
}

Fraction operator *(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.num;
denom = x.denom * y.denom;
return Fraction(num,denom);
}

Fraction operator -(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.denom - y.num*x.denom;
denom = x.denom * y.denom;
return Fraction(num,denom);
}

Fraction operator /(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.denom;
denom = x.denom * y.num;
return Fraction(num,denom);
}

ostream& operator <<(ostream& out, const Fraction &x)
{
out<<x.num<<' '<<x.denom;
="" return="" out;
}

istream&="" operator="">>(istream& in, Fraction &x)
{
char junk;
in>>x.num>>junk>>x.denom;
return in;
}

char menu()
{
cout<<"Please make a selection: "<<endl
<<"="" a="" input="" the="" fraction="" a"<<endl
="" b="" b"<<endl
="" +="" add="" and="" -="" subtract="" from="" divide="" by="" *="" multiply="" c="" print="" answer="" c"<<endl
="" 1="" move="" to="" 2="" q="" quit"<<endl
="" <<"your="" selection:="" ";
="" char="" ans;
="" cin="">>ans;
cin.ignore(1000,'\n');
return ans;
}


void operation(char choice, Fraction &a, Fraction &b, Fraction &c)
{
switch(toupper(choice))
{
case 'A' : cout<<"Please enter the fraction A: ";
cin>>a;
break;
case 'B' : cout<<"Please enter the fraction B: ";
cin>>b;
break;
case 'C' : cout<<"The fraction C is: "<
AnswerRe: I need help in creating a fraction calculator Pin
x87Bliss18-Mar-07 0:25
x87Bliss18-Mar-07 0:25 
QuestionFor gurus only - others need not apply [modified] Pin
Jim_Csoft17-Mar-07 18:30
Jim_Csoft17-Mar-07 18:30 
AnswerRe: For gurus only - others need not apply [modified] Pin
Jim_Csoft18-Mar-07 16:20
Jim_Csoft18-Mar-07 16:20 
QuestionHow Can I Get Text Under Mouse ? Pin
ngooduwcs17-Mar-07 15:37
ngooduwcs17-Mar-07 15:37 
AnswerRe: How Can I Get Text Under Mouse ? Pin
cp987617-Mar-07 16:23
cp987617-Mar-07 16:23 
GeneralRe: How Can I Get Text Under Mouse ? [modified] Pin
ngooduwcs17-Mar-07 17:12
ngooduwcs17-Mar-07 17:12 
QuestionRe: How Can I Get Text Under Mouse ? Pin
David Crow17-Mar-07 17:21
David Crow17-Mar-07 17:21 
AnswerRe: How Can I Get Text Under Mouse ? Pin
David Crow17-Mar-07 17:17
David Crow17-Mar-07 17:17 
AnswerRe: How Can I Get Text Under Mouse ? Pin
Michael Dunn17-Mar-07 18:35
sitebuilderMichael Dunn17-Mar-07 18:35 
QuestionHow can add a menu selection Pin
Member 378635617-Mar-07 14:59
Member 378635617-Mar-07 14:59 
Questionv. clear() ? Pin
Yonggoo17-Mar-07 8:25
Yonggoo17-Mar-07 8:25 
AnswerRe: v. clear() ? Pin
toxcct17-Mar-07 8:53
toxcct17-Mar-07 8:53 
GeneralRe: v. clear() ? Pin
David Crow17-Mar-07 17:23
David Crow17-Mar-07 17:23 
GeneralRe: v. clear() ? Pin
toxcct17-Mar-07 22:00
toxcct17-Mar-07 22:00 
AnswerRe: v. clear() ? Pin
Stephen Hewitt18-Mar-07 0:23
Stephen Hewitt18-Mar-07 0:23 
QuestionChange the project font Pin
Joe Smith IX17-Mar-07 7:44
Joe Smith IX17-Mar-07 7:44 
AnswerRe: Change the project font Pin
PJ Arends17-Mar-07 10:53
professionalPJ Arends17-Mar-07 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.