Fraction calculator help

The project I'm doing is a fraction calculator that gets data from linux redirection. I'm having problems printing out the right answer and putting the negative sign in front of the numerator instead of the denominator.

Project description: Design a C++ program that implements a simple fraction calculator. The program MUST include functions to modularize the code. Each line of the input file (read via Linux redirection) will contain an expression to be evaluated. The form of the expresion will be n1/d1 operator n2/d2. n1, d1, n2,
and d2 are the numerators and denominators of fraction1 and fraction2, respectively. The operator will be a single character (+, -, * or /). For each problem read, the program should display the expression to be evaluated (if the fraction needs to be adjusted to avoid displaying a negative denominator, do so BEFORE printing) followed by an = sign
if the expression cannot be solved, display an appropriate error message
if the expression can be solved, display the resulting UNSIMPLIFIED (unreduced) fraction
if the resulting fraction can be simplified, display an = sign, followed by the simplified fraction
special case: if numerator of result is 0, simplify the fraction to 0
if the resulting fraction is improper (see description of improper fractions above), display an = sign, followed by the whole or mixed number equivalent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using namespace std;

void addfrac(int n1, int n2, int d1, int d2);

void subfrac(int n1, int n2, int d1, int d2);

void multifrac(int n1, int n2, int d1, int d2);

void dividefrac(int n1, int n2, int d1, int d2);

void prtfrac(int n1, int n2, int d1, int d2, char sign);

int main()
{
  int n1, n2, d1, d2;
  char line1;
  char line2;
  char sign;

  cin >> n1 >> line1 >> n2 >> sign >> d1 >> line2 >>  d2;

  while(cin)
    {
      if(sign == '+')
        {
          addfrac(n1, n2, d1, d2);
        }
      else if(sign == '-')
        {
          subfrac(n1, n2, d1, d2);
        }
      else if(sign == '*')
        {
          multifrac(n1, n2, d1, d2);
        }
      else if(sign == '/')
        {
          dividefrac(n1, n2, d1, d2);
        }
      cin >> n1 >> line1 >> n2 >> sign >> d1 >> line2 >> d2;
    }

  return 0;
}
void addfrac(int n1, int n2, int d1, int d2)
{
  int num, denom;
  if(n2 != 0 && d2 !=0)
    {
      num = ((n1 * d2) + (d1 * n2));
      denom = (n2 * d2);
      prtfrac(n1, n2, d1, d2, '+');
    }
  else
    cout <<"Invalid input, denominator can't be 0." << endl;
}
void subfrac(int n1, int n2, int d1, int d2)
 {
   int num, denom;
   if(n2 != 0 && d2 != 0)
     {
       num = ((n1*d2) - (d1 * n2));
       denom = (n2*d2);
       prtfrac(n1, n2, d1, d2, '-');
     }
   else
     cout <<"Invalid input, denominator can't be 0." << endl;
 }
void multifrac( int n1, int n2, int d1, int d2)
 {
   int num, denom;
   if(n2 !=0 && d2 != 0)
     {
       num = (n1 * d1);
       denom = (n2 * d2);
       prtfrac(n1, n2, d1, d2, '*');
     }
   else
     cout <<"Invalid input, denominator can't be 0." << endl;
 }
void dividefrac(int n1, int n2, int d1, int d2)
{
  int num, denom;
  if(n2 != 0 && d2 != 0)
    {
      num = (n1 * d2);
      denom = (d1 * n2);
      prtfrac(n1, n2, d1, d2, '/');
    }
  else 
  cout <<"Invalid input, denominator can't be 0." << endl;
}
void prtfrac(int n1, int n2, int d1, int d2, char sign)
{
  int num, denom;
  cout << n1 << " / " << n2 << " " <<  sign << " " << d1 << " / " << d2 << " = " << num << " / " << denom << endl;


This is the datafile I'm using:

0 / 2 * 5 / 16
1 /3 + 5/7
6/-7 - 11/7
3/9 + 15/9
5 / 10 * 3/2
8 /-3 / 4/-2
4/5 / 0/3
12/3 + 5 / 0

This is the output I get from my datafile:

-bash-4.1$ g++ project3.cpp
.-bash-4.1$ ./a.out < datafile
0 / 2 * 5 / 16 = 32767 / 4196704
1 / 3 + 5 / 7 = 32539 / -1
6 / -7 - 11 / 7 = 32539 / -1
3 / 9 + 15 / 9 = 32539 / -1
5 / 10 * 3 / 2 = 32539 / -1
8 / -3 / 4 / -2 = 32539 / -1
4 / 5 / 0 / 3 = 32539 / -1
Invalid input, denominator can't be 0.

Last edited on
Line 96 needs to be the parameters for prtfrac(...).
Topic archived. No new replies allowed.