473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

FractionCalc

Alright! So this is a project that I've continued now for two or three days, and I'm pretty determined to figure this one out.

I've been reading about constructors and classes, unfortunately I've never been able to read and comprehend very well, it would help greatly if I could have some feedback on constructors and the like.

I am making a Fraction Calculator, which is basically going to accept two different fractions # / # and # / #, and it's going to reduce the fractions to decimals so they're easier to work with, and then it's going to compare these fractions (By divding them, adding them, multiplying them, and subtracting them)

It's basically going to be a program that's similiar to this: http://www.dreamincode.net/code/snip...tm#lastComment

I've been reading over that code since I started this project, and I've got an okay handle on how to work things, but a little input would be nice as well. Here's my code to date:

Expand|Select|Wrap|Line Numbers
  1. // Written by Michael C. Dillon
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <stdlib.h>
  6. #include <sstream>
  7. #include <stdio.h>
  8. using namespace std;
  9.  
  10. class rational
  11. {
  12.     int num;
  13.     int den;
  14.     void simplify();
  15.  
  16. public:
  17.     rational();
  18.     rational(int,int);
  19.  
  20. };
  21.  
  22. rational::rational()
  23. {
  24.     num=0;
  25.     den=1;
  26. }
  27.  
  28. rational::rational(int n, int d)
  29. {
  30.     int i = n/d;
  31. }
  32.  
  33.  
  34. void rational::simplify()
  35. {
  36.     float n=(float)this->num;
  37.     float d=(float)this->den;
  38.     int i = 2;
  39.     while ((i<=n) && (n!=1))
  40.     {
  41.         while ((n/i==(int)n/i) && (d/i == (int)d/i))
  42.         {
  43.             n/=i;
  44.             d/=i;
  45.         }
  46.         i++;
  47.     }
  48.     num = n;
  49.     den=(n==0)? 1:d;
  50. }
  51.  
  52. int GetIntVal(string strConvert) {
  53.     int intReturn;
  54.     intReturn = atoi(strConvert.c_str());
  55.  
  56.     return(intReturn);
  57. }
  58.  
  59. int main ()
  60. {
  61.     string first,f2,f1;
  62.     size_t pos;
  63.     size_t space;
  64.  
  65.     while (true)
  66.     {
  67.         cout << "Enter your first fraction (0 to exit): ";
  68.         getline(cin,first,'\n'); // Enter first fraction
  69.  
  70.         if (first=="0") //Exit function
  71.         {
  72.             break;
  73.         }
  74.         else
  75.         {
  76.             pos = first.find('/'); // Searches for the '/'
  77.             f2 = first.substr(pos+1);
  78.             f1 = first.substr (0,pos);
  79.             space = first.find_first_of("\ "); // Searches for a space
  80.  
  81.             if (pos!=string::npos) //If it contains a /, indicating a proper fraction
  82.             {
  83.                 if (space!=string::npos) // If there are spaces
  84.                 {
  85.                     f1.replace(space,space,""); // Remove them
  86.                     f2.replace(space,space,""); // Remove them
  87.                 }
  88.                 int num = GetIntVal(f1); // Conversion to Int
  89.                 int den = GetIntVal(f2); // Conversion to Int
  90.                 double percent = double(num)/den; //Cast that Int!
  91.                 cout << "pos = " << pos + 1 << "\nnum = " << num << "\nden = " << den << "\nThe rational decimal of this fraction is: " << percent << "\n";
  92.             }
  93.             else
  94.             {
  95.                 cout << "Correct formatting: ##/##\n"; // Just incase. ^-^
  96.             }
  97.         }
  98.     }
  99.     return EXIT_SUCCESS;
Oct 16 '07 #1
1 1215
weaknessforcats
9,208 Expert Mod 8TB
Please do not double post.

A version of this code was on your other thread.
Oct 16 '07 #2

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.