Loading...
  所在位置:论坛首页 -> 计算机科学与技术 -> 程序设计 -> c语言习题答案(第8章,新版第9章)(潭浩强,清华出版社)
回复

c语言习题答案(第8章,新版第9章)(潭浩强,清华出版社)

作者:pp 时间:2007-12-15 14:47:46 收藏 编辑

第八章  编译预处理
8.1
#define SWAP(a,b) t=b;b=a;a=t
main()
{int a,b,t;
 scanf("%d,%d",&a,&b);
 SWAP(a,b);
 printf("a=%d\tb=%d\n",a,b);
}

8.2
#define SURPLUS(a,b) ((a)%(b))
main()
{int a,b;
 scanf("%d,%d",&a,&b);
 printf("%d",SURPLUS(a,b));
}

8.3
#include"math.h"
#define S(a,b,c) ((a+b+c)/2)
#define AREA(a,b,c) (sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)))
main()
{float a,b,c;
 scanf("%f,%f,%f",&a,&b,&c);
 if(a+b>c&&b+c>a&&a+c>b)
   printf("area=%8.2f\n",AREA(a,b,c));
 else
   printf("input error");
}

8.4
#define LEAP(y) (y%4==0)&&(y%100!=0)||(y%400==0)
main()
{int year;
 scanf("%d",&year);
 if(LEAP(year))
   printf("yes");
 else
   printf("no");
}

8.5
printf("value=%format\t",x);
printf("value=%format\t",x);putchar('\n');
printf("value=%format\t",x1);printf("value=%format\t",x2);putchar('\n');
输出结果:
value=5.000000ormat        value=5.000000ormat
value=3.000000ormat        value=8.000000ormat

8.6
#define PR printf
#define NL "\n"
#define Fs "%f"
#define F "%6.2f"
#define F1 F NL
#define F2 F "\t" F NL
#define F3 F "\t" F "\t" F NL
main()
{float a,b,c;
 scanf(Fs,&a);
 scanf(Fs,&b);
 scanf(Fs,&c);
 PR(NL);
 PR(F1,a);
 PR(F1,b);
 PR(F1,c);
 PR(NL);
 PR(F2,a,b);
 PR(F1,c);
 PR(NL);
 PR(F3,a,b,c);
}

8.7
/*format.h
文件*/
#define DIGIT(d) printf("%d\n",d)
#define FLOAT(f) printf("%8.2f\n",f)
#define STRING(s) printf("%s\n",s)
/*
用户文件*/
#include"format.h"
main()
{int d,num;
 float f;
 char s[80];
 scanf("%d",&num);
 switch(num)
   {case 1:scanf("%d",&d);
           DIGIT(d);
           break;
    case 2:scanf("%f",&f);
           FLOAT(f);
           break;
    case 3:scanf("%s",s);
           STRING(s);
           break;
    default:printf("error");
   }
}

8.8
main()
{int a,b,c;
 scanf("%d,%d,%d",&a,&b,&c);
 printf("max=%d\n",max(a,b,c));
}
max(a,b,c)
int a,b,c;
{int t;
 t=x>y?x:y;
 return(t>z?t:z);
}

#define MAX(a,b) ((a)>(b)?(a):(b))
main()
{int a,b,c;
 scanf("%d,%d,%d",&a,&b,&c);
 printf("max=%d\n",MAX(MAX(a,b),c));
}

8.10
#include"stdio.h"
#define MAX 80
#define CHANGE 1
main()
{char str[MAX];
 int i;
 scanf("%s",str);
 #if(CHANGE)
   {for(i=0;i<MAX;i++)
      {if(str[i]!='\0')
         if(str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z')
           str[i]++;
         else if(str[i]=='z'||str[i]=='Z')
           str[i]-=25;
      }
    }
 #endif
 printf("\n%s\n",str);
}
高级回复

操作选项: 评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
  首页
Copyright 2006-2012 HistoryCreator.com Powered By: BBSGood 5
吉ICP备06005902号