《c语言教程》

下载本书

添加书签

c语言教程- 第33节


按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
    if(num》=50)
      again=TRUE;
    else
      again=FALSE;
  }
  getch();


【程序47】
题目:宏#define命令练习(2)
1。程序分析:            
2。程序源代码:
#include 〃stdio。h〃
#include 〃conio。h〃
/*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上〃”*/
#define exchange(a;b) { 
                        int t;
                        t=a;
                        a=b;
                        b=t;
                      }
void main(void)

  int x=10;
  int y=20;
  printf(〃x=%d; y=%dn〃;x;y);
  exchange(x;y);
  printf(〃x=%d; y=%dn〃;x;y);
  getch();


【程序48】
题目:宏#define命令练习(3)   
1。程序分析:
2。程序源代码:
#define LAG 》
#define SMA 《
#define EQ 
#include 〃stdio。h〃
#include 〃conio。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〃);
  getch();


【程序49】
题目:#if #ifdef和#ifndef的综合应用。
1。 程序分析: 
2。程序源代码:
#include 〃stdio。h〃
#include 〃conio。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 %dn〃;MAXIMUM(a;b));
#else
  printf(〃40: The lower one is %dn〃;MINIMUM(a;b));
#endif
#ifndef MIN
  printf(〃40: The lower one is %dn〃;MINIMUM(a;b));
#else
  printf(〃40: The larger one is %dn〃;MAXIMUM(a;b));
#endif
#undef MAX
#ifdef MAX
  printf(〃40: The larger one is %dn〃;MAXIMUM(a;b));
#else
  printf(〃40: The lower one is %dn〃;MINIMUM(a;b));
#endif
#define MIN
#ifndef MIN
  printf(〃40: The lower one is %dn〃;MINIMUM(a;b));
#else
  printf(〃40: The larger one is %dn〃;MAXIMUM(a;b));
#endif
  getch();


【程序50】
题目:#include 的应用练习   
1。程序分析:
2。程序源代码:
test。h 文件如下:
#define LAG 》
#define SMA 《
#define EQ 

主文件如下:
#include 〃test。h〃 /*一个新文件50。c,包含test。h*/
#include 〃stdio。h〃
#include 〃conio。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〃);
  getch();

 
 
  。:。:经典c程序100例5160:。:。 
  

    经典c程序100例5160

【程序51】
题目:学习使用按位与 & 。   
1。程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
2。程序源代码:
#include 〃stdio。h〃
main()

int a;b;
a=077;
b=a&3;
printf(〃40: The a & b(decimal) is %d n〃;b);
b&=7;
printf(〃40: The a & b(decimal) is %d n〃;b);


【程序52】
题目:学习使用按位或 | 。
1。程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1            
2。程序源代码:
#include 〃stdio。h〃
main()

int a;b;
a=077;
b=a|3;
printf(〃40: The a & b(decimal) is %d n〃;b);
b|=7;
printf(〃40: The a & b(decimal) is %d n〃;b);


【程序53】
题目:学习使用按位异或 ^ 。   
1。程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
2。程序源代码:
#include 〃stdio。h〃
main()

int a;b;
a=077;
b=a^3;
printf(〃40: The a & b(decimal) is %d n〃;b);
b^=7;
printf(〃40: The a & b(decimal) is %d n〃;b);


【程序54】
题目:取一个整数a从右端开始的4~7位。
程序分析:可以这样考虑: 
(1)先使a右移4位。
(2)设置一个低4位全为1;其余全为0的数。可用~(~04;
c=~(~0
小提示:按 回车 [Enter] 键 返回书目,按 ← 键 返回上一页, 按 → 键 进入下一页。 赞一下 添加书签加入书架