/* * inc.c * * Copyright (C) 2007 Pau Espin Pedrol (Sharwin_F) * * 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 * (at your option) 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see */ #include #include #include #include #include GtkWidget *crea_hbox(GtkWidget *parent) { GtkWidget *box = gtk_hbox_new(0,1); gtk_container_add(GTK_CONTAINER (parent), box); gtk_widget_show(box); return box; } gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data ) { gtk_main_quit(); return FALSE; } static int CalculaX(double a, double b, double c, double* x1, double* x2) { double dins = (b*b) + (-4)*a*c; if(dins<0) { *x1 = dins; return 0; } else if(dins==0) { *x1 = *x2 = ( -1*b ) / ( 2*a ); return 1; } double arrel = sqrt( dins ); *x1 = ( -1*b + arrel ) / ( 2*a ); *x2 = ( -1*b - arrel ) / ( 2*a ); return 2; } static char* formata_double(double lf) { char *str = malloc( 32 ); sprintf(str, "%lf", lf); int i , j; i = j = strlen(str)-1; for (i; i >= 0; i--) if (str[i]=='.') { str[i] = ','; break; //recorrem array agafant la posicio del punt = i } if(i!=0) //hi ha punt en algun lloc (i) for (j; j >= i && str[j]=='0'; j--) { str[j] = '\0'; if(j!=0 && str[j-1]==',') //treiem punt si no hi han decimals { str[j-1] = '\0'; break; } } return str; } void Resol( GtkWidget** dades ) { double A, B, C; double x1, x2; char *s1, *s2; A = atof( gtk_entry_get_text (GTK_ENTRY (dades[0])) ); B = atof( gtk_entry_get_text (GTK_ENTRY (dades[1])) ); C = atof( gtk_entry_get_text (GTK_ENTRY (dades[2])) ); int nX = CalculaX(A, B, C, &x1, &x2); s1 = formata_double(x1); s2 = formata_double(x2); /* Imprimim resultat */ char strresult[90]; if(nX==2) sprintf(strresult, "X1 = %s;\n\nX2 = %s;", s1, s2); else if (nX==1) sprintf(strresult, "\nX = %s;\n", s1); else sprintf(strresult, "\nArrel negativa! [ sqrt(%s) ]\n No té solució", s1); //escribim l'interior l'arrel gtk_label_set_text( GTK_LABEL (dades[3]), strresult ); free(s1); free(s2); }