Λοιπόν έριξα μια ματιά στον κώδικά σου, και άλλαξα μερικά πράγματα, συμπεριλαμβανομένων των 2 προβλημάτων που αναφέρω στο προηγούμενο ποστ.
Συνοπτικά:
1. Άλλαξα τα x,y από int σε double ώστε το πρόγραμμα να δουλεύει και με πραγματικούς αριθμούς
2. Αντικατέστησα τα 2 goto labels s και r που είχες με ένα do-while loop (τα goto είναι καλό να αποφεύγονται τελείως)
3. Άλλαξα την επιλογή του μενού σε απλό χαρακτήρα (ήταν ακέραιος) που διαβάζεται με getchar() και προσάρμοσα ανάλογα τους ελέγχους στο switch
4. Προσέθεσα επιλογή "8. Exit" στο κυρίως μενού
5. Έβαλα έλεγχο για μηδενικό παρανομαστή στη διαίρεση και προσέθεσα στο αποτέλεσμα της πράξης και το υπόλοιπο
Άλλαξα και τα μηνύματα που βγάζει το πρόγραμμα, αλλά αυτό δεν είναι κάτι το ουσιώδες. Τον παλιό κώδικα δεν τον έχω σβήσει, τον έχω βάλει σε σχόλια που ξεκινάνε με: // ***
Ελπίζω να σου φανεί χρήσιμος:
- Κώδικας: Επιλογή όλων
/* Copyright © 2011 UnKnOwN96.
A simple Command Line Calculator by UnKnOwN96.
This program is Free Software:
You can redistribute it and/or modify it under the terms of the GNU General Public License,
as published by the Free Software Foundation, either version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY,
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details. */
#include <stdio.h>
#include <math.h>
#include <stdlib.h> // for exit()
// Main Class
int main (void)
{
// Variables
// *** int x, y, c, r; // *** Variables set: x, y, c (choice), r (restart).
double x, y;
int stop; // boolean
// *** s: (Start)
// *** s:
stop = 0; // FALSE
do
{
// Asking kind of calculation.
puts("\n\nAVAILABLE OPTIONS\n=================");
printf ("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Percentage\n6. Power\n7. Square Root\n8. Exit\n\nYour choice: ");
// **** scanf ("%d", &c);
fflush(stdin);
// Checking the answer.
switch ( getchar() )
{
// ADDITTION // *** In case the choice is No1, do:
case '1': // *** case 1:
puts("\nAddition\n--------");
printf ("1st addendum: ");
fflush(stdin);
scanf("%lf", &x); // *** scanf ("%d", &x);
printf ("2nd addendum: ");
fflush(stdin);
scanf("%lf", &y); // *** scanf ("%d", &y);
printf ("--------\nRESULT: %g\n", x + y);
break;
// SUBTRACTION // *** In case the choice is No2, do:
case '2':
puts("\nSubtraction\n-----------");
printf ("Reducer : ");
fflush(stdin);
scanf ("%lf", &x);
printf ("Subtrahend: ");
fflush(stdin);
scanf ("%lf", &y);
printf ("-----------\nRESULT: %g\n", x - y);
break;
// MULTIPLICATION // *** In case the choice is No3, do:
case '3':
puts("\nMultiplication\n--------------");
printf ("1st number: ");
fflush(stdin);
scanf ("%lf", &x);
printf ("2nd number: ");
fflush(stdin);
scanf ("%lf", &y);
printf ("--------------\nRESULT: %g\n", x * y);
break;
// DIVISION // *** In case the choice is No4, do:
case '4':
puts("\nDivision\n--------");
printf("Divident: ");
fflush(stdin);
scanf ("%lf", &x);
do { // demand non-zero value
printf ("Divisor : ");
fflush(stdin);
scanf ("%lf", &y);
}while (y == 0.0);
printf ("--------\nRESULT: %g (quotient = %d, remainder = %d)\n",
x/y, (int)x/(int)y, (int)x%(int)y );
break;
// PERCENTAGE // *** In case the choice is No5, do:
case '5':
puts("\nPercentage\n----------");
printf ("Number: ");
fflush(stdin);
scanf ("%lf", &x);
printf ("----------\nRESULT: %g\n", x / 100);
break;
// POWER // *** In case the choice is No6, do:
case '6':
puts("\nPower\n-----");
printf ("Base : ");
fflush(stdin);
scanf ("%lf", &x);
printf ("Exponent: ");
fflush(stdin);
scanf ("%lf", &y);
printf ("-----\nRESULT: %g\n", pow(x, y));
break;
// SQUARE ROOT // *** In case the choice is No7, do:
case '7':
puts("\nSquare Root\n-----------");
printf ("Number: ");
fflush(stdin);
scanf ("%lf", &x);
printf ("-----------\nRESULT: %g\n", sqrt(x));
break;
// EXIT
case '8':
stop = 1; // TRUE
break;
// In case the user is too stupid, do:
default:
printf ("Wrong answer...\n");
break;
}
// *** r: (Restart)
// *** r:
// *** Asking if another calculation is needed.
// *** if ( !stop ) {
// *** printf ("\n\nWill you make another calculation?\n1. Yes\n2. No\n\nYour choice:\n");
// *** }
// *** scanf ("%d", &r);
} while ( !stop );
// *** Checking the answer.
// *** if (r == 1) {printf ("\nRestarting...\n"); goto s;}
// *** else if (r == 2) printf ("\nPress any key to terminate the application.");
// *** else {printf ("\nWrong number given..."); goto r;}
// *** Ending up the Program.
// *** getchar();
exit(0); // *** return 0;
}
// End