简单易懂的C语言源代码供初学者使用.doc
《简单易懂的C语言源代码供初学者使用.doc》由会员分享,可在线阅读,更多相关《简单易懂的C语言源代码供初学者使用.doc(13页珍藏版)》请在冰点文库上搜索。
#include
main()
{floatpi=3.14159265,r;
printf("enterradius:
\n");
scanf("%f",&r);
printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r);
system("pause");
}
#include
main()
{intm,n,x,y;
printf("inputm,n:
\n");
scanf("%d%d",&m,&n);
if(n%2==1)
{printf("error!
!
nbushijishu!
\n",n);
return;}
/*n在这里不能是奇数*/
x=(4*m-n)/2;
y=(n-2*m)/2;
if((x>=0)&&(y>=0))
printf("x=%d,y=%d\n",x,y);
else
printf("shurucuowu!
\n");
getch();
}
#include
#include
#include
main()
{floata,b,C;
printf("enter3number(a,b,C):
\n");
scanf("%.2f%.2f%.2f",&a,&b,&C);
s=0.5*a*b*sinC;
printf("s=%.2f\n",s);
system("pause");
}
#include
main()
{intds,nl,yf;
charc;
printf("shuruds;\n");
scanf("%d",&ds);
if(ds<14||ds>1184)
{printf("inputerror!
pressanyend...\n");
scanf("%c",&c);
exit(0);
}
/*输入的数字必须是-14~1184之间*/
nl=(ds+115)%100;
yf=(ds+115)/100;
printf("nl=%d,yf=%d\n",nl,yf);
system("pause");}
#include
#include
main()
{chars1[100],s2[100],s3[100],s4[100];
printf("inputastring:
\n");
gets(s1);
strcpy(s1,s2);
strcat(s1,"--------------");
strcpy(s3,strcat(s1,"--------------"));
strcat(strcpy(s3,strcat(s1,"--------------")),s2);
strcpy(s4,strcat(strcpy(s3,strcat(s1,"--------------")),s2));
puts(s4);
system("pasue");
getch();
}
#include
#include
main()
{floatx1,x2,a,b,c;
printf("input3number(a,b,c):
\n");
scanf("%f%f%f",&a,&b,&c);
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("x1=%f,X2=%f\n",x1,x2);
system("pause");
getch();
}
#include
main()
{inta,b,c,t;
printf("input3number:
\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{t=a;a=b,b=t;}
if(a>c)
{t=a;a=c;c=t;}
if(b>c)
{t=b;b=c;c=t;}
printf("1:
%d,2:
%d,3:
%d\n",a,b,c);
system("pause");
}
#include
#include
#include
main()
{floatpi=3.14159265,r;
textbackground(YELLOW);/*设置背景色为黄色,注意颜色应该大写,可更改*/
textcolor(RED);/*设置文件颜色为红色,可更改*/
clrscr();/*清屏,使设置生效*/
printf("enterradius:
");
scanf("%f",&r);
if(r<0)
printf("EnterError!
\n");
else
printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r);
system("pause");/*暂停,按任一键继续*/
#include
#include
#include
main()
{floata,b,c,delt,x1,x2,p,q;
textcolor(YELLOW);
clrscr();
printf("Inputabc:
\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{printf("It'snotaquadraticequation!
\n");
system("pause");
return;}
delt=b*b-4*a*c;
if(delt>=0)
{x1=(-b+sqrt(delt))/(2*a);
x2=(-b-sqrt(delt))/(2*a);
printf("x1=%.3fx2=%.4f\n",x1,x2);}
else
{p=-b/(2*a);q=sqrt(-delt)/(2*a);
printf("p=%.4fq=%.4f\n",p,q);
}
system("pause");
getch();}
}
#include
main()
{floatpi=3.14159265,r;
intk=0;
while(k<=3){
printf("enterradius:
\n");
scanf("%f",&r);
printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r);
printf("pressanykeytocontinue\npressesctoexit.");
k++;
}
}
#include
#include
#include
#include
main()
{inta,b,c,oper;
longlimit,i=0;
charchar1;
textcolor(GREEN);/*设置字体颜色为绿色*/
clrscr();/*清屏,是设置生效*/
while(i<=3)
{printf("qingxuanzejiahuojian(1or2,1:
+,2:
-\n");
scanf("%d",&oper);
printf("Entermax(<10000):
\n");
scanf("%ld",&limit);
srand((unsignedlong)time(0));
a=rand()*limit/RAND_MAX;
b=rand()*limit/RAND_MAX;
while((a
{a=rand()*limit/RAND_MAX;
b=rand()*limit/RAND_MAX;}
char1=(oper==2?
'-':
'+');
printf("%d%c%d\n",a,char1,b);
scanf("%d",&c);
if((oper==2&&a-b==c)||(oper!
=2&&a+b==c))
printf("OK!
Youareveryclever!
\n");
else
printf("Theresultisnotcorrect!
\n");
i++;}
getch();
}
#include
#include
main()
{inty,i=0;
textcolor(YELLOW);
/*天下事无难易之分只有做与不做之别*/
textbackground(GREEN);
clrscr();/*清屏,是设置生效*/
while(i<=3)
{printf("Inputyear:
\n");
scanf("%d",&y);
if(y%4==0)
{if(y%100==0)
{if(y%400==0)
printf("yshirunnian!
\n");
else
printf("ybushirunnian!
\n");
}
else
printf("yshirunnian!
\n");
}
else
printf("ybushirunnian!
");
i++;
}
getche();
}
#include
#include
#include
main()
{
intxz,wday1,hour1;
structtm*timeptr;
time_tsecsnow;
chars1[30],s2[30],s3[30];
printf("inputwhomdoyousayto?
:
\n");
scanf("%d",&xz);
if(xz==1)
strcpy(s1,"mother");
elseif(xz==2)
strcpy(s1,"father");
else
strcpy(s1,"");
time(&secsnow);
timeptr=localtime(&secsnow);
wday1=timeptr->tm_wday;
if(wday1==6)
strcpy(s2,"Happysaturday!
");
elseif(wday1==0)
strcpy(s2,"Happysunday");
else
strcpy(s2,"");
hour1=timeptr->tm_hour;
if(hour1>=4&&hour1<=10)
strcpy(s3,"Goodmorning!
");
elseif(hour1>=17&&hour1<=22)
strcpy(s3,"Goodafternoon!
");
else
strcpy(s3,"Goodevening!
");
printf("%s%s%s",s1,s2,s3);
getch();
}
#include
#include
main()
{intday,year,month,i=0;
textbackground(BROWN);
clrscr();
while(i<=3)
{
printf("Input2number:
\n");
scanf("%d%d",&year,&month);
switch(month)
{case1:
case3:
case5:
case7:
case8:
case10:
case12:
day=31;break;
case4:
case6:
case11:
day=30;
break;
case2:
day=28;
if((year%4==0&&year%100!
=0)||year%400==0)
day=29;break;
deflault:
printf("Invalidmonthinput!
\n");
return;
}
printf("Thereare%ddaysin%d.%d\n",day,year,month);
i++;
getch();
}
}