c语言小学生四则运算出题_软件工程第一次作业,小学生四则运算的出题程序...
一、背景
阿超有個兒子上小學二年級,老師每天讓家長給孩子出30道加減法題,雖然不多,但是每天都做也算是個負擔,阿超作為一個老牌程序員當然想用計算機來解決這個小問題,目前對于這個問題對于任何語言都不是問題,比如:
C/C++、C#、Java、Python、VB、JavaScript、Perl……
具體要求如下:
能自動生成小學四則運算題目(注意是給小學生用的,要是結果出現負數的話他們會迷茫的!)
除了整數外,還要支持真分數的四則運算
請大家用任何一種自己擅長的語言來編寫這段程序,并把程序的介紹和自己編寫的過程寫一個博客
二、分析
(一) 自己擅長的是c語言,準備用c語言,但是自己學的java,想用java試試,支持真分數運算,如果用C語言,我們可以這么考慮,a,b,c,d隨機生成。
a/b????? c/d,
(1)可能存在a,b,c,d;a和b存在公約數,c和d存在公約數。
解 決辦法:先求a和b的最大公約數m,先求c和d的最大公約數n,然后a=a/m;b=b/m;c=c/m;d=d/m;,然后就可以算加"+"法 (a*d+b*c)/(b*d);減“-”法(a*d-b*c)/(b*d);乘“*”法a*c/(b*d);除法“/”,判斷一下分母是否為0,分數符 號直接輸出;
(2)可能存在a>b,c>d的情況,真假分數情況。輸出直接輸出符號“/”。
(二)有判斷正確和錯誤,每答一次就判斷一次,回答正確和回答錯誤,一次性答對是10分,答兩次才答對得5分,答三次才答對得3分。
(三)輸入一個數,知道出題的數目,隨機產生的題數目,多輸也會提示輸入錯誤。
三、代碼部分
我用的是java寫的:
importjava.util.InputMismatchException;
importjava.util.Random;
importjava.util.Scanner;
import java.io.*;
public classPratices {
public static voidmain(String[] args) {
newPratices().list_Pratices();
}
public int random_Num(intrange) {
return (int) (Math.random() *range);
}
public voidlist_Pratices() {
int right = 0;
int wrongtimes = 0;
intnum_1, num_2, temp;
int type = random_Num(4);
int score = 0;
int count = 1;
System.out.println("請輸入題目數量:");
Scanner sc = newScanner(System.in);
int n =sc.nextInt();
while (count <=n) {
type = random_Num(2);
num1 = random_Num(100); //100以內隨機數
num2 = random_Num(100); //100以內的隨機數
wrongtimes = 0;
if (type == 0)
{
System.out.print("(" + count + ") " + num1 + " + " + num2+ " = ");//加法
}
else if(type == 1)
{
if ((num1
{
temp =num1;
num1 =num2;
num2 =temp;
}
System.out.print("(" + count + ") " + num1 + " - " + num2+ " = ");//減法
}
else if(type == 2)
System.out.print("(" + count + ") " + num1 + " * " + num2+ " = ");//乘法
}
else if(type == 3)
{
if(num2!=0)
System.out.print("(" + count + ") " + num1 + " / " + num2+ " = ");//除法
elseSystem.out.println("分母為零");
}
int answer = this.getAnswer(count);
boolean flag =check(num1, num2, type, answer, count);
if(flag) {
right++;
System.out.println("回答正確");
score += this.getScore(wrongtimes);
} else{
while (wrongtimes < 2) {
wrongtimes++;
System.out.println("回答錯誤 " + wrongtimes + " 次");
answer = this.getAnswer(count);
flag =check(num1, num2, type, answer, count);
if(flag) {
score += this.getScore(wrongtimes);
right++;
wrongtimes = 0;
break;
}
}
if (wrongtimes == 3)
System.out.println("回答錯誤 ");
elseSystem.out.println("回答正確");
}
count++;
}
System.out.println("回答正確 : " +right);
System.out.println("回答錯誤: " + (10 -right));
System.out.println("獲得分數: " +score);
System.out.println(getDegree(score));
}
public boolean check(int num_1, int num_2, int type, intmy_Answer,
intcount) {
int answer = 0;
if (type == 1) {
answer = num_1 -num_2;
} else if (type == 0) {
answer = num_1 +num_2;
}
return my_Answer ==answer;
}
public int getAnswer(intcount) {
int my_Answer = 0;
BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
try{
my_Answer =Integer.parseInt(br.readLine());
} catch(IOException e) {
e.printStackTrace();
} catch(NumberFormatException e) {
System.out.println("輸入有誤");
return 0;
} finally{
if (count >= n && (br != null)) {//不會超出輸入的n
try{
br.close();
} catch(IOException e) {
e.printStackTrace();
}
br = null;
}
}
returnmy_Answer;
}
public int getScore(intwrongtimes) {
if (wrongtimes == 0) {
return 10;
} else if (wrongtimes == 1) {
return 7;
} else if (wrongtimes == 2) {
return 5;
} else
return 0;
}
public String getDegree(int score) {
if (score > 90)
return "SMART";
else if (score > 80)
return "GOOD";
else if (score > 70)
return "OK";
else if (score > 60)
return "PASS";
else
return "TRY AGAIN";
}
}
總結
以上是生活随笔為你收集整理的c语言小学生四则运算出题_软件工程第一次作业,小学生四则运算的出题程序...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java8 di_java8 多个lis
- 下一篇: php object 对象不存在。增加对