PDA

View Full Version : Keep the State



icymint3
August 4, 2005, 06:57 PM
A simple coding challenge, create a paged display screen ( menu if you like).

screen 1


Math Test
1. Paper 1
2. Paper 2


screen 2 ... when user enters 1 at screen 1


Paper 1
1. Hard Question
2. 1 + 1


screen 3 ... when user enters 2 at screen 1


Paper 2
1. define tangent
2. integrate sin x - cos 2x


screen 4 ... when user enters 1 at screen 2


Hard Question
something very hard should be here


__________________________________________________ _____________

the constraints
1. you only define function main(){ ... }, no other function
2. you can define structures in main if you want
3. no classes methods, macros, inlines

GO
you are allowed to make your code as obfuscated as you want (if you wish).
I prefer if you submit pseudocode before posting your coded solution, but its up to you.

icymint3
August 4, 2005, 06:58 PM
please put a return ( if you wish ), but you are allowede to keep it simple.
exit whenever an invalid input is made when a leaf menu if selected just exit or show a message.

icymint3
August 4, 2005, 07:01 PM
the challenge is to make your solution short

Mr. Unstoppable
August 8, 2005, 09:29 AM
Well it seems no one has paid this challenge any mind. I can not have this. Please hold i will post a solution shortly.

Mr. Unstoppable
August 8, 2005, 05:05 PM
42 LOC i have a simple menu. Man it seem so simple but yet these menus usually cost you the most LOC. That is why i never like designing the stuff i prefer work on functionality not Display Menu's any day.

Here it is.


// Name : DPD
// Program : Simple Menu
// Description : This is a simple menu done in function main.
// Date : AUG 8, 2005
// Last Updated: ----------
// Proposed mod:

#include <stdio.h>
#include <conio.h>

void main()
{
char ans;
while( 1 )
{
clrscr();
printf("\tMath Test\n\n1. Paper 1\n2. Paper 2");

switch( getch() )
{
case '1':
printf( "\n\n\n\tPaper 1\n\n1. Hard Question\n2. 1 + 1" );
if( (ans = getch()) == '1' || ans == '2' )
printf("%s", ans == '1' ? "\n\nHard Question":"\n\n1 + 1" );

getch();
return;

case '2':
printf( "\n\n\n\tPaper 2\n\n1. Define Tangent\n2. Integrate sinx" );
if( (ans = getch()) == '1' || ans == '2' )
printf("%s", ans == '1' ? "\n\nDefine tangent?":"\n\nIntegrate sin x" );

getch();
return;

default:
printf("\n\n\Invalid INPUT!!!!");
break;
}
}
}

crosswire
August 8, 2005, 11:08 PM
Only 69 lines without the decription header, I am sure that is the max possible


#include <stdio.h>
#include <stdlib.h>

/*
Display Page 1; Get Input.
If user Enters 1, then Display Page 2, Else Exit; Get New Input; If user Enters 1, then Display Page 4, Else Exit.
Else If user Enters 2, then Display Page 3, Else Exit.
*/

char buffer[64];

void main()
{
//Page 1
system("cls");
printf(
"Math Test" "\n"
"1. Paper 1" "\n"
"2. Paper 2" "\n" );
scanf("%s", buffer);

switch(buffer[0])
{
case '1':

//Page 2
system("cls");
printf(
"Paper 1" "\n"
"1. Hard Question" "\n"
"2. 1 + 1" "\n" );
scanf("%s", buffer);

switch(buffer[0])
{
case '1':

//Page 4
system("cls");
printf(
"Hard Question" "\n"
"something very hard should be here" "\n" );
scanf("%s", buffer);

break;
default:
exit(1);
}

break;
case '2':

//Page 3
system("cls");
printf(
"Paper 2" "\n"
"1. define tangent" "\n"
"2. integrate sin x - cos 2x" "\n" );
scanf("%s", buffer);

break;
default:
exit(1);
}

exit(1);
}

icymint3
August 12, 2005, 06:23 PM
This is my proposed solution. there are of course better, and shorter ways.


#include <stdio.h>
#include <conio.h>

void main()
{
char *prompts[4][3] = {
{"1Math Test", " Paper 1", " Paper 2"},
{"1Paper 1", " Hard Question", " 1 + 1"},
{"1Paper 2", " define tangent"," integrate sin x - cos 2x"},
{"1Hard Question", " something very hard should be here" }};
unsigned mFrame[] = {2,2,2,1}, stack[4] = {0}, top = 1, offset, ix;

do
{
clrscr();
printf("%s\n", &prompts[stack[top-1]][0][1]);
for(ix=0,offset=prompts[stack[top-1]][0][0];ix<mFrame[stack[top-1]];ix++)
printf("\t%c. %s\n",ix + offset, &prompts[stack[top-1]][ix + 1][1] );
printf("\t%c. %s\n",ix + offset, "Return" );

ix = getch() - offset;
switch( ( stack[top-1] << 4 ) | ix )
{
case 0x00: stack[top++] = 1; break;
case 0x01: stack[top++] = 2; break;
case 0x10: stack[top++] = 3; break;

case 0x11: case 0x20: case 0x21: case 0x30: // do more interesting stuff
printf("\n\n%s : ", &prompts[stack[top-1]][ix+1][1] ); getch(); break;

case 0x02: case 0x12: case 0x22: case 0x31: --top; break;

default: return;
}
}while( top );
}

it was suppossed to be a continuing problem. tell you the rest later

crosswire
August 12, 2005, 09:15 PM
I was working on a way that used goto and some logic to decode which line to go to

I was also going to ask you if the what a return to main menu.