1 / 7

Binary multiplication

Binary multiplication. #define Sj (strlen(B)-j-1) #define Si (strlen(A)-i-1) int main() { int i, T[1000]; char A[300], B[300]; while (cin >> A >> B) { for (i=0 ; i<1000 ; ++i) T[i] = 0; for (i=0 ; A[i] != 0 ; ++i) if (A[Si] == '1')

lieu
Download Presentation

Binary multiplication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary multiplication #define Sj (strlen(B)-j-1) #define Si (strlen(A)-i-1) int main() { int i, T[1000]; char A[300], B[300]; while (cin >> A >> B) { for (i=0 ; i<1000 ; ++i) T[i] = 0; for (i=0 ; A[i] != 0 ; ++i) if (A[Si] == '1') for (int j=0 ; B[j] != 0 ; ++j) if (B[Sj] == '1') ++T[999-i-j]; /* add columns */ /* then simplify and print */ } } 1001110 times 11001 create a table 0000000000001001110 0000000000000000000 0000000000000000000 0000000001001110000 0000000010011100000 0000000011013211110 add the columns 0000000011110011110 simplify STL: http://www.sgi.com/Technology/STL

  2. Fraction calculator getNum(…) getOp(…) operate(n1,d1,n2,d2,op) simplify(int& n,int& d) gets a number or returns an error functions to help parse... gets an operator or returns an error does the arithmetic reduces n/d to lowest terms 18 - --5 / 7 * 3 + 1 - 3 ... evaluate as you go getNum(…) when you see this, catch up with any pending operations getOp(…) 18 - 15/7 pending immediate 111/7 + pending

  3. C Output printf, fprintf, sprintf(char* s, const char* format, …) the destination the format string the values possible format strings h % -#0 12 .4 d d decimal integers u unsigned (decimal) ints o octal integers x hexadecimal integers f doubles (floats are cast) e doubles (exp. notation) g f or e, if exp < -3 or -4 c character s string n outputs # of chars written !! % two of these print a ‘%’ type size modifier minimum field width precision allowed size modifiers flags - left-justify 0 pad w/ zeros + use sign (+ or -) (space) use sign ( or -) # deviant operation h short l long (lowercase L) L long double start character

  4. C Output value = 42 value = -42 %10.4d 0042 -0042 %-#12x 0x2a 0xffffffd6 value = 42 value = -42.419 %+10.4g +42 -42.42 %- 10.4g 42 -42.42 %-#10.4g 42.00 -42.42 value = “forty-two” %10.5s forty

  5. Stuff I’d never heard of... sprintf(s,”%*.*f”,width,prec,number) if width = 10, prec = 4, and number = 3.14159, then s == “ 3.142” What if you want to know how much was written to s ? sprintf(s,”%*.*f %n”,width,prec,number,&n) after which n == 11

  6. Bugs & No Bugs #include <stdio.h> char *T="IeJKLMaYQCE]jbZRskc[SldU^V\\X\\|/_<[<:90!\"$434-./2>]s", K[3][1000],*F,x,A,*M[2],*J,r[4],*g,N,Y,*Q,W,*k,q,D;X(){r [r [r[3]=M[1-(x&1)][*r=W,1],2]=*Q+2,1]=x+1+Y,*g++=((((x& 7) -1)>>1)-1)?*r:r[x>>3],(++x<*r)&&X();}E(){A||X(x=0,g =J ),x=7&(*T>>A*3),J[(x[F]-W-x)^A*7]=Q[x&3]^A*(*M)[2 +( x&1)],g=J+((x[k]-W)^A*7)-A,g[1]=(*M)[*g=M[T+=A ,1 ][x&1],x&1],(A^=1)&&(E(),J+=W);}l(){E(--q&&l () );}B(){*J&&B((D=*J,Q[2]<D&&D<k[1]&&(*g++=1 ), !(D-W&&D-9&&D-10&&D-13)&&(!*r&&(*g++=0) ,* r=1)||64<D&&D<91&&(*r=0,*g++=D-63)||D >= 97&&D<123&&(*r=0,*g++=D-95)||!(D-k[ 3] )&&(*r=0,*g++=12)||D>k[3]&&D<=k[ 1] -1&&(*r=0,*g++=D-47),J++));}j( ){ putchar(A);}b(){(j(A=(*K)[D* W+ r[2]*Y+x]),++x<Y)&&b();}t () {(j((b(D=q[g],x=0),A=W) ), ++q<(*(r+1)<Y?*(r+1): Y) )&&t();}R(){(A=(t( q= 0),'\n'),j(),++r [2 ]<N)&&R();}O() {( j((r[2]=0,R( )) ),r[1]-=q) && O(g-=-q) ;} C(){( J= gets (K [1]))&&C((B(g=K[2]),*r=!(!*r&&(*g++=0)),(*r)[r]=g-K[2],g=K[2 ],r[ 1]&& O()) );;} main (){C ((l( (J=( A=0) [K], A[M] =(F= (k=( M[!A ]=(Q =T+( q=(Y =(W= 32)- (N=4 )))) +N)+ 2)+7 )+7) ),Y= N<<( *r=! -A)) );;} class Line { Point* p1; Point* p2; Line(Point* p1, Point* p2) { p1 = new Point(); p2 = new Point(); this.p1 = p1; this.p2 = p2; } // other methods } 2000 IOCCC winner “Best use of flags” www.cs.hmc.edu/~dodds/anderson.c

  7. “Power Towers” 20000 3 Input: n b Idea: Write n in base b -- including all exponents! 9 2 3 5 20000 == 3 + 3 + 2*3 + 2*3 + 2 Output: 2 3 3+2 3 2 20000 = 3 +3 +2*3 +2*3 +2

More Related