From g97p0855@mn.waseda.ac.jp Fri May 14 16:23:02 1999 Received: from wise13.mn.waseda.ac.jp (wise13.mn.waseda.ac.jp [133.9.4.145]) by newton.kashi.info.waseda.ac.jp (8.8.8/3.7W) with ESMTP id QAA03786 for ; Fri, 14 May 1999 16:23:01 +0900 (JST) Received: from lavie (mc133029.net.waseda.ac.jp [133.9.133.29]) by wise13.mn.waseda.ac.jp (8.9.1a/3.7W-19980811) with SMTP id QAA20243 for ; Fri, 14 May 1999 16:12:01 +0900 (JST) Message-ID: <000301be9dd8$c0e6cac0$1d850985@lavie> From: "Hiroki Masuda" To: Subject: =?iso-2022-jp?B?GyRCJWwlXSE8JUgjMhsoQg==?= Date: Fri, 14 May 1999 16:09:46 +0900 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 4.72.3155.0 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0 Status: OR x÷0を避けるためにしたパッチが逆効果になったので、最終的には割って商が10 余りが0ならば 10/1 とみなすことにしました。 /*************************************************************************** **/ /* Make 10 from four 1 digit numbers */ /* G97P085-5 */ /* Hiroki Masuda 1999.5. */ /*************************************************************************** **/ #include #include #include #include #define KETA 4 /*-- The number of (1 digit number) --*/ #define MAX 10 /*-- Maximum letters on one formula --*/ #define TRUE 1 /*-- As you see --*/ #define FALSE 0 #define PLS 0 /*-- calculation operator --*/ #define SUB 1 #define MUL 2 #define DIV 3 typedef struct{ /*-- structure of ratinal number --*/ long numerator; long denominator; } rational_number; /*-- rational number's stack operation --*/ rational_number r_pop(void); void r_push(rational_number); /*-- The four basic rules of arithmetic calculation --*/ rational_number r_plus(rational_number,rational_number); rational_number r_sub(rational_number,rational_number); rational_number r_mult(rational_number,rational_number); rational_number r_div(rational_number,rational_number); rational_number r_simplify(rational_number *); int check(char *);/*-- check whether the formula become 10 or not --*/ /*************************************************************************** **/ /* Make Combination */ /* G97P085-5 Hiroki Masuda 1999.5 */ /*************************************************************************** **/ char *make_combi(char *to,char *formula1,char *formula2,int operation){ char my_operator[2]; switch(operation){ case PLS: my_operator[0]='+'; break; case SUB: my_operator[0]='-'; break; case MUL: my_operator[0]='*'; break; case DIV: my_operator[0]='/'; break; default: printf("Operator_Error\n"); exit(-1); } my_operator[1]='\0'; /*-- for safety operation --*/ strcpy(to,formula1); /*-- return formula is postfix notation --*/ strcat(to,formula2); strcat(to,my_operator); return to; } /*************************************************************************** **/ /* Make up next formula & check it */ /* G97P085-5 Hiroki Masuda 1999.5 */ /*************************************************************************** **/ int makeup(char formula[KETA][MAX],int number_of_element){ char next_term[KETA][MAX]; /*-- retantion the origin --*/ int i,j,k; /*-- for --*/ int mono_ptr; /*-- pointer to not combining term --*/ int ope; /*-- --*/ int kekka=0; /*-- the number of --*/ int next_element; /*-- number of terms for next calculation --*/ if(number_of_element==1){ kekka=check(formula[0]); /*-- check the formula --*/ return kekka; } next_element=number_of_element-1; for(i=number_of_element-1; i>0; i--){ for(j=0; jnumerator>1000 || a->numerator<-10000){ return *a; }/*-- patch for ?/0 --*/ if(a->numerator==0){ a->denominator=1; return *a; } gcd = ( abs(a->numerator) > abs(a->denominator) )? abs(a->denominator):abs(a->numerator) ; if( gcd==0 ) return *a; do{ if(gcd==1) break; if( (a->numerator % gcd == 0) && (a->denominator % gcd == 0) ) break; else gcd--; }while(1); a->numerator /= gcd; a->denominator /=gcd; if( a->numerator < 0 && a->denominator < 0 ){ a->numerator = - a->numerator; a->denominator = -a->denominator; } else if( a->numerator <0 || a->denominator<0){ a->numerator = -abs(a->numerator); a->denominator = abs(a->denominator); } } /*************************************************************************** **/ /* Call the calclation function */ /* G97P085-5 Hiroki Masuda 1999.5 */ /*************************************************************************** **/ rational_number computation(rational_number a,rational_number b,char ope){ rational_number result; switch(ope){ case '+': result=r_plus(a,b); break; case '-': result=r_sub(a,b); break; case '*': result=r_mult(a,b); break; case '/': if(b.numerator==0){/*-- pathch for ?/0 --*/ result.numerator=INT_MAX; result.denominator=1; break; } result=r_div(a,b); break; } return result; } /*************************************************************************** **/ /* check whether the formula become 10 or not */ /* G97P085-5 Hiroki Masuda 1999.5 */ /*************************************************************************** **/ int check(char *formula){ rational_number temp,operant1,operant2; int i,number; for(i=0; formula[i]!='\0' && i