Switch - Case ">" ve "<" operatörlerini desteklemiyor (javascript hariç) switch case kullanabilmek için -e kadar mantığı katan "..." operatörünü ve limits kütüphanesini kullandım. Ne kadar doğru çalıştığını test edemedim ancak mantığını çıkarabilirsiniz diye düşünüyorum umarım bir şeyler öğrenebilirsiniz.
[CODE lang="c" title="Kök Hesaplama"]#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <locale.h>
// GLOBAL VARIABLES //
double a;
double b;
double c;
double delta;
int delta_as_int;
double kok1;
double kok2;
// GLOBAL VARIABLES //
// FUNCTION PROTOTYPES //
void Get_Variables();
void Calculate_Delta();
// FUNCTION PROTOTYPES //
// FUNCTION DEFINITIONS //
void Get_Variables()
{
puts("Denklemimiz : y= ax^2 +bx+c \n");
puts("Lütfen a için bir tamsayı giriniz: ");
scanf("%lf",&a);
puts("\n");
puts("Lütfen b için bir tamsayı giriniz: ");
scanf("%lf",&b);
puts("\n");
puts("Lütfen c için bir tamsayı giriniz: ");
scanf("%lf",&c);
puts("\n");
}
void Calculate_Delta()
{
puts("Delta aşağıdaki formül ile hesaplanacak ve deltanın durumuna göre aşağıdaki gibi kökler bulunarak ekranda gösterilecek:\n");
puts("delta= b2-4ac\n");
delta = pow(b,2) - (4 * a * c);
delta_as_int = (int) delta;
switch(delta_as_int)
{
case INT_MIN ... -1: // Less than zero //
puts("Denklemin gerçel kökü yoktur.");
exit(1);
case 0: // absolute zero //
kok1 = -b / (2 * a);
kok2 = kok1;
printf("Kökler: %f , %f", kok1,kok2);
exit(1);
case 1 ... INT_MAX: // bigger than zero //
kok1 = -b / (2 * a);
kok2 = b / (2 * a);
printf("Kökler: %f , %f", kok1,kok2);
exit(1);
default: // if they all failed throw an error and exit with code 0 //
puts("\n\n\n !!ERROR!! \n\n\n");
exit(0);
}
}
// FUNCTION DEFINITIONS //
int main()
{
setlocale(LC_ALL, "Turkish"); // Set language to turkish //
Get_Variables(); // get variables requested //
Calculate_Delta(); // process those variables and print the results //
return 0; // you should not even reach here but some IDE's alergic to not having one so here it is //
}
[/CODE]