我已经编写了一个用于在两个不同的switch语句之间切换的c代码,
但我正在寻找一个更通用的解决方案,这意味着不使用状态变量和while循环,或从一个switch语句跳转到另一个switch语句。
我想要一些建议或关键输入。
谢谢你,请原谅我可怜的英语。
#include <stdio.h>
int main()
{
unsigned char state,state1;
enum
{
Add,Sub,Mul,Div,START,STOP
}Arith_Op;
enum
{
Add1,Sub1,Mul1,Div1,START1,STOP1
}Arith_Op1;
Arith_Op = Add;
Arith_Op1 = Add1;
state = START;
while (state != STOP)
{
switch(Arith_Op)
{
case Add :
printf("Add\n");
Arith_Op = Sub;
break;
case Sub :
printf("sub\n");
Arith_Op = Mul;
break;
case Mul :
printf("mul\n");
Arith_Op = Div;
break;
case Div :
printf("div\n");
Arith_Op = STOP;
break;
default :
printf("default\n");
state = STOP;
state1 = START1;
break;
}
}
printf(" state out of while and Switch case ");
while (state1 != STOP1)
{
switch(Arith_Op1)
{
case Add1 :
printf("Add1\n");
Arith_Op1 = Sub1;
break;
case Sub1 :
printf("sub1\n");
Arith_Op1 = Mul1;
break;
case Mul1 :
printf("mul1\n");
Arith_Op1 = Div;
break;
case Div1 :
printf("div1\n");
Arith_Op1 = STOP1;
break;
default :
printf("default1\n");
state1 = STOP1;
break;
}
}
printf(" state 1 out of while and Switch case ");
return 0;
}