Python rewrite C language program 100 example -- Part6

Source: Internet
Author: User

''' [Program 41] Question: Learn how to use static variables to define static variables. 1. program Analysis: 2. program source code: ''' # python does not have this function. It can only be like this :) def varfunc (): var = 0 print 'var = % d' % var + = 1if _ name _ = '_ main _': for I in range (3 ): varfunc () # attribut of class # as an attribute of the class. class Static: StaticVar = 5 def varfunc (self): self. staticVar + = 1 print self. staticVarprint Static. staticVara = Static () for I in range (3):. varfunc ()

'''Question: Learn how to use auto to define variables. 1. program Analysis: 2. program source code: without the auto keyword, use the variable scope for example '''num = 2def autofunc (): num = 1 print 'internal block num = % d' % num + = 1for I in range (3 ): print 'the num = % d' % num + = 1 autofunc ()

''' [Program 43] Question: Learn another usage of static. 1. program Analysis: 2. program source code: There is a static variable usage, python does not exist, demonstrate the use of a python scope method '''class Num: nNum = 1 def inc (self): self. nNum + = 1 print 'nnum = % d' % self. nNumif _ name _ = '_ main _': nNum = 2 inst = Num () for I in range (3 ): nNum + = 1 print 'the num = % d' % nNum inst. inc ()

''' [Program 45] Question: Learn How to Use register to define variables. 1. program Analysis: 2. program source code: without The register keyword, replace '''tmp = 0for I in range (1,101): tmp + = iprint 'the sum is % d' % tmp with an integer variable


''' [Program 46] Question: Macro # define command exercise (1) 1. program Analysis: 2. program source code: If there is no macro in C language, then '''true = 1 FALSE = 0def SQ (x) is written ): return x * xprint 'program will stop if input value less than 50. 'Again = 1 while again: num = int (raw_input ('Please input number') print 'the square for this number is % d' % (SQ (num )) if num> = 50: again = TRUE else: again = FALSE


'''Question: Macro # define command exercise (2) 1. program Analysis: 2. program source code: # include "stdio. h "# define exchange (a, B) {\/* The macro definition allows two clothes commands. In this case, you must add" \ "*/int t on the rightmost side; \ t = a; \ a = B; \ B = t; \} 'This macro-defined python does not support '''def exchange (a, B): a, B = B, a return (a, B) if _ name _ = '_ main _': x = 10 y = 20 print 'x = % d, y = % d' % (x, y) x, y = exchange (x, y) print 'X = % d, y = % d' % (x, y)


''' [Program 48] Question: Macro # define command exercise (3) 1. program Analysis: 2. program source code: # define LAG> # define SMA <# define EQ = # include "stdio. h "void main () {int I = 10; int j = 20; if (I LAG j) printf (" \ 40: % d larger than % d \ n ", i, j); else if (I EQ j) printf ("\ 40: % d equal to % d \ n", I, j); else if (I SMA j) printf ("\ 40: % d smaller than % d \ n", I, j); elseprintf ("\ 40: No such value. \ n ") ;}do not know how to use python to implement similar functions ''' if _ name _ = '_ main __': I = 10 j = 20 if I> j: print '% d larger than % d' % (I, j) elif I = j: print '% d equal to % d' % (I, j) elif I <j: print' % d smaller than % d' % (I, j) else: print 'no such value'


''' [Program 49] Question: # if # ifdef and # ifndef comprehensive application. 1. Program Analysis: 2. program source code: # include "stdio. h" # define MAX # define MAXIMUM (x, y) (x> y )? X: y # define MINIMUM (x, y) (x> y )? Y: xvoid main () {int a = 10, B = 20; # ifdef MAXprintf ("\ 40: The larger one is % d \ n", MAXIMUM (, b); # elseprintf ("\ 40: The lower one is % d \ n", MINIMUM (a, B); # endif # ifndef MINprintf ("\ 40: the lower one is % d \ n ", MINIMUM (a, B); # elseprintf (" \ 40: The larger one is % d \ n ", MAXIMUM (, b); # endif # undef MAX # ifdef MAXprintf ("\ 40: The larger one is % d \ n", MAXIMUM (a, B )); # elseprintf ("\ 40: The lower one is % d \ n", MINIMUM (a, B )); # Endif # define MIN # ifndef MINprintf ("\ 40: The lower one is % d \ n", MINIMUM (a, B); # elseprintf ("\ 40: the larger one is % d \ n ", MAXIMUM (a, B); # endif} is used for preprocessing. python does not support this mechanism to demonstrate lambda usage. '''Maximum = lambda x, y: (x> y) * x + (x <y) * yMINIMUM = lambda x, y: (x> y) * y + (x <y) * xif _ name _ = '_ main __': a = 10 B = 20 print 'the largar one is % d' % MAXIMUM (a, B) print 'the lower one is % d' % MINIMUM (a, B)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.