文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C语言经典100例(Python版本)046-050

C语言经典100例(Python版本)046-050

时间:2007-09-17  来源:jcodeer

'''
【程序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

'''
【程序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 \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);
    else
        printf("\40: No such value.\n");
}
不知道如何用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\n",MAXIMUM(a,b));
#else
    printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#ifndef MIN
    printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
    printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
#undef MAX
#ifdef MAX
    printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
    printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#define MIN
#ifndef MIN
    printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
    printf("\40: The larger one is %d\n",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)

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载