题目41:学习static定义静态变量的用法
#include<stdio.h>
void f()
{
static int m = 0; //定义静态变量m,下次访问此函数不再执行声明和赋值语句,故第2次循环时m为1
int n = 0; //定义动态变量n
printf("静态变量m=%d 动态变量n=%d\n", m, n);
m++;
n++;
}
int main()
{
int i= 0;
for(i = 1; i <= 3; i++)
{
f();
}
return 0;
}
运行结果
题目42:学习使用auto定义变量的用法
#include<stdio.h>
int main()
{
int a = 0, i = 0; //相当于auto int
for(i = 1; i <= 3; i++)
{
printf("%d ", a);
a++;
{
auto int b = 0; //auto动态变量b仅在模块{}内存在,每次结束循环都消失,auto可省略
printf("%d\n", b);
b++;
}
}
return 0;
}
运行结果
题目43:学习使用static的另一用法。static修饰局部变量,扩大应用范围,修饰全局变量,缩小应用范围
#include<stdio.h>
//int a = 1; //定义全局变量
static int a = 1; //定义静态全局变量,只可在本文件中引用
int main()
{
void f();
f();
return 0;
}
//文件2
#include<stdio.h>
extern int a; //声明a为已经定义的外部变量
void f() //定义函数f
{
printf("%d\n", a);
}
运行结果
报错,static修饰的变量只能在本文件中引用
题目44:学习使用external的用法
#include<stdio.h>
int main()
{
extern int a; //在定义时就使用,需用extern声明
printf("%d\n", a);
return 0;
}
int a = 1; //声明全局变量a
运行结果
题目45:学习使用register定义变量的方法
#include<stdio.h>
int main()
{
register int i = 0, sum = 0; //将循环中频繁使用的变量定义为寄存器变量,提高效率
for(i = 1; i <= 10000; i++)
{
sum = sum + i;
}
printf("%d\n", sum);
return 0;
}
运行结果
题目46:宏#define命令练习(1)
#include <stdio.h>
#define PI 3.14
#define R 4
int main()
{
double area;
area = PI * R * R;
printf("面积是%.2f\n", area);
return 0;
}
运行结果
题目47:宏#define命令练习(2)
#include <stdio.h>
#define PI 3.14
#define area(r) PI*r*r
int main()
{
double r = 4;
printf("面积是%.2f\n", area(r));
return 0;
}
运行结果
题目48:宏#define命令练习(3)
#include <stdio.h>
#define PRINT printf("*\n");
int main()
{
PRINT
PRINT
return 0;
}
运行结果
题目49:#if #ifdef和#ifndef的综合应用。
#if是条件编译语句,在编译阶段执行,如果后面的条件成立,就编译对应的语句
#ifdef是条件编译语句,在编译阶段执行,后面跟一个宏的名称,如果这个宏已经定义了,就编译对应的语句
#ifndef是条件编译语句,在编译阶段执行,后面跟一个宏的名称,如果这个宏没有定义,就编译对应的语句
#include <stdio.h>
#define MAX(a,b) a>b?a:b
int main()
{
int a = 2, b = 1;
#if(1) //1为真,编译第1个printf语句
printf("1 %d\n", MAX(a,b));
#endif
#if(0) //0为假,不能编译第2个printf语句
printf("2 %d\n", MAX(a,b));
#endif
#ifdef MAX(a,b) //MAX(a,b)已经定义,编译第3个printf语句
printf("3 %d\n", MAX(a,b));
#else
printf("4 not define\n");
#endif
#ifndef MIN(a,b) //MIN(a,b)未定义,编译第5个printf语句
printf("5 not define\n");
#else
printf("6 %d\n", MIN(a,b));
#endif
#define MIN(a,b) a<b?a:b //宏定义MIN(a,b)
#ifdef MIN(a,b) //MIN(a,b)已经定义,编译第7个printf语句
printf("7 %d\n", MIN(a,b));
#else
printf("8 not define\n");
#endif
return 0;
}
运行结果
题目50:#include 的应用练习
#include <stdio.h>
#include "PI.h" //引用自定义的头文件
int main()
{
double r = 2;
printf("面积是%.2f\n", r*r*PI); //PI未定义
return 0;
}
#define PI 3.14
运行结果
题目51:学习使用按位与 &
#include <stdio.h>
int main()
{
unsigned char a, b;
a = 63; //00111111
b = 170; //10101010
printf("%d\n", a&b); //00101010
return 0;
}
运行结果
题目52:学习使用按位或 |
#include <stdio.h>
int main()
{
unsigned char a, b;
a = 63; //00111111
b = 170; //10101010
printf("%d\n", a|b); //10111111
return 0;
}
运行结果
题目53:学习使用按位异或^
#include <stdio.h>
int main()
{
unsigned char a, b;
a = 63; //00111111
b = 170; //10101010
printf("%d\n", a^b); //10010101
return 0;
}
运行结果
题目54:取一个整数a从右端开始的4~7位
#include <stdio.h>
int main()
{
unsigned char a = 0, b = 0;
a = 63; //00111111
b = a & 0xf0;
printf("%d\n", b);
return 0;
}
运行结果
#include <stdio.h>
int main()
{
int a;
printf("输入整数a:");
scanf("%d", &a);
a = a >> 4; //把a右移4位,从右往左,依次是第0位,第1位……
a = a & 15; //与后四位是1111的数15按位与,不过最好是用~(~a>>4)
printf("%d\n", a);
return 0;
}
//2469 1001 1010 0101
//右移4位 0000 1001 1010
//15 0000 0000 1111
//&15结果 0000 0000 1010 取得最右端开始的4~7位
运行结果
题目55:学习使用按位取反~
#include <stdio.h>
int main()
{
unsigned char a;
a = 240; //11110000
a = ~a; //00001111按位取反
printf("%d\n", a); //15
return 0;
}
运行结果
题目56:画图,学用circle画圆形。
画图需要:
1.需安装easyX
2.文件格式要用cpp
3.声明头文件#include<graphics.h>
4.initgraph初始化绘图屏幕,closegraph关闭绘图屏幕
5.getch()暂停看图,否则会一闪而过
#include <stdio.h>
#include <graphics.h> //绘图库头文件,绘图语句需要
#include <conio.h> //控制台输入输出头文件,getch()语句需要
int main()
{
initgraph(640, 480); //初始化640*480的绘图屏幕
circle(320, 240, 100); //画以(320,240)为圆心,半径为100的圆
getch();
closegraph(); //关闭绘图屏幕
return 0;
}
运行结果
题目57:画图,学用line画直线。
#include <stdio.h>
#include <graphics.h> //绘图库头文件,绘图语句需要
#include <conio.h> //控制台输入输出头文件,getch()语句需要
int main()
{
initgraph(640, 480);
line(160,240,480,240); //绘制点(160,240)到点(480,240)之间的线
line(320,120,320,360); //绘制点(320,120)到点(320,360)之间的线
getch();
closegraph();
return 0;
}
运行结果
题目58:画图,学用rectangle画方形。
#include <stdio.h>
#include <graphics.h> //绘图库头文件,绘图语句需要
#include <conio.h> //控制台输入输出头文件,getch()语句需要
int main()
{
initgraph(640, 480);
rectangle(160,120,480,360); //以点(160,120)和点(480,360)为对角线作矩形
getch();
closegraph();
return 0;
}
运行结果
题目59:画图,综合例子。
#include <graphics.h> //绘图库头文件,绘图语句需要
#include <conio.h> //控制台输入输出头文件,getch()语句需要
int main()
{
initgraph(480,360); //设置大小为480*360的绘图窗口
setorigin(240,180); //设置原点,默认是(0,0),向右向下为正
setbkcolor(0x7c5731); //填充背景,参数可以是BLUE或0xff0000或RGB(0,0,255)
cleardevice(); //清除图形屏幕
//setlinecolor(WHITE); //设置线条颜色
//设置线条样式,PS_SOLID表示线型为实线, PS_ENDCAP_FLAT表示端点平坦,10表示宽度为10像素
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 10);
//setfillcolor(0x24c097); //设置填充颜色
//fillroundrect函数用于画填充圆角矩形
int point[] = {-75, -111, 75, 39, 36, 36};
fillpoly(3, point);
getch(); //暂停看图
closegraph(); //关闭绘图窗口
return 0;
}
运行结果
题目60:画图,综合例子。
#include <graphics.h> //绘图库头文件,绘图语句需要
#include <conio.h> //控制台输入输出头文件,getch()语句需要
#include <time.h>
int main()
{
initgraph(640,480); //设置大小为640*480的绘图窗口
srand(time(NULL)); //设置随机种子 用time函数返回系统时间,然后利用系统时间作为种子产生随机数
setfont(30, 0, "Arial"); //设置字母的字体和大小 setfont(字号,风格,字体)
setfillstyle(BLACK); //设置清除字母的填充区域颜色
char target;
int x, y;
while(true) //无限循环
{
target = 65 + rand() % 26; //产生任意大写字母,rand()产生一个随机数,对26取余,结果为0-25,加上65,正好是A-Z的ASCII码
x = rand() % 640; //产生任意下落位置
for(y = 0; y < 480; y++)
{
setcolor(GREEN); //设置字母的颜色
outtextxy(x, y, target); //outtextxy在指定位置输出字符串,利用循环不断在纵坐标差1个像素的位置输出相同字符,给人下落的视错觉
Sleep(10); //延时,并清除字母,Sleep(10)延时10毫秒,sleep(10)延时10秒
}
}
closegraph(); //关闭绘图窗口
return 0;
}
运行结果为动态图,产生随机的字母,并且从随机位置下落