首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

模拟小学生加减乘除混同运算

2012-12-31 
模拟小学生加减乘除混合运算#include stdio.h#include stdlib.hint get_choice()void show_menu()vo

模拟小学生加减乘除混合运算

#include <stdio.h>
#include <stdlib.h>

int get_choice();
void show_menu();
void do_exercise(int);
int test(int);

int main(int argc, const char *argv[])
{
 int choice;
 
 do
 {
  show_menu();

  choice = get_choice();

  if (0 == choice)
  {
   break;
  }

  do_exercise(choice);

  sleep(2);
  system("clear");
 } while (choice != 0);

 printf("\n welcome to use this software, goodbye!\n");
 return 0;
}

int get_choice()
{
 int choice;
 scanf("%d", &choice);

 return choice;
}

void show_menu()
{
 printf("===============================================\n");
 printf("\twelcome to use  calculator-software for pupil\n");
 printf("\t1.Addition Practise       2.Subtration Practise\n");
 printf("\t3.Multiplation Practise   4.Division Practise\n");
 printf("\t5.General Practise        0.Quit System\n");
 printf("===============================================\n");
 printf("Please input your choice(0~5)\n");
}

void do_exercise(int n)
{
 int score = 0;
 int i;

 for(i = 1; i <= 10; i++)
  score = score + test(n);
 printf("This practise ten question you did %d the right thing and way\n", score);
}

int test(int n)
{
 int ranswer;
 int uanswer;
 int t;
 char operator;

 srand(time(NULL));
 int num1 = rand() % 10;
 int num2 = rand() % 10;

 if (5 == n)
  n = rand() % 4 + 1;

 switch(n)
 {
  case 1:
   operator = '+';
   break;
  case 2:
   operator = '-';
   break;
  case 3:
   operator = '*';
   break;
  case 4:
   operator = '/';
   break;
  default:
   break;

 }

 if ((operator == '-') && (num1 < num2))
 {
  t = num1;
  num1 = num2;
  num2 = t;
 }

 if (operator == '/')
 {
  if (num2 == 0)
   num2 = 1;
  num1 = num1 * num2;
 }

 printf("%d %c %d\n", num1, operator, num2);
 scanf("%d", &uanswer);

 switch (operator)
 {
  case '+':
   ranswer = num1 + num2;
   break;
  case '-':
   ranswer = num1 - num2;
   break;
  case '*':
   ranswer = num1 * num2;
   break;
  case '/':
   ranswer = num1 / num2;
   break;
 }

 if (uanswer == ranswer)
 {
  printf("you do it right!\n");
  return 1;
 }

 else
 {
  printf("you do it default!\n");
  return 0;
 }
}

 

 

运行结果:

[haoyue@centos practices]$ ./a.out
===============================================
 welcome to use  calculator-software for pupil
 1.Addition Practise       2.Subtration Practise
 3.Multiplation Practise   4.Division Practise
 5.General Practise        0.Quit System
===============================================
Please input your choice(0~5)
1
1 + 5
6
you do it right!
5 + 6
11
you do it right!
8 + 7
15
you do it right!
4 + 6
10
you do it right!
6 + 7
13
you do it right!
1 + 7
8
you do it right!
1 + 8
9
you do it right!
6 + 0
6
you do it right!
5 + 6
11
you do it right!
1 + 2
3
you do it right!
This practise ten question you did 10 the right thing and way

===============================================
 welcome to use  calculator-software for pupil
 1.Addition Practise       2.Subtration Practise
 3.Multiplation Practise   4.Division Practise
 5.General Practise        0.Quit System
===============================================
Please input your choice(0~5)
2
7 - 0
7
you do it right!
9 - 2
7
you do it right!
7 - 4
3
you do it right!
8 - 1
7
you do it right!
8 - 0
8
you do it right!
3 - 0
3
you do it right!
7 - 6
1
you do it right!
9 - 5
4
you do it right!
3 - 2
1
you do it right!
9 - 3
6
you do it right!
This practise ten question you did 10 the right thing and way

===============================================
 welcome to use  calculator-software for pupil
 1.Addition Practise       2.Subtration Practise
 3.Multiplation Practise   4.Division Practise
 5.General Practise        0.Quit System
===============================================
Please input your choice(0~5)
5
4 + 0
4
you do it right!
5 * 8
40
you do it right!
7 + 7
14
you do it right!
1 + 8
9
you do it right!
4 - 0
4
you do it right!
8 - 1
7
you do it right!
56 / 7
8
you do it right!
1 + 4
5
you do it right!
2 - 1
1
you do it right!
4 - 1
3
you do it right!
This practise ten question you did 10 the right thing and way

===============================================
 welcome to use  calculator-software for pupil
 1.Addition Practise       2.Subtration Practise
 3.Multiplation Practise   4.Division Practise
 5.General Practise        0.Quit System
===============================================
Please input your choice(0~5)
0

 welcome to use this software, goodbye!

 

热点排行