1. 程式人生 > >幫二柱子解決一個煩惱

幫二柱子解決一個煩惱

要求:1、實現線上答題。

           2、答題結束後,可以判斷對錯。

           3、並將錯題的結果儲存起來。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#define N 10
typedef struct _ti {
    int a, b;
    int op;
    int result;
    int input;
    int rw;
}ti;
ti timu[N];
int fen;
int flag;
int mulu()
{
    int s;
    while(1) {
        printf("\t\t歡迎進入考試系統\n\n");
        printf("1隨機出題\t\t\t\t");
        printf("2開始考試\n");
        printf("3試卷評分\t\t\t\t");
        printf("4顯示批改的試卷\n");
        printf("5退出系統\n\n");
        printf("請選擇:");
    
        scanf("%d", &s);
        while(getchar() != '\n');
        
        if(s < 1 || s > 5)
            printf("選擇有誤!請重新輸入.\n");
        else
            break;
    }
    return s;
}
void chuti()
{
    int i, f;
    printf("確認隨機出%d道題嗎?(y確認):", N);
    if(getchar() != 'y')
        return;
    
    for(i = 0, f = 1; i < N; f ? i++ : 0) {
        timu[i].a = rand() % 100+1;
        timu[i].b = rand() % 100+1;
        timu[i].op = rand() % 4;
        if(timu[i].b == 0 && timu[i].op == 3) {
            f = 0;
            continue;
        }
        else
            f = 1;
        switch(timu[i].op) {
        case 0: timu[i].result = timu[i].a + timu[i].b;break;
        case 1: timu[i].result = timu[i].a - timu[i].b;break;
        case 2: timu[i].result = timu[i].a * timu[i].b;break;
        case 3: timu[i].result = timu[i].a / timu[i].b;break;
        }
    }
    printf("出題完畢!\n");
    flag = 1;
}
void kaoshi()
{
    int i, f, n;
    char op[] = "+-*/";
    if(!flag) {
        printf("還未出題!\n");
        return;
    }
    printf("確認開始練習嗎?(y確認):");
    if(getchar() != 'y')
        return;
    fen = 0;
    for(i = 0, f = 1; i < N; f ? ++i : 0) {
        printf("題目%d: %d %c %d = ", i+1, timu[i].a, op[timu[i].op], timu[i].b);
        if(!scanf("%d", &n)) {
            printf("輸入有誤!請重試.\n");
            f = 0;
        } else {
            timu[i].input = n;
            timu[i].rw = (n == timu[i].result ? (fen += 10, 1) : 0);
            f = 1;
        }
    }
    printf("練習完畢!\n");
}
void pinfen()
{
    char* a[] = {"優秀", "良", "及格", "不及格"};
    char** p;
    int n;
    p = a + 3;
    n = fen / 10;
    
    if(n >= 6)p--;
    if(n >= 8)p--;
    if(n == 10)p--;
    
    printf("您測試的總分為 %s:%d分\n", *p, fen);
} 
void shijuan()
{
    int i;
    char op[] = "+-*/";
    for(i = 0; i < N; ++i){
        printf("%d %c %d = %d\t%s", timu[i].a, op[timu[i].op], 
        timu[i].b, timu[i].input, timu[i].rw ? "√": "×");
        if(!timu[i].rw)
            printf(" 正確答案: %d", timu[i].result);
        putchar('\n');
    }
}
int main()
{
    int i;
    srand(time(0));
    void (*f[])() = {chuti, kaoshi, pinfen, shijuan};
    while((i = mulu()) != 5) {
        f[i-1]();
        printf("按任意鍵繼續...");
        getch();
    }
    printf("再見");
    return 0;
}