二柱子四則運算在線答題系統
#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("請做出選擇:");
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;
}
四:結果截圖:
五:實驗總結:
通過這次實驗我學會了對問題進行降維,把比較難的題目分解成一個個簡單的題目,這樣許多難的題目就迎刃而解,給我們的工作帶來很大的方便,通過這次實驗我學習到了很多,希望以後的實驗我能夠更上一層樓。
二柱子四則運算在線答題系統