【C/C++学院】（1）分支结构/熊猫烧香/自我删除/switch/循环结构/break/contine/goto/递归

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

# 1.if-else分支结构

```void run(int n)
{
if (1 == n)
{
}
else if (2 == n)
{
system("shutdown -s -t 60");//60s后关机
}
else if (3 == n)
{
system("msconfig");//打开系统配置
}
else if (4 == n)
{
while (1)
{
malloc(1024 * 1024);//1M内存，不断吃直到卡死
}
}
else if (5 == n)
{
system("shutdown -a");//取消关机
}
else if (6 == n)
{
ShellExecuteA(0, "open", "http://www.baidu.com", 0, 0, 3);//打开网址
}
else if (7 == n)
{
system("echo 小伙你中木马了");
}
else
{
}
}
```

# 2.获取随机数

#include <time.h>//使用时间数据类型 time_t
time_t ts;//时间的变量
srand((unsigned int)time(&ts));//time是一个函数，获取时间保存结果ts中
int sNum = rand();//获取随机数

# 4.自我删除

selfdel.bat

del path\selfdel.bat

# 5.解一元二次方程

```//#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)//去掉安全检查
#include <stdio.h>
#include <stdlib.h>
void calc(int a, int b, int c)
{
if (a == 0)//bx+c=0
{
if (b == 0)//c=0
{
if (c == 0)
{
printf("x为任意值\n");
}
else
{
printf("无解\n");
}
}
else//x=-c/b
{
printf("x=%f\n",-1.0*c/b);
}
}
else//ax^2+bx+c=0
{
int B24AC = b*b - 4 * a*c;
printf("B24AC = %d\n", B24AC);
float B_2A = -1.0*b / 2 / a;
printf("B_2A = %f\n", B_2A);
if (B24AC == 0)
{
printf("x1=x2=%f\n", B_2A);
}
else if (B24AC > 0)
{
float TmpSqrt = sqrt(B24AC)/2.0/a;
printf("x1=%f,x2=%f\n", B_2A + TmpSqrt, B_2A - TmpSqrt);
}
else//B24AC < 0
{
float TmpSqrt = sqrt(-1*B24AC) / 2.0 / a;
printf("x1=%f+%fi,x2=%f-%fi\n", B_2A , TmpSqrt, B_2A, TmpSqrt);
}
}
}
int main()
{
int a = 0, b = 0, c = 0;
scanf("%d %d %d", &a, &b, &c);
printf("%dx^2+%dx+%d=0\n", a, b, c);
printf("解为:\n");
calc(a, b, c);
system("pause");
return 0;
}
```

int num;
char ch;
float fl;
double db;
switch(num)//成立
{

}
switch(ch)//成立
{

}
switch(fl)//错误
{

}

# 7.循环结构

cmd中敲入命令：
for /l %i in (1,1,5) do echo gogogo
for /? 查看帮助

## for,while, do-while,  1加到100；

```#include <stdio.h>
{
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sum += i;
printf("i=[%d], sum=[%d]\n", i, sum);
}
}
{
int n = 1;
int sum = 0;
while (n <= 100)
{
sum += n;
printf("n=[%d], sum=[%d]\n", n, sum);
n++;
}
}
{
int sum = 0;
int n = 1;
do
{
sum += n;
printf("n=[%d], sum=[%d]\n", n, sum);
n++;
}while (n<=100);
}
void main()
{
system("pause");
}
```

## 2的n次方；

```#pragma warning(disable:4996)
#include <stdio.h>
void funfor()
{
int n = 0;
int iRes = 1;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
iRes = iRes * 2;
}
printf("2的%d次方=%d\n", n, iRes);
}
void funwhile()
{
int n = 0;
int iRes = 1;
scanf("%d", &n);
printf("2的%d次方=", n);
while (n>0)
{
iRes = iRes * 2;
n--;
}
printf("%d\n", iRes);
}
void fundowhile()
{
int n = 0;
int iRes = 1;
scanf("%d", &n);
printf("2的%d次方=", n);
do
{
if (n != 0)
{
iRes = iRes * 2;
}
n--;
} while (n > 0);
printf("%d\n", iRes);
}
void main()
{
fundowhile();
system("pause");
}
```

整数逆序输出；
```#pragma warning(disable:4996)
#include <stdio.h>
void main()
{
int ShuZi = 0;
scanf("%d", &ShuZi);
printf("----------------------\n");
int WeiShu = 0;
while (ShuZi)
{
printf("%d", ShuZi % 10);
ShuZi = ShuZi / 10;
}
system("pause");
}
```

# 8. break

```//316 = 11 * x + 15 * y;
#include <stdio.h>
void main()
{
int x = 0;
for (x = 0;; x++)
{
if( (316 - 11 * x )% 15 == 0)
{
printf("%d,%d\n", x, (316 - 11 * x)/15);
break;
}
}
system("pause");
}
```

# 9.continue

```#include <stdio.h>
void main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0)
{
continue;
}
printf("%d\n", i);
}
system("pause");
}
```

# 10.goto

1加到100
```#include <stdio.h>
void main()
{
int i = 1;
int sum = 0;
loop:
if (i<=100)
{
sum += i;
printf("%d,%d\n", i, sum);
i++;
}
goto loop;
system("pause");
}
```

# 11.递归

```//10进制转2进制
#define _CRT_SECURE_NO_WARNINGS
//10进制转2进制
#include <stdio.h>
#include <stdlib.h>
void change2(int n)
{
if (n == 1)//终止循环条件
{
printf("%d", n);
}
else
{
change2(n / 2);
printf("%d", n%2);
}
}
void main()
{
int num = 0;