Programmieraufgabe



  • Star69 schrieb:

    Ich kann die sachen nicht wirklich öffnen!einmal stehen da nur komische sachen und einmal alles durcheinander oder alles hintereinander!da find ich nicht durch!Alle Quelltexte sollten kommentiert werden, d.h. an den Funktionen steht,
    was diese machen und was ihre Parameter und R¨uckgabewerte bedeuten. Beschreiben sie die
    Algorithmen!Der Quelltext sollte leserlich formatiert sein, d.h. der Inhalt eines
    Blocks oder einer Kontrollstruktur wird eingerückt!

    So sieht es aus wenn du's mit notepad öffnest, öffne die Datei mal mit WordPad.



  • ja ich werds noch kommentieren und formatieren. wollte nur zeigen wie weit ich bin.
    Gibt es eine funktion die getline unter windows ersetzt?



  • Ja ist ok konnte es auch mit wordpad lesen.Sieht schon gut aus!Das andere mit getline weiß ich leider nicht!



  • gibt es einen wert, der entweder nur 2 nachkomma stellen hat? ich glaub nicht, oder? wenn nicht gibt es eine funktion die einen float auf zwei stellen rundet? sonst kann ich nicht genau testen wie viel zurückgegeben werden muss.



  • hab mal ein gefragt und der meinte das hier:
    scheibste bei der ausgabe einfach :
    %5.2f
    oder %7.2f
    dann macht der 2 nachkommastellen bei der ausgabe



  • ja wie man das bei der ausgabe macht weiss ich, aber das problem ist dass ich auch eine abfrage brauche wann man nicht mehr einzahlen kan und wie viel er auszahlen muss.



  • machs doch erst ungerundet, mach das programm fertig und ganz zum schluss programmierst du noch was, damit das ding dem Rückzahlungsbetragrechner den gerundeten Wert übergibt -> fertig.



  • ich bin jetzt ja ganz am schluss:)



  • Erster!!!

    #include <stdio.h>
    
    const char* cities[10]  =
    {	"Aachen", "Berlin", "Bremen", "Dresden", "Essen",
    	"Hannover", "Leipzig", "Muenchen", "Osnabrueck", "Stuttgart" }; 
    
    static const int dist[10][10] =
    {
        {   0, 637, 369, 651, 123, 354, 573, 650, 256, 408 },
        { 637,   0, 357, 214, 480, 258, 184, 596, 410, 631 },
        { 369, 375,   0, 478, 249, 118, 367, 766, 120, 645 },
        { 651, 214, 478,   0, 581, 385, 140, 496, 590, 531 },
        { 123, 480, 249, 581,   0, 258, 475, 646, 135, 472 },
        { 354, 258, 118, 385, 258,   0, 252, 647, 135, 526 },
        { 573, 184, 367, 140, 475, 252,   0, 436, 410, 471 },
        { 650, 596, 766, 496, 646, 647, 436,   0, 655, 210 },
        { 256, 410, 120, 590, 135, 135, 410, 655,   0, 557 },
        { 408, 631, 645, 531, 472, 526, 471, 210, 557,   0 }
    };
    
    struct money {
    	int notes_fiftie,
    		 notes_twentie,
    		 notes_tenner,
    		 notes_fiver,
    		 coins_double,
    		 coins_ones,
    		 coins_fiftie,
    		 coins_twentie,
    		 coins_tenner,
    		 coins_fiver;
    	float total;
    };
    
    int main()
    {
    	// declare needed variables
    	int start, end,
    		distance,
    		ice, intercity, bike,
    		i;
    	float	normalprice, additionalprice, totalprice,
    			totalreturnleft;
    
    	struct money payed = {0,0,0,0,0,0,0,0,0,0,0.0};
    	struct money toreturn = {0,0,0,0,0,0,0,0,0,0,0.0};
    	struct money cash = {0,0,0,0,0,0,0,0,0,0,0.0};
    	struct money machine = {10,20,30,40,50,60,70,80,90,100,0.0};
    
    goto start; start:
    	// reset some variables
    	ice = 0, intercity = 0, bike = 0, additionalprice = 0;
    	payed.notes_fiftie = 0; payed.notes_twentie = 0; payed.notes_tenner = 0;
    	payed.notes_fiver = 0;  payed.coins_double = 0;  payed.coins_ones = 0;
    	payed.coins_fiftie = 0; payed.coins_twentie = 0; payed.coins_tenner = 0;
    	payed.coins_fiver = 0;  payed.total = 0;
    
    goto task1; task1:
    
    	// printing out the available travelcities
    	for( i = 0;i < 10; i++ ){
    		printf( "[%i] %s\n", i+1, cities[i] );
    	}
    	// getting start of travel
    	printf( "Where do you want to start? \n> " );
    	scanf( "%i", &start);
    	start--; // I hope you know why ;)
    
    	// getting end of travel
    	printf( "Where is the destination? \n> " );
    	scanf( "%i", &end );
    	end--; // I hope you know why ;)
    
    	// get distance from array
    	distance = dist[start][end];
    	//calculate price of travel
    	normalprice = distance * 0.15;
    
    	// print out information about distance and price
    	printf( "The distance is: %i km\n", distance);
    	printf( "Travel expenses: %.2f EUR\n", normalprice);
    
    goto task2; task2:
    	printf( "\nAdditional services:\n" \
    			  "[0] nothing\n" \
    			  "[1] ICE       (+6.00 EUR)\n" \
    			  "[2] intercity (+4.00 EUR)\n" \
    			  "[3] bike      (+3.00 EUR)\n");
    	// Repeat for asking for additional services until user selects 'nothing' or
    	// everything is select
    	do{
    		printf( "> " );
    		scanf( "%i", &i );
    
    		if( i == 1 && ice == 0 ){	// if 'ice' haven't been selected before
    			ice = 1;
    			additionalprice += 6.00;
    		}else if(i == 2 && intercity == 0){ // if 'intercity' haven't been 
    			intercity = 1;							//             selected before
    			additionalprice += 4.00;
    		}else if( i == 3 && bike == 0){ // if 'bike' haven't been seleceed before
    			bike = 1;
    			additionalprice += 3.00;
    		}	
    	}while( i != 0 );// || (ice == 0 && intercity == 0 && bike == 0) );
    
    	// calculate total price
    	totalprice = normalprice + additionalprice;
    
    goto task3; task3:
    	printf( "\nStartcity:   %s\n" \
    			  "Destination: %s\n" \
    			  "Distance:    %i\n" \
    			  "Normal price:       %.2f EUR\n" \
    			  "Additional services:\n",
    			  cities[start],
    			  cities[end],
    			  distance,
    			  normalprice );
    	// print out the additional services
    	if( ice == 1 )       printf( "\tICE         + 6.00 EUR\n" );
    	if( intercity == 1 ) printf( "\tintercity   + 4.00 EUR\n" );
    	if( bike == 1 )      printf( "\tbike        + 3.00 EUR\n" );
    	// print out the total price	
    	printf( "Total price:  %.2f EUR\n", totalprice);
    
    goto task4; task4:
    	// ask if user wants to cancel travel
    	printf( "\nDo you want to cancel? yes[1] or no[0]\n> " );
    	scanf( "%i", &i );
    	if( i == 1)
    		goto task1;
    
    goto task5; task5:
    	printf( "\nWhat do you want to pay?\n"
    			  "[1] 50.00 EUR (note)\n" \
    			  "[2] 20.00 EUR (note)\n" \
    			  "[3] 10.00 EUR (note)\n" \
    			  "[4]  5.00 EUR (note)\n" \
    			  "[5]  2.00 EUR (coin)\n" \
    			  "[6]  1.00 EUR (coin)\n" \
    			  "[7]  0.50 EUR (coin)\n" \
    			  "[8]  0.20 EUR (coin)\n" \
    			  "[9]  0.10 EUR (coin)\n" \
    			  "[0]  0.05 EUR (coin)\n");
    	// ask user for money
    	do{
    		printf( "left to pay: %.2f\n> ", payed.total - totalprice);
    		scanf( "%i", &i );
    		     if( i == 1) payed.notes_fiftie++;
    		else if( i == 2) payed.notes_twentie++;
    		else if( i == 3) payed.notes_tenner++;
    		else if( i == 4) payed.notes_fiver++;
    		else if( i == 5) payed.coins_double++;
    		else if( i == 6) payed.coins_ones++;
    		else if( i == 7) payed.coins_fiftie++;
    		else if( i == 8) payed.coins_twentie++;
    		else if( i == 9) payed.coins_tenner++;
    		else if( i == 0) payed.coins_fiver++;
    
    		// calculate total payed money
    		payed.total =
    			(payed.notes_fiftie  * 50.00) + (payed.notes_twentie * 20.00) +
    			(payed.notes_tenner  * 10.00) + (payed.notes_fiver   *  5.00) +
    			(payed.coins_double  *  2.00) + (payed.coins_ones    *  1.00) +
    			(payed.coins_fiftie  *  0.50) + (payed.coins_twentie *  0.20) +
    			(payed.coins_tenner  *  0.10) + (payed.coins_fiver   *  0.05);
    	}while( payed.total < totalprice );
    
    	// calculate total return of money
    	totalreturnleft = (payed.total - totalprice);
    
    	// return money to user
    	if((int)(totalreturnleft / 50) > 0 &&
    		machine.notes_fiftie >= (int)(totalreturnleft / 50)){
    			toreturn.notes_fiftie = (int)(totalreturnleft / 50);
    			totalreturnleft -= (int)(totalreturnleft / 50) * 50;
    	}
    	if((int)(totalreturnleft / 20) > 0 &&
    		machine.notes_twentie >= (int)(totalreturnleft / 20)){
    			toreturn.notes_twentie = (int)(totalreturnleft / 20);
    			totalreturnleft -= (int)(totalreturnleft / 20) * 20;
    	}
    	if((int)(totalreturnleft / 10) > 0 &&
    		machine.notes_tenner >= (int)(totalreturnleft / 10)){
    			toreturn.notes_tenner = (int)(totalreturnleft / 10);
    			totalreturnleft -= (int)(totalreturnleft / 10) * 10;
    	}
    	if((int)(totalreturnleft / 5) > 0 &&
    		machine.notes_fiver >= (int)(totalreturnleft / 5)){
    			toreturn.notes_fiver = (int)(totalreturnleft / 5);
    			totalreturnleft -= (int)(totalreturnleft / 5) * 5;
    	}
    	if((int)(totalreturnleft / 2) > 0 &&
    		machine.coins_double >= (int)(totalreturnleft / 2)){
    			toreturn.coins_double = (int)(totalreturnleft / 2);
    			totalreturnleft -= (int)(totalreturnleft / 2) * 2;
    	}
    	if((int)(totalreturnleft / 1) > 0 &&
    		machine.coins_ones >= (int)(totalreturnleft / 1)){
    			toreturn.coins_ones = (int)(totalreturnleft / 1);
    			totalreturnleft -= (int)(totalreturnleft / 1) * 1;
    	}
    	if((int)(totalreturnleft / 0.50) > 0 &&
    		machine.coins_fiftie >= (int)(totalreturnleft / 0.50)){
    			toreturn.coins_fiftie = (int)(totalreturnleft / 0.50);
    			totalreturnleft -= (int)(totalreturnleft / 0.50) * 0.502;
    	}
    	if((int)(totalreturnleft / 0.20) > 0 &&
    		machine.coins_twentie >= (int)(totalreturnleft / 0.20)){
    			toreturn.coins_twentie = (int)(totalreturnleft / 0.20);
    			totalreturnleft -= (int)(totalreturnleft / 0.20) * 0.20;
    	}
    	if((int)(totalreturnleft / 0.10) > 0 &&
    		machine.coins_tenner >= (int)(totalreturnleft / 0.10)){
    			toreturn.coins_tenner = (int)(totalreturnleft / 0.10);
    			totalreturnleft -= (int)(totalreturnleft / 0.10) * 0.102;
    	}
    	if((int)(totalreturnleft / 0.05) > 0 &&
    		machine.coins_fiver >= (int)(totalreturnleft / 0.05)){
    			toreturn.coins_fiver = (int)(totalreturnleft / 0.05);
    			totalreturnleft -= (int)(totalreturnleft / 0.05) * 0.05;
    	}
    
    	// check if there is still money left to return
    	if( totalreturnleft > 0.01 ){
    		printf("There is not enough money to return.\n" \
    				 "Here you have your money back: (%.2f EUR)\n", payed.total);
    		if(payed.notes_fiftie > 0)  printf("50.00 EUR * %i\n", payed.notes_fiftie);
    		if(payed.notes_twentie > 0) printf("20.00 EUR * %i\n", payed.notes_twentie);
    		if(payed.notes_tenner > 0)  printf("10.00 EUR * %i\n", payed.notes_tenner);
    		if(payed.notes_fiver > 0)   printf(" 5.00 EUR * %i\n", payed.notes_fiver);
    		if(payed.coins_double > 0)  printf(" 2.00 EUR * %i\n", payed.coins_double);
    		if(payed.coins_ones > 0)    printf(" 1.00 EUR * %i\n", payed.coins_ones);
    		if(payed.coins_fiftie > 0)  printf(" 0.50 EUR * %i\n", payed.coins_fiftie);
    		if(payed.coins_twentie > 0) printf(" 0.20 EUR * %i\n", payed.coins_twentie);
    		if(payed.coins_tenner > 0)  printf(" 0.10 EUR * %i\n", payed.coins_tenner);
    		if(payed.coins_fiver > 0)   printf(" 0.05 EUR * %i\n", payed.coins_fiver);
    	}
    
    	// check if user wants to print or cancel
    	printf("Do you want to print the ticket [1] or cancel [0]");
    	scanf("%i", &i);
    	if( i == 0){
    		printf("Here you have your money back: (%.2f EUR)\n", payed.total);
    		if(payed.notes_fiftie > 0)  printf("50.00 EUR * %i\n", payed.notes_fiftie); payed.notes_fiftie = 0;
    		if(payed.notes_twentie > 0) printf("20.00 EUR * %i\n", payed.notes_twentie); payed.notes_twentie = 0;
    		if(payed.notes_tenner > 0)  printf("10.00 EUR * %i\n", payed.notes_tenner); payed.notes_tenner = 0;
    		if(payed.notes_fiver > 0)   printf(" 5.00 EUR * %i\n", payed.notes_fiver); payed.notes_fiver = 0;
    		if(payed.coins_double > 0)  printf(" 2.00 EUR * %i\n", payed.coins_double); payed.coins_double = 0;
    		if(payed.coins_ones > 0)    printf(" 1.00 EUR * %i\n", payed.coins_ones); payed.coins_ones = 0;
    		if(payed.coins_fiftie > 0)  printf(" 0.50 EUR * %i\n", payed.coins_fiftie); payed.coins_fiftie = 0;
    		if(payed.coins_twentie > 0) printf(" 0.20 EUR * %i\n", payed.coins_twentie); payed.coins_twentie = 0;
    		if(payed.coins_tenner > 0)  printf(" 0.10 EUR * %i\n", payed.coins_tenner); payed.coins_tenner = 0;
    		if(payed.coins_fiver > 0)   printf(" 0.05 EUR * %i\n", payed.coins_fiver); payed.coins_fiver = 0;
    		payed.total = 0;
    	}
    
    	// put the money from the user to the "cash"-box
    	cash.notes_fiftie  += payed.notes_fiftie;
    	cash.notes_twentie += payed.notes_fiftie;
    	cash.notes_tenner  += payed.notes_fiftie;
    	cash.notes_fiver   += payed.notes_fiftie;
    	cash.coins_double  += payed.notes_fiftie;
    	cash.coins_ones    += payed.notes_fiftie;
    	cash.coins_fiftie  += payed.notes_fiftie;
    	cash.coins_twentie += payed.notes_fiftie;
    	cash.coins_tenner  += payed.notes_fiftie;
    	cash.coins_fiver   += payed.notes_fiftie;
    
    	// calculate total return money
    	toreturn.total =
    		(toreturn.notes_fiftie  * 50.00) + (toreturn.notes_twentie * 20.00) +
    		(toreturn.notes_tenner  * 10.00) + (toreturn.notes_fiver   *  5.00) +
    		(toreturn.coins_double  *  2.00) + (toreturn.coins_ones    *  1.00) +
    		(toreturn.coins_fiftie  *  0.50) + (toreturn.coins_twentie *  0.20) +
    		(toreturn.coins_tenner  *  0.10) + (toreturn.coins_fiver   *  0.05);
    
    	// return money to user
    	printf("Money to return: %.2f EUR\n", toreturn.total);
    	if(toreturn.notes_fiftie > 0)  printf("50.00 EUR * %i\n", toreturn.notes_fiftie);
    	if(toreturn.notes_twentie > 0) printf("20.00 EUR * %i\n", toreturn.notes_twentie);
    	if(toreturn.notes_tenner > 0)  printf("10.00 EUR * %i\n", toreturn.notes_tenner);
    	if(toreturn.notes_fiver > 0)   printf(" 5.00 EUR * %i\n", toreturn.notes_fiver);
    	if(toreturn.coins_double > 0)  printf(" 2.00 EUR * %i\n", toreturn.coins_double);
    	if(toreturn.coins_ones > 0)    printf(" 1.00 EUR * %i\n", toreturn.coins_ones);
    	if(toreturn.coins_fiftie > 0)  printf(" 0.50 EUR * %i\n", toreturn.coins_fiftie);
    	if(toreturn.coins_twentie > 0) printf(" 0.20 EUR * %i\n", toreturn.coins_twentie);
    	if(toreturn.coins_tenner > 0)  printf(" 0.10 EUR * %i\n", toreturn.coins_tenner);
    	if(toreturn.coins_fiver > 0)   printf(" 0.05 EUR * %i\n", toreturn.coins_fiver);
    
    goto task6; task6:
    	//Printing the complete ticket
    	printf("\n+--------------------------------------+\n" \
    			   "| Start:       %10s              |\n" \
    				"| Destination: %10s              |\n" \
    			   "|-  -  -  -  -  -  -  -  -  -  -  -  - |\n" \
    			   "| Distance: %3i km                     |\n" \
    			   "| Normal Price: %6.2f EUR             |\n" \
    			   "| Additional serice:                   |\n",
    				cities[start], cities[end], distance, normalprice);
    
    	if( ice == 1 )       printf( "|   ICE         + 6.00 EUR             |\n" );
    	if( intercity == 1 ) printf( "|   intercity   + 4.00 EUR             |\n" );
    	if( bike == 1 )      printf( "|   bike        + 3.00 EUR             |\n" );
    	printf("| Total Price: %6.2f EUR              |\n" \
    			 "| Tax:         %6.2f EUR              |\n" \
    			 "| Money returned: %6.2f EUR           |\n" \
    			 "+--------------------------------------+\n",
    			 totalprice, totalprice / 1.19, toreturn.total);
    
    goto task7; task7:
    	// check if user wants to cancel or make a new travel
    	printf("Do you want to cancel[1] or make a new travel[0] ?\n>");
    	scanf("%i", &i);
    	if(i == 0)
    		goto task1;
    
    	// calculate cash
    	cash.total =
    			(cash.notes_fiftie  * 50.00) + (cash.notes_twentie * 20.00) +
    			(cash.notes_tenner  * 10.00) + (cash.notes_fiver   *  5.00) +
    			(cash.coins_double  *  2.00) + (cash.coins_ones    *  1.00) +
    			(cash.coins_fiftie  *  0.50) + (cash.coins_twentie *  0.20) +
    			(cash.coins_tenner  *  0.10) + (cash.coins_fiver   *  0.05);
    
    	// show cash
    	printf("Cash: %.2f", cash.total);
    
    	fflush(stdin);
       getchar();
       return 0;
    }
    

    Viele Grüße



  • schön schön....
    ich machs trotzdem fertig....



  • jo, kein problem. kannst ja mal schauen, was bei mir "vielleicht" besser ist, und deines dann verbessern



  • Schlechter Code Bullja!



  • Schlecht ist sogar noch untertrieben. Da würde mir mein Compiler leid tun, wenn ich ihm sowas vorsetze.
    goto forever ... 👎



  • tweenki wie siehts bei dir aus?wie weit biste? kannst dich ja dann mal melden wenn es soweit ist! E-Mail oder icq!denke das ich morgen erst wieder online bin!
    mfg



  • :-((( schrieb:

    Schlechter Code Bullja!

    wer schreibt doch gleich die dicksten programme?
    🙂



  • Schlecht, aber funktioniert 🙂



  • DieDümmstenProgrammierer. schrieb:

    :-((( schrieb:

    Schlechter Code Bullja!

    wer schreibt doch gleich die dicksten programme?
    🙂

    Falsch! - "Die dümmsten Bauern haben die größten Kartoffeln!"
    :p



  • oh die goto anweisungen hab ich noch gar nicht gesehen. falls ich zeit hab werd ichs versuchen.
    ich guck mir mal den algorithmus von bullja an.



  • tweenki schrieb:

    oh die goto anweisungen hab ich noch gar nicht gesehen. falls ich zeit hab werd ichs versuchen.
    ich guck mir mal den algorithmus von bullja an.

    die labels und gotos hab ich so gemacht:

    goto task2; task2:
    

    denn wenn ein label vorhanden ist, auf das nie zugegriffen wurde, dann bringt der compiler eine warnung...deswegen hab ich immer ein goto davor gesetzt (kann man natürlich weglassen).

    Und das ganze Programm in Funktionen zu schreiben ist wohl nicht schwer^^



  • Bullja schrieb:

    Und das ganze Programm in Funktionen zu schreiben ist wohl nicht schwer^^

    Warum (zum Teufel) machst Du's dann nicht!?

    greetz, Swordfish


Anmelden zum Antworten