zoukankan      html  css  js  c++  java
  • Python重写C语言程序100例--Part6

    '''
    【程序41】
    题目:学习static定义静态变量的使用方法   
    1.程序分析:
    2.程序源码:
    '''
    # python没有这个功能了,仅仅能这样了:)
    def varfunc():
        var = 0
        print 'var = %d' % var
        var += 1
    if __name__ == '__main__':
        for i in range(3):
            varfunc()
    
    # attribut of class
    # 作为类的一个属性吧
    class Static:
        StaticVar = 5
        def varfunc(self):
            self.StaticVar += 1
            print self.StaticVar
    
    print Static.StaticVar
    a = Static()
    for i in range(3):
        a.varfunc()
    

    '''
    题目:学习使用auto定义变量的使用方法
    1.程序分析:      
    2.程序源码:
    没有autokeyword,使用变量作用域来举例吧
    '''
    num = 2
    def autofunc():
        num = 1
        print 'internal block num = %d' % num
        num += 1
    for i in range(3):
        print 'The num = %d' % num
        num += 1
        autofunc()
        
    

    '''
    【程序43】
    题目:学习使用static的还有一使用方法。   
    1.程序分析:
    2.程序源码:
    有一个static变量的使用方法,python是没有,演示一个python作用域使用方法
    '''
    class Num:
        nNum = 1
        def inc(self):
            self.nNum += 1
            print 'nNum = %d' % self.nNum
    
    if __name__ == '__main__':
        nNum = 2
        inst = Num()
        for i in range(3):
            nNum += 1
            print 'The num = %d' % nNum
            inst.inc()
    
    '''
    【程序45】
    题目:学习使用register定义变量的方法。
    1.程序分析:
    2.程序源码:
    没有registerkeyword,用整型变量取代
    '''
    tmp = 0
    for i in range(1,101):
        tmp += i
    print 'The sum is %d' % tmp
    


     

    '''
    【程序46】
    题目:宏#define命令练习(1)   
    1.程序分析:
    2.程序源码:
    没有C语言的宏,就这么写了
    '''
    TRUE = 1
    FALSE = 0
    def SQ(x):
        return x * x
    print '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
    


     

    '''
    题目:宏#define命令练习(2)
    1.程序分析:            
    2.程序源码:
    #include "stdio.h"
    #define exchange(a,b) {  /*宏定义中同意包括两道衣裳命令的情形,此时必须在最右边加上""*/
                int t;
                t=a;
                a=b;
                b=t;
               }'
    这个宏定义python不支持
    '''
    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)
    


     

    '''
    【程序48】
    题目:宏#define命令练习(3)   
    1.程序分析:
    2.程序源码:
    #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 
    ",i,j);
    	else if(i EQ j)
    		printf("40: %d equal to %d 
    ",i,j);
    	else if(i SMA j)
    		printf("40:%d smaller than %d 
    ",i,j);
    	else
    		printf("40: No such value.
    ");
    }
    不知道怎样用python实现相似的功能
    '''
    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'
        
    


     

    '''
    【程序49】
    题目:#if #ifdef和#ifndef的综合应用。
    1. 程序分析:
    2.程序源码:
    #include "stdio.h"
    #define MAX
    #define MAXIMUM(x,y) (x>y)?x:y
    #define MINIMUM(x,y) (x>y)?y:x
    void main()
    { 
    	int a=10,b=20;
    #ifdef MAX
    	printf("40: The larger one is %d
    ",MAXIMUM(a,b));
    #else
    	printf("40: The lower one is %d
    ",MINIMUM(a,b));
    #endif
    #ifndef MIN
    	printf("40: The lower one is %d
    ",MINIMUM(a,b));
    #else
    	printf("40: The larger one is %d
    ",MAXIMUM(a,b));
    #endif
    #undef MAX
    #ifdef MAX
    	printf("40: The larger one is %d
    ",MAXIMUM(a,b));
    #else
    	printf("40: The lower one is %d
    ",MINIMUM(a,b));
    #endif
    #define MIN
    #ifndef MIN
    	printf("40: The lower one is %d
    ",MINIMUM(a,b));
    #else
    	printf("40: The larger one is %d
    ",MAXIMUM(a,b));
    #endif
    }
    这个还是预处理的使用方法,python不支持这种机制,演示lambda的使用。
    '''
    MAXIMUM = lambda x,y :  (x > y) * x + (x < y) * y
    MINIMUM = lambda x,y :  (x > y) * y + (x < y) * x
    
    if __name__ == '__main__':
        a = 10
        b = 20
        print 'The largar one is %d' % MAXIMUM(a,b)
        print 'The lower one is %d' % MINIMUM(a,b)
    



     

  • 相关阅读:
    软件测试人员的年终绩效考核怎么应对
    收藏
    顶踩组件 前后两版
    订阅组件
    hdu 1963 Investment 完全背包
    hdu 4939 Stupid Tower Defense 动态规划
    hdu 4405 Aeroplane chess 动态规划
    cf 414B Mashmokh and ACM 动态规划
    BUPT 202 Chocolate Machine 动态规划
    hdu 3853 LOOPS 动态规划
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4338911.html
Copyright © 2011-2022 走看看