题:https://www.codewars.com/kata/killer-garage-door/train/ (梯子自理)
代码:
#include <stdio.h>
#include <string.h>
void DoorControl(char Control,int *DoorStatus,_Bool *Reverse_Flag,_Bool *Pushed);
int main(int argc, char** argv)
{
_Bool Pushed = 0;
_Bool Reverse_Flag = 0;
int DoorStatus = 0;
char Control[500];
scanf("%s",Control);
int str_length = strlen(Control);
for(int i=0;i<str_length;++i)
{
DoorControl(Control[i],&DoorStatus,&Reverse_Flag,&Pushed);
printf("%d",DoorStatus);
}
return (0);
}
void DoorControl(char Control,int *DoorStatus,_Bool *Reverse_Flag,_Bool *Pushed)
{
if(Control == 'O') *Reverse_Flag = !(*Reverse_Flag);
if(Control == 'P' && *DoorStatus == 0) *(Pushed) = 1;
if(Control == 'P' && *DoorStatus == 5) *Reverse_Flag = !(*Reverse_Flag);
if(*Reverse_Flag && *Pushed && *DoorStatus >0 && *DoorStatus <=5) --(*DoorStatus);
else if(*Pushed && *DoorStatus >=0 && *DoorStatus <5 && !*Reverse_Flag) ++(*DoorStatus);
}
因为目标是程序越短越好,所以写逻辑的时候绝望死了。。。(。