Δημοσιεύτηκε: 05 Ιουν 2011, 10:41
από UnKnown96
Για σας παιδιά!
Λιπόν μετά από αρκετό διάβασμα και εξάσκηση στη C αποφάσησα να φτιάξω ένα απλό Calculator.
Το ονόμασα CL-Calc (Command Line Calculator).
Λοιπόν εδώ είναι ο κώδικας:

Spoiler: show
Κώδικας: Επιλογή όλων

#include <stdio.h>
#include <math.h>

void main (void) {

double x, y;
char c, e=0;

puts ("Usage:\n======\n\nAddition:\t\t+ Addendum1 Addendum2\nSubtraction:\t\t- Reducer Subtrahend\nMultiplication:\t\t* Factor1 Factor2\nDivision:\t\t/ Divisor Divident\nPercentage:\t\t% Number\nPower:\t\t\t^ Base Exponent\nSquare root:\t\tr Number\nQuit:\t\t\tx");

do {

printf ("\n\n>>> ");
scanf ("%s", &c);

switch (c) {
case '+' :
scanf ("%lf %lf", &x, &y);
printf ("Total: %g", x + y);
break;

case '-' :
scanf ("%lf %lf", &x, &y);
printf ("Difference: %g", x - y);
break;

case '*' :
scanf ("%lf %lf", &x, &y);
printf ("Product: %g", x * y);
break;

case '/' :
scanf ("%lf %lf", &x, &y);
if (y == 0) printf ("Invalid divisor...");
else printf ("Quotient: %g | Remainder: %d", x / y, (int) x % (int) y);
break;

case '%' :
scanf ("%lf", &x);
printf ("Percentage: %g", x / 100);
break;

case '^' :
scanf ("%lf %lf", &x, &y);
printf ("Result: %g", pow (x, y) );
break;

case 'r' :
scanf ("%lf", &x);
printf ("Result: %g", sqrt (x) );
break;

case 'x' :
e=1;
break;

default:
printf ("Wrong option chosen...");
break;
}
}

while (!e);
}


Και ένα από τον MigF1.

Spoiler: show
Κώδικας: Επιλογή όλων

Ελέξτε εδώ:
http://ideone.com/Ce88n


Κάντε το compile με την εντολή gcc -lm <ονομα αρχείου>.c