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