/* Ficha Números primos */ #include #include #include #define _(String) gettext (String) gchar *numero; gint num_int; gint k=1; GArray *numeros; gdouble cx=10.0; gdouble cy=20.0; GString *etiqueta, *etiqueta2; gint digitos=1; gchar *texto, *texto2; void crea_arrays() { gint i, temp=0; numeros = g_array_new (TRUE, FALSE, sizeof (gint)); g_array_append_val (numeros, temp); g_array_append_val (numeros, temp); for (i=2; i<=num_int; i=i+1) g_array_append_val (numeros, i); } int num_digitos(int entero) { if (entero>99) return 3; else if (entero>9) return 2; else return 1; } int main (int argc, char *argv[]) { setlocale (LC_ALL, ""); bindtextdomain ("gmateso",NULL); textdomain ("gmateso"); GtkWidget *ventana; /*declaracion de widgets*/ GtkWidget *vbox1; GtkWidget *label1, *labelb1; GtkWidget *hbox1; GtkWidget *label3, *label4; GtkWidget *entry1; GtkWidget *boton1, *boton2, *boton3; GtkWidget *hbox3; GtkWidget *label6; GtkWidget *hseparator1, *hseparator2, *hseparator3; GtkWidget *scrolled_window, *scrolled_window2; GtkWidget *canvas; GnomeCanvasGroup *root; GnomeCanvasPoints *points; GtkTextBuffer *bufer; GtkTextTagTable *tags; GtkTextView *view; GtkTextTag *negrita, *cursiva, *color_azul; GtkTextTag *color_rojo, *subrayado; GtkTextTag *fuente_11; GtkTextIter start, end; gnome_init ("f113", "0.1", argc, argv); ventana = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request (GTK_WIDGET (ventana), 700, 540); gtk_window_set_resizable (GTK_WINDOW (ventana), TRUE); gtk_window_set_title (GTK_WINDOW (ventana), _("gMatESO - Números primos")); g_signal_connect (G_OBJECT (ventana), "delete_event", G_CALLBACK (exit), NULL); vbox1 = gtk_vbox_new (FALSE, 0); gtk_widget_show (vbox1); gtk_container_add (GTK_CONTAINER (ventana), vbox1); gtk_container_set_border_width(GTK_CONTAINER (vbox1), 5); label1 = gtk_label_new ("Números primos"); gtk_widget_show (label1); gtk_box_pack_start (GTK_BOX (vbox1), label1, FALSE, FALSE, 1); gtk_widget_set_size_request(GTK_WIDGET (label1), 780, 30); gtk_label_set_markup (GTK_LABEL(label1), _(" Números primos ")); hseparator1 = gtk_hseparator_new (); gtk_widget_show (hseparator1); gtk_box_pack_start (GTK_BOX (vbox1), hseparator1, FALSE, FALSE, 1); scrolled_window2 = gtk_scrolled_window_new (NULL, NULL); gtk_widget_show (scrolled_window2); gtk_box_pack_start (GTK_BOX (vbox1), scrolled_window2, TRUE, TRUE, 1); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window2), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); tags = gtk_text_tag_table_new(); bufer = gtk_text_buffer_new(tags); view = GTK_TEXT_VIEW(gtk_text_view_new_with_buffer(bufer)); gtk_text_view_set_left_margin (view, 5); gtk_container_add (GTK_CONTAINER (scrolled_window2), GTK_WIDGET(view)); g_object_set(view, "wrap-mode", GTK_WRAP_WORD, NULL); gtk_widget_show (GTK_WIDGET(view)); negrita = gtk_text_buffer_create_tag(bufer, "negrita", "weight", 500, NULL); cursiva = gtk_text_buffer_create_tag(bufer, "cursiva", "style", PANGO_STYLE_ITALIC, NULL); fuente_11 = gtk_text_buffer_create_tag(bufer, "fuente_11", "size", 11 * PANGO_SCALE, NULL); color_rojo = gtk_text_buffer_create_tag(bufer, "color_rojo", "foreground", "#ff0000", NULL); color_azul = gtk_text_buffer_create_tag(bufer, "color_azul", "foreground", "#0000bb", NULL); subrayado = gtk_text_buffer_create_tag(bufer, "subrayado", "underline", PANGO_UNDERLINE_SINGLE, NULL); gtk_text_buffer_set_text(bufer, "", -1); gtk_text_buffer_get_selection_bounds (bufer, &start, &end); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" Un número es "), -1, "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name (bufer, &start, _("primo"), -1, "negrita","color_rojo", "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" cuando tenga solamente dos divisores (el 1 y el propio número).\n - Los números que no son primos se llaman"), -1, "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" compuestos "), -1, "negrita","fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _("y tienen más de dos divisores.\n - El "), -1, "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _("número 1"), -1, "negrita","fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" no es ni primo ni compuesto\n - Por tanto el primer número primo es el "), -1, "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, "2", -1, "negrita","fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _("\n\n Para calcular los primeros números primos"), -1, "fuente_11","subrayado", "cursiva", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" se usa la "), -1, "fuente_11","cursiva", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _("criba de ERASTÓTENES"), -1, "negrita","fuente_11", "cursiva", "color_azul", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" que consiste en ir "), -1, "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _("tachando"), -1, "negrita","fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" los múltiplos de 2 (excepto el 2), los múltiplos de 3 (excepto el 3), los de 5, etc. \n Terminamos cuando hayamos sobrepasado la mitad de los números. \n Los que queden "), -1, "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _("sin tachar"), -1, "negrita","fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _(" son los "), -1, "fuente_11", NULL); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, _("primos"), -1, "negrita","fuente_11", NULL); gtk_text_view_set_editable (view, FALSE); hseparator2 = gtk_hseparator_new (); gtk_widget_show (hseparator2); gtk_box_pack_start (GTK_BOX (vbox1), hseparator2, FALSE, FALSE, 1); hbox1 = gtk_hbox_new (FALSE, 0); gtk_widget_show (hbox1); gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 1); label3 = gtk_label_new ("Ejemplo: "); gtk_widget_show (label3); gtk_box_pack_start (GTK_BOX (hbox1), label3, FALSE, FALSE, 1); gtk_label_set_markup (GTK_LABEL(label3), _(" Ejemplo: ")); label4 = gtk_label_new (_("Calculamos los primos menores a:")); gtk_widget_show (label4); gtk_box_pack_start (GTK_BOX (hbox1), label4, FALSE, FALSE, 1); gtk_label_set_markup (GTK_LABEL(label4), _(" Calculamos los primos menores a: ")); entry1 = gtk_entry_new_with_max_length (3); gtk_entry_set_width_chars(GTK_ENTRY(entry1), 3); gtk_box_pack_start (GTK_BOX (hbox1), entry1, FALSE, FALSE, 1); gtk_widget_show (entry1); gtk_widget_grab_focus (entry1); boton1 = gtk_button_new(); gtk_widget_show (boton1); gtk_box_pack_start (GTK_BOX (hbox1), boton1, FALSE, FALSE, 3); labelb1=gtk_label_new(""); gtk_container_add (GTK_CONTAINER (boton1), labelb1); gtk_label_set_markup (GTK_LABEL(labelb1), _("Calcular")); boton2=gtk_button_new_from_stock("gtk-go-forward"); gtk_box_pack_start (GTK_BOX (hbox1), boton2, FALSE, FALSE, 3); boton3=gtk_button_new_from_stock("gtk-goto-last"); gtk_box_pack_start (GTK_BOX (hbox1), boton3, FALSE, FALSE, 3); scrolled_window = gtk_scrolled_window_new (NULL, NULL); gtk_widget_show (scrolled_window); gtk_box_pack_start (GTK_BOX (vbox1), scrolled_window, TRUE, TRUE, 1); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_widget_set_size_request(GTK_WIDGET (scrolled_window), -1, 200); canvas = gnome_canvas_new_aa(); gtk_container_add(GTK_CONTAINER(scrolled_window), canvas); root = gnome_canvas_root(GNOME_CANVAS(canvas)); gnome_canvas_set_center_scroll_region (GNOME_CANVAS(canvas), FALSE); gnome_canvas_set_scroll_region (GNOME_CANVAS(canvas),0.0,0.0,660.0,2515.0); hseparator3 = gtk_hseparator_new (); gtk_widget_show (hseparator3); gtk_box_pack_start (GTK_BOX (vbox1), hseparator3, FALSE, FALSE, 1); hbox3 = gtk_hbox_new (FALSE, 0); gtk_widget_show (hbox3); gtk_box_pack_start (GTK_BOX (vbox1), hbox3, FALSE, FALSE, 1); label6 = gtk_label_new (""); gtk_widget_show (label6); gtk_box_pack_start (GTK_BOX (hbox3), label6, FALSE, FALSE, 1); gtk_label_set_markup (GTK_LABEL(label6), _(" AYUDA: Teclea un número y pulsa 'CALCULAR' ")); void avanza_scroll() { gtk_text_view_set_editable (view, FALSE); gtk_text_buffer_get_iter_at_offset( bufer, &start, -1 ); gtk_text_buffer_place_cursor( bufer, &start ); gtk_text_view_scroll_mark_onscreen (view, gtk_text_buffer_get_insert(bufer)); } void escribe_numeros() { gint i; cx=25.0; cy=20.0; gchar *temporal; etiqueta=g_string_new(NULL); for (i=1; i<=num_int; i++) { temporal=numero; g_ascii_dtostr(temporal, 6, i); g_string_append(etiqueta, temporal); if (((i-1)%10==0)&&(i>1)) { cy=cy+25.0; cx=25.0; } else if ((i>1)&&((i-1)%10!=0)) cx=cx+60; digitos=num_digitos(i); switch (digitos) { case 1: cx=cx+20; break; case 2: cx=cx+10; break; } gnome_canvas_item_new(root, gnome_canvas_text_get_type(), "font", "monospace 14", "markup", etiqueta->str, "fill_color", "black", "x", cx, "y", cy, "anchor", GTK_ANCHOR_WEST, NULL); switch (digitos) { case 1: cx=cx-20; break; case 2: cx=cx-10; break; } g_string_erase(etiqueta, 0, -1); if ((i%10==0)&&(i<111)) gnome_canvas_update_now (GNOME_CANVAS(canvas)); } gnome_canvas_item_new(root, gnome_canvas_ellipse_get_type(), "x1", 38.0, "y1", 10.0, "x2", 60.0, "y2", 32.0, "fill_color_rgba", 0xff000077, "outline_color", "black", NULL); } void tacha_numero(gint n) { gint x1,x2,y1,y2; x1=22+((n%10)-1)*60; if ((n%10)==0) x1=22+9*60; x2=x1+40; y1=22+(n/10)*25; if ((n%10)==0) y1=y1-25; y2=y1; points = gnome_canvas_points_new(2); /* 2 puntos */ points->coords[0] = x1; points->coords[1] = y1; points->coords[2] = x2; points->coords[3] = y2; gnome_canvas_item_new(root, gnome_canvas_line_get_type(), "points", points, "fill_color_rgba", 0xff0000f0, "width_units", 3.0, NULL); gnome_canvas_points_unref(points); } void Aceptar() { gchar num_letra[5]; gint i; gboolean primera_vez=TRUE; gchar *temporal=" "; gtk_entry_set_editable (GTK_ENTRY(entry1), FALSE); numero = gtk_editable_get_chars(GTK_EDITABLE(entry1),0,-1); /*capturamos numero */ for (i=0; i<5; i=i+1) num_letra[i] = numero[i]; num_int = g_ascii_strtod(num_letra,NULL); gnome_canvas_item_new(root, gnome_canvas_rect_get_type(), "x1", 0.0, /*cordenas de la */ "y1", 0.0, /*esquina sup-izq */ "x2", 660.0, /*ancho */ /*coord de la esquina inf-der*/ "y2", 2510.0, /*alto */ "fill_color_rgba", 0xefffefff, /*color de relleno*/ "outline_color_rgba", 0x000000ff, /*color del borde */ "width_units", 1.0, /*tamaño del borde*/ NULL); if (num_int>1) { etiqueta2=g_string_new(NULL); temporal=_(" En primer lugar escribimos todos los números desde el 1 hasta el "); g_string_append(etiqueta2, temporal); g_string_append(etiqueta2, numero); g_string_append(etiqueta2, _("\n Excluimos el número 1 (que no es ni primo ni compuesto)")); g_string_append(etiqueta2, _("\n")); texto2= g_string_free (etiqueta2, FALSE); gtk_text_buffer_set_text(bufer, "", -1); gtk_text_buffer_get_selection_bounds (bufer, &start, &end); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, texto2, -1, "fuente_11", NULL); if (primera_vez==FALSE) g_array_free (numeros,TRUE); primera_vez=FALSE; k=1; crea_arrays(); escribe_numeros(); gtk_widget_show(boton2); gtk_widget_show(boton3); gtk_label_set_markup (GTK_LABEL(label6), _(" AYUDA: Pulsa Adelante para ir paso a paso. Pulsa Último para llegar hasta el final ")); gtk_widget_set_sensitive(boton1, FALSE); gtk_widget_grab_focus (boton2); } else { GtkWidget *dialog; dialog = gtk_message_dialog_new (GTK_WINDOW (ventana), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, _(" Introduce un número mayor que 1 ")); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); gtk_entry_set_editable (GTK_ENTRY(entry1), TRUE); } /* fin de else */ } /* fin de funcion callback*/ void fin() { numero = gtk_editable_get_chars(GTK_EDITABLE(entry1),0,-1); gchar *temporal=" "; etiqueta=g_string_new(NULL); temporal=_("\n\n FINAL. Los números primos entre 1 y "); g_string_append(etiqueta2, temporal); temporal= numero; g_string_append(etiqueta2, temporal); temporal=_(" son los que no están tachados: \n"); g_string_append(etiqueta2, temporal); gtk_text_buffer_get_selection_bounds (bufer, &start, &end); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, etiqueta2->str, -1, "fuente_11", "cursiva", NULL); avanza_scroll(); g_string_erase(etiqueta2, 0, -1); temporal=""; gint i; for (i=0; i<=num_int; i=i+1) if (0!= g_array_index(numeros, gint, i)) { temporal=numero; g_ascii_dtostr(temporal, 6, g_array_index(numeros, gint, i)); g_string_append(etiqueta2, temporal); temporal=" "; g_string_append(etiqueta2, temporal); } texto2 = g_string_free (etiqueta2, FALSE); gtk_text_buffer_get_selection_bounds (bufer, &start, &end); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, texto2, -1, "fuente_11", "negrita", "color_azul", NULL); avanza_scroll(); gtk_widget_hide(boton2); gtk_widget_hide(boton3); gtk_label_set_text (GTK_LABEL(label6),_("AYUDA: Teclea un número y pulsa 'CALCULAR'")); gtk_entry_set_editable (GTK_ENTRY(entry1), TRUE); } void tacha_multiplos () { gchar *temporal=" "; gint i, temp=0; if (k<(num_int/2)) { for (i=k+1; i<=num_int; i++) if (0!=g_array_index(numeros, gint, i)) { k=g_array_index(numeros, gint, i); break; } for (i=2*k; i<=num_int; i=i+k) tacha_numero(i); for (i=2*k; i<=num_int; i=i+k) { g_array_remove_index(numeros, i); g_array_insert_val(numeros, i, temp); } etiqueta2=g_string_new(NULL); temporal=_("\n Ahora tachamos los múltiplos de "); g_string_append(etiqueta2, temporal); temporal=numero; g_ascii_dtostr(temporal, 6, k); g_string_append(etiqueta2, temporal); temporal=" (excepto el "; g_string_append(etiqueta2, temporal); temporal=numero; g_ascii_dtostr(temporal, 6, k); g_string_append(etiqueta2, temporal); temporal=") "; g_string_append(etiqueta2, temporal); texto2 = g_string_free (etiqueta2, FALSE); gtk_text_buffer_get_selection_bounds (bufer, &start, &end); gtk_text_buffer_insert_with_tags_by_name(bufer, &start, texto2, -1, "fuente_11", NULL); avanza_scroll(); } else fin(); } void callback (GtkWidget *widget, gchar data) { Aceptar(); } void callback2 (GtkWidget *widget, gchar data) { tacha_multiplos(); } void callback3 (GtkWidget *widget, gchar data) { while (kstr, length, position); g_signal_handlers_unblock_by_func (editable, (gpointer) insert_text_handler1, data); g_signal_stop_emission_by_name (editable, "insert_text"); g_string_free (result, TRUE); } gboolean on_entry1_key_press_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data) { if ((event->keyval == 0xFF0D)||(event->keyval == 0xFF8D)) Aceptar(); return FALSE; } g_signal_connect ((gpointer) entry1, "key_press_event", G_CALLBACK (on_entry1_key_press_event), NULL); gtk_signal_connect (GTK_OBJECT (entry1), "insert_text", GTK_SIGNAL_FUNC (insert_text_handler1), NULL); gtk_signal_connect (GTK_OBJECT (boton1), "clicked", GTK_SIGNAL_FUNC (callback), NULL); gtk_signal_connect (GTK_OBJECT (boton2), "clicked", GTK_SIGNAL_FUNC (callback2), NULL); gtk_signal_connect (GTK_OBJECT (boton3), "clicked", GTK_SIGNAL_FUNC (callback3), NULL); GString *carpeta; gchar *texto; carpeta=g_string_new(NULL); g_string_append (carpeta, g_get_home_dir ()); g_string_append (carpeta, "/gmateso/estilos.rc"); texto = g_string_free (carpeta, FALSE); gtk_rc_parse(texto); gtk_widget_show_all (ventana); gtk_widget_hide(boton2); gtk_widget_hide(boton3); gtk_main (); return 0; }