Zhen Liu / Mbed 2 deprecated Mini_project

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Revision:
1:9465d72db54f
Parent:
0:9e4f9c949728
Child:
2:346250dedcb6
--- a/main.cpp	Mon Mar 09 21:04:44 2015 +0000
+++ b/main.cpp	Mon Mar 09 22:04:56 2015 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "uLCD_4DGL.h"
+#include <vector>
 
 DigitalIn yes(p5);
 DigitalIn no(p6);
@@ -24,7 +25,7 @@
     lcd.filled_rectangle(0,90,100,95,RED);
     for (int i = 10; i>=0; i--) {
         mutex.lock();
-        lcd.locate(9,10);
+        lcd.locate(8,10);
         lcd.printf("00:%02d", i);
         lcd.filled_rectangle(i*10,90,(i+1)*10,95,BLACK);
         mutex.unlock();
@@ -62,24 +63,43 @@
     lcd.cls();
     lcd.printf("Ready");
     wait(2);
-    int total_no_questions = 100;
-    std::vector<std::string> question_inds;
-    for (i=0; i<total_no_questions; i++) {
-        question_ind[i] = i;
+    //===========================
+    int total_no_questions = 10;
+    vector<int> question_inds(total_no_questions);
+    for (int i=0; i<total_no_questions; i++) {
+        question_inds[i] = i;
     }
-    FILE *f = fopen("/local/test.txt", "r"); 
-    char buffer[1];
+    int randind;
+    int rand_no;
+    
+    int max_question_no = 5; // maximum_number; assert it smaller than total_no_questions!!!
+    //============================
     while (!endFlag) { 
-        fgets(question, 150, f);
-        fgets(answerStr, 150, f);
-        sprintf(buffer, "%s", answerStr);
-        if (buffer[0] != 't' && buffer[0] != 'f') {
+        if (question_no == max_question_no) {
             lcd.cls();
             lcd.printf("Quiz ends");
             wait(2);
             endFlag = true;
             break;
         }
+        randind = (rand() % total_no_questions);
+        total_no_questions--;
+        rand_no = question_inds[randind];
+        question_inds.erase(question_inds.begin() + randind);
+        char file_name[100];
+        char no_str[1];
+        char txt[4];
+        sprintf(no_str, "%d",rand_no);
+        sprintf(txt, "%s", ".txt");
+        strcpy(file_name, "/local/test");
+        strncat(file_name, no_str,10);
+        strncat(file_name, txt,10);
+        FILE *f = fopen(file_name , "r"); 
+        fgets(question, 150, f);
+        fgets(answerStr, 150, f);
+        char buffer[1];
+        sprintf(buffer, "%s", answerStr);
+        fclose(f);
         lcd.cls();
         lcd.printf("Next Question");
         wait(2);
@@ -103,6 +123,8 @@
         lcd.printf("Yes");
         lcd.locate(14,14);
         lcd.printf("No");
+        lcd.locate(0,8);
+        lcd.printf("%02d out of %02d", question_no, max_question_no);
         time = 10;
         Thread timerr(timer, (void *) timePointer);
         wait(0.1);
@@ -152,7 +174,6 @@
         response = -2;
         flag = false;
     }
-    fclose(f);
     lcd.cls();
     lcd.printf("Total Score: %2.2f", score/(question_no+0.0) * 100);
     while (true) {}