2016 ソフトウェア設計及び演習用の班Wiki

g01::クイズモジュール

#include <gtk/gtk.h> #include <stdlib.h> #include <time.h> #include <stdio.h>

GtkWidget *start(GtkWidget *); GtkWidget *quiz1(GtkWidget *); GtkWidget *Ans1(GtkWidget *); GtkWidget *quiz2(GtkWidget *); GtkWidget *Ans2(GtkWidget *); GtkWidget *quiz3(GtkWidget *); GtkWidget *Ans3(GtkWidget *); GtkWidget *quiz4(GtkWidget *); GtkWidget *Ans4(GtkWidget *); GtkWidget *quiz5(GtkWidget *); GtkWidget *Ans5(GtkWidget *);

GtkWidget *image;

/*マウスから選択された値*/ int sentaku;

/*問題の答え*/ int seikai;

/*正解数・進化するときの値(0〜5)*/ int sinka = 0;

int mondaisu1, mondaisu2, mondaisu3, mondaisu4, mondaisu5;

/*マウスをクリックした時に呼び出す関数*/ static gboolean cb_mouse_perfect_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data) { /*縦の範囲指定*/ if(event->y >= 613 && event->y <=716){

/*1*/ if(event->x >= 41 && event->x <= 353){ sentaku = 1; gtk_main_quit(); }

/*2*/ if(event->x >= 396 && event->x <= 725){ sentaku = 2; gtk_main_quit(); }

/*3*/ if(event->x >= 776 && event->x <= 1108){ sentaku = 3; gtk_main_quit(); } } }

/*main文*/ int main(int argc, char *argv[]) { gtk_init(&argc, &argv);

GtkWidget *window;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "たんいっち"); gtk_widget_set_size_request (window, 1200, 800);

gtk_widget_show_all(start(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(quiz1(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(Ans1(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(quiz2(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(Ans2(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(quiz3(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(Ans3(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(quiz4(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(Ans4(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(quiz5(window)); gtk_main(); gtk_widget_destroy(image);

gtk_widget_show_all(Ans5(window)); gtk_main();

return 0; }

/*最初の画面*/ GtkWidget *start(GtkWidget *window) { image = gtk_image_new_from_file("tyukan.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK); g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image);

return window; }

/*問題1*/ GtkWidget *quiz1(GtkWidget *window) { /*最初の画面でのクリックを初期化*/ sentaku = 0;

/*mondai1をランダムで。*/ srandPlugin not found.; mondaisu1 = GetRandom(1,15);

if (mondaisu1 == 1) { image = gtk_image_new_from_file("mondai1.png"); seikai = 1; }

if (mondaisu1 == 2) { image = gtk_image_new_from_file("mondai2.png"); seikai = 2; }

if (mondaisu1 == 3) { image = gtk_image_new_from_file("mondai3.png"); seikai = 3; }

if (mondaisu1 == 4) { image = gtk_image_new_from_file("mondai4.png"); seikai = 1; }

if (mondaisu1 == 5) { image = gtk_image_new_from_file("mondai5.png"); seikai = 3; }

if (mondaisu1 == 6) { image = gtk_image_new_from_file("mondai6.png"); seikai = 1; }

if (mondaisu1 == 7) { image = gtk_image_new_from_file("mondai7.png"); seikai = 3; }

if (mondaisu1 == 8) { image = gtk_image_new_from_file("mondai8.png"); seikai = 1; }

if (mondaisu1 == 9) { image = gtk_image_new_from_file("mondai9.png"); seikai = 2; }

if (mondaisu1 == 10) { image = gtk_image_new_from_file("mondai10.png"); seikai = 3; }

if (mondaisu1 == 11) { image = gtk_image_new_from_file("mondai11.png"); seikai = 2; }

if (mondaisu1 == 12) { image = gtk_image_new_from_file("mondai12.png"); seikai = 3; }

if (mondaisu1 == 13) { image = gtk_image_new_from_file("mondai13.png"); seikai = 2; }

if (mondaisu1 == 14) { image = gtk_image_new_from_file("mondai14.png"); seikai = 1; }

if (mondaisu1 == 15) { image = gtk_image_new_from_file("mondai15.png"); seikai = 1; }

/*windowに貼り付ける*/ gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image);

return window; }

/*問題1の正誤判定*/ GtkWidget *Ans1(GtkWidget *window) {

/*正解*/ if (sentaku == seikai) { image = gtk_image_new_from_file("qa1.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = 1; //sinkaに1を代入 }

/*不正解*/ if (sentaku != seikai) { image = gtk_image_new_from_file("qa2.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = 0; //sinkaに0を代入 }

/*マウスのクリックの初期化*/ sentaku = 0;

return window; }

/*問題2*/ GtkWidget *quiz2(GtkWidget *window) { do { srandPlugin not found.; mondaisu2 = GetRandom(1,15);

if (mondaisu2 == 1) { image = gtk_image_new_from_file("mondai1.png"); seikai = 1; }

if (mondaisu2 == 2) { image = gtk_image_new_from_file("mondai2.png"); seikai = 2; }

if (mondaisu2 == 3) { image = gtk_image_new_from_file("mondai3.png"); seikai = 3; }

if (mondaisu2 == 4) { image = gtk_image_new_from_file("mondai4.png"); seikai = 1; }

if (mondaisu2 == 5) { image = gtk_image_new_from_file("mondai5.png"); seikai = 3; }

if (mondaisu2 == 6) { image = gtk_image_new_from_file("mondai6.png"); seikai = 1; }

if (mondaisu2 == 7) { image = gtk_image_new_from_file("mondai7.png"); seikai = 3; }

if (mondaisu2 == 8) { image = gtk_image_new_from_file("mondai8.png"); seikai = 1; }

if (mondaisu2 == 9) { image = gtk_image_new_from_file("mondai9.png"); seikai = 2; }

if (mondaisu2 == 10) { image = gtk_image_new_from_file("mondai10.png"); seikai = 3; }

if (mondaisu2 == 11) { image = gtk_image_new_from_file("mondai11.png"); seikai = 2; }

if (mondaisu2 == 12) { image = gtk_image_new_from_file("mondai12.png"); seikai = 3; }

if (mondaisu2 == 13) { image = gtk_image_new_from_file("mondai13.png"); seikai = 2; }

if (mondaisu2 == 14) { image = gtk_image_new_from_file("mondai14.png"); seikai = 1; }

/*問題が被らないようにdo〜while文をつける*/ }while (mondaisu2 == mondaisu1);

gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sentaku = sentaku;

return window; }

GtkWidget *Ans2(GtkWidget *window) { if (sentaku == seikai) { image = gtk_image_new_from_file("qa1.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 1; } if (sentaku != seikai) { image = gtk_image_new_from_file("qa2.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 0; } //sentaku_light = sentaku; sentaku = 0; return window;

}

GtkWidget *quiz3(GtkWidget *window) { do { srandPlugin not found.; mondaisu3 = GetRandom(1,15);

if (mondaisu3 == 1) { image = gtk_image_new_from_file("mondai1.png"); seikai = 1; }

if (mondaisu3 == 2) { image = gtk_image_new_from_file("mondai2.png"); seikai = 2; }

if (mondaisu3 == 3) { image = gtk_image_new_from_file("mondai3.png"); seikai = 3; }

if (mondaisu3 == 4) { image = gtk_image_new_from_file("mondai4.png"); seikai = 1; }

if (mondaisu3 == 5) { image = gtk_image_new_from_file("mondai5.png"); seikai = 3; }

if (mondaisu3 == 6) { image = gtk_image_new_from_file("mondai6.png"); seikai = 1; }

if (mondaisu3 == 7) { image = gtk_image_new_from_file("mondai7.png"); seikai = 3; }

if (mondaisu3 == 8) { image = gtk_image_new_from_file("mondai8.png"); seikai = 1; }

if (mondaisu3 == 9) { image = gtk_image_new_from_file("mondai9.png"); seikai = 2; }

if (mondaisu3 == 10) { image = gtk_image_new_from_file("mondai10.png"); seikai = 3; }

if (mondaisu3 == 11) { image = gtk_image_new_from_file("mondai11.png"); seikai = 2; }

if (mondaisu3 == 12) { image = gtk_image_new_from_file("mondai12.png"); seikai = 3; }

if (mondaisu3 == 13) { image = gtk_image_new_from_file("mondai13.png"); seikai = 2; }

if (mondaisu3 == 14) { image = gtk_image_new_from_file("mondai14.png"); seikai = 1; }

if (mondaisu3 == 15) { image = gtk_image_new_from_file("mondai15.png"); seikai = 1; }

}while (mondaisu3 mondaisu1 && mondaisu3 mondaisu2);

gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image);

return window; }

GtkWidget *Ans3(GtkWidget *window) { if (sentaku == seikai) { image = gtk_image_new_from_file("qa1.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 1; } if (sentaku != seikai) { image = gtk_image_new_from_file("qa2.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 0; }

sentaku = 0; return window;

}

GtkWidget *quiz4(GtkWidget *window) { do{ srandPlugin not found.; mondaisu4 = GetRandom(1,15);

if (mondaisu4 == 1) { image = gtk_image_new_from_file("mondai1.png"); seikai = 1; }

if (mondaisu4 == 2) { image = gtk_image_new_from_file("mondai2.png"); seikai = 2; }

if (mondaisu4 == 3) { image = gtk_image_new_from_file("mondai3.png"); seikai = 3; }

if (mondaisu4 == 4) { image = gtk_image_new_from_file("mondai4.png"); seikai = 1; }

if (mondaisu4 == 5) { image = gtk_image_new_from_file("mondai5.png"); seikai = 3; }

if (mondaisu4 == 6) { image = gtk_image_new_from_file("mondai6.png"); seikai = 1; }

if (mondaisu4 == 7) { image = gtk_image_new_from_file("mondai7.png"); seikai = 3; }

if (mondaisu4 == 8) { image = gtk_image_new_from_file("mondai8.png"); seikai = 1; }

if (mondaisu4 == 9) { image = gtk_image_new_from_file("mondai9.png"); seikai = 2; }

if (mondaisu4 == 10) { image = gtk_image_new_from_file("mondai10.png"); seikai = 3; }

if (mondaisu4 == 11) { image = gtk_image_new_from_file("mondai11.png"); seikai = 2; }

if (mondaisu4 == 12) { image = gtk_image_new_from_file("mondai12.png"); seikai = 3; }

if (mondaisu4 == 13) { image = gtk_image_new_from_file("mondai13.png"); seikai = 2; }

if (mondaisu4 == 14) { image = gtk_image_new_from_file("mondai14.png"); seikai = 1; }

if (mondaisu4 15) { image = gtk_image_new_from_file("mondai15.png"); seikai = 1; } }while (mondaisu4 mondaisu1 && mondaisu4 mondaisu2 && mondaisu4 mondaisu3);

gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image);

return window; }

GtkWidget *Ans4(GtkWidget *window) { if (sentaku == seikai) { image = gtk_image_new_from_file("qa1.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 1; } if (sentaku != seikai) { image = gtk_image_new_from_file("qa2.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 0; }

sentaku = 0; return window;

}

GtkWidget *quiz5(GtkWidget *window) { do{ srandPlugin not found.; mondaisu5 = GetRandom(1,15);

if (mondaisu5 == 1) { image = gtk_image_new_from_file("mondai1.png"); seikai = 1; }

if (mondaisu5 == 2) { image = gtk_image_new_from_file("mondai2.png"); seikai = 2; }

if (mondaisu5 == 3) { image = gtk_image_new_from_file("mondai3.png"); seikai = 3; }

if (mondaisu5 == 4) { image = gtk_image_new_from_file("mondai4.png"); seikai = 1; }

if (mondaisu5 == 5) { image = gtk_image_new_from_file("mondai5.png"); seikai = 3; }

if (mondaisu5 == 6) { image = gtk_image_new_from_file("mondai6.png"); seikai = 1; }

if (mondaisu5 == 7) { image = gtk_image_new_from_file("mondai7.png"); seikai = 3; }

if (mondaisu5 == 8) { image = gtk_image_new_from_file("mondai8.png"); seikai = 1; }

if (mondaisu5 == 9) { image = gtk_image_new_from_file("mondai9.png"); seikai = 2; }

if (mondaisu5 == 10) { image = gtk_image_new_from_file("mondai10.png"); seikai = 3; }

if (mondaisu5 == 11) { image = gtk_image_new_from_file("mondai11.png"); seikai = 2; }

if (mondaisu5 == 12) { image = gtk_image_new_from_file("mondai12.png"); seikai = 3; }

if (mondaisu5 == 13) { image = gtk_image_new_from_file("mondai13.png"); seikai = 2; }

if (mondaisu5 == 14) { image = gtk_image_new_from_file("mondai14.png"); seikai = 1; }

if (mondaisu5 15) { image = gtk_image_new_from_file("mondai15.png"); seikai = 1; } }while (mondaisu5 mondaisu1 && mondaisu5 mondaisu2 && mondaisu5 mondaisu3 && mondaisu5 == mondaisu4);

gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); printf("選択 %d\n", sentaku);

return window; }

GtkWidget *Ans5(GtkWidget *window) { if (sentaku == seikai) { image = gtk_image_new_from_file("qa1.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 1; } if (sentaku != seikai) { image = gtk_image_new_from_file("qa2.png"); gtk_container_add(GTK_CONTAINER(window), image);

gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK(cb_mouse_perfect_press), image); sinka = sinka + 0; }

sentaku = 0; return window;

}

int GetRandom(int min, int max) { return min + (int)(rand()*(max-min+1.0)/(1.0+RAND_MAX)); }


最終更新日:2016/07/15 16:52:38