プログラミング演習 課題4 問題3 分数の演算 과제空間



#include <stdio.h>
#include <conio.h>

int a=0, b=0, c=0, d=0;
int C=0, C1=0, C2=0, C3=0, C4=0, M=0;
int e=0;
int gcd(int, int, int);
int lcm=0;
int main()
{
 printf("input the first number\n");
 scanf("%d" "%d", &a, &b);
 printf("first number is %d/%d\n", a, b);
 
 printf("input the second number\n");
 scanf("%d" "%d", &c, &d);
 printf("second number is %d/%d\n", c, d);
 
 printf("input 1(+) or 2(-) or 3(*) or 4(/)\n");
 scanf("%d", &e);

C1=d*a+b*c;
C2=d*a-b*c;
C3=a*c;
C4=a*d;
if(e==4)
{
        M=b*c;
}
else
{
    M=b*d;
}

printf("%d %d %d %d %d %d\n", C1, C2, C3, C4, M, gcd(e, C, M));

 switch(e)
{
 case 1: if((gcd(e, C, M)!=0)&&(C1%M!=0))
         {
                         printf("%d/%d + %d/%d/=%d/%d\n", a, b, c, d, C1/gcd(e, C, M), M/gcd(e, C, M));
                         break;
         }
         else if(C1%M==0)
         {
                         printf("%d/%d + %d/%d/=%d\n", a, b, c, d, C1/M);
                         break;
         }
         else
         {               printf("%d/%d + %d/%d/=%d/%d\n", a, b, c, d, C1, M);
                         break;
         }
 case 2: if((gcd(e, C, M)!=0)&&(C2%M!=0))
         {
                         printf("%d/%d - %d/%d/=%d/%d\n", a, b, c, d, C2/gcd(e, C, M), M/gcd(e, C, M));
                         break;
         }
         else if(C2%M==0)
         {
                         printf("%d/%d - %d/%d/=%d\n", a, b, c, d, C2/M);
                         break;
         }
         else
         {
                        printf("%d/%d - %d/%d/=%d/%d\n", a, b, c, d, C2, M);
                        break;
         }
 case 3: if((gcd(e, C, M)!=0)&&(C3%M!=0))
         {
                        printf("%d/%d * %d/%d/=%d/%d\n", a, b, c, d, C3/gcd(e, C, M), M/gcd(e, C, M));
                        break;
         }
         else if(C3%M==0)
         {
                        printf("%d/%d * %d/%d/=%d\n", a, b, c, d, C3/M);
                        break;
         }
         else
         {
                        printf("%d/%d * %d/%d/=%d/%d\n", a, b, c, d, C3, M);
                        break;
         }
 case 4: if((gcd(e, C, M)!=0)&&(C4%M!=0))
         {
                       printf("%d/%d / %d/%d/=%d/%d\n", a, b, c, d, C4/gcd(e, C, M), M/gcd(e, C, M));
                       break;
         }
         else if(C4%M==0)
         {
              
                      printf("%d/%d / %d/%d=%d\n", a, b, c, d, C4/M);
                      break;
         }
         else
         {
                      printf("%d/%d / %d/%d=%d/%d\n", a, b, c, d, C4, M);
                      break;
         }            
 default : printf("input one more time\n");
}


getch();

}


int gcd(int e, int C, int M)
{
    if(e==1)
    {
           C=C1;
          
    }
    else if(e==2)
    {

           C=C2;
          
    }
    else if(e==3)
    {
         C=C3;
        
    }
    else if(e==4)
    {
         C=C4;
        
    }
   
       
    if(C<0)
           {
                  C*=-1;
           }
   

    while(C)
    {
            if(C<M)
            {
                   C=C^M;
                   M=C^M;
                   C=C^M;
            }
    C-=M;
    }
return M;
}


덧글

댓글 입력 영역



이글루스 8주년 기념 위젯