今日学习打卡

顺序结构

顺序结构是C语言中最基础的执行方式,代码按照从上到下的顺序逐行执行。每条语句执行完毕后,自动进入下一条语句,没有跳转或分支。例如:

int a = 1;  
int b = 2;  
int sum = a + b;  // 执行顺序:赋值a → 赋值b → 计算sum

分支结构

分支结构通过条件判断决定代码的执行路径,常见的实现方式包括:

  • if语句
    根据条件选择是否执行某段代码:

    if (a > b) {  
        printf("a大于b");  
    } else {  
        printf("a不大于b");  
    }  
    

  • switch语句
    多分支选择结构,适用于离散值匹配:

    switch (value) {  
        case 1: printf("值为1"); break;  
        case 2: printf("值为2"); break;  
        default: printf("其他值");  
    }  
    

分支结构的关键在于条件表达式(如a > bvalue的值),程序会根据条件结果跳转到对应的代码块执行。

作业:

  1. 终端输入一个三位数,判断是否是水仙花数
    水仙花数:个位的三次方+十位的三次方+百位的三次方==数本身
    1. #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>
      #include <math.h>
      int main(int argc, const char *argv[])
      {
      	int num;
      	int num_g,num_s,num_b;
      	printf("请输入一个三位数:\n");
      	scanf("%d",&num);
      	num_g=num%10;
      	num_s=num/10%10;
      	num_b=num/100;
      	if(num_g*num_g*num_g+num_s*num_s*num_s+num_b*num_b*num_b==num)
      	{
      		printf("%d是水仙花数!\n",num);
      	}
      	else
      	{
      		printf("%d不是水仙花数!\n",num);
      	}
      	return 0;
      }
      

      运行结果:

  2. 终端输入年月日,判断是这一年的第几天,考虑闰平年
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    int main(int argc, const char *argv[])
    {
    	int day,month,year;
    input:
    	printf("请输入年月日:\n");
    	scanf(" %d %d %d",&year,&month,&day);
    	if(year<0)
    	{
    		printf("输入有误!!!请重新输入:\n");
    		goto input;
    	}
    	if(month<1||month>12)
    	{
    		printf("输入有误!!!请重新输入:\n");
    		goto input;
    	}
    	if(month==12||month==10||month==8||month==7||month==5||month==3||month==1)
    	{
    		if(day<1||day>31)
    		{
    			printf("输入有误!!!请重新输入:\n");
    			goto input;
    		}
    	}
    	else if(month==11||month==9||month==6||month==4)
    	{
    		if(day<1||day>30)
    		{
    			printf("输入有误!!!请重新输入:\n");
    			goto input;
    		}
    	}
    	else if(month==2)
    	{
    		if(year%4==0||year%400==0&&year%100!=0)
    		{
    			if(day<1||day>29)
    			{
    				printf("输入有误!!!请重新输入:\n");
    				goto input;
    			}
    		}
    		else
    		{
    			if(day<1||day>28)
    			{
    				printf("输入有误!!!请重新输入:\n");
    				goto input;
    			}
    		}
    	}
    	else
    	{
    		printf("输入有误!!!请重新输入:\n");
    		goto input;
    	}
    	switch(month)
    	{
    	case 12:day=day+30;
    	case 11:day=day+31;
    	case 10:day=day+30;
    	case 9:day=day+31;
    	case 8:day=day+31;
    	case 7:day=day+30;
    	case 6:day=day+31;
    	case 5:day=day+30;
    	case 4:day=day+31;
    	case 3:{
    			   if(year%4==0||year%400==0&&year%100!=0)
    				   day=day+29;
    			   else
    				   day=day+28;
    		   }
    	case 2:day=day+31;
    	case 1:printf("今天是%d的第%d天!\n",year,day);
    	}
    	return 0;
    }

    运行结果:

  3. 终端输入年月,判断这个月有多少天,考虑闰平年
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    int main(int argc, const char *argv[])
    {
    	int day,month,year;
    	input:
    	printf("请输入年月:\n");
    	scanf(" %d %d",&year,&month);
    	if(year<1)
    	{
    		printf("输入有误!!!请重新输入:\n");
    		goto input;
    	}
    	if(month<1||month>12)
    	{
    		printf("输入有误!!!请重新输入:\n");
    		goto input;
    	}
    	switch(month)
    	{   //小月月份
    		case 11:
    		case 9:
    		case 6:
    		case 4:day=30;printf("%d年的%d月有%d天\n",year,month,day);break;
    
    		//二月单独判断
    		case 2:{
    				if(year%4==0||year%400==0&&year%100!=0)
    				{
    					day=29;printf("%d年的%d月有%d天\n",year,month,day);break;
    				}
    				else
    				{	
    					day=28;printf("%d年的%d月有%d天\n",year,month,day);break;
    				}
    			   }
    
    		//大月月份
    		case 12:
    		case 10:
    		case 8:
    		case 7:
    		case 5:
    		case 3:
    		case 1:day=31;printf("%d年的%d月有%d天\n",year,month,day);break;
    	}
    	return 0;
    }
    
    
    

    运行结果:

Logo

openvela 操作系统专为 AIoT 领域量身定制,以轻量化、标准兼容、安全性和高度可扩展性为核心特点。openvela 以其卓越的技术优势,已成为众多物联网设备和 AI 硬件的技术首选,涵盖了智能手表、运动手环、智能音箱、耳机、智能家居设备以及机器人等多个领域。

更多推荐