day11.1
谁能横刀立马,唯我飞牛大将军!
下面是day11所学到的知识:
涵盖的知识点:
goto语句
关机程序的两种实现
1.goto语句
(1)跳转语句
(2)在跳出深层嵌套的结构中比较有用
向上跳转
#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int main()
{
int flag = 1;
START:
printf("hello world!\n");
if (flag == 1)
{
goto START;
}
system("pause");
return 0;
}
向下跳转
#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int main()
{ int flag = 1;
if (flag == 1)
{
goto START;
}
printf("1\n");
printf("2\n");
printf("3\n");
START:
printf("4\n");
printf("5\n");
system("pause");
return 0;
}
2.关机程序
goto语句实现
#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int main()
{
system("shutdown -s -t 180");
char str[10];
START:
printf("你的电脑将在180s后关机!若输入正确关键字,则取消关机!\n");
printf("请输入:");
scanf("%s",str);
if (strcmp(str, "爱生活") == 0)
{
system("shutdown -a");
printf("关机取消!\n");
}
else{
goto START;
}
system("pause");
return 0;
}
循环实现
#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int main()
{
system("shutdown -s -t 180");
char str[10];
while (1)
{
printf("你的电脑将在180s后关机!若输入正确关键字,则取消关机!\n");
printf("请输入:");
scanf("%s", str);
if (strcmp(str, "爱生活") == 0)
{
system("shutdown -a");
printf("关机取消!\n");
break;
}
}
system("pause");
return 0;
}