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

#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!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值