Pirates and C programming

2»

Comments

  • edited August 2009
    /*
    Author: Tom Moore
    Date: 15/8/2009
    Title: Pyrite Parrot
    Purpose: For the lols
    */
    
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
    	printf("\nPyrite Parrot Knock Knock Utility v0.1\n\n");
    	string response0;
    	:start
    	string prompt = "\nSay something to the parrot:  ";
    	
    	printf(prompt);
    	scanf("%s", &response0);
    	
    	printf("\nYou: %s", response0);
    	if(response0 == "Knock knock")
    		runJoke(prompt);
    		else
    		{
    			printf("\n...\n\n\n...\n\nNo response. Try the knock knock joke.");
    			response0 = null;
    			getchar();
    			goto start;
    		}
    	return 0;
    }
    
    
    void runJoke(string prompt)
    {
    	string response1, dummy;
    	printf("\nParrot: Who's there?");
    	printf(prompt);
    	scanf("%s", &response1);
    	printf("\nYou: %s", response1);
    	printf("\nParrot: Bwaaaarrrrk!\n\t%s", response1);
    	:repeat
    	printf(prompt);
    	scanf("%s", &dummy);
    	printf("\nYou: %s", dummy);
    	dummy = null;
    	getchar();
    	printf("\nParrot: Bwaaarrrrk!\n\t%s", response1);
    	printf("\n\n\nHint: when you're bored of this, type Ctrl-c");
    	goto repeat;
    }
    

    No comments but I don't think a simple text program needs them. Will get improved if there's a demand.

    EDIT: I studied C four years ago and haven't touched it since, ok? I'm rather rusty, hence inevitable mistakes.
  • edited August 2009
    [TTG] Yare wrote: »
    Hold up guys, I got this.

    Q: Why don't pirates like combinatorial optimization of NP-complete problems?
    A: Because they're NP-Harrrrrrrrd.

    I guessed "Because its Non Pirate" Guess I'm even worse than you ;)

    And now for another bad joke. What do you call Guybrush when he's handling his inventory?
    Answer:
    OOP - Object Oriented Pirate
  • edited August 2009
    tbm1986 wrote: »
    /*
    Author: Tom Moore
    Date: 15/8/2009
    Title: Pyrite Parrot
    Purpose: For the lols
    */
    
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
    	printf("\nPyrite Parrot Knock Knock Utility v0.1\n\n");
    	string response0;
    	:start
    	string prompt = "\nSay something to the parrot:  ";
    	
    	printf(prompt);
    	scanf("%s", &response0);
    	
    	printf("\nYou: %s", response0);
    	if(response0 == "Knock knock")
    		runJoke(prompt);
    		else
    		{
    			printf("\n...\n\n\n...\n\nNo response. Try the knock knock joke.");
    			response0 = null;
    			getchar();
    			goto start;
    		}
    	return 0;
    }
    
    
    void runJoke(string prompt)
    {
    	string response1, dummy;
    	printf("\nParrot: Who's there?");
    	printf(prompt);
    	scanf("%s", &response1);
    	printf("\nYou: %s", response1);
    	printf("\nParrot: Bwaaaarrrrk!\n\t%s", response1);
    	:repeat
    	printf(prompt);
    	scanf("%s", &dummy);
    	printf("\nYou: %s", dummy);
    	dummy = null;
    	getchar();
    	printf("\nParrot: Bwaaarrrrk!\n\t%s", response1);
    	printf("\n\n\nHint: when you're bored of this, type Ctrl-c");
    	goto repeat;
    }
    

    No comments but I don't think a simple text program needs them. Will get improved if there's a demand.

    EDIT: I studied C four years ago and haven't touched it since, ok? I'm rather rusty, hence inevitable mistakes.

    You included a goto statement. DIE DIE DIE!!!

    :P

    I'm sorry, but I'm a firm believer that goto statements are considered harmful...
  • edited August 2009
    You included a goto statement. DIE DIE DIE!!!

    :P

    I'm sorry, but I'm a firm believer that goto statements are considered harmful...

    My lecturers shared this view. Unfortunately, since then I've been doing more GNU Assembly than C, hence I've become lazy. I can't be arsed atm to redo it properly with while loops, switch cases and more functions.
  • edited August 2009
    Ohhhhh, all right...
  • edited August 2009
    /**********************
    Author: Tom Moore
    Created: 15/8/2009
    Updated: 15/8/2009
    Title: Pyrite Parrot
    Purpose: For the lols
    **********************/
    
    #include <stdio.h>
    #include <string.h>
    
    
    void runJoke(string prompt);
    void clear_stdin(void);
    
    int main(void)
    {
    	printf("\nPyrite Parrot Knock Knock Utility v0.2\n\n");
    	string response0;
    	bool knock = false;
    	while(!knock)
    	{
    		string prompt = "\nSay something to the parrot:  ";
    	
    		printf(prompt);
    		scanf("%s", &response0);
    		
    		printf("\nYou: %s", response0);
    		switch(response0)
    		{
    			case "Knock knock":
    			{
    				~knock;
    				break;
    			}
    			
    			case "knock knock":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock, knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "knock, knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "knock knock":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock knock, you stupid fucking parrot!":
    			{
    				~knock;
    				break;
    			}
    
    			case "exit":
    				return 0;
    		
    			default:
    			{
    				printf("\n...\n\n\n...\n\nNo response. Try the knock knock joke.");
    				response0 = null;
    				clear_stdin();
    				break;
    			}			
    	}
    	runJoke(prompt);
    
    	return 0;
    }
    
    
    void runJoke(prompt)
    {
    	string response1, dummy;
    	printf("\nParrot: Who's there?");
    	printf(prompt);
    	scanf("%s", &response1);
    	printf("\nYou: %s", response1);
    	printf("\nParrot: Bwaaaarrrrk!\n\t%s", response1);
    	do
    	{
    		printf(prompt);
    		scanf("%s", &dummy);
    		printf("\nYou: %s", dummy);
    		dummy = null;
    		clear_stdin();
    		printf("\nParrot: Bwaaarrrrk!\n\t%s", response1);
    		printf("\n\n\nHint: when you're bored of this, type exit or Ctrl-c");
    	} while(dummy != "exit");
    	
    	return;
    }
    
    void clear_stdin(void)
    {
    	short length = strlen(stdin);
    	for(; length > 0; length--)
    		getchar();
    }
    
  • edited August 2009
    tbm1986 wrote: »
    /**********************
    Author: Tom Moore
    Created: 15/8/2009
    Updated: 15/8/2009
    Title: Pyrite Parrot
    Purpose: For the lols
    **********************/
    
    #include <stdio.h>
    #include <string.h>
    
    
    void runJoke(string prompt);
    void clear_stdin(void);
    
    int main(void)
    {
    	printf("\nPyrite Parrot Knock Knock Utility v0.2\n\n");
    	string response0;
    	bool knock = false;
    	while(!knock)
    	{
    		string prompt = "\nSay something to the parrot:  ";
    	
    		printf(prompt);
    		scanf("%s", &response0);
    		
    		printf("\nYou: %s", response0);
    		switch(response0)
    		{
    			case "Knock knock":
    			{
    				~knock;
    				break;
    			}
    			
    			case "knock knock":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock, knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "knock, knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "knock knock":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock knock!":
    			{
    				~knock;
    				break;
    			}
    
    			case "Knock knock, you stupid fucking parrot!":
    			{
    				~knock;
    				break;
    			}
    
    			case "exit":
    				return 0;
    		
    			default:
    			{
    				printf("\n...\n\n\n...\n\nNo response. Try the knock knock joke.");
    				response0 = null;
    				clear_stdin();
    				break;
    			}			
    	}
    	runJoke(prompt);
    
    	return 0;
    }
    
    
    void runJoke(prompt)
    {
    	string response1, dummy;
    	printf("\nParrot: Who's there?");
    	printf(prompt);
    	scanf("%s", &response1);
    	printf("\nYou: %s", response1);
    	printf("\nParrot: Bwaaaarrrrk!\n\t%s", response1);
    	do
    	{
    		printf(prompt);
    		scanf("%s", &dummy);
    		printf("\nYou: %s", dummy);
    		dummy = null;
    		clear_stdin();
    		printf("\nParrot: Bwaaarrrrk!\n\t%s", response1);
    		printf("\n\n\nHint: when you're bored of this, type exit or Ctrl-c");
    	} while(dummy != "exit");
    	
    	return;
    }
    
    void clear_stdin(void)
    {
    	short length = strlen(stdin);
    	for(; length > 0; length--)
    		getchar();
    }
    

    That's much better. :)
  • edited August 2009
    That's much better. :)

    I'm glad someone appreciates it! It took me a good half hour typing all that out.
  • edited August 2009
    tbm1986 wrote: »
    I'm glad someone appreciates it! It took me a good half hour typing all that out.

    We're not commenting for your own good. Spending that much time on a joke isn't healthy! ;)
  • DjNDBDjNDB Moderator
    edited August 2009
    tbm1986 wrote: »
    I'm glad someone appreciates it! It took me a good half hour typing all that out.

    I had fun reading it too :)
  • edited August 2009
    You included a goto statement. DIE DIE DIE!!!

    :P

    I'm sorry, but I'm a firm believer that goto statements are considered harmful...

    They're a tool like all other tools in the programming toolbox, used correctly they are quite powerful and can help you optimise your code quite a bit, but then, using them wrongfully you get awfully bad code ;)
  • edited August 2009
    Goto statements are most useful in Assembly, in the form of Jumps. Having said that, the CJNE is the equivalent of a While loop.

    Was Goto instruction invented for beginner C users that're familiar with Assembly? You can get tighter code with Assembly if you're compact, but if you do every program the same, imo, ppl are better using either C or Basic.
  • edited August 2009
    Goto statements were a necessary evil at some point, but today we are rarely interested enough in performance to justify sacrificing good code readability for that.

    Truth be told, I haven't thought about goto in almost a decade and that's a good thing. Goto is too dangerous to let the average programmer decide when it can or cannot be used, so we simply say it's evil. Of course you can write good code with goto and bad code without, but you're simply more likely to produce bad code if you splatter gotos and labels all over the code.
  • edited August 2009
    That it's tricky to use gotos is very true and for the most part you should avoid them, at the same time, using them correctly can really boost the performance on your coding allowing you to get more for your money when writing very processing intense algorithms.
  • edited August 2009
    That it's tricky to use gotos is very true and for the most part you should avoid them, at the same time, using them correctly can really boost the performance on your coding allowing you to get more for your money when writing very processing intense algorithms.

    That applies to little beyond realtime applications on microcontrollers these days (having said that, what are cellphones and wireless routers? lol)
  • DjNDBDjNDB Moderator
    edited August 2009
    tbm1986 wrote: »
    That applies to little beyond realtime applications on microcontrollers these days (having said that, what are cellphones and wireless routers? lol)

    Routers/cell phones are no realtime applications, that's for sure.

    I think in terms of performance a "goto statement" should make no difference. I haven't tried it, but i am pretty confident that compiler optimization will get similar, if not the same, results with or without "goto".

    There is a legendary discussion on that topic, by the way.
Sign in to comment in this discussion.