程序源代码: test.h 文件如下:
#define LAG > #define SMA < #define EQ ==
#include “test.h” /*一个新文件50.c,包含test.h*/ #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”); }
题目:学习使用按位与 & 。
___________________________________________________________________ 程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
___________________________________________________________________ 程序源代码:
#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); }
题目:学习使用按位或 | 。
___________________________________________________________________ 程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
___________________________________________________________________
程序源代码:
#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); }
题目:学习使用按位异或 ^ 。
___________________________________________________________________ 程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
___________________________________________________________________ 程序源代码:
#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); }
题目:取一个整数a从右端开始的4~7位。
___________________________________________________________________ 程序分析:可以这样考虑: (1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4) (3)将上面二者进行&运算。
___________________________________________________________________ 程序源代码: main() {
unsigned a,b,c,d; scanf(“%o”,&a);
b=a>>4;
c=~(~0<<4); d=b&c;
printf(“%o\\n%o\\n”,a,d); }
题目:学习使用按位取反~。
___________________________________________________________________ 程序分析:~0=1; ~1=0;
___________________________________________________________________ 程序源代码:
#include “stdio.h” main() {
int a,b; a=234; b=~a;
printf(“\\40: The a’s 1 complement(decimal) is %d \\n”,b); a=~a;
printf(“\\40: The a’s 1 complement(hexidecimal) is %x \\n”,a); }
题目:画图,学用circle画圆形。
___________________________________________________________________ 程序源代码:
/*circle*/
#include “graphics.h” main() {
int driver,mode,i; float j=1,k=1;
driver=VGA;mode=VGAHI; initgraph(&driver,&mode,”\ setbkcolor(YELLOW); for(i=0;i<=25;i++) {
setcolor(8);
circle(310,250,k); k=k+j; j=j+0.3;
} }
题目:画图,学用line画直线。
___________________________________________________________________ 程序源代码:
#include “graphics.h” main() {
int driver,mode,i; float x0,y0,y1,x1; float j=12,k;
driver=VGA;mode=VGAHI; initgraph(&driver,&mode,”\ setbkcolor(GREEN);
x0=263;y0=263;y1=275;x1=275; for(i=0;i<=18;i++) {
setcolor(5);
line(x0,y0,x0,y1); x0=x0-5; y0=y0-5; x1=x1+5; y1=y1+5; j=j+10; }
x0=263;y1=275;y0=263; for(i=0;i<=20;i++) {
setcolor(5);
line(x0,y0,x0,y1); x0=x0+5; y0=y0+5; y1=y1-5; } }
题目:画图,学用rectangle画方形。
___________________________________________________________________ 程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 ___________________________________________________________________
程序源代码:
#include “graphics.h” main() {
int x0,y0,y1,x1,driver,mode,i; driver=VGA;mode=VGAHI; initgraph(&driver,&mode,”\ setbkcolor(YELLOW);
x0=263;y0=263;y1=275;x1=275; for(i=0;i<=18;i++) {
setcolor(1);
rectangle(x0,y0,x1,y1); x0=x0-5; y0=y0-5; x1=x1+5; y1=y1+5; }
settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy(150,40,”How beautiful it is!”); line(130,60,480,60); setcolor(2);
circle(269,269,137); }
(完整版)C语言100个经典题目
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)