Results 1 to 8 of 8

Thread: Keep the State

  1. #1
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default Keep the State

    A simple coding challenge, create a paged display screen ( menu if you like).

    screen 1
    Code:
    Math Test
    1. Paper 1
    2. Paper 2
    screen 2 ... when user enters 1 at screen 1
    Code:
    Paper 1
    1. Hard Question
    2. 1 + 1
    screen 3 ... when user enters 2 at screen 1
    Code:
    Paper 2
    1. define tangent
    2. integrate sin x - cos 2x
    screen 4 ... when user enters 1 at screen 2
    Code:
    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.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  2. #2
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    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.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  3. #3
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    the challenge is to make your solution short
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  4. #4
    Join Date
    Jul 2005
    Posts
    75
    Rep Power
    0

    Default

    Well it seems no one has paid this challenge any mind. I can not have this. Please hold i will post a solution shortly.
    "A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips -

  5. #5
    Join Date
    Jul 2005
    Posts
    75
    Rep Power
    0

    Default

    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.
    Code:
    // 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;
          }
       }
    }
    "A computer once beat me at chess, but it was no match for me at kick boxing." - Emo Philips -

  6. #6
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Only 69 lines without the decription header, I am sure that is the max possible
    Code:
    #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);
    }
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  7. #7
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    This is my proposed solution. there are of course better, and shorter ways.

    Code:
    #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
    Last edited by icymint3; Aug 12, 2005 at 06:29 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  8. #8
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    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.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •