数据类型
运算符
表达式
数组
关键字
语句
选择与循环
函数
预处理命令
指针
结构体与共用体
位运算
文件
[ Last edited by plp626 on 2008-5-15 at 08:34 AM ]
运算符
表达式
数组
关键字
语句
选择与循环
函数
预处理命令
指针
结构体与共用体
位运算
文件
[ Last edited by plp626 on 2008-5-15 at 08:34 AM ]
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!
main()
{
int i,j,k;
printf("\n");
for(i=1;i<5;i++) /*以下为三重循环*/
for(j=1;j<5;j++)
for (k=1;k<5;k++)
{
if (i!=k&&i!=j&&j!=k) /*确保i、j、k三位互不相同*/
printf("%d,%d,%d\n",i,j,k);
}
}
==============================================================
main()
{
long int i;
int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
scanf("%ld",&i);
bonus1=100000*0.1;bonus2=bonus1+100000*0.75;
bonus4=bonus2+200000*0.5;
bonus6=bonus4+200000*0.3;
bonus10=bonus6+400000*0.15;
if(i<=100000)
bonus=i*0.1;
else if(i<=200000)
bonus=bonus1+(i-100000)*0.075;
else if(i<=400000)
bonus=bonus2+(i-200000)*0.05;
else if(i<=600000)
bonus=bonus4+(i-400000)*0.03;
else if(i<=1000000)
bonus=bonus6+(i-600000)*0.015;
else
bonus=bonus10+(i-1000000)*0.01;
printf("bonus=%d",bonus);
}
==============================================================
#include "math.h"
main()
{
long int i,x,y,z;
for (i=1;i<100000;i++)
{ x=sqrt(i+100); /*x为加上100后开方后的结果*/
y=sqrt(i+268); /*y为再加上168后开方后的结果*/
if(x*x==i+100&&y*y==i+268)/*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/
printf("\n%ld\n",i);
}
}
==============================================================
main()
{
int day,month,year,sum,leap;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month)/*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+day; /*再加上某天的天数*/
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
printf("It is the %dth day.",sum);}
==============================================================
main()
{
int x,y,z,t;
scanf("%d%d%d",&x,&y,&z);
if (x>y)
{t=x;x=y;y=t;} /*交换x,y的值*/
if(x>z)
{t=z;z=x;x=t;}/*交换x,z的值*/
if(y>z)
{t=y;y=z;z=t;}/*交换z,y的值*/
printf("small to big: %d %d %d\n",x,y,z);
}
==============================================================
#include "stdio.h"
main()
{
printf("Hello C-world!\n");
printf(" ****\n");
printf(" *\n");
printf(" * \n");
printf(" ****\n");
}
==============================================================
#include "stdio.h"
main()
{
char a=176,b=219;
printf("%c%c%c%c%c\n",b,a,a,a,b);
printf("%c%c%c%c%c\n",a,b,a,b,a);
printf("%c%c%c%c%c\n",a,a,b,a,a);
printf("%c%c%c%c%c\n",a,b,a,b,a);
printf("%c%c%c%c%c\n",b,a,a,a,b);}
==============================================================
#include "stdio.h"
main()
{
int i,j,result;
printf("\n");
for (i=1;i<10;i++)
{ for(j=1;j<10;j++)
{
result=i*j;
printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/
}
printf("\n");/*每一行后换行*/
}
}
==============================================================
#include "stdio.h"
main()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
if((i+j)%2==0)
printf("%c%c",219,219);
else
printf(" ");
printf("\n");
}
}
==============================================================
#include "stdio.h"
main()
{
int i,j;
printf("\1\1\n");/*输出两个笑脸*/
for(i=1;i<11;i++)
{
for(j=1;j<=i;j++)
printf("%c%c",219,219);
printf("\n");
}
}
==============================================================
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; /*前两个月加起来赋值给第三个月*/
}
}
==============================================================
#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);
}
==============================================================
main()
{
int i,j,k,n;
printf("'water flower'number is:");
for(n=100;n<1000;n++)
{
i=n/100;/*分解出百位*/
j=n/10%10;/*分解出十位*/
k=n%10;/*分解出个位*/
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
{
printf("%-5d",n);
}
}
printf("\n");
}
==============================================================
/* zheng int is divided yinshu*/
main()
{
int n,i;
printf("\nplease input a number:\n");
scanf("%d",&n);
printf("%d=",n);
for(i=2;i<=n;i++)
{
while(n!=i)
{
if(n%i==0)
{ printf("%d*",i);
n=n/i;
}
else
break;
}
}
printf("%d",n);}
==============================================================
main()
{
int score;
char grade;
printf("please input a score\n");
scanf("%d",&score);
grade=score>=90?'A':(score>=60?'B':'C');
printf("%d belongs to %c",score,grade);
}
==============================================================
main()
{
int a,b,num1,num2,temp;
printf("please input two numbers:\n");
scanf("%d,%d",&num1,&num2);
if(num1< NUM2)
/ *交换两个数,使大数放在num1上*/ { temp=num1;
num1=num2;
num2=temp;
}
a=num1;b=num2;
while(b!=0)/*利用辗除法,直到b为0为止*/
{
temp=a%b;
a=b;
b=temp;
}
printf("gongyueshu:%d\n",a);
printf("gongbeishu:%d\n",num1*num2/a);
}
==============================================================
#include "stdio.h"
main()
{char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
}
==============================================================
main()
{
int a,n,count=1;
long int sn=0,tn=0;
printf("please input a and n\n");
scanf("%d,%d",&a,&n);
printf("a=%d,n=%d\n",a,n);
while(count<=n)
{
tn=tn+a;
sn=sn+tn;
a=a*10;
++count;
}
printf("a+aa+...=%ld\n",sn);
}
==============================================================
main()
{
static int k;
int i,j,n,s;
for(j=2;j<1000;j++)
{
n=-1;
s=j;
for(i=1;i<J;I++)
{
if((j%i)==0)
{ n++;
s=s-i;
k=i;
}
}
if(s==0)
{
printf("%d is a wanshu",j);
for(i=0;i<N;I++)
printf("%d,",k);
printf("%d\n",k);
}
}
}
==============================================================
main()
{
float sn=100.0,hn=sn/2;
int n;
for(n=2;n<=10;n++)
{
sn=sn+2*hn;/*第n次落地时共经过的米数*/
hn=hn/2; /*第n次反跳高度*/
}
printf("the total of road is %f\n",sn);
printf("the tenth is %f meter\n",hn);
}
==============================================================
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);
}
==============================================================
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);
}
}
}
}
==============================================================
main()
{
int i,j,k;
for(i=0;i<=3;i++)
{
for(j=0;j<=2-i;j++)
printf(" ");
for(k=0;k<=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i<=2;i++)
{
for(j=0;j<=i;j++)
printf(" ");
for(k=0;k<=4-2*i;k++)
printf("*");
printf("\n");
}
}
==============================================================
main()
{
int n,t,number=20;
float a=2,b=1,s=0;
for(n=1;n<=number;n++)
{
s=s+a/b;
t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜t的作用*/
}
printf("sum is %9.6f\n",s);
}
==============================================================
main()
{
float n,s=0,t=1;
for(n=1;n<=20;n++)
{
t*=n;
s+=t;
}
printf("1+2!+3!...+20!=%e\n",s);
}
==============================================================
#include "stdio.h"
main()
{
int i;
int fact();
for(i=0;i<5;i++)
printf("\40:%d!=%d\n",i,fact(i));
}
int fact(j)
int j;
{
int sum;
if(j==0)
sum=1;
else
sum=j*fact(j-1);
return sum;
}
==============================================================
#include "stdio.h"
main()
{
int i=5;
void palin(int n);
printf("\40:");
palin(i);
printf("\n");
}
void palin(n)
int n;
{
char next;
if(n<=1)
{
next=getchar();
printf("\n\0:");
putchar(next);
}
else
{
next=getchar();
palin(n-1);
putchar(next);
}
}
==============================================================
age(n)
int n;
{
int c;
if(n==1) c=10;
else c=age(n-1)+2;
return(c);
}
main()
{ printf("%d",age(5));
}
==============================================================
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出万位*/
b=x%10000/1000;/*分解出千位*/
c=x%1000/100;/*分解出百位*/
d=x%100/10;/*分解出十位*/
e=x%10;/*分解出个位*/
if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
else if (e!=0) printf(" there are 1,%ld\n",e);
}
==============================================================
main( )
{
long ge,shi,qian,wan,x;
scanf("%ld",&x);
wan=x/10000;
qian=x%10000/1000;
shi=x%100/10;
ge=x%10;
if (ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/
printf("this number is a huiwen\n");
else
printf("this number is not a huiwen\n");
}
==============================================================
#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");
}
}
}
==============================================================
#include <conio.h>
void main(void)
{
int color;
for (color = 0; color < 8; color++)
{
textbackground(color);/*设置文本的背景颜色*/
cprintf("This is color %d\r\n", color);
cprintf("Press any key to continue\r\n");
getch();/*输入字符看不见*/
}
}
==============================================================
#include <conio.h>
void main(void)
{
clrscr();/*清屏函数*/
textbackground(2);
gotoxy(1, 5);/*定位函数*/
cprintf("Output at row 5 column 1\n");
textbackground(3);
gotoxy(20, 10);
cprintf("Output at row 10 column 20\n");
}
==============================================================
#include <stdio.h>
void hello_world(void)
{
printf("Hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter <= 3; counter++)
hello_world();/*调用此函数*/
}
void main(void)
{
three_hellos();/*调用此函数*/
}
==============================================================
#include <conio.h>
void main(void)
{
int color;
for (color = 1; color < 16; color++)
{
textcolor(color);/*设置文本颜色*/
cprintf("This is color %d\r\n", color);
}
textcolor(128 + 15);
cprintf("This is blinking\r\n");
}
==============================================================
#include <stdio.h>
#include "math.h"
#define N 101
main()
{
int i,j,line,a;
for(i=2;i<N;i++) a=i;
for(i=2;i<sqrt(N);i++)
for(j=i+1;j<N;j++)
{
if(a!=0&&a!=0)
if(a%a==0)
a=0;}
printf("\n");
for(i=2,line=0;i<N;i++)
{
if(a!=0)
{printf("%5d",a);
line++;}
if(line==10)
{printf("\n");
line=0;}
}
}
==============================================================
#define N 10
main()
{int i,j,min,tem,a;
/*input data*/
printf("please input ten num:\n");
for(i=0;i<N;i++)
{
printf("a=",i);
scanf("%d",&a);}
printf("\n");
for(i=0;i<N;i++)
printf("%5d",a);
printf("\n");
/*sort ten num*/
for(i=0;i<N-1;i++)
{min=i;
for(j=i+1;j<N;j++)
if(a>a) min=j;
tem=a;
a=a;
a=tem;
}
/*output data*/
printf("After sorted \n");
for(i=0;i<N;i++)
printf("%5d",a);
}
main()
{
float a,sum=0;
int i,j;
printf("please input rectangle element:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&a);
for(i=0;i<3;i++)
sum=sum+a;
printf("duijiaoxian he is %6.2f",sum);
}
main()
{
int a={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;
printf("original array is:\n");
for(i=0;i<10;i++)
printf("%5d",a);
printf("\n");
printf("insert a new number:");
scanf("%d",&number);
end=a;
if(number>end)
a=number;
else
{for(i=0;i<10;i++)
{ if(a>number)
{temp1=a;
a=number;
for(j=i+1;j<11;j++)
{temp2=a;
a=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;i<11;i++)
printf("%6d",a);
}
==============================================================
#define N 5
main()
{ int a={9,6,5,4,1},i,temp;
printf("\n original array:\n");
for(i=0;i<N;i++)
printf("%4d",a);
for(i=0;i<N/2;i++)
{temp=a;
a=a;
a=temp;
}
printf("\n sorted array:\n");
for(i=0;i<N;i++)
printf("%4d",a);
}
==============================================================
#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();
}
==============================================================
#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++;
}
}
}
==============================================================
#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++;
}
}
}
==============================================================
#include "stdio.h"
int a,b,c;
void add()
{ int a;
a=3;
c=a+b;
}
void main()
{ a=b=4;
add();
printf("The value of c is equal to %d\n",c);
}
==============================================================
void main()
{
register int i;
int tmp=0;
for(i=1;i<=100;i++)
tmp+=i;
printf("The sum is %d\n",tmp);
}
==============================================================
#include "stdio.h"
#define TRUE 1
#define FALSE 0
#define SQ(x) (x)*(x)
void main()
{
int num;
int again=1;
printf("\40: Program will stop if input value less than 50.\n");
while(again)
{
printf("\40:Please input number==>");
scanf("%d",&num);
printf("\40:The square for this number is %d \n",SQ(num));
if(num>=50)
again=TRUE;
else
again=FALSE;
}
}
==============================================================
#include "stdio.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=%d\n",x,y);
exchange(x,y);
printf("x=%d; y=%d\n",x,y);
}
==============================================================
#define LAG >
#define SMA <
#define EQ ==
#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");
}
==============================================================
#include "stdio.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 %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
#undef MAX
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#define MIN
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
}
==============================================================
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");
}
==============================================================
#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);
}
==============================================================
#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);
}
==============================================================
#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);
}
==============================================================
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);
}
==============================================================
#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*/
#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;
}
}
==============================================================
#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;
}
}
==============================================================
#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);
}
==============================================================
# define PAI 3.1415926
# define B 0.809
# include "graphics.h"
#include "math.h"
main()
{
int i,j,k,x0,y0,x,y,driver,mode;
float a;
driver=CGA;mode=CGAC0;
initgraph(&driver,&mode,"");
setcolor(3);
setbkcolor(GREEN);
x0=150;y0=100;
circle(x0,y0,10);
circle(x0,y0,20);
circle(x0,y0,50);
for(i=0;i<16;i++)
{
a=(2*PAI/16)*i;
x=ceil(x0+48*cos(a));
y=ceil(y0+48*sin(a)*B);
setcolor(2); line(x0,y0,x,y);}
setcolor(3);circle(x0,y0,60);
/* Make 0 time normal size letters */
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
outtextxy(10,170,"press a key");
getch();
setfillstyle(HATCH_FILL,YELLOW);
floodfill(202,100,WHITE);
getch();
for(k=0;k<=500;k++)
{
setcolor(3);
for(i=0;i<=16;i++)
{
a=(2*PAI/16)*i+(2*PAI/180)*k;
x=ceil(x0+48*cos(a));
y=ceil(y0+48+sin(a)*B);
setcolor(2); line(x0,y0,x,y);
}
for(j=1;j<=50;j++)
{
a=(2*PAI/16)*i+(2*PAI/180)*k-1;
x=ceil(x0+48*cos(a));
y=ceil(y0+48*sin(a)*B);
line(x0,y0,x,y);
}
}
restorecrtmode();
}
==============================================================
#include "graphics.h"
#define LEFT 0
#define TOP 0
#define RIGHT 639
#define BOTTOM 479
#define LINES 400
#define MAXCOLOR 15
main()
{
int driver,mode,error;
int x1,y1;
int x2,y2;
int dx1,dy1,dx2,dy2,i=1;
int count=0;
int color=0;
driver=VGA;
mode=VGAHI;
initgraph(&driver,&mode,"");
x1=x2=y1=y2=10;
dx1=dy1=2;
dx2=dy2=3;
while(!kbhit())
{
line(x1,y1,x2,y2);
x1+=dx1;y1+=dy1;
x2+=dx2;y2+dy2;
if(x1<=LEFT||x1>=RIGHT)
dx1=-dx1;
if(y1<=TOP||y1>=BOTTOM)
dy1=-dy1;
if(x2<=LEFT||x2>=RIGHT)
dx2=-dx2;
if(y2<=TOP||y2>=BOTTOM)
dy2=-dy2;
if(++count>LINES)
{
setcolor(color);
color=(color>=MAXCOLOR)?0:++color;
}
}
closegraph();
}
==============================================================
main()
{int i,j;
int a;
printf("\n");
for(i=0;i<10;i++)
{a=1;
a=1;}
for(i=2;i<10;i++)
for(j=1;j<i;j++)
a=a+a;
for(i=0;i<10;i++)
{for(j=0;j<=i;j++)
printf("%5d",a);
printf("\n");
}
}
==============================================================
#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);
}
==============================================================
#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(&driver,&mode,"");
top=y-30;
bottom=y-30;
for(i=0;i<num;i++)
{
ellipse(250,250,0,360,top,bottom);
top-=5;
bottom+=5;
}
getch();
}
==============================================================
#include "stdio.h"
#include "graphics.h"
#include "conio.h"
main()
{
int driver=VGA,mode=VGAHI;
int i,num=15,top=50;
int left=20,right=50;
initgraph(&driver,&mode,"");
for(i=0;i<num;i++)
{
ellipse(250,250,0,360,right,left);
ellipse(250,250,0,360,20,top);
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
right+=5;
left+=5;
top+=10;
}
getch();
}
==============================================================
#include "graphics.h"
#include "math.h"
#include "dos.h"
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#include "stdarg.h"
#define MAXPTS 15
#define PI 3.1415926
struct PTS {
int x,y;
};
double AspectRatio=0.85;
void LineToDemo(void)
{
struct viewporttype vp;
struct PTS points;
int i, j, h, w, xcenter, ycenter;
int radius, angle, step;
double rads;
printf(" MoveTo / LineTo Demonstration" );
getviewsettings( &vp );
h = vp.bottom - vp.top;
w = vp.right - vp.left;
xcenter = w / 2; /* Determine the center of circle */
ycenter = h / 2;
radius = (h - 30) / (AspectRatio * 2);
step = 360 / MAXPTS; /* Determine # of increments */
angle = 0; /* Begin at zero degrees */
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
rads = (double)angle * PI / 180.0; /* Convert angle to radians */
points.x = xcenter + (int)( cos(rads) * radius );
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
angle += step; /* Move to next increment */
}
circle( xcenter, ycenter, radius ); /* Draw bounding circle */
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
moveto(points.x, points.y); /* Move to beginning of cord */
lineto(points.x, points.y); /* Draw the cord */
} } }
main()
{int driver,mode;
driver=CGA;mode=CGAC0;
initgraph(&driver,&mode,"");
setcolor(3);
setbkcolor(GREEN);
LineToDemo();}
==============================================================
/*pointer*/
main()
{
int n1,n2,n3;
int *pointer1,*pointer2,*pointer3;
printf("please input 3 number:n1,n2,n3:");
scanf("%d,%d,%d",&n1,&n2,&n3);
pointer1=&n1;
pointer2=&n2;
pointer3=&n3;
if(n1>n2) swap(pointer1,pointer2);
if(n1>n3) swap(pointer1,pointer3);
if(n2>n3) swap(pointer2,pointer3);
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
}
swap(p1,p2)
int *p1,*p2;
{int p;
p=*p1;*p1=*p2;*p2=p;
}
==============================================================
main()
{
int number;
input(number);
max_min(number);
output(number);
}
input(number)
int number;
{int i;
for(i=0;i<9;i++)
scanf("%d,",&number);
scanf("%d",&number);
}
max_min(array)
int array;
{int *max,*min,k,l;
int *p,*arr_end;
arr_end=array+10;
max=min=array;
for(p=array+1;p<arr_end;p++)
if(*p>*max) max=p;
else if(*p<*min) min=p;
k=*max;
l=*min;
*p=array;array=l;l=*p;
*p=array;array=k;k=*p;
return;
}
output(array)
int array;
{ int *p;
for(p=array;p<array+9;p++)
printf("%d,",*p);
printf("%d\n",array);
}
==============================================================
main()
{
int number,n,m,i;
printf("the total numbers is:");
scanf("%d",&n);
printf("back m:");
scanf("%d",&m);
for(i=0;i<n-1;i++)
scanf("%d,",&number);
scanf("%d",&number);
move(number,n,m);
for(i=0;i<n-1;i++)
printf("%d,",number);
printf("%d",number);
}
move(array,n,m)
int n,m,array;
{
int *p,array_end;
array_end=*(array+n-1);
for(p=array+n-1;p>array;p--)
*p=*(p-1);
*array=array_end;
m--;
if(m>0) move(array,n,m);
}
==============================================================
#define nmax 50
main()
{
int i,k,m,n,num,*p;
printf("please input the total of numbers:");
scanf("%d",&n);
p=num;
for(i=0;i<n;i++)
*(p+i)=i+1;
i=0;
k=0;
m=0;
while(m<n-1)
{
if(*(p+i)!=0) k++;
if(k==3)
{ *(p+i)=0;
k=0;
m++;
}
i++;
if(i==n) i=0;
}
while(*p==0) p++;
printf("%d is left\n",*p);
}
==============================================================
main()
{
int len;
char *str;
printf("please input a string:\n");
scanf("%s",str);
len=length(str);
printf("the string has %d characters.",len);
}
length(p)
char *p;
{
int n;
n=0;
while(*p!='\0')
{
n++;
p++;
}
return n;
}
==============================================================
#define N 5
struct student
{ char num;
char name;
int score;
} stu;
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.num);
printf("name: ");
scanf("%s",stu.name);
for(j=0;j<3;j++)
{ printf("score %d.",j+1);
scanf("%d",&stu.score);
}
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.num,stu.name);
for(j=0;j<3;j++)
printf("%-8d",stu.score);
printf("\n");
}
}
main()
{
input();
print();
}
==============================================================
/*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;
printf("please input 5 numbers==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
ptr->next=(link)malloc(sizeof(node));
if(i==4) ptr->next=NULL;
else ptr=ptr->next;
}
ptr=head;
while(ptr!=NULL)
{ printf("The value is ==>%d\n",ptr->data);
ptr=ptr->next;
}
}
==============================================================
/*reverse output 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,tail;
int num,i;
tail=(link)malloc(sizeof(node));
tail->next=NULL;
ptr=tail;
printf("\nplease input 5 data==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
head=(link)malloc(sizeof(node));
head->next=ptr;
ptr=head;
}
ptr=ptr->next;
while(ptr!=NULL)
{ printf("The value is ==>%d\n",ptr->data);
ptr=ptr->next;
}}
==============================================================
#include "stdlib.h"
#include "stdio.h"
struct list
{ int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
link delete_node(link pointer,link tmp)
{if (tmp==NULL) /*delete first node*/
return pointer->next;
else
{ if(tmp->next->next==NULL)/*delete last node*/
tmp->next=NULL;
else /*delete the other node*/
tmp->next=tmp->next->next;
return pointer;
}
}
void selection_sort(link pointer,int num)
{ link tmp,btmp;
int i,min;
for(i=0;i<num;i++)
{
tmp=pointer;
min=tmp->data;
btmp=NULL;
while(tmp->next)
{ if(min>tmp->next->data)
{min=tmp->next->data;
btmp=tmp;
}
tmp=tmp->next;
}
printf("\40: %d\n",min);
pointer=delete_node(pointer,btmp);
}
}
link create_list(int array,int num)
{ link tmp1,tmp2,pointer;
int i;
pointer=(link)malloc(sizeof(node));
pointer->data=array;
tmp1=pointer;
for(i=1;i<num;i++)
{ tmp2=(link)malloc(sizeof(node));
tmp2->next=NULL;
tmp2->data=array;
tmp1->next=tmp2;
tmp1=tmp1->next;
}
return pointer;
}
link concatenate(link pointer1,link pointer2)
{ link tmp;
tmp=pointer1;
while(tmp->next)
tmp=tmp->next;
tmp->next=pointer2;
return pointer1;
}
void main(void)
{ int arr1={3,12,8,9,11};
link ptr;
ptr=create_list(arr1,5);
selection_sort(ptr,5);
}
==============================================================
main()
{
int i,n;
for(i=1;i<5;i++)
{ n=0;
if(i!=1)
n=n+1;
if(i==3)
n=n+1;
if(i==4)
n=n+1;
if(i!=4)
n=n+1;
if(n==3)
printf("zhu hao shi de shi:%c",64+i);
}
}
==============================================================
main()
#include "stdio.h"
main()
{
float peven(),podd(),dcall();
float sum;
int n;
while (1)
{
scanf("%d",&n);
if(n>1)
break;
}
if(n%2==0)
{
printf("Even=");
sum=dcall(peven,n);
}
else
{
printf("Odd=");
sum=dcall(podd,n);
}
printf("%f",sum);
}
float peven(int n)
{
float s;
int i;
s=1;
for(i=2;i<=n;i+=2)
s+=1/(float)i;
return(s);
}
float podd(n)
int n;
{
float s;
int i;
s=0;
for(i=1;i<=n;i+=2)
s+=1/(float)i;
return(s);
}
float dcall(fp,n)
float (*fp)();
int n;
{
float s;
s=(*fp)(n);
return(s);
}
==============================================================
main()
{ char *s={"man","woman","girl","boy","sister"};
char **q;
int k;
for(k=0;k<5;k++)
{ ;/*这里填写什么语句*/
printf("%s\n",*q);
}
}
==============================================================
#define N 4
#include "stdio.h"
static struct man
{ char name;
int age;
} person={"li",18,"wang",19,"zhang",20,"sun",22};
main()
{struct man *q,*p;
int i,m=0;
p=person;
for (i=0;i<N;i++)
{if(m<p->age)
q=p++;
m=q->age;}
printf("%s,%d",(*q).name,(*q).age);
}
==============================================================
main()
{
char *str1,*str2,*str3;
char swap();
printf("please input three strings\n");
scanf("%s",str1);
scanf("%s",str2);
scanf("%s",str3);
if(strcmp(str1,str2)>0) swap(str1,str2);
if(strcmp(str1,str3)>0) swap(str1,str3);
if(strcmp(str2,str3)>0) swap(str2,str3);
printf("after being sorted\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}
char swap(p1,p2)
char *p1,*p2;
{
char *p;
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}
==============================================================
main()
{int i,m,j,k,count;
for(i=4;i<10000;i+=4)
{ count=0;
m=i;
for(k=0;k<5;k++)
{
j=i/4*5+1;
i=j;
if(j%4==0)
count++;
else
break;
}
i=m;
if(count==4)
{printf("%d\n",count);
break;}
}
}
==============================================================
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); }
}
==============================================================
main()
{ char *p,s;int n;
p=s;
gets(p);
n=0;
while(*(p)!='\0')
{n=n*8+*p-'0';
p++;}
printf("%d",n);
}
==============================================================
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;
else
s*=8;
sum+=s;}
printf("\nsum=%ld",sum);
}
==============================================================
#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(b%c==0) break;
if(c>sqrt(b))
d=a-b;
else
break;
for(c=2;c<=sqrt(d);c++)
if(d%c==0) break;
if(c>sqrt(d))
printf("%d=%d+%d\n",a,b,d);
}
}
==============================================================
main()
{ long int m9=9,sum=9;
int zi,n1=1,c9=1;
scanf("%d",&zi);
while(n1!=0)
{ if(!(sum%zi))
n1=0;
else
{m9=m9*10;
sum=sum+m9;
c9++;
}
}
printf("%ld,can be divided by %d \"9\"",sum,c9);
}
==============================================================
#include "stdio.h"
main()
{char a="acegikm";
char b="bdfhjlnpq";
char c,*p;
int i=0,j=0,k=0;
while(a!='\0'&&b!='\0')
{if (a<B)
{ c=a;i++;}
else
c=b;
k++;
}
c='\0';
if(a=='\0')
p=b+j;
else
p=a+i;
strcat(c,p);
puts(c);
}
==============================================================
#include "stdio.h"
struct student
{ int x;
char c;
} a;
main()
{a.x=3;
a.c='a';
f(a);
printf("%d,%c",a.x,a.c);
}
f(struct student b)
{
b.x=20;
b.c='y';
}
==============================================================
main()
{int i,a,n=1;
while(n<=7)
{ do {
scanf("%d",&a);
}while(a<1||a>50);
for(i=1;i<=a;i++)
printf("*");
printf("\n");
n++;}
getch();
}
==============================================================
main()
{int a,i,aa,t;
scanf("%d",&a);
aa=a%10;
aa=a%100/10;
aa=a%1000/100;
aa=a/1000;
for(i=0;i<=3;i++)
{aa+=5;
aa%=10;
}
for(i=0;i<=3/2;i++)
{t=aa;
aa=aa;
aa=t;
}
for(i=3;i>=0;i--)
printf("%d",aa);
}
==============================================================
#include "stdio.h"
#define M 5
main()
{int a={1,2,3,4,5};
int i,j,t;
i=0;j=M-1;
while(i<J)
{t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
i++;j--;
}
for(i=0;i<M;I++)
printf("%d",*(a+i));
}
==============================================================
#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*/
}
==============================================================
/*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));
}
==============================================================
/*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));
}
==============================================================
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
main()
{char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{if(guess>i)
{printf("please input a little smaller.\n");
scanf("%d",&guess);}
else
{printf("please input a little bigger.\n");
scanf("%d",&guess);}
}
end=clock();
b=time(NULL);
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congradulations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
if((c=getch())=='y')
goto loop;
}
==============================================================
/*money management system*/
#include "stdio.h"
#include "dos.h"
main()
{
FILE *fp;
struct date d;
float sum,chm=0.0;
int len,i,j=0;
int c;
char ch="",ch1="",chtime="",chshop,chmoney;
pp: clrscr();
sum=0.0;
gotoxy(1,1);printf("|---------------------------------------------------------------------------|");
gotoxy(1,2);printf("| money management system(C1.0) 2000.03 |");
gotoxy(1,3);printf("|---------------------------------------------------------------------------|");
gotoxy(1,4);printf("| -- money records -- | -- today cost list -- |");
gotoxy(1,5);printf("| ------------------------ |-------------------------------------|");
gotoxy(1,6);printf("| date: -------------- | |");
gotoxy(1,7);printf("| | | | |");
gotoxy(1,8);printf("| -------------- | |");
gotoxy(1,9);printf("| thgs: ------------------ | |");
gotoxy(1,10);printf("| | | | |");
gotoxy(1,11);printf("| ------------------ | |");
gotoxy(1,12);printf("| cost: ---------- | |");
gotoxy(1,13);printf("| | | | |");
gotoxy(1,14);printf("| ---------- | |");
gotoxy(1,15);printf("| | |");
gotoxy(1,16);printf("| | |");
gotoxy(1,17);printf("| | |");
gotoxy(1,18);printf("| | |");
gotoxy(1,19);printf("| | |");
gotoxy(1,20);printf("| | |");
gotoxy(1,21);printf("| | |");
gotoxy(1,22);printf("| | |");
gotoxy(1,23);printf("|---------------------------------------------------------------------------|");
i=0;
getdate(&d);
sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day);
for(;;)
{
gotoxy(3,24);printf(" Tab __browse cost list Esc __quit");
gotoxy(13,10);printf(" ");
gotoxy(13,13);printf(" ");
gotoxy(13,7);printf("%s",chtime);
j=18;
ch=getch();
if(ch==27)
break;
strcpy(chshop,"");
strcpy(chmoney,"");
if(ch==9)
{
mm:i=0;
fp=fopen("home.dat","r+");
gotoxy(3,24);printf(" ");
gotoxy(6,4);printf(" list records ");
gotoxy(1,5);printf("|-------------------------------------|");
gotoxy(41,4);printf(" ");
gotoxy(41,5);printf(" |");
while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&chm)!=EOF)
{ if(i==36)
{ getch();
i=0;}
if ((i%36)<17)
{ gotoxy(4,6+i);
printf(" ");
gotoxy(4,6+i);}
else
if((i%36)>16)
{ gotoxy(41,4+i-17);
printf(" ");
gotoxy(42,4+i-17);}
i++;
sum=sum+chm;
printf("%10s %-14s %6.1f\n",chtime,chshop,chm);}
gotoxy(1,23);printf("|---------------------------------------------------------------------------|");
gotoxy(1,24);printf("| |");
gotoxy(1,25);printf("|---------------------------------------------------------------------------|");
gotoxy(10,24);printf("total is %8.1f$",sum);
fclose(fp);
gotoxy(49,24);printf("press any key to.....");getch();goto pp;
}
else
{
while(ch!='\r')
{ if(j<10)
{ strncat(chtime,ch,1);
j++;}
if(ch==8)
{
len=strlen(chtime)-1;
if(j>15)
{ len=len+1; j=11;}
strcpy(ch1,"");
j=j-2;
strncat(ch1,chtime,len);
strcpy(chtime,"");
strncat(chtime,ch1,len-1);
gotoxy(13,7);printf(" ");}
gotoxy(13,7);printf("%s",chtime);ch=getch();
if(ch==9)
goto mm;
if(ch==27)
exit(1);
}
gotoxy(3,24);printf(" ");
gotoxy(13,10);
j=0;
ch=getch();
while(ch!='\r')
{ if (j<14)
{ strncat(chshop,ch,1);
j++;}
if(ch==8)
{ len=strlen(chshop)-1;
strcpy(ch1,"");
j=j-2;
strncat(ch1,chshop,len);
strcpy(chshop,"");
strncat(chshop,ch1,len-1);
gotoxy(13,10);printf(" ");}
gotoxy(13,10);printf("%s",chshop);ch=getch();}
gotoxy(13,13);
j=0;
ch=getch();
while(ch!='\r')
{ if (j<6)
{ strncat(chmoney,ch,1);
j++;}
if(ch==8)
{ len=strlen(chmoney)-1;
strcpy(ch1,"");
j=j-2;
strncat(ch1,chmoney,len);
strcpy(chmoney,"");
strncat(chmoney,ch1,len-1);
gotoxy(13,13);printf(" ");}
gotoxy(13,13);printf("%s",chmoney);ch=getch();}
if((strlen(chshop)==0)||(strlen(chmoney)==0))
continue;
if((fp=fopen("home.dat","a+"))!=NULL);
fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney);
fputc('\n',fp);
fclose(fp);
i++;
gotoxy(41,5+i);
printf("%10s %-14s %-6s",chtime,chshop,chmoney);
}}}
#include "string.h"
#include "stdio.h"
main()
{ char str1,str2,*p1,*p2;
int sum=0;
printf("please input two strings\n");
scanf("%s%s",str1,str2);
p1=str1;p2=str2;
while(*p1!='\0')
{
if(*p1==*p2)
{while(*p1==*p2&&*p2!='\0')
{p1++;
p2++;}
}
else
p1++;
if(*p2=='\0')
sum++;
p2=str2;
}
printf("%d",sum);
getch();}
#include "stdio.h"
main()
{ FILE *fp;
char ch,filename;
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{printf("cannot open file\n");
exit(0);}
ch=getchar();
ch=getchar();
while(ch!='#')
{fputc(ch,fp);putchar(ch);
ch=getchar();
}
fclose(fp);
}
==============================================================
#include "stdio.h"
main()
{FILE *fp;
char str,filename;
int i=0;
if((fp=fopen("test","w"))==NULL)
{ printf("cannot open the file\n");
exit(0);}
printf("please input a string:\n");
gets(str);
while(str!='!')
{ if(str>='a'&&str<='z')
str=str-32;
fputc(str,fp);
i++;}
fclose(fp);
fp=fopen("test","r");
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
}
==============================================================
#include "stdio.h"
main()
{ FILE *fp;
int i,j,n,ni;
char c,t,ch;
if((fp=fopen("A","r"))==NULL)
{printf("file A cannot be opened\n");
exit(0);}
printf("\n A contents are :\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{c=ch;
putchar(c);
}
fclose(fp);
ni=i;
if((fp=fopen("B","r"))==NULL)
{printf("file B cannot be opened\n");
exit(0);}
printf("\n B contents are :\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{c=ch;
putchar(c);
}
fclose(fp);
n=i;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(c>c)
{t=c;c=c;c=t;}
printf("\n C file is:\n");
fp=fopen("C","w");
for(i=0;i<n;i++)
{ putc(c,fp);
putchar(c);
}
fclose(fp);
}
==============================================================
#include "stdio.h"
struct student
{ char num;
char name;
int score;
float avr;
} stu;
main()
{int i,j,sum;
FILE *fp;
/*input*/
for(i=0;i<5;i++)
{ printf("\n please input No. %d score:\n",i);
printf("stuNo:");
scanf("%s",stu.num);
printf("name:");
scanf("%s",stu.name);
sum=0;
for(j=0;j<3;j++)
{ printf("score %d.",j+1);
scanf("%d",&stu.score);
sum+=stu.score;
}
stu.avr=sum/3.0;
}
fp=fopen("stud","w");
for(i=0;i<5;i++)
if(fwrite(&stu,sizeof(struct student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
==============================================================──────────────────────────────
File Edit Run Compile Project Options Debug Break/watch
┌────────────Edit──────────────┐
│ Line 1 Col 1 Insert Indent Tab File Unindent c:NONAME.C│
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│─────────Message─────────────── │
│ │
│ │
└────────────────────────────┘
F1-Help F5-Zoom F6-Switch F7-Trace F8-Step F9-Make F10-Menu
★ 1 主菜单
主菜单 在Turbo C 2.0主屏幕顶上一行, 显示下列内容:
File Edit Run Compile Project Options Debug Break/watch
除Edit外, 其它各项均有子菜单, 只要用Alt加上某项中第一个字母(即大写字
母), 就可进入该项的子菜单中。
一、File(文件)菜单
按Alt+F可进入File菜单, 该菜单包括以下内容:
.Load(加载)
装入一个文件, 可用类似DOS的通配符(如*.C)来进行列表选择。也可装入其它
扩展名的文件, 只要给出文件名(或只给路径)即可。该项的热键为F3, 即只要在主
菜单中按F3即可进入该项, 而不需要先进入File菜单再选此项。
.Pick(选择)
将最近装入编辑窗口的8个文件列成一个表让用户选择, 选择后将该程序装入
编辑区, 并将光标置在上次修改过的地方。其热健为Alt-F3。
.New(新文件)
说明文件是新的, 缺省文件名为NONAME.C, 存盘时可改名。
.Save(存盘)
将编辑区中的文件存盘, 若文件名是NONAME.C时, 将询问是否更改文件名, 其
热键为F2。
.Write to(存盘)
可由用户给出文件名将编辑区中的文件存盘, 若该文件已存在, 则询问要不要
覆盖。
.Directory(目录)
显示目录及目录中的文件, 并可由用户选择。
.Change dir(改变目录)
显示当前目录, 用户可以改变显示的目录。
.Os shell(暂时退出)
暂时退出Turbo C 2.0到DOS提示符下, 此时可以运行DOS 命令, 若想回到
Turbo C 2.0中, 只要在DOS状态下键入EXIT即可。
.Quit(退出)
退出Turbo C 2.0, 返回到DOS操作系统中, 其热键为Alt+X。
说明:
以上各项可用光标键移动色棒进行选择, 回车则执行。也可用每一项的第一个
大写字母直接选择。若要退到主菜单或从它的下一级菜单列表框退回均可用Esc键,
Turbo C 2.0所有菜单均采用这种方法进行操作, 以下不再说明。
二、Edit(编辑)菜单
按Alt+E可进入编辑菜单, 若再回车, 则光标出现在编辑窗口, 此时用户可以
进行文本编辑。
编辑方法基本与wordstar相同, 可用F1键获得有关编辑方法的帮助信息。
与编辑有关的功能键如下:
F1 获得Turbo C 2.0编辑命令的帮助信息
F5 扩大编辑窗口到整个屏幕
F6 在编辑窗口与信息窗口之间进行切换
F10 从编辑窗口转到主菜单
编辑命令简介:
PageUp 向前翻页
PageDn 向后翻页
Home 将光标移到所在行的开始
End 将光标移到所在行的结尾
Ctrl+Y 删除光标所在的一行
Ctrl+T 删除光标所在处的一个词
Ctrl+KB 设置块开始
Ctrl+KK 设置块结尾
Ctrl+KV 块移动
Ctrl+KC 块拷贝
Ctrl+KY 块删除
Ctrl+KR 读文件
Ctrl+KW 存文件
Ctrl+KP 块文件打印
Ctrl+F1 如果光标所在处为Turbo C 2.0库函数, 则获得有关该函数的帮助
信息
Ctrl+Q
查找Turbo C 2.0双界符的前匹配符
说明:
1. Turbo C 2.0的双界符包括以下几种符号:
花括符 {和}
尖括符 <和>
圆括符 (和)
方括符
注释符 /*和*/
双引号 "
单引号 '
2. Turbo C 2.0在编辑文件时还有一种功能, 就是能够自动缩进, 即光标定位
和上一个非空字符对齐。在编辑窗口中, Ctrl+OL为自动缩进开关的控制键。
三、Run(运行)菜单
按Alt+R可进入Run菜单, 该菜单有以下各项:
.Run(运行程序)
运行由Project/Project name项指定的文件名或当前编辑区的文件。如果对上
次编译后的源代码未做过修改, 则直接运行到下一个断点(没有断点则运行到结束)。
否则先进行编译、连接后才运行, 其热键为Ctrl+F9。
.Program reset(程序重启)
中止当前的调试, 释放分给程序的空间, 其热键为Ctrl+F2。
.Go to cursor(运行到光标处)
调试程序时使用, 选择该项可使程序运行到光标所在行。光标所在行必须为一
条可执行语句, 否则提示错误。其热键为F4。
.Trace into(跟踪进入)
在执行一条调用其它用户定义的子函数时, 若用Trace into项, 则执行长条将
跟踪到该子函数内部去执行, 其热键为F7。
.Step over(单步执行)
执行当前函数的下一条语句, 即使用户函数调用, 执行长条也不会跟踪进函数
内部, 其热键为F8。
.User screen(用户屏幕)
显示程序运行时在屏幕上显示的结果。其热键为Alt+F5。
四、Compile(编译)菜单
按Alt+C可进入Compile菜单, 该菜单有以下几个内容:
.Compile to OBJ(编译生成目标码)
将一个C源文件编译生成.OBJ目标文件, 同时显示生成的文件名。其热键为
Alt+F9。
.Make EXE file(生成执行文件)
此命令生成一个.EXE的文件, 并显示生成的.EXE文件名。其中.EXE文件名是下
面几项之一。
1. 由Project/Project name说明的项目文件名。
2. 若没有项目文件名, 则由Primary C file说明的源文件。
3. 若以上两项都没有文件名, 则为当前窗口的文件名。
.Link EXE file(连接生成执行文件)
把当前.OBJ文件及库文件连接在一起生成.EXE文件。
.Build all(建立所有文件)
重新编译项目里的所有文件, 并进行装配生成.EXE文件。该命令不作过时检查
(上面的几条命令要作过时检查, 即如果目前项目里源文件的日期和时间与目标文
件相同或更早, 则拒绝对源文件进行编译)。
.Primary C file(主C文件)
当在该项中指定了主文件后, 在以后的编译中, 如没有项目文件名则编译此项
中规定的主C文件, 如果编译中有错误, 则将此文件调入编辑窗口, 不管目前窗口
中是不是主C文件。
.Get info(获得有关当前路径、源文件名、源文件字节大小、编译中的错误数
目、可用空间等信息。
五、Project(项目)菜单
按Alt+P可进入Project菜单, 该菜单包括以下内容:
.Project name(项目名)
项目名具有.PRJ的扩展名, 其中包括将要编译、连接的文件名。例如有一个程
序由file1.c, file2.c, file3.c组成, 要将这3个文件编译装配成一个file.exe的
执行文件, 可以先建立一个file.prj的项目文件, 其内容如下:
file1.c
file2.c
file3.c
此时将file.prj放入Project name项中, 以后进行编译时将自动对项目文件中
规定的三个源文件分别进行编译。然后连接成file.exe文件。
如果其中有些文件已经编译成.OBJ文件, 而又没有修改过, 可直接写上.OBJ扩
展名。此时将不再编译而只进行连接。
例如: file1.obj
file2.c
file3.c
将不对file1.c进行编译, 而直接连接。
说明:
当项目文件中的每个文件无扩展名时, 均按源文件对待, 另外, 其中的文件也
可以是库文件, 但必须写上扩展名.LIB。
.Break make on(中止编译)
由用户选择是否在有Warining(警告)、Errors(错误)、Fatal Errors( 致命错
误)时或Link(连接)之前退出Make编译。
.Auto dependencies(自动依赖)
当开关置为on, 编译时将检查源文件与对应的.OBJ文件日期和时间, 否则不进
行检查。
.Clear project(清除项目文件)
清除Project/Project name中的项目文件名。
.Remove messages(删除信息)
把错误信息从信息窗口中清除掉。
六、Options(选择菜单)
按Alt+O可进入Options菜单, 该菜单对初学者来说要谨慎使用。
.Compiler(编译器)
本项选择又有许多子菜单, 可以让用户选择硬件配置、存储模型、调试技术、
代码优化、对话信息控制和宏定义。这些子菜单如下:
Model
共有Tiny, small, medium, compact, large, huge 六种不同模式可由同户选
择。
Define
打开一个宏定义框, 同户可输入宏定义。多重定义可同分号, 赋值可用等号。
Code generation
它又有许多任选项, 这些任选项告诉编译器产生什么样的目标代码。
Calling convention 可选择C或Pascal方式传递参数。
Instruction set 可选择8088/8086或80186/80286指令系列。
Floating point 可选择仿真浮点、数学协处理器浮点或无浮点运算。
Default char type 规定char的类型。
Alignonent 规定地址对准原则。
Merge duplicate strings 作优化用, 将重复的字符串合并在一起。
Standard stack frame 产生一个标准的栈结构。
Test stack overflow 产生一段程序运行时检测堆栈溢出的代码。
Line number 在.OBJ文件中放进行号以供调试时用。
OBJ debug information 在.OBJ文件中产生调试信息。
Optimization
Optimize for 选择是对程序小型化还是对程序速度进行优
化处理。
Use register variable 用来选择是否允许使用寄存器变量。
Register optimization 尽可能使用寄存器变量以减少过多的取数操
作。
Jump optimization 通过去除多余的跳转和调整循环与开关语句
的办法, 压缩代码。
Source
Indentifier length 说明标识符有效字符的个数, 默认为32个。
Nested comments 是否允许嵌套注释。
ANSI keywords only 是只允许ANSI关键字还是也允许Turbo C
2.0关键字
Error
Error stop after 多少个错误时停止编译, 默认为25个。
Warning stop after 多少个警告错误时停止编译, 默认为100个。
Display warning
Portability warning 移植性警告错误。
ANSI Violations 侵犯了ANSI关键字的警告错误。
Common error 常见的警告错误。
Less common error 少见的警告错误。
Names 用于改变段(segment)、 组( group) 和类
(class)的名字, 默认值为CODE,DATA,BSS。
.Linker(连接器)
本菜单设置有关连接的选择项, 它有以下内容:
Map file menu 选择是否产生.MAP文件。
Initialize segments 是否在连接时初始化没有初始化的段。
Devault libraries 是否在连接其它编译程序产生的目标文件时去寻
找其缺省库。
Graphics library 是否连接graphics库中的函数。
Warn duplicate symbols 当有重复符号时产生警告信息。
Stack warinig 是否让连接程序产生No stack的警告信息。
Case-sensitive link 是否区分大、小写字。
.Environment(环境)
本菜单规定是否对某些文件自动存盘及制表键和屏幕大小的设置
Message tracking
Current file 跟踪在编辑窗口中的文件错误。
All files 跟踪所有文件错误。
Off 不跟踪。
Keep message 编译前是否清除Message窗口中的信息。
Config auto save 选on时, 在Run, Shell或退出集成开发环境之前,
如果Turbo C 2.0的配置被改过, 则所做的改动
将存入配置文件中。选off时不存。
Edit auto save 是否在Run或Shell之前, 自动存储编辑的源文件。
Backup file 是否在源文件存盘时产生后备文件(.BAK文件)。
Tab size 设置制表键大小, 默认为8。
Zoomed windows 将现行活动窗口放大到整个屏幕, 其热键为F5。
Screen size 设置屏幕文本大小。
.Directories(路径)
规定编译、连接所需文件的路径, 有下列各项:
Include directories 包含文件的路径, 多个子目录用";"分开。
Library directories 库文件路径, 多个子目录用";"分开。
Output directoried 输出文件(.OBJ, .EXE, .MAP文件)的目录。
Turbo C directoried Turbo C 所在的目录。
Pick file name 定义加载的pick文件名, 如不定义则从current
pick file中取。
.Arguments(命令行参数)
允许用户使用命令行参数。
.Save options(存储配置)
保存所有选择的编译、连接、调试和项目到配置文件中, 缺省的配置文件为
TCCONFIG.TC。
.Retrive options
装入一个配置文件到TC中, TC将使用该文件的选择项。
七、Debug(调试)菜单
按Alt+D可选择Debug菜单, 该菜单主要用于查错, 它包括以下内容:
Evaluate
Expression 要计算结果的表达式。
Result 显示表达式的计算结果。
New value 赋给新值。
Call stack 该项不可接触。而在Turbo C debuger 时用于检
查堆栈情况。
Find function 在运行Turbo C debugger时用于显示规定的函数。
Refresh display 如果编辑窗口偶然被用户窗口重写了可用此恢复
编辑窗口的内容。
八、Break/watch(断点及监视表达式)
按Alt+B可进入Break/watch菜单, 该菜单有以下内容:
Add watch 向监视窗口插入一监视表达式。
Delete watch 从监视窗口中删除当前的监视表达式。
Edit watch 在监视窗口中编辑一个监视表达式。
Remove all watches 从监视窗口中删除所有的监视表达式。
Toggle breakpoint 对光标所在的行设置或清除断点。
Clear all breakpoints 清除所有断点。
View next breakpoint 将光标移动到下一个断点处。
★ 2 Turbo C 2.0的配置文件
所谓配置文件是包含Turbo C 2.0有关信息的文件, 其中存有编译、连接的选
择和路径等信息。
可以用下述方法建立Turbo C 2.0的配置:
1. 建立用户自命名的配置文件
可以从Options菜单中选择Options/Save options命令, 将当前集成开发环境
的所有配置存入一个由用户命名的配置文件中。下次启动TC时只要在DOS下键入:
tc/c<用户命名的配置文件名>
就会按这个配置文件中的内容作为Turbo C 2.0的选择。
2. 若设置Options/Environment/Config auto save 为on, 则退出集成开发环
境时, 当前的设置会自动存放到Turbo C 2.0配置文件TCCONFIG.TC中。Turbo C 在
启动时会自动寻找这个配置文件。
3. 用TCINST设置Turbo C的有关配置, 并将结果存入TC.EXE中。Turbo C 在启
动时, 若没有找到配置文件, 则取TC.EXE中的缺省值。