switch case语句的用法cp
时间: 2024-10-09 12:05:43 浏览: 25
`switch case`语句是一种控制结构,在编程中常用于基于某个变量值执行不同的代码块,尤其常见于需要判断多种可能情况的场景。它的基本语法如下:
```java
switch (expression) {
case value1:
// 当expression等于value1时,执行这里的代码
code_block1;
break;
case value2:
// 当expression等于value2时,执行这里的代码
code_block2;
break;
// 可以有多个case
default:
// 如果expression都不匹配前面的case,执行这里(可选)
code_block_default;
break; // 结束默认分支,防止后续case影响
}
```
- `expression`: 需要判断其值的表达式。
- `case value`: 指定一个可能的值,如果`expression`的值与之相等,则执行紧跟其后的代码块。
- `break`: 关键字,用来结束当前`case`内的代码并跳出`switch`结构,避免进入下一个未匹配的`case`。
相关问题
#define HC165_CE {PBout(0),PBout(8),PEout(4),PEout(8),PEout(12)} #define HC165_PL {PAout(11),PBout(6),PEout(2),PEout(6),PEout(10)} #define HC165_CP {PAout(12),PBout(7),PEout(3),PEout(11),PEout(11)} #define HC165_Q7 {PBin(1),PBin(9),PEin(5),PEin(9),PEin(13)} uint8_t readVoltage(int group) { uint8_t i = 0; uint8_t temp = 0; HC165_CE[group] = 0; HC165_PL[group] = 0; HC165_PL[group] = 1; if(HC165_Q7[group] == 1) temp |= 0x01; for(i = 0; i < 7; i ++) { temp = temp << 1; HC165_CP[group] = 0; delayUs(1); HC165_CP[group] = 1; if(HC165_Q7[group] == 1) temp |= 0x01; } return temp; } 请帮我改成纯C语言代码
以下是改成纯 C 语言代码后的代码:
```c
#define HC165_CE_0 GPIOB->ODR &= ~(1 << 0)
#define HC165_CE_1 GPIOB->ODR &= ~(1 << 8)
#define HC165_CE_2 GPIOE->ODR &= ~(1 << 4)
#define HC165_CE_3 GPIOE->ODR &= ~(1 << 8)
#define HC165_CE_4 GPIOE->ODR &= ~(1 << 12)
#define HC165_PL_0 GPIOA->ODR &= ~(1 << 11)
#define HC165_PL_1 GPIOB->ODR &= ~(1 << 6)
#define HC165_PL_2 GPIOE->ODR &= ~(1 << 2)
#define HC165_PL_3 GPIOE->ODR &= ~(1 << 6)
#define HC165_PL_4 GPIOE->ODR &= ~(1 << 10)
#define HC165_CP_0 GPIOA->ODR &= ~(1 << 12)
#define HC165_CP_1 GPIOB->ODR &= ~(1 << 7)
#define HC165_CP_2 GPIOE->ODR &= ~(1 << 3)
#define HC165_CP_3 GPIOE->ODR &= ~(1 << 11)
#define HC165_CP_4 GPIOE->ODR &= ~(1 << 15)
#define HC165_Q7_0 GPIOB->IDR & (1 << 1)
#define HC165_Q7_1 GPIOB->IDR & (1 << 9)
#define HC165_Q7_2 GPIOE->IDR & (1 << 5)
#define HC165_Q7_3 GPIOE->IDR & (1 << 9)
#define HC165_Q7_4 GPIOE->IDR & (1 << 13)
uint8_t readVoltage(int group)
{
uint8_t i = 0;
uint8_t temp = 0;
switch (group) {
case 0:
HC165_CE_0 = 0;
HC165_PL_0 = 0;
HC165_PL_0 = 1;
if (HC165_Q7_0 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_0 = 0;
delayUs(1);
HC165_CP_0 = 1;
if (HC165_Q7_0 == 1)
temp |= 0x01;
}
break;
case 1:
HC165_CE_1 = 0;
HC165_PL_1 = 0;
HC165_PL_1 = 1;
if (HC165_Q7_1 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_1 = 0;
delayUs(1);
HC165_CP_1 = 1;
if (HC165_Q7_1 == 1)
temp |= 0x01;
}
break;
case 2:
HC165_CE_2 = 0;
HC165_PL_2 = 0;
HC165_PL_2 = 1;
if (HC165_Q7_2 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_2 = 0;
delayUs(1);
HC165_CP_2 = 1;
if (HC165_Q7_2 == 1)
temp |= 0x01;
}
break;
case 3:
HC165_CE_3 = 0;
HC165_PL_3 = 0;
HC165_PL_3 = 1;
if (HC165_Q7_3 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_3 = 0;
delayUs(1);
HC165_CP_3 = 1;
if (HC165_Q7_3 == 1)
temp |= 0x01;
}
break;
case 4:
HC165_CE_4 = 0;
HC165_PL_4 = 0;
HC165_PL_4 = 1;
if (HC165_Q7_4 == 1)
temp |= 0x01;
for (i = 0; i < 7; i++) {
temp = temp << 1;
HC165_CP_4 = 0;
delayUs(1);
HC165_CP_4 = 1;
if (HC165_Q7_4 == 1)
temp |= 0x01;
}
break;
}
return temp;
}
```
注意,为了让代码更清晰易懂,我将宏定义改成了对应的 C 语言语句。同时,由于寄存器操作需要使用 STM32 的相关库函数,因此我将它们改成了对应的寄存器操作语句。
#include <stdio.h> #include <stdlib.h> typedef struct{ char name[5]; int need_time; int privilege; char state; }NODE; typedef struct node{ NODE data; struct node *link; }LNODE; void delay(int i) { int x,y; while(i--) { x=0 ; while(x < 10000) { y = 0; while(y < 40000) y++; x++ ; } } } void len_queue(LNODE **hpt, NODE x) { LNODE *q,*r,*p; q = *hpt; 8 r = *hpt; p = (LNODE *)malloc(sizeof(LNODE)); p->data = x; p->link = NULL; if(*hpt == NULL) *hpt = p; else { while(q!=NULL && (p->data).privilege < (q->data).privilege) { r = q; q = q->link; } if(q == NULL) r->link = p; else if(r == q) { p->link = *hpt; *hpt = p; }else { r->link = p; p->link = q; } } } void lde_queue(LNODE **hpt, NODE *cp) { LNODE *p = *hpt; *cp = (*hpt)->data; *hpt = (*hpt)->link; free(p); printf("the elected process's name : %s \n",cp->name); } void output(LNODE **hpt) { LNODE *p = *hpt; printf("Name \t Need_time \t privilege \t state\n"); do { 9 printf("%s \t %d \t\t %d \t\t %c \n", (p->data).name,(p->data).need_time,(p->data).privilege,(p->data).state); p = p->link; }while(p!= NULL); delay(4); } int main() { LNODE *head = NULL; NODE curr,temp; printf("The period time is 4s \n"); printf("please input \n"); printf("if need_time = 0,input over\n"); printf("Name\t Need_time\t privilege\n"); while(1) { scanf("%s %d %d", temp.name,&temp.need_time,&temp.privilege); if(temp.need_time == 0) break; temp.state = 'R'; len_queue(&head,temp); } while(head != NULL) { output(&head); lde_queue(&head,&curr); curr.need_time-- ; curr.privilege-- ; if(curr.need_time != 0) len_queue(&head,curr); } return 0; }
以下是将该段 C 代码转换为 Python 的结果:
```python
import time
class Node:
def __init__(self, name, need_time, privilege, state):
self.name = name
self.need_time = need_time
self.privilege = privilege
self.state = state
class LNode:
def __init__(self, data, link):
self.data = data
self.link = link
def delay(i):
while i > 0:
x = 0
while x < 10000:
y = 0
while y < 40000:
y += 1
x += 1
i -= 1
def len_queue(hpt, x):
p = LNode(x, None)
q = hpt
r = hpt
if hpt is None:
hpt = p
else:
while q is not None and p.data.privilege < q.data.privilege:
r = q
q = q.link
if q is None:
r.link = p
elif r == q:
p.link = hpt
hpt = p
else:
r.link = p
p.link = q
return hpt
def lde_queue(hpt):
if hpt is None:
return None, hpt
else:
cp = hpt.data
hpt = hpt.link
print(f"the elected process's name: {cp.name}")
return cp, hpt
def output(hpt):
p = hpt
print("Name\tNeed_time\tPrivilege\tState")
while p is not None:
print(f"{p.data.name}\t{p.data.need_time}\t\t{p.data.privilege}\t\t{p.data.state}")
p = p.link
delay(4)
def main():
head = None
curr = None
print("The period time is 4s")
print("Please input")
print("If need_time = 0, input over")
print("Name\tNeed_time\tPrivilege")
while True:
name, need_time, privilege = input().split()
need_time = int(need_time)
privilege = int(privilege)
if need_time == 0:
break
temp = Node(name, need_time, privilege, 'R')
head = len_queue(head, temp)
while head is not None:
output(head)
curr, head = lde_queue(head)
curr.need_time -= 1
curr.privilege -= 1
if curr.need_time != 0:
head = len_queue(head, curr)
if __name__ == "__main__":
main()
```
注意,由于 Python 的语法和 C 有所不同,因此代码中存在一些细微的差异,例如:
- 在 Python 中,结构体可以使用类来实现;
- 在 Python 中,链表节点可以使用类来实现;
- Python 中没有指针类型,因此需要使用类或者元组来返回多个值;
- Python 中没有 switch-case 语句,可以使用 if-elif-else 语句来代替;
- Python 中字符串可以使用单引号或者双引号来表示。
阅读全文