1 #ifndef lint 2 static char sccsid[] = "@(#)mout.c 5.3 (Berkeley) 03/02/87"; 3 #endif not lint 4 5 #include <stdio.h> 6 #include <mp.h> 7 m_in(a,b,f) MINT *a; FILE *f; 8 { MINT x,y,ten; 9 int sign,c; 10 short qten,qy; 11 xfree(a); 12 sign=1; 13 ten.len=1; 14 ten.val= &qten; 15 qten=b; 16 x.len=0; 17 y.len=1; 18 y.val= &qy; 19 while((c=getc(f))!=EOF) 20 switch(c) 21 { 22 case '\\': (void)getc(f); 23 continue; 24 case '\t': 25 case '\n': a->len *= sign; 26 xfree(&x); 27 return(0); 28 case ' ': 29 continue; 30 case '-': sign = -sign; 31 continue; 32 default: if(c>='0' && c<= '9') 33 { qy=c-'0'; 34 mult(&x,&ten,a); 35 madd(a,&y,a); 36 move(a,&x); 37 continue; 38 } 39 else 40 { VOID ungetc(c,stdin); 41 a->len *= sign; 42 return(0); 43 } 44 } 45 return(EOF); 46 } 47 m_out(a,b,f) MINT *a; FILE *f; 48 { int sign,xlen,i; 49 short r; 50 MINT x; 51 char *obuf, *malloc(); 52 register char *bp; 53 sign=1; 54 xlen=a->len; 55 if(xlen<0) 56 { xlen= -xlen; 57 sign= -1; 58 } 59 if(xlen==0) 60 { fprintf(f,"0\n"); 61 return; 62 } 63 x.len=xlen; 64 x.val=xalloc(xlen,"m_out"); 65 for(i=0;i<xlen;i++) x.val[i]=a->val[i]; 66 obuf=malloc(7*(unsigned)xlen); 67 bp=obuf+7*xlen-1; 68 *bp--=0; 69 while(x.len>0) 70 { for(i=0;i<10&&x.len>0;i++) 71 { sdiv(&x,(short)b,&x,&r); 72 *bp--=r+'0'; 73 } 74 if(x.len>0) *bp--=' '; 75 } 76 if(sign==-1) *bp--='-'; 77 fprintf(f,"%s\n",bp+1); 78 free(obuf); 79 FREE(x) 80 return; 81 } 82 sdiv(a,n,q,r) MINT *a,*q; short n; short *r; 83 { MINT x,y; 84 int sign; 85 sign=1; 86 x.len=a->len; 87 x.val=a->val; 88 if(n<0) 89 { sign= -sign; 90 n= -n; 91 } 92 if(x.len<0) 93 { sign = -sign; 94 x.len= -x.len; 95 } 96 s_div(&x,n,&y,r); 97 xfree(q); 98 q->val=y.val; 99 q->len=sign*y.len; 100 *r = *r*sign; 101 return; 102 } 103 s_div(a,n,q,r) MINT *a,*q; short n; short *r; 104 { int qlen,i; 105 long int x; 106 short *qval; 107 x=0; 108 qlen=a->len; 109 qval=xalloc(qlen,"s_div"); 110 for(i=qlen-1;i>=0;i--) 111 { 112 x=x*0100000L+a->val[i]; 113 qval[i]=x/n; 114 x=x%n; 115 } 116 *r=x; 117 if(qlen && qval[qlen-1]==0) qlen--; 118 q->len=qlen; 119 q->val=qval; 120 if(qlen==0) shfree(qval); 121 return; 122 } 123 min(a) MINT *a; 124 { 125 return(m_in(a,10,stdin)); 126 } 127 omin(a) MINT *a; 128 { 129 return(m_in(a,8,stdin)); 130 } 131 mout(a) MINT *a; 132 { 133 m_out(a,10,stdout); 134 } 135 omout(a) MINT *a; 136 { 137 m_out(a,8,stdout); 138 } 139 fmout(a,f) MINT *a; FILE *f; 140 { m_out(a,10,f); 141 } 142 fmin(a,f) MINT *a; FILE *f; 143 { 144 return(m_in(a,10,f)); 145 } 146