//the main quiz loop void run_question_loop() { int i; //interator //loops through each question and evaluates the answer for (i = 0; i < questions_amount; i++) { char answer[10]; /*stores answer of user*/ //asks the question by printing it on the screen print_question(questions[i]); //prints choices for user print_answers(questions[i]); //prints "Answer" printf("Answer: "); //allows user to asnwer the question, and stores the answer in the answer variable gets(answer); //stores the answer correctness result int answer_result = check_answer(answer[0], questions[i].answer); //checks the answer if (!answer_result) { //checks of the user inputted 'q' to quit check_quit(answer[0]); //make the question repeat i--; //loose a life lose_life(player); } //if this is the last question, then print "Great Job!" and the amount of lives if (i == questions_amount - 1) { printf("Great Job :)\n"); printf("You ended with: %i lives!", player.lives); } } }
int main(void) {/***********/ srand(time(NULL)); int nofquestions, nofanswers = 0, wrongs = 0, rights = 0; for (nofquestions = 1;; nofquestions++) { int a = rng(), b = rng(); for (;; nofanswers++) { print_stat(nofquestions, wrongs, rights, nofanswers); if ((nofanswers % 10 == 0) && (bad_stats(nofanswers, rights))) { interruptme = 1; extra_help(); break; } if (check_answer(a, b)) { /* if it's right ... */ right_answer(); rights++; nofanswers++; /* I add this because break make nofanswer not to increment when answer is not true */ break; /* start a new question */ } else { wrong_answer(); wrongs++; } } if (interruptme) break; } return 0; }
/* * Obtention d'un fichier distant. * 'serverfd' est un socket connecte a la machine 'servername'. * 'distname' est le nom du fichier a demander, 'localname' est le nom du * fichier resultat. */ void get_file (int serverfd, char *servername, char *distname, char *localname) { // REQUEST GET struct request request_get; request_get.kind=REQUEST_GET; strcpy(request_get.path,distname); // ANSWER GET struct answer answer_get; // Envoie de la requete au serveur if(write(serverfd,&request_get,sizeof(request_get))==-1) { perror("erreur envoi requette de get\n"); exit(EXIT_FAILURE); } //Lecture de la réponse du serveur a la requete if(read(serverfd,&answer_get,sizeof(answer_get))==-1){ perror("erreur lors de la lecture de la reponse\n"); exit(EXIT_FAILURE); } // CHECK ack GET check_answer(&answer_get); // Création d'n fichier int fd= open(localname, O_WRONLY | O_CREAT| O_EXCL, 0644); if (fd == -1) { perror("Erreur de creation du fichier\n"); exit(EXIT_FAILURE); } else { // Récuperation des données printf("Debut de la recuperation du fichier\n"); int taille=answer_get.nbbytes; copy_n_bytes(serverfd,fd,taille); printf("Recuperation terminee\n"); close(fd); } }
void Sudoku::fillBlank(){ int i,j,zero,next;//zero:現在要填的格(0~80),next:再下一格要填的 zero = findZero(); for(i=0;i<9;i++){ answerBoard[zero]=i+1; if(check_answer(zero)==false){ continue;//不用更換記號因為繼續迴圈後會往更大的數字嘗試 } else{ next = findZero(); if(next == -1 && ansCount <= 1){ ansCount++; for(i=0;i<81;i++){ keepAns[i]=answerBoard[i]; } } else if(next == -1 && ansCount > 1){ cout << "2" << endl; return; } else{ fillBlank(); } } } if(ansCount > 1){ return; } for(i=80;i>=0;i--){ if(answerBoard[i]!=sudokuIn[i]){ answerBoard[i] = 0; break; } } };
void judge_exe(char *input_file, char *stand_answer, char *output_file, char *ld_path, int max_cpu_time_limit, int max_mem_limit, struct judge_result* jr, char *path, char *argv[]) { pid_t pid ; int insyscall = 0 ; //struct user_regs_struct uregs ; struct user context ; pid = fork() ; if (pid == 0) { //child struct rlimit rl_cpu ; getrlimit(RLIMIT_CPU, &rl_cpu) ; rl_cpu.rlim_cur = max_cpu_time_limit ; setrlimit(RLIMIT_CPU, &rl_cpu) ; struct rlimit rl_fsize ; getrlimit(RLIMIT_FSIZE, &rl_fsize) ; rl_fsize.rlim_cur = max_output_size; setrlimit(RLIMIT_FSIZE, &rl_fsize) ; struct rlimit rl_as ; getrlimit(RLIMIT_AS, &rl_as) ; rl_as.rlim_cur = max_as ; setrlimit(RLIMIT_AS, &rl_as) ; freopen(input_file,"r",stdin) ; freopen(output_file,"w",stdout) ; ptrace(PTRACE_TRACEME,0,NULL,NULL) ; execv(path,argv) ; } else {//parent int status ; int first_sys_call = 1 ; ptrace(PTRACE_SYSCALL, pid, NULL, NULL) ; while (1) { wait(&status) ; if (WIFEXITED(status)) //normally terminated break; else if (WIFSTOPPED(status)) { if (WSTOPSIG(status)==SIGTRAP) { if (!insyscall) { if(!first_sys_call) insyscall = 1 ; ptrace(PTRACE_GETREGS,pid,NULL,&context.regs) ; if (!check_syscall_ok(first_sys_call,&context.regs)) { //bad system call jr->state = BAD_SYSCALL ; #ifdef __x86_64__ jr->bad_syscall_number = context.regs.orig_rax ; #elif __i386__ jr->bad_syscall_number = uregs.orig_eax ; #endif //kill process kill(pid, SIGKILL) ; wait(NULL) ; return ; } if(first_sys_call) first_sys_call = 0 ; }else insyscall = 0 ; }else if(WSTOPSIG(status) == SIGXCPU) { jr->state = TLE ; kill(pid,SIGKILL) ; wait(NULL); return ; }else if(WSTOPSIG(status) == SIGXFSZ){ jr->state = OLE ; kill(pid, SIGKILL) ; wait(NULL) ; return ; }else{ kill(pid, SIGKILL) ; wait(NULL) ; jr->state = RE ; return ; } }else if (WIFSIGNALED(status)) { jr->state = RE ; return ; }else { fprintf(stderr,"Unknown status: %d\n", status) ; return ; } ptrace(PTRACE_SYSCALL,pid,NULL,NULL); } struct rusage usage ; getrusage(RUSAGE_CHILDREN, &usage) ; if (usage.ru_maxrss > max_mem_limit) { jr->state = MLE ; return ; } //check answer int ac = -1; if (ld_path) { void *ld_handler = dlopen(ld_path,RTLD_LAZY) ; if (ld_handler) { void * sym_addr = NULL ; sym_addr = dlsym(ld_handler,"ta_check") ; if (sym_addr) { ac = ((check_function)sym_addr)(stand_answer,output_file) ; } } } if (ac<0) ac = check_answer(stand_answer,output_file) ; //remove output if (ac) { jr->state = AC ; jr->mem_kb = usage.ru_maxrss ; set_cost_time(jr,&usage) ; return ; }else { jr->state = WA ; return ; } } }
void DrillbitWin::on_pbChoice4_clicked(void) { check_answer(pbChoice4); }
int playgame(void) { int ans = 0; MC_FlashCard* fc = NULL; char buf[NET_BUF_LEN]; Uint32 timer = 0; fprintf(stderr, "\nStarting Tux, of the Math Command Line ;-)\n"); fprintf(stderr, "Waiting for other players to be ready...\n\n"); //Tell server we're ready to start: LAN_StartGame(); game_status = GAME_IN_PROGRESS; /* Start out with our "comets" empty: */ { int i; for(i = 0; i < QUEST_QUEUE_SIZE; i ++) erase_flashcard(&comets[i]); } //Begin game loop: while (game_status == GAME_IN_PROGRESS) { //Check our network messages, bailing out for fatal errors: if (game_check_msgs() == -1) return -1; //Now we check for any user responses //This function returns 1 and updates buf with input from //stdin if input is present. //If no input, it returns 0 without blocking or waiting if(read_stdin_nonblock(buf, NET_BUF_LEN)) { //While in game, these just quit the current math game: if ((strncmp(buf, "quit", 4) == 0) ||(strncmp(buf, "exit", 4) == 0) ||(strncmp(buf, "q", 1) == 0)) { game_status = GAME_OVER_ESCAPE; // end = 1; //Exit our loop in playgame() //Tell server we are quitting current game: LAN_LeaveGame(); } else { /*NOTE atoi() will return zero for any string that is not a valid int, not just '0' - should not be a big deal for our test program - DSB */ ans = atoi(buf); fc = check_answer(ans); if((fc != NULL)) { fprintf(stderr, "%s is correct!\nAwait next question...\n>\n", buf); //Tell server we answered it right: //NOTE the '-1' means we aren't tracking times for testclient LAN_AnsweredCorrectly(fc->question_id, -1); erase_flashcard(fc); print_current_quests(); } else //we got input, but not the correct answer: { int i = rand()%QUEST_QUEUE_SIZE; fprintf(stderr, "Sorry, %s is incorrect. Try again!\n", buf); // Can't tell which question was the 'wrong' one, so we // a non-empty one at random. Note that this is just for // purposes of testing LAN_NotAnsweredCorrectly() while(-1 == comets[i].question_id) i = rand()%QUEST_QUEUE_SIZE; LAN_NotAnsweredCorrectly(comets[i].question_id); print_current_quests(); } } //input wasn't any of our keywords } // Input was received Throttle(10, &timer); //so don't eat all CPU } //End of game loop switch(game_status) { case GAME_OVER_ESCAPE: fprintf(stderr, "You quit :(\n"); break; case GAME_OVER_WON: fprintf(stderr, "You won! :-)\n"); } DEBUGMSG(debug_lan, "Leaving playgame()\n"); return 1; }