• 分支与循环(2)


    1. do…while()循环

    do while 用的比较少,至少循环一次,因为有限制。

    1.1 do while 语句用法

    do  
    	循环语句;
    While(表达式);
    
    • 1
    • 2
    • 3

    //使用do while 循环打印1_10

    int main()
    
    {
    	int I = 1;
    	do
    	{
    		printf(“%d”,i);
    		i++;
    	}
    	while(i<=10)
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    do while 中do默认控制一条语句,超过一条语句要带上大括号。

    1.2 do while循环中的break和continue

    添加上break后的效果:

    #include 
    int main()
    
    {
    	int i = 1;
    	do
    	{
    		if (i == 5)
    			break;
    		printf("%d ",i);
    		i++;
    	} while (i <= 10);
    	return 0;
    }
    //-------------------
    //编译器运行结果为
    //1 2 3 4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    break换成continue

    #include 
    int main()
    
    {
    	int i = 1;
    	do
    	{
    		if (i == 5)
    			continue;
    		printf("%d ",i);
    		i++;
    	} while (i <= 10);
    	return 0;
    }
    //---------------
    //编译器运行结果为
    //1 2 3 4 死循环
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    原因:i++在continue的后面造成了死循环。

    continue跳过本次循环进入判断部分,进行下一次循环。

    想要输出1-10没有5

    int main()
    
    {
        int i = 0; 
        do
        {
            i++; 
        if (i == 5)
            continue;
        printf("%d ", i);
        } while (i < 10);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结局办法:将i++提前到continue前面。

    2. 练习

    2.1 计算n!

    计算10的阶乘

    10!=1 * 2 * 3 …*10

    代码:

    #include 
    int main()
    {
    	int n = 0;
    	scanf("%d", &n);
    	int i = 0;
    	int ret = 1;
    	for (i = 1; i <= n; i++)
    	{
    		ret = ret * i;
    	}
    	printf("%d\n", ret);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.2 求1!+2!+3!…+10!

    #include 
    int main()
    {
    	int n = 0;
    	scanf("%d", &n);
    	int i = 0;
    	int sum = 0;
    	for (n = 1; n <= 10; n++)
    	{
    		int ret = 1;
    
    		//计算n的阶乘
    
    		for (i = 1; i <= n; i++)
    		{
    			ret = ret * i;
    		}
    		sum = sum + ret;
    	}
    		printf("%d\n", sum);
    		return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    精良版

    #include 
    int main()
    {
    	int n = 0;
    	int ret = 1;
    	int sum = 0;
        for(n=1;n<=3;n++)
        {
    		ret = ret*n;
    		sum=sum+ret;
    	}
    	printf("%d",sum);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.3 在一个有序数组中查找具体的数字(二分法/折半查找)

    通过下标,多次折半查找

    // 1 2 3 4 5 6 7 8 9 10
    //查找7 怎么查找?
    //折半查找。通过下标,多次折半查找。
    int main()
    {
    	int arr[]={1,2,3,4,5,6,7,8,9,10};
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    left = 0 right = 9

    mid =(lef+right)/2

    arr[mid]与查找数字比较大小,多次进行折半查找。

    int main()
    {
    	int arr[]={1,2,3,4,5,6,7,8,9,10};
    	int left = 0;
    	int sz = sizeof(arr)/sizeof(arr[0]);
    	int right = sz;
    	int k = 7;  //也可以输入一个数字存储在k变量里面。
    	while(left <= right)
    	{
    	int mid = (left + right)/2;  //每次循环都要进行折半,所以在循环内部。
    	if(arr[mid] < k)
    	{
    		left = mid +1;
    	}
    	else if(arr[mid] > k)
    	{
    		right = mid - 1;
    	}
    	else
    	{
    		printf("下标是%d",mid);
    		break;
    	}
    	}
    	if(left>right)
    	{
    		printf("找不到");
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在求坐标的平均值时,可能存在问题。

    int 是整形 如果left right很大 大到快要超过整型最大值

    left+right超过整形最大值,会丢失部分

    int mid = left + (right-left)/2;
    
    • 1

    求折半坐标的另一种方法。

    2.4 编写代码,演示多个字符从两端移动,向中间汇聚

    welcome to bit!!!

    ############

    w##########!

    we########!!

    wel########!!!

    welcome to bit!!!

    #include 
    #include 
    
    int main()
    {
    	char arr1[]="welcome to bit!!!";
    	char arr2[]="#################";
    	int left = 0;
    	int right = strlen(arr1)-1;
    	
    	while(left<=right)
    	{
    		arr2[left] = arr1[left];
    		arr2[right] = arr1[right];
    		printf("%s\n",arr2);
    		
    		left++;
    		right--;
    	}
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    带有动画效果版本代码

    #include 
    #include 
    #include 
    #include 
    int main()
    {
    	char arr1[]="welcome to bit!!!";
    	char arr2[]="#################";
    	int left = 0;
    	int right = strlen(arr1)-1;
    	
    	while(left<=right)
    	{
    		arr2[left] = arr1[left];
    		arr2[right] = arr1[right];
    		printf("%s\n",arr2);
    		Sleep(1000);
    		system("cls");
    		left++;
    		right--;
    	}
    	printf("welcome to bit!!!");
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    windows系统下Sleep 休眠函数需要引用windows.h头文件

    Sleep(1000)中单位是毫秒,1000ms即1s。

    strlen 对应头文件string.h

    system执行系统命令,需要引用头文件stdlib.h

    cls在cmd中意味着清理屏幕

    5 编写代码实现,模拟用户登陆情景,并且只能登陆三次。(只允许输入三次密码,如果密码正确则提示登陆成功,如果三次均输入错误,则退出程序。)

    假设正确密码是bitbit

    #include 
    #include 
    int main()
    { 
    	int i = 0;
    	char password[20]={0};
    	for (i=0;i<3;i++)
    	{
    		 printf("请输入密码:>");
    		 scanf("%s",password);
    		 //判断密码的正确性
    		 if(strcmp(password,"bitbit")==0)
    		 {
    			printf("密码正确,登陆成功");
    			break;
    		 }
    		 else
    		 {
    			printf("密码错误\n");
    		 }
    	}
    	if(i==3)
    	{
    		printf("三次密码错误,退出程序\n");
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    数组名本身就是地址,scanf中不需要带&

    比较字符串是否相同用strcmp函数。若是相同,返回0.

    strcmp函数所需要的头文件是string.h

    不能用等号简单的对两个字符串进行比较是否相同,如果用等号比较,比的是他们的字符串的起始地址。

    C语言中有字符串,但是没有字符串类型。

    strcmp返回值有三种

    第一个字符串大于第二个字符串,返回>0

    第一个字符串小于第二个字符串,返回<0

    第一个字符串等于第二个字符串,返回=0

    比较两个字符串不是比较他们的长度

    比较对应的字符。

    例如:abcd与abq,ab对应相同,q对应的ASCII码值大于c,所以abq字符串大于abcd字符串。且不再继续往下比较。

    猜数字游戏实现

    //电脑随机生成一个1——100之间的数字
    //然后猜数字
    //如果猜小了,告诉你猜小了
    //如果猜大了,告诉你猜大了
    //如果猜对了,告诉你猜对了
    //游戏至少进去一 次,所以用do while循环
    #include 
    void menu()
    {
    	printf("*******************\n");
    	printf("******1.play*******\n");
    	printf("******0.exit*******\n");
    }
    int main()
    {
    	int input = 0;
    	do
    	{
    		
    		menu();
    		printf("请选择:>");
    		scanf("%d", &input);
    		switch (input)
    		{
    		case 1:
    			printf("猜数字\n");
    			break;
    		case 0:
    			printf("退出游戏\n");
    				break;
    		default:
    			printf("选择错误,请重新选择\n");
    			break;
    		}
    	} while (input);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    #include 
    #include 
    #include 
    void menu()
    {
    	printf("*******************\n");
    	printf("******1.play*******\n");
    	printf("******0.exit*******\n");
    }
    
    void game()
    {
    	//1生成随机数
    	
    	int ret = rand()%100+1;
    	
    	//2猜数字
    	while(1)
    	{
    	printf("请猜数字:>");
    	scanf("%d",&guess);
    	if(guess ret)
    	{
    		printf("大了");
    	}
    	else
    	{
    	printf("对了");
    	break;
    	}
    	}
    }
    int main()
    {
    	int input = 0;
    	srand((unsigned int)time(NULL));
    	do
    	{
    		
    		menu();
    		printf("请选择:>");
    		scanf("%d", &input);
    		switch (input)
    		{
    		case 1:
    			game();
    			break;
    		case 0:
    			printf("退出游戏\n");
    				break;
    		default:
    			printf("选择错误,请重新选择\n");
    			break;
    		}
    	} while (input);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    rand 函数可以生成随机数

    int rand (void)

    不需要参数,返回一个数字,返回一个0—RAND_MAX(32767)的数字

    rand函数的头文件是stdlib.h

    如果仅仅使用rand函数,每次运行出现的随机值是相同的。

    在使用rand函数之前应该调用srand函数,来设置我们随机数的生成起点

    void srand (unsigned int)

    srand括号中添加一个数字后,rand函数生成的随机值与与这个数字有关系,数字不变,随机值不变

    如果在srand括号中能有一个一直变化的值,那么随机数就产生了

    而这个变化的值可以是时间戳

    time函数可以帮我们生成一个当前时间的时间戳

    time 函数所需要的头文件time.h

    由于每次开始玩游戏只需要生成一个随机数就可以了

    所以把

    	srand((unsigned int)time(NULL));
    
    • 1

    放在主函数中

    debug改成release,并通过调试显示的路径就可以找到exe版本的文件

    3. goto语句

    C语言中提供了可以随意滥用的 goto语句和标记跳转的标号。

    从理论上 goto语句是没有必要的,实践中没有goto语句也可以很容易的写出代码。

    但是某些场合下goto语句还是用得着的,最常见的用法就是终止程序在某些深度嵌套的结构的处理过

    程。

    goto语句的用法

    int main()
    {
    flag:
    	printf("hehe\n");
    	goto flag;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    死循环打印hehe

    int main()
    {
    
    	printf("hehe\n");
    	goto flag;
    	printf("123\n");
    flag:
    	printf("haha\n");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    跳过打印123

    在正常情况下,不建议使用goto语句,但是

    例如:一次跳出两层或多层循环。

    多层循环这种情况使用break是达不到目的的。它只能从最内层循环退出到上一层的循环。

    goto语言真正适合的场景如下:

    for(...)
        for(...)
       {
            for(...)
           {
                if(disaster)
                    goto error;
           }
       }
        …
    error:
     if(disaster)
             // 处理错误情况
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    关机程序,只要程序运行起来,电脑在一分钟内关机,如果输入123,则取消关机。

    cmd中关机程序 shutdown -s -t 60 (60秒后关机)

    shutdown -a 取消关机

    #include 
    #include 
    #include 
    int main()
    {
    	system("shutdown -s -t 60");
    again:	
    	printf("你的电脑将在一分钟内关机,不想关机,输入:123");
    	
    	char input[20]={0};
    	scanf("%s",input);
    	if(strcmp(input,"123")==0)
    	{
    		system("shutdown -a");
    	}
    	else
    	{
    		goto again:
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    用循环代替goto语句

    #include 
    #include 
    #include 
    int main()
    {
    	system("shutdown -s -t 60");
    
    	while(1)
    	{
    		printf("你的电脑将在一分钟内关机,不想关机,输入:123");
    	
    	char input[20]={0};
    	scanf("%s",input);
    	if(strcmp(input,"123")==0)
    	{
    		system("shutdown -a");
    	}
    	}
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    debug是程序员的调试版本

    release是用户使用的发行版本

  • 相关阅读:
    (附源码)php二手服装网站 毕业设计 201711
    518. 零钱兑换II(完全背包问题)
    每天一个数据分析题(三百九十七)- 逻辑回归
    Python OpenCV 视频抽帧处理并保存
    记一次使用动态引入vue组件的经历
    GC FullGC
    【python】使用Selenium获取(2023博客之星)的参赛文章
    几句话理解Kubetnetes中的Service资源的几种类型
    一台电脑如何登录多个抖音小店(抖音小店怎么关联多个账号)
    始祖双碳新闻 | 2022年8月3日碳中和行业早知道
  • 原文地址:https://blog.csdn.net/m0_71545436/article/details/126274982