回覆列表
  • 1 # 上進的白雲5

    #include <stdio.h>

    #include <stdlib.h>

    #include <time.h>

    struct question {

    char ask[200];

    char answer[4][80];

    int right;

    struct question *next;

    };

    int MenuChoice(void);

    struct question *InsertList(struct question *fst, const struct question *ad);

    struct question *ListSeek(struct question *seek, long len, long max);

    void GetQuestion(struct question *src);

    void SaveFile(const struct question *ed, FILE *saf);

    struct question *LoadFile(struct question *td, FILE *laf);

    int GetAnswer(void);

    void ExplainQuestion(const struct question *que, int n);

    main()

    {

    struct question *start = NULL, temp;

    long choice, line = 0, c;

    FILE *fp = fopen("kstm.dat", "a+");

    start = LoadFile(start, fp);

    while ((choice = MenuChoice()) != 3)

    if (choice == 1) {

    GetQuestion(&temp);

    start = InsertList(start, &temp);

    ++line;

    }

    else if (choice == 2){

    c =600;

    while (c > 500 || c > line) {

    printf("Please scanf the number of the questions you want to answer: ");

    scanf("%d", &c);

    }

    ExplainQuestion(start, line);

    }

    SaveFile(start, fp);

    fclose(fp);

    return 0;

    }

    struct question *ListSeek(struct question *seek, long len, long max)

    {

    int i;

    srand(time(NULL));

    while (i = rand() % max + len < max)

    ;

    while (i--)

    seek = seek->next;

    return seek;

    }

    struct question *InsertList(struct question *fst, const struct question *ad)

    {

    struct question *newPtr = (struct question *)malloc(sizeof(struct question));

    if (newPtr == NULL)

    exit(0);

    *newPtr = *ad;

    newPtr->next = fst;

    return newPtr;

    }

    void GetQuestion(struct question *src)

    {

    int i = 0;

    printf("Please scanf the question:\n");

    scanf("%s", src->ask);

    while (i < 4) {

    printf("Please scanf the answer of %c:\n", i + "A");

    scanf("%s", src->answer[i++]);

    }

    src->right = GetAnswer();

    }

    struct question *LoadFile(struct question *td, FILE *laf)

    {

    struct question temp;

    while (fread(&temp, 1, sizeof(struct question), laf))

    td = InsertList(td, &temp);

    return td;

    }

    void SaveFile(const struct question *ed, FILE *saf)

    {

    fclose(saf);

    if ((saf = fopen("kstm.dat", "w")) == NULL)

    return ;

    while (ed) {

    fwrite(ed, 1, sizeof(struct question), saf);

    ed = ed->next;

    }

    }

    int GetAnswer(void)

    {

    int c = 0;

    fflush(stdin);

    while (c < "A" || c > "D") {

    printf("Please scanf the right answer: ");

    scanf("%c", &c);

    if(c > 96)

    c=c-32;

    }

    return c;

    }

    void ExplainQuestion(const struct question *que, int n)

    {

    int i = 0, t = n;

    char result[1001], *p = result;

    for (i = 0; n--; que = que->next) {

    printf("%s\nA.%s\nB.%s\nC.%s\nD.%s\n\n", que->ask, que->answer[0], que->answer[1],

    que->answer[2], que->answer[3]);

    if ((*p = que->right) == (*(p + 1) = GetAnswer()))

    ++i;

    p += 2;

    }

    *p = "\0";

    printf("\n%-20s%-20s%s\n", "Right answer", "Your answer", "Result");

    for (p = result; *p != "\0"; p += 2)

    printf("%-20c%-20c%s\n", *p, *(p + 1), *p == *(p + 1) ? "True" : "Flase");

    printf("\n You have answered %d questions, You pass %d questions , Score: %.2f\n\n", t, i, (float)i / t * 100.00);

    }

    int MenuChoice(void)

    {

    int value;

    printf("1 - Scanf more questions\n2 - Answer the questions\n3 - Exit\n");

    scanf("%d", &value);

    return value;

    }

  • 中秋節和大豐收的關聯?
  • 哈登連續出手超過30次以上對球隊是好是壞?