博文
函数大全(w开头)(2006-06-16 09:42:00)
摘要:函数名: wherex 功 能: 返回窗口内水平光标位置 用 法: int wherex(void); 程序例:
#include
int main(void) { clrscr(); gotoxy(10,10); cprintf("Current location is X: %d Y: %d\r\n", wherex(), wherey()); getch();
return 0; }
函数名: wherey 功 能: 返回窗口内垂直光标位置 用 法: int wherey(void); 程序例:
#include
int main(void) { clrscr(); gotoxy(10,10); cprintf("Current location is X: %d Y: %d\r\n", wherex(), wherey()); getch();
return 0; }
函数名: window 功 能: 定义活动文本模式窗口 用 法: void window(int left, int top, int right, int bottom); 程序例:
#include
int main(void) {
window(10,10,40,11); textcolor(BLACK); textbackground(WHITE); cprintf("This is a test\r\n");
return 0; }
函数名: write 功 能: 写到一文件中 用 法: int write(int handel, void *buf, int nbyte); 程序例:
#include #include #include #include #include #include
int main(void) { int handle; char string[40]; int length, res;
/* Create a file named "TEST.$$$" in the current directory and write a string to it. If "TEST.$$$" already exists, it will be overwritten. */
if ((handle......
经典c程序100例==91--100(2006-06-16 09:07:00)
摘要:【程序91】题目:时间函数举例11.程序分析:2.程序源代码:#include "stdio.h"#include "time.h"void main(){ time_t lt; /*define a longint time varible*/lt=time(NULL);/*system time and date*/printf(ctime(<)); /*english format output*/printf(asctime(localtime(<)));/*tranfer to tm*/printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/}==============================================================【程序92】题目:时间函数举例21.程序分析: 2.程序源代码:/*calculate time*/#include "time.h"#include "stdio.h"main(){ time_t start,end;int i;start=time(NULL);for(i=0;i<3000;i++){ printf("\1\1\1\1\1\1\1\1\1\1\n");}end=time(NULL);printf("\1: The different is %6.3f\n",difftime(end,start));}==============================================================【程序93】题目:时间函数举例31.程序分析:2.程序源代码:/*calculate time*/#include "time.h"#include "stdio.h"main(){ clock_t start,end;int i;double var;start=clock();for(i=0;i<10000;i++){ printf("\1\1\1\1\1\1\1\1\1\1\n");}end=clock();printf("\1: The different is %6.3f\n",(double)(end-start));}==========......
经典c程序100例==81--90(2006-06-16 09:06:00)
摘要:【程序81】题目:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。1.程序分析:2.程序源代码:output(long b,long i){ printf("\n%ld/%ld=809*%ld+%ld",b,i,i,b%i);}main(){long int a,b,i;a=809;for(i=10;i<100;i++){b=i*a+1;if(b>=1000&&b<=10000&&8*i<100&&9*i>=100)output(b,i); }}==============================================================【程序82】题目:八进制转换为十进制1.程序分析: 2.程序源代码:main(){ char *p,s[6];int n;p=s;gets(p);n=0;while(*(p)!='\0'){n=n*8+*p-'0';p++;}printf("%d",n);}==============================================================【程序83】题目:求0—7所能组成的奇数个数。1.程序分析:2.程序源代码:main(){long sum=4,s=4;int j;for(j=2;j<=8;j++)/*j is place of number*/{ printf("\n%ld",sum);if(j<=2)s*=7;elses*=8;sum+=s;}printf("\nsum=%ld",sum);}==============================================================【程序84】题目:一个偶数总能表示为两个素数之和。1.程序分析:2.程序源代码:#include "stdio.h"#include "math.h"main(){ int a,b,c,d;scanf("%d",&a);for(b=3;b<=a/2;b+=2){ for(c=2;c<=sqrt(b);c++)if......
经典c程序100例==71--80(2006-06-16 09:06:00)
摘要:【程序71】题目:编写input()和output()函数输入,输出5个学生的数据记录。1.程序分析:2.程序源代码:#define N 5struct student{ char num[6]; char name[8]; int score[4];} stu[N];input(stu)struct student stu[];{ int i,j; for(i=0;i<N;i++) { printf("\n please input %d of %d\n",i+1,N); printf("num: "); scanf("%s",stu[i].num); printf("name: "); scanf("%s",stu[i].name); for(j=0;j<3;j++) { printf("score %d.",j+1); scanf("%d",&stu[i].score[j]); } printf("\n"); }}print(stu)struct student stu[];{ int i,j;printf("\nNo. Name Sco1 Sco2 Sco3\n");for(i=0;i<N;i++){ printf("%-6s%-10s",stu[i].num,stu[i].name); for(j=0;j<3;j++) printf("%-8d",stu[i].score[j]); printf("\n");}}main(){ input(); print();}==============================================================【程序72】题目:创建一个链表。1.程序分析: 2.程序源代码:/*creat a list*/#include "stdlib.h"#include "stdio.h"struct list{ int data;struct list *next;};typedef struct list node;typedef node *link;void main(){ link ptr,head;int num,i;ptr=(link)malloc(sizeof(node));ptr=head;pr......
经典c程序100例==61--70(2006-06-16 09:05:00)
摘要:【程序61】题目:打印出杨辉三角形(要求打印出10行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 2.程序源代码:main(){int i,j;int a[10][10];printf("\n");for(i=0;i<10;i++) {a[i][0]=1; a[i][i]=1;}for(i=2;i<10;i++) for(j=1;j<i;j++) a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=0;i<10;i++) {for(j=0;j<=i;j++) printf("%5d",a[i][j]); printf("\n"); }}==============================================================【程序62】题目:学习putpixel画点。1.程序分析: 2.程序源代码:#include "stdio.h"#include "graphics.h"main(){int i,j,driver=VGA,mode=VGAHI;initgraph(&driver,&mode,"");setbkcolor(YELLOW);for(i=50;i<=230;i+=20) for(j=50;j<=230;j++) putpixel(i,j,1);for(j=50;j<=230;j+=20) for(i=50;i<=230;i++) putpixel(i,j,1);}==============================================================【程序63】题目:画椭圆ellipse 1.程序分析:2.程序源代码:#include "stdio.h"#include "graphics.h"#include "conio.h"main(){int x=360,y=160,driver=VGA,mode=VGAHI;int num=20,i;int top,bottom;initgraph(&driv......
经典c程序100例==51--60(2006-06-16 09:02:00)
摘要:【程序51】题目:学习使用按位与 & 。 1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=12.程序源代码:#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=02.程序源代码:#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的数。可用~(~0<<4)(3)将上面二者进行&运算。2.程序源代码:main(){unsigned a,b,c,d......
经典c程序100例==41--50(2006-06-16 09:01:00)
摘要:【程序41】题目:学习static定义静态变量的用法 1.程序分析:2.程序源代码:#include "stdio.h"varfunc(){int var=0;static int static_var=0;printf("\40:var equal %d \n",var);printf("\40:static var equal %d \n",static_var);printf("\n");var++;static_var++;}void main(){int i; for(i=0;i<3;i++) varfunc();}==============================================================【程序42】 题目:学习使用auto定义变量的用法1.程序分析: 2.程序源代码:#include "stdio.h"main(){int i,num;num=2; for (i=0;i<3;i++) { printf("\40: The num equal %d \n",num); num++; { auto int num=1; printf("\40: The internal block num equal %d \n",num); num++; } }}==============================================================【程序43】题目:学习使用static的另一用法。 1.程序分析:2.程序源代码:#include "stdio.h"main(){int i,num;num=2;for(i=0;i<3;i++){printf("\40: The num equal %d \n",num);num++;{static int num=1;printf("\40:The internal block num equal %d\n",num);num++;}}}==============================================================【程序44】题目:学习使用external的用法。1.程序分析:2.程序源代码:#include "stdio.h"int a,b,......
经典c程序100例==31--40(2006-06-16 09:00:00)
摘要:【程序31】题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。2.程序源代码:#include <stdio.h>void main(){char letter;printf("please input the first letter of someday\n");while ((letter=getch())!='Y')/*当所按字母为Y时才结束*/{ switch (letter){case 'S':printf("please input second letter\n"); if((letter=getch())=='a') printf("saturday\n"); else if ((letter=getch())=='u') printf("sunday\n"); else printf("data error\n"); break;case 'F':printf("friday\n");break;case 'M':printf("monday\n");break;case 'T':printf("please input second letter\n"); if((letter=getch())=='u') printf("tuesday\n"); else if ((letter=getch())=='h') printf("thursday\n"); else printf("data error\n"); break;case 'W':printf("wednesday\n");break;default: printf("data error\n"); } }}==============================================================【程序32】题目:Press any key to change color, do you want to try it. Please hurry up!1.程序分析: ......
经典c程序100例==21--30(2006-06-16 08:59:00)
摘要:【程序21】题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。1.程序分析:采取逆向思维的方法,从后往前推断。2.程序源代码:main(){int day,x1,x2;day=9;x2=1;while(day>0) {x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/ x2=x1; day--; }printf("the total is %d\n",x1);}==============================================================【程序22】题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定 比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出 三队赛手的名单。 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。 2.程序源代码:main(){char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/for(i='x';i<='z';i++) for(j='x';j<='z';j++) { if(i!=j) for(k='x';k<='z';k++) { if(i!=k&&j!=k) { if(i!='x'&&k!='x'&&k!='z') printf("order is a--%c\tb--%c\tc--%c\n",i,j,k); } } }}==============================================================【程序23】 题目:打印出如下图案(菱形)
****************************1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。 2.程序源代码:main(){......
经典c程序100例==11--20(2006-06-16 08:57:00)
摘要:【程序11】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月 后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....2.程序源代码:main(){long f1,f2;int i;f1=f2=1;for(i=1;i<=20;i++) { printf("%12ld %12ld",f1,f2); if(i%2==0) printf("\n");/*控制输出,每行四个*/ f1=f1+f2; /*前两个月加起来赋值给第三个月*/ f2=f1+f2; /*前两个月加起来赋值给第三个月*/ }}==============================================================【程序12】题目:判断101-200之间有多少个素数,并输出所有素数。1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。 2.程序源代码:#include "math.h"main(){ int m,i,k,h=0,leap=1; printf("\n"); for(m=101;m<=200;m++) { k=sqrt(m+1); for(i=2;i<=k;i++) if(m%i==0) {leap=0;break;} if(leap) {printf("%-4d",m);h++; if(h%10==0) printf("\n"); } leap=1; } printf("\nThe total is %d",h);}==============================================================【程序13】题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数 本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。2.程序源代码:main(){int i......
