"Program 41" title: Learn how static variables are used to define the use Method 1. Program Analysis: 2. Program Source: "' # Python does not have this function, just can:) def varfunc (): var = 0 print ' var =% d ' var var + = 1if __name__ = = ' __main__ ': for I in range (3): Varfunc () # Attribut of class# as a property of class St atic: Staticvar = 5 def varfunc (self): self . Staticvar + = 1 print self. Staticvarprint Static.staticvara = Static () for I in Range (3): A.varfunc ()
"topic: Learning how to use Auto to define variables 1. Program Analysis: 2. Program Source: No Autokeyword, use variable scope for example" num = 2def autofunc (): num = 1 print ' int Ernal Block num =%d '% num num + = 1for i in range (3): print ' The num =%d '% num num + 1 autofunc ()
"Program 43" topic: There is a way to learn to use static. 1. Program Analysis: 2. Program Source: There is a static variable to use, Python is not, to demonstrate a Python scope using the 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 rang E (3): nnum + = 1 print ' The num =%d '% nnum inst.inc ()
"Program 45" topic: Learn how to use register to define variables. 1. Program Analysis: 2. Program Source: No Registerkeyword, replace with integer variable ' tmp = 0for i in range (1,101): tmp + = IPrint ' The sum is%d '% tmp
"" "program 46" title: Macro # define command exercises (1) 1. Program Analysis: 2. Program Source: No C language macros, so write "TRUE = 1FALSE = 0def SQ (x): return x * xprint ' programs wil L Stop if input value less than 50. ' again = 1while again: num = int (raw_input (' Please input number ') "print ' The square for this number is %d '% (SQ (n UM) if num >=: again = TRUE else: again = FALSE
"Title: Macro # define command exercises (2) 1. Program Analysis: 2. Program Source: #include" stdio.h "#define Exchange (A, b) {\ */* Macros in the macro definition agree to include two clothes commands, at which point you must add" \ "*/int t; T=a; A=b; b=t; } ' This macro defines that Python does not support ' Def exchange (A, B): A, B = b,a return (A, b) if __name__ = = ' __main__ ': x = ten y = print ' x =%d,y =%d '% (x, y) x, y = Exchange (x, y) print ' x =%d,y =%d '% (x, y)
"Program 48" title: Macro # define command exercises (3) 1. Program Analysis: 2. Program Source: #define LAG > #define SMA < #define EQ = = #include "stdio.h" void Main () {in T 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); Els E 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 achieve similar functions ' if __name__ = = ' __main__ ': i = ten j = 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" title: #if #ifdef和 Comprehensive application of #ifndef. 1. Program Analysis: 2. Program Source: #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 (A, b)); #elseprintf ("\40:the Lower one is%d\n ", MINIMUM (A, b)); #endif #ifndef minprintf (" \40:the Lower one is%d\n ", MINIMUM (b)); #elseprintf (" \40:the larger one is%d \ n ", MAXIMUM (A, 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, 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} This is also a preprocessing use, and Python does not support this mechanism to demonstrate the use of lambda. "' MAXIMUM = lambda x, y: (x > Y) * x + (x < y) * Yminimum = lambda x, y: (x > Y) * y + (x < y) * xif __name __ = = ' __main__ ': a = ten b = print ' The Largar one is%d '% MAXIMUM (A, B) print ' The lower one is%d '% MINI MUM (A, B)
Python rewrite C language program 100 cases--PART6