/* add.c: A demonstration/test program for the GEF (General Exception- Handling Facility) library. Demonstrates the use of exception-handling. Copyright (C) 1998-2000 Bruce W. Bigby, bbigby@alum.MIT.edu 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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include static void UnhandledException(void* e) { fprintf(stderr, "Unhandled exception = %d\n", (int) e); } static void gef_configure(int* argc, char*** argv) { GEFAttr_t gefattrs; sigset_t set; GEFInitialize(argc, argv); sigemptyset(&set); sigaddset(&set, SIGALRM); GEFInitializeSignals(set); GEFAttr_Init(&gefattrs); GEFAttr_SetUnhandledException(&gefattrs, UnhandledException); GEFInitializeThread(gefattrs); } static void PrintAdditionQuestion(int number1, int number2) { printf("\t\t\t %2d\n", number1); printf("\t\t\t+%2d\n", number2); printf("\t\t\t---\n", number2); printf("\t\t\t %2s\n", "?"); } static void PrintAdditionAnswer(int number1, int number2) { printf("\t\t\t %2d\n", number1); printf("\t\t\t+%2d\n", number2); printf("\t\t\t---\n", number2); printf("\t\t\t %2d\n", number1 + number2); } static void ExecuteAdditionTest(void) { int number1; int number2; int keypress; int count; int maxtime = 5; system("clear"); printf("Welcome to Math Practice: Addition!\n"); printf("You will have %d seconds to answer each question.\n", maxtime); printf("When you are ready to begin, press 'Enter'."); (void) getchar(); do { system("clear"); printf("You have %d seconds to answer...\n\n", maxtime); number1 = 1 + (random() % 9); number2 = 5 + (random() % 5); PrintAdditionQuestion(number1, number2); putchar('\n'); count = 5; gef_enable_async_support; gef_protect_repeat_try(count) { printf("\rFor the answer, press 'Enter' (%d sec)...", count--); alarm(1); (void) getchar(); alarm(0); system("clear"); } gef_catch(e) { switch((int) e) { case SIGALRM: if (count == 0) { putchar('\a'); system("clear"); printf("Time's up! "); gef_break; } break; default: break; } } gef_end; gef_disable_async_support; printf("The answer is...\n\n"); PrintAdditionAnswer(number1, number2); putchar('\n'); printf("\rTo continue, press 'Enter'. To stop, press 'q'; then press 'Enter':"); keypress = getchar(); } while (keypress != 'q'); } int main(int argc, char** argv) { int number1; int number2; int keypress; int count; int maxtime = 5; gef_configure(&argc, &argv); srandom((unsigned int) time((void*) NULL)); ExecuteAdditionTest(); return(0); }