Chinaunix首页 | 论坛 | 博客
  • 博客访问: 189412
  • 博文数量: 54
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 630
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-02 18:41
文章分类

全部博文(54)

文章存档

2011年(1)

2009年(30)

2008年(23)

我的朋友

分类: C/C++

2008-11-02 20:08:36

请定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句
#include
#define MAX(a,b) ((long)(a-b)&0x80000000 ? b:a)    //判断符号位
void main()
{
 long a=99,b=100,c;
 c=MAX(a,b);
 printf("The bigger number is:%d\n",c);
}
 
 
计算 a^b << 2
运算符优先级:
单目 > 双目
算术双目 > 其他双目
位运算(>>,<<) > 关系运算(==,!=,<,>,<=,>=) > 按位运算(~,&,|,^) > 逻辑运算(&&,||,!) > 三目运算(? :条件运算)
赋值运算仅仅高于顺序运算
 
 
如何输出源文件的标题和目前执行行的行数?
#include
void main()
{
 printf("The file name is:%s\n",__FILE__);
 printf("The current line is:%d\n",__LINE__);
}
ANSI C标准预定义宏:
__LINE__
__FILE__
__DATE__
__TIME__
__STDC__ 当要求程序严格遵循ANSI C标准时该标识符被赋值为1
__cplusplus__ 当编写C++程序时该标识符被定义
 
不能做switch()的参数类型是:
浮点型
 
 
不使用其他变量,交换两个整型a,b的值(两种方法)
建议使用第一种(使用异或^),因为第二种方法容易产生和溢出。
(1)
#include
void main()
{
 int a=5,b=6;
 a=a^b;
 b=a^b;
 a=a^b;
 printf("NOW a=%d,b=%d\n",a,b);
}
(2)
#include
void main()
{
 int a=5,b=6;
 a=a+b;
 b=a-b;
 a=a-b;
 printf("NOW a=%d,b=%d\n",a,b);
}
 
 
写出float x 与“零值”比较的if语句。
if(x>=0.000001 && x<=-0.000001)(x不为0的比较)
float: 6位精度
double: 16位精度
 
 
不使用if,?:,switch或其他判断语句找出两个数较大值。
(1)
 int max=((a+b)+abs(a-b))/2;
(2)
 int c[2]={a,b};
 int max=c[unsigned int(a-b)>>(sizeof(int)*8-1)]; //强制转换成无符号数再右移只剩符号位
 
阅读(976) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~