Fraction problem in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by sicarie
    (THREAD HIJACK ALERT!)

    Jos-

    Ever consider writing one of the tutorials? That was a good, concise explanation of a struct that was pretty easy to understand.
    Maybe somewhere in the near future I might when I can find the time. I'll do some
    of the Java tips of the week first and then we'll see whether people like it or if
    they get sick and tired of my babbling ;-)

    In the mean time it's difficult enough not to let posters go astray with their own
    code.

    Thanks for the compliment and

    kind regards,

    Jos

    Comment

    • xpun
      New Member
      • Apr 2007
      • 39

      #17
      Originally posted by JosAH
      Maybe somewhere in the near future I might when I can find the time. I'll do some
      of the Java tips of the week first and then we'll see whether people like it or if
      they get sick and tired of my babbling ;-)

      In the mean time it's difficult enough not to let posters go astray with their own
      code.

      Thanks for the compliment and

      kind regards,

      Jos
      I was talking to my professor and he told me not to screw around with structures just yet so i did this instead . The program works i just need to figure out a way to reduce the resulting fraction.

      Code:
      #include <cstdlib>
      #include <stdio.h>
      #include <math.h>
      
      char symbol;
      int num1=0;
      int num2=0;
      int denum1=0;
      int denum2=0;
      int ansnum=0;
      int denumans=0;
      int ans=0;
      
      
      int add(int n1,int d1,int n2,int d2, int*top,int*bottom) 
      {
       int fnum = 0;
       int fden = 0;
       char  symhold = '/'; 
       if (d1==d2)
            { 
            fnum=(n1+n2);
            fden=(d2);
            *top=fnum;
            *bottom=fden;
            }
       else{       
         fnum=((n1 * d2) + (n2 * d1)); 
         fden=(d1 * d2); 
       *top=fnum;
       *bottom=fden;
       }
      }
       int sub(int n1,int d1,int n2,int d2, int*top,int*bottom) 
       {
       int fnum = 0;
       int fden = 0;
       char  symhold = '/';
       if (d1==d2)
            { 
            fnum=(n1-n2);
            fden=(d2);
            *top=fnum;
            *bottom=fden;
            }
       else{     
         fnum=((n1 * d2)-(n2 * d1)); 
         fden=(d1*d2) ; 
       *top=fnum;
       *bottom=fden;
      }
       }
      int mul(int n1,int d1,int n2,int d2, int*top,int*bottom) 
      {
       int fnum = 0;
       int fden = 0;
       char  symhold = '/';   
         fnum=(n1 * n2); 
         fden=(d1 * d2); 
       *top=fnum;
       *bottom=fden;
      }
      int div(int n1,int d1,int n2,int d2, int*top,int*bottom) 
      {
       int fnum = 0;
       int fden = 0;
       int nh ;
       int dh ;
       char  symhold = '/';   
         nh=d2;
         dh=n2;
          fnum=(n1 * nh); 
         fden=(d1 * dh); 
       *top=fnum;
       *bottom=fden;
      }
      int main(void)
      {
      int numerator;
      int denomonator;
       char   ra = 'y';
       while (ra != 'n')   
       {   
         
      
       printf(" Eter the symbol ");
         scanf ("%c",&symbol);  
        printf("Enter The 1st Numerator  ");
          scanf("%d",&num1);
        printf("Enter the 1st Denomanator  ");
          scanf("%d",&denum1);
        printf("Enter the 2nd Numerator  ");
         scanf("%d",&num2); 
        printf("Enter the 2nd Denomanator  ");
          scanf("%d",&denum2); 
             
          
          if (symbol == '+')
          {
          ans = add(num1,denum1,num2,denum2,&numerator,&denomonator);
          printf(" %d/%d\n",numerator,denomonator);
           }
          if (symbol == '-')
          {
          ans = sub(num1,denum1,num2,denum2,&numerator,&denomonator);
          printf(" %d/%d\n",numerator,denomonator);
           }
         if (symbol == '*')
          {
          ans = mul(num1,denum1,num2,denum2,&numerator,&denomonator);
          printf(" %d/%d\n",numerator,denomonator);
          }
          if (symbol == '/')
          {
          ans = div(num1,denum1,num2,denum2,&numerator,&denomonator);
          printf(" %d/%d\n",numerator,denomonator);
          }
      getchar();
      printf("would you like to use the fraction operator again? Y or N\n");
      ra = getchar();
      getchar();
      }
      }

      Comment

      Working...