Finite state machine: Difference between revisions

Added C implementation.
(Added BASIC (Sinclair ZX81 dialect))
(Added C implementation.)
Line 68:
PRESS D(EPOSIT) OR Q(UIT)</pre>
 
=={{header|C}}==
This is an unapologetic implementation of goto. There have been a lot of curse words and obituaries written about it and the inventors of Java were glad to exclude it from the language, but to be fair, goto is one of the many things C inherited from languages such as Assembly or BASIC that make it truly awesome, especially when it comes to such requirements. After all, can there be a clearer and simpler implementation of a Finite State Machine (not counting BASIC ) ?
<lang C>
/*Abhishek Ghosh, 25th October 2017*/
 
#include<stdio.h>
 
int main()
{
char str[10];
ready: do{
printf("\nMachine is READY. (D)eposit or (Q)uit :");
scanf("%s",str);
}while(!(str[0]!='D'||str[0]!='d'||str[0]!='q'||str[0]!='Q'));
if(str[0]=='q'||str[0]=='Q')
goto quit;
goto waiting;
waiting: do{
printf("(S)elect product or choose to (R)efund :");
scanf("%s",str);
}while(!(str[0]!='s'||str[0]!='S'||str[0]!='r'||str[0]!='R'));
if(str[0]=='s'||str[0]=='S'){
printf("Dispensing product...");
goto dispense;
}
else{
printf("Please collect refund.");
goto ready;
}
dispense: do{
printf("\nPlease (C)ollect product. :");
scanf("%s",str);
}while(!(str[0]!='c'||str[0]!='C'));
goto ready;
quit: printf("Thank you, shutting down now.");
return 0;
}
</lang>
Machine simulation :
<pre>
C:\rosettaCode>fsm.exe
 
Machine is READY. (D)eposit or (Q)uit :D
(S)elect product or choose to (R)efund :S
Dispensing product...
Please (C)ollect product. :C
 
Machine is READY. (D)eposit or (Q)uit :D
(S)elect product or choose to (R)efund :R
Please collect refund.
Machine is READY. (D)eposit or (Q)uit :Q
Thank you, shutting down now.
</pre>
=={{header|C++}}==
<lang C>
503

edits