C语言第四天
【1】if
if (表达式)
{语句块1}
else
{语句块2}
//else只跟上一条if有关
【2】
switch (表达式)
{
case 常量表达式1 : 语句块1;break;
case 常量表达式2 : 语句块2;break;
…
case 常量表达式n :语句块n;break;
default
:语句块n+1;
}
【3】goto
进行跳转的语句,需要定义标识符
【4】while
while(表达式)
{
循环体;
}
【5】for
for(expression1; expression2; expression3)
{
statements;
}
1--- expression1:赋值语句,对循环变量初始化。最先执行,且只执行一次
2--- expression2:控制循环条件,为真:执行循环体,为假:跳出for
3--- expression3:一般用于,改变循环变量。执行完循环体后再执行;
_F
__FE
___FED
____FEDC
_____FEDCB
______FEDCBA
大小写转换代码<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">
# include <stdio.h>
int main(int argc,constchar*argv[])
{
#if 0
int a =10;
if(a>0)
printf("a>0");
elseif(a==0)
printf("a=0");
else
printf("a<0");
putchar(10);
return0;
#endif
puts("please input word");
int a =0;
a= getchar();
if('A'<=a&&a<='Z')
{
putchar(a+32);
}
elseif( a>='a'&&a<='z')
{
putchar (a-32);
}
else
puts("error");
}
奇数偶数判断求平均值
#include<stdio.h>
int main(int argc,constchar*argv[])
{
int i =0, count_ji =0, count_ou =0, num_ji =0, num_ou =0;
while(1)
{
if(scanf("%d",&i)!=1)
{
puts("error");
return-1;
}
if(i ==0)
{
puts("end");
break;
}
if(i %2)
{
count_ji++;
num_ji += i;
}
else
{
count_ou++;
num_ou += i;
}
}
if(count_ji !=0)
{
num_ji /= count_ji;
}
if(count_ou !=0)
{
num_ou /= count_ou;
}
printf("count_ji = %d, count_ou = %d\n",count_ji, count_ou);
printf("平均值_ji = %d, 平均值_ou = %d\n",num_ji, num_ou);
return0;
}
输出分钟转小时
int a =3;
int i=0,j=0;
float sum_ji=0;
float sum_ou=0;
while(1)
{
if( scanf("%d",&a)!=1)
{
return-1;
}
else
if(a==0)
{
puts("end");
break;
}
if(a%2==0)
{
i++;
sum_ou+=a;
}
else
{
j++;
sum_ji+=a;
}
}
printf("%d %d\n",i,j);
if(i!=0)
{
printf("%f\n",sum_ou);
}
if(j!=0)
{
printf("%f\n",sum_ji);
}
#endif
int a;
int xiaoshi=0,fen=0;
while(1)
{
if(scanf("%d",&a)!=1)
{
printf("try again!");
getchar();
continue;
}
else
{
xiaoshi=a/60;
fen= a%60;
printf("%02d:%02d\n",xiaoshi,fen);
}
}
return0;
}
乘法解决
# include <stdio.h>
int main(int argc,constchar*argv[])
{
/* int i=0;
int sum =0;
for( i=0;i<=10;i++)
{
sum+=i;
}
printf("%d\n",sum);
*/
int a,b,i,j;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
printf("%-2d*%-2d=%-2d ",j,i,i*j);
}
putchar(10);
}
return0;
}
水仙花数 abc=a的三次+b的三次+c的三次
int i ;
int bai,shi,ge;
for(i=100;i<=999;i++)
{
ge = i%10;
shi=i/10%10;
bai=i/100;
// printf("%d\n%d\n%d\n",ge,shi,bai);
if(i==ge*ge*ge+shi*shi*shi+bai*bai*bai)
printf("%d\n",i);
}
输出这个图像
int i,j,h;
for(i=1;i<7;i++)
{
for(j=i;j>=1;j--)
{
printf("_");
}
for(h=0;h<i;h++)
{
printf("%c",'F'-h);
}
putchar(10);
}
return0;
}
判断100以内质数 。1既不是质数也不是。。。。
int i ,j;
for(i=2;i<100;i++)
{
for(j=2;j<=i;j++)
{
if(( i%j==0)&&(i!=j))
break;
if(i==j)
printf("%d\n",i);
}
}
计算某年的某天是第多少天。
# include <stdio.h>
int main(int argc,constchar*argv[])
{
int nian,yue,ri;
int flag =1;
int sum=0;
while(1)
{
puts("please input:");
if(scanf("%d %d %d",&nian,&yue,&ri)!=3)
{
puts("input error,try again:");
getchar();
getchar();
getchar();
continue;
}
if((nian<0||nian>=9999)||0>yue||yue>12)
{
puts("输入错误!请重新输入");
continue;
}
//判断输入到数字是否合理
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(0>ri||ri>31)
{
puts("日输入错误!请重新输入");
continue;
}
}
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri>30)
{
puts("日输入错误!请重新输入");
continue;
}
}
{
if((nian%4==0)&&(nian%100!=0)||nian%400==0)
{
flag=1;
if(yue==2)
if(ri<0||ri>29)
{
puts("日输入错误!请重新输入");
continue;
}
}
else
{
if(yue==2)
if(ri<0||ri>28)
{
puts("日输入错误!请重新输入");
continue;
}
flag=0;
}
}
switch(yue-1)
{
case11:sum+=30;
case10:sum+=31;
case9:sum+=30;
case8:sum+=31;
case7:sum+=31;
case6:sum+=30;
case5:sum+=31;
case4:sum+=30;
case3:sum+=31;
case2:{if(flag)sum+=29;else sum+=28;}
case1:sum+=31;
}
printf("%d\n",sum+ri);
sum=0;
}
return0;
}