作业:
- 整理思维导图
- 打印九九乘法表
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, const char *argv[]) { int i = 0; int j = 0; #if 1 i++; j++; while(i < 10){ j = 1; while(j <= i){ printf("%d * %d = %-4d",i,j,i*j); j++; } printf("\n"); i++; } #endif #if 0 for(i = 1;i < 10;i++){ for(j = 1;j <= i;j++){ printf("%d * %d = %-4d",i,j,i*j); } printf("\n"); } #endif return 0; }
- 整理课上的代码
- 复习while的嵌套循环
目录
C语言的语法结构
- 顺序结构
- 分支结构
- 循环结构
一、if分支
【1】if单分支结构
if(表达式1)
{
表达式1成立执行语句块;
}
【2】if双分支
if(表达式1)
{
表达式1成立执行语句块;
}
else
{
表达式1不成立执行的代码块;
}
【3】if多分支
if(表达式1)
{
表达式1成立执行语句块;
}
else if(表达式2)
{
表达式1不成立,表达式2成立执行的代码块;
}
else
{
表达式1和2都不成立执行的代码块;
}
练习:
1、终端输入年份,判断闰年还是平年,并输出。
四年一闰,百年不闰,四百年再闰
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//定义变量接收年份
int year;
//终端输入年份
scanf("%d",&year);
if(year%4==0&&year%100!=0||year%400==0)
{
printf("%d是闰年\n",year);
}
else
{
printf("%d是平年\n",year);
}
return 0;
}
2、终端输入一个三位数,判断是否是水仙花数,(水仙花数=个位的三次方+十位的三次方+百位的三次方=这个数)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int num;
scanf("%d",&num);
//求num的个位、十位和百位
int a = num%10; //个位
int b = num%100/10; //十位
int c = num/100; //百位
if(a*a*a+b*b*b+c*c*c==num)
{
printf("%d是一个水仙花数\n",num);
}
else
printf("%d不是水仙花数\n",num);
return 0;
}
3、终端输入一个字符,判断字符是大写字母,小写字母还是数字字符?
如果是大写字母,转成小写字母;如果是小写字母,转成大写字母;如果是数字字符转成#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//定义了一个字符变量c
char c;
//终端输入c
scanf("%c",&c);
if(c>='a'&&c<='z')
{
printf("%c是一个小写字母\n",c);
c-=32; //把小写字母转换成大写字母
}
else if(c>='A'&&c<='Z')
{
printf("%c是一个大写字母\n",c);
c+=32; //大写字母转换成小写字母
}
else if(c>='0'&&c<='9')
{
printf("%c是一个数字字符\n",c);
c='#';
}
printf("%c\n",c);
return 0;
}
【4】嵌套if
if(表达式1)
{
if(表达式2)
{
表达式1成立并且表达式2也成立执行的代码块;
}
else
{
表达式1成立但是表达式2不成立执行的代码块;
}
}
【5】if语句的注意事项
- if后面需要(),()内是条件
- else后面如果想加条件,一定写else if
- 一个else必须对应一个if
- if和else后面只有一条语句{}可以不加
- if()后面不要加;
二、switch···case语句
【1】格式
switch(变量名)
{
case 常量表达式1:
如果变量的值和表达式1相等执行的语句块;
break;
case 常量表达式2:
如果变量的值和表达式2相等执行的语句块;
break;
····
default:
如果所有的case都不符合执行的语句块;
}
执行逻辑:
switch···case语句,判断变量可能出现的情况,并且做出不同的处理
switch后面()中的内容,是要判断的内容
case列出了变量可能出现的情况
【2】switch···case的注意事项
- switch只能判断,整形和char类型的数据
- case后面的结果是一个常量,判断的是变量可能出现的情况
- switch(),括号里不能定义变量
- break的作用,用于结束switch···case语句,如果一个case中没有break,会继续向下执行其他的case语句,这个现象叫做:case击穿
- case后面的常量表达式的值,不能重复出现
- default的执行逻辑是:只有所有的case都不满足时才去执行default,如果default放在了case之前,不会影响到程序的运行结果
- case的顺序也不会影响执行的结果
练习:
1、使用switch···case完成建议计算器功能,实现 +、-、*、/、%
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int num1,num2; //操作数
char oper; //运算符
scanf("%d%c%d",&num1,&oper,&num2);
switch(oper)
{
case '+':
printf("%d\n",num1+num2);
break;
case '-':
printf("%d\n",num1-num2);
break;
case '*':
printf("%d\n",num1*num2);
break;
case '/':
//因为除法要实现两个数整除,所以给其中一个操作数做强转
printf("%f\n",(float)num1/num2);
break;
case '%':
printf("%d\n",num1%num2);
}
return 0;
}
2、终端输入年月日,判断是这一年的第几天?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int year,mon,day;
scanf("%d%d%d",&year,&mon,&day);
switch(mon)
{
case 12:
day+=30;
case 11:
day+=31;
case 10:
day+=30;
case 9:
day+=31;
case 8:
day+=31;
case 7:
day+=30;
case 6:
day+=31;
case 5:
day+=30;
case 4:
day+=31;
case 3:
if(year%4==0&&year%100!=0||year%400==0)
{
day+=29;
}
else
{
day+=28;
}
case 2:
day+=31;
case 1:
printf("%d\n",day);
}
return 0;
}