4、試撰寫一個程式來幫助小學生學習乘法,按下列要求編程:從鍵盤輸入兩個10以內的正整數(也可以用隨機函式產生兩個正整數),在螢屏上列印出問題,如:6*7=?
然后通過鍵盤學生輸入答案。程式檢查學生輸入的答案是否正確。若正確,則列印“Right!”,否則列印“Wrong!”。然后詢問是否繼續答題;若回答是“Y”則繼續答題,若回答“N”則退出答題。統計并輸出共出了多少題,做對了多少題?分數是多少?
uj5u.com熱心網友回復:
/*************************************************************************
> File Name: abb.cpp
> Author: cfjtaishan
> Mail: [email protected]
> Created Time: 2020年03月18日 星期三 20時29分46秒
************************************************************************/
#include <stdio.h>
#include <math.h>
float get_result(float a, char opt, float b);
int main(void)
{
char opt1, opt2;
float a, b, c, result;
int choice;
printf("Please input your choice: (1: two number opt, 2: three nubers opt)");
scanf("%d", &choice);
if (choice != 1 && choice != 2) {
printf("Input error!\n");
return -1;
}
if (choice == 1) {
printf("Please input your operation: (eg: 1*2)");
scanf("%f%c%f", &a, &opt1, &b);
printf("=%.2f\n", get_result(a, opt1, b));
return 0;
}
printf("Please input your operation: (eg: 1*2*3)");
scanf("%f%c%f%c%f", &a, &opt1, &b, &opt2, &c);
if (opt1 == '*' || opt1 == '/') {
result = get_result(a, opt1, b);
printf("=%.2f\n", get_result(result, opt2, c));
} else if (opt2 == '*' || opt2 == '/') {
result = get_result(b, opt2, c);
printf("=%.2f\n", get_result(a, opt1, result));
}
return 0;
}
float get_result(float a, char opt, float b)
{
float result;
switch (opt) {
case '+':
result = a+b;
break;
case '-':
result = a-b;
break;
case '*':
result = a*b;
break;
case '/':
if (fabsf(b) > 1e-6)
result = a/b;
break;
}
return result;
}
供參考~
雖然不是完全符合要求,建議在這個基礎上可以改一改。
uj5u.com熱心網友回復:
按照你的需求寫個簡單的例子,隨機生成的題。只是小學生除法不知道到什么程度,是用分數表示還是小數,所以除法部分LZ再修改吧。
typedef struct expr {
double num1;
double num2;
char opt;
double answer1;
double answer2;
int result;
expr *next;
} EXPR, *PEXPR;
typedef struct ques {
PEXPR expr;
int count;
int right;
} QUES, *PQUES;
char opts[4] = {'+', '-', '*', '/'};
PQUES pQ;
void genQues() {
if (pQ==NULL) {
srand(time(NULL));
pQ = (PQUES)malloc(sizeof(QUES));
pQ->count = 0;
pQ->right = 0;
}
int tmp = 0;
PEXPR exp = (PEXPR)malloc(sizeof(EXPR));
exp->num1 = rand()%10;
exp->num2 = rand()%9 + 1;
exp->opt = rand()%4;
exp->next = NULL;
switch (exp->opt) {
case 0:
exp->answer1 = exp->num1 + exp->num2;
break;
case 1:
if (exp->num1 > exp->num2) {
tmp = exp->num1;
exp->num1 = exp->num2;
exp->num2 = tmp;
}
exp->answer1 = exp->num1 - exp->num2;
break;
case 2:
exp->answer1 = exp->num1 * exp->num2;
break;
case 3:
exp->answer1 = exp->num1 / exp->num2; //除法根據需要修改
default:
break;
}
exp->result = 0;
if (pQ->count > 0) {
exp->next = pQ->expr;
}
pQ->expr = exp;
pQ->count++;
}
int main(int argc, const char * argv[]) {
char Y = 'Y';
PEXPR exp;
while (Y == 'Y' || Y == 'y') {
genQues();
printf("%.0f%c%.0f=?\n", pQ->expr->num1, opts[pQ->expr->opt], pQ->expr->num2);
scanf("%lf", &pQ->expr->answer2);
getchar();
if (pQ->expr->answer2==pQ->expr->answer1) {
pQ->expr->result = 1;
pQ->right++;
printf("Right!\n");
} else {
printf("Wrong!\n");
}
printf("是否繼續答題?繼續答題輸入Y,退出輸入N\n");
Y = getchar();
}
printf("總共答題%d,做對%d題,分數%.0f\n", pQ->count, pQ->right, 100.0*pQ->right/pQ->count);
while(pQ->expr!=NULL) {
exp = pQ->expr->next;
free(pQ->expr);
pQ->expr = exp;
}
free(pQ);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/91633.html
標籤:C語言
上一篇:建立判定素數的自定義函式,對隨機的 n 個數,判斷哪些是素數
下一篇:單鏈表輸出報錯提示越界
