四则运算

| 这个作业属于哪个课程 | <a href="https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/">软件工程</a> |
|----------------------- |------------------------------|
| 这个作业要求在哪里 | <a href="https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/homework/11377">作业要求</a>|
| 这个作业的目标 | <能对需求进行分析并实现;编码格式规范;学习博客撰写> |
| 学号 | <3180701304> |
<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li><a href="#题目要求">题目要求</a></li><li><a href="#函数说明">函数说明</a><ul><li><a href="#分数结构体">分数结构体</a></li><li><a href="#找公约数">找公约数</a></li><li><a href="#分数约分">分数约分</a></li><li><a href="#加法">加法</a></li><li><a href="#减法">减法</a></li><li><a href="#乘法">乘法</a></li><li><a href="#除法">除法</a></li><li><a href="#主函数">主函数</a></li></ul></li><li><a href="#运行截图">运行截图</a><ul><li><a href="#加法">加法</a></li><li><a href="#减法">减法</a></li><li><a href="#乘法">乘法</a></li><li><a href="#除法">除法</a></li></ul></li><li><a href="#小结">小结</a></li><li><a href="#源码">源码</a></li></ul></div><p></p>
  <h1 id="题目要求">题目要求</h1>
  <p>写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:</p>
  <p>1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24</p>
  <p>2)程序要求能处理用户的输入,判断对错,累积分数</p>
  <p>3)程序支持可以由用户自行选择加、减、乘、除运算</p>
  <p>4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目</p>

<h1 id="函数说明">函数说明</h1>
  <h2 id="分数结构体">分数结构体</h2>
  <pre><code>typedef struct //定义分数结构体
  {
  int up, down;
  }fraction;
  </code></pre>
  <h2 id="找公约数">找公约数</h2>
  <pre><code>int gcd(int a, int b) //最大公约数
  {
  if (b == 0) return a; //辗转相除法
  return gcd(b, a % b);
  }
  </code></pre>
  <h2 id="分数约分">分数约分</h2>
  <pre><code>void yuefen(int &amp;a, int &amp;b,int flag) //分数约分,并自动进行输出,用flag标志答案,答案不输出
  {
  int d = gcd(abs(a), abs(b));
  a /= d;
  b /= d;
  if (flag == 0) {
  if (b == 1)printf("%d", a);
  else printf("%d/%d", a, b);
  }
  }
  </code></pre>
  <h2 id="加法">加法</h2>
  <pre><code>void add(int num,fraction fraans[],int ans[]) //加法题目生成及运算
  {
  int a, b, up1, down1, up2, down2,flag=0;
  for (int i = 0; i &lt;=num / 2; i++) //整数部分
  {
  unsigned int times = (unsigned int)time(0); //生成随机数
  srand(times * (i + 1));
  a = rand() % 10 + 1;
  b = rand() % 10 + 1;
  ans[i] = a + b;
  printf("%d.%d+%d\n", i+1, a, b); //输出整数题目
  }
  for (int i = num/2+1; i &lt; num; i++) //分数部分
  {
  unsigned int times = (unsigned int)time(0);
  srand(times * (i + 1));
  up1 = rand() % 10 + 1;
  down1 = rand() % 10 + 1;
  up2 = rand() % 10 + 1;
  down2 = rand() % 10 + 1;
  printf("%d.", i + 1);
  yuefen(up1, down1,flag);
  printf("+");
  yuefen(up2, down2,flag);
  printf("\n"); //将随机生成的分数进行约分后再输出
  fraans[i].up = up1 * down2 + up2 * down1;
  fraans[i].down = down1 * down2;
  flag = 1; //用flag标志答案,对答案不进行输出
  yuefen(fraans[i].up, fraans[i].down,flag);
  flag = 0;
  }
  }
  </code></pre>
  <h2 id="减法">减法</h2>
  <pre><code>void subtract(int num, fraction fraans[], int ans[]) //减法题目生成及运算
  {
  int a, b, up1, down1, up2, down2,flag=0;
  for (int i = 0; i &lt;= num / 2; i++)
  {
  unsigned int times = (unsigned int)time(0);
  srand(times * (i + 1));
  a = rand() % 10 + 1;
  b = rand() % 10 + 1;
  ans[i] = a - b;
  printf("%d.%d-%d\n", i, a, b);
  }
  for (int i = num / 2 + 1; i &lt; num; i++)
  {
  unsigned int times = (unsigned int)time(0);
  srand(times * (i + 1));
  up1 = rand() % 10 + 1;
  down1 = rand() % 10 + 1;
  up2 = rand() % 10 + 1;
  down2 = rand() % 10 + 1;
  printf("%d.", i + 1);
  yuefen(up1, down1, flag);
  printf("-");
  yuefen(up2, down2, flag);
  printf("\n");
  fraans[i].up = up1 * down2 - up2 * down1;
  fraans[i].down = down1 * down2;
  flag = 1;
  yuefen(fraans[i].up, fraans[i].down, flag);
  flag = 0;
  }
  }
  </code></pre>
  <h2 id="乘法">乘法</h2>
  <pre><code>void multiply(int num, fraction fraans[], int ans[]) //乘法题目生成及运算
  {
  int a, b, up1, down1, up2, down2,flag = 0;
  for (int i = 0; i &lt;= num / 2; i++)
  {
  unsigned int times = (unsigned int)time(0);
  srand(times * (i + 1));
  a = rand() % 10 + 1;
  b = rand() % 10 + 1;
  ans[i] = a * b;
  printf("%d.%d*%d\n", i+1, a, b);
  }
  for (int i = num / 2 + 1; i &lt; num; i++)
  {
  unsigned int times = (unsigned int)time(0);
  srand(times * (i + 1));
  up1 = rand() % 10 + 1;
  down1 = rand() % 10 + 1;
  up2 = rand() % 10 + 1;
  down2 = rand() % 10 + 1;
  printf("%d.", i + 1);
  yuefen(up1, down1, flag);
  printf("*");
  yuefen(up2, down2, flag);
  printf("\n");
  fraans[i].up = up1 * up2;
  fraans[i].down = down1 * down2;
  flag = 1;
  yuefen(fraans[i].up, fraans[i].down, flag);
  flag = 0;
  }
  }
  </code></pre>
  <h2 id="除法">除法</h2>
  <pre><code>void divide(int num, fraction fraans[])
  { //除法题目生成及运算
  int up1, down1, up2, down2, flag = 0;
  for (int i = 0; i &lt; num; i++)
  {
  unsigned int times = (unsigned int)time(0);
  srand(times * (i + 1));
  up1 = rand() % 10 + 1;
  down1 = rand() % 10 + 1;
  up2 = rand() % 10 + 1;
  down2 = rand() % 10 + 1;
  printf("%d.(", i + 1);
  yuefen(up1, down1, flag);
  printf(")/(");
  yuefen(up2, down2, flag);
  printf(")\n");
  fraans[i].up = up1 * down2;
  fraans[i].down = down1 * up2;
  flag = 1;
  yuefen(fraans[i].up, fraans[i].down, flag);
  flag = 0;
  }
  }
  </code></pre>
  <h2 id="主函数">主函数</h2>
  <pre><code>int main()
  {
  char f = 'Y';
  while(f!='N'&amp;&amp;f!='n')
  {
  fraction fraans[400], userfraans[400];
  int ans[100] = { 0 }, userans[100] = { 0 };
  int num, symbol, grade = 0;
  printf("四则运算题目生成程序\n");
  printf("请选择符号,1,2,3,4对应加减乘除:");
  scanf_s("%d", &amp;symbol); //用symbol标志用户选择的符号,1234对应加减乘除;
  printf("请输入题目数量(最少为2,最大100):");
  scanf_s("%d", &amp;num);
  switch (symbol) //对应选择不同的算法
  {
  case 1:add(num, fraans, ans); break;
  case 2:subtract(num, fraans, ans); break;
  case 3:multiply(num, fraans, ans); break;
  case 4:divide(num, fraans); break;
  }
  if (symbol != 4) //当用户选择的运算不为除法时,分别对整数和分数答案进行保存
  {
   
  for (int i = 0; i &lt;= num / 2; i++)
  {
  printf("第%d道题的答案为:", i + 1);
  scanf_s("%d", &amp;userans[i]);
  }
  for (int i = num / 2 + 1; i &lt; num; i++)
  {
  printf("第%d道题的答案为:", i + 1);
  if (fraans[i].down == 1)
  {
  scanf_s("%d", &amp;userfraans[i].up);
  userfraans[i].down = 1;
  }
  else
  {
  scanf_s("%d/%d", &amp;userfraans[i].up, &amp;userfraans[i].down);
  }
  }
  }
  else //当用户选择为出发时,对用户输入的答案进行保存
  {
  for (int i = 0; i &lt; num; i++)
  {
  printf("第%d道题的答案为:", i + 1);
  if (fraans[i].down == 1)
  {
  scanf_s("%d", &amp;userfraans[i].up);
  userfraans[i].down = 1;
  }
  else
  {
  scanf_s("%d/%d", &amp;userfraans[i].up, &amp;userfraans[i].down);
  }
  }
  }
  for (int i = 0; i &lt;= num / 2; i++) //对用户输入的答案和答案进行对比,如果一致,得分+1
  {
  if (userans[i] == ans[i])grade++;
  }
  for (int i = num / 2 + 1; i &lt; num; i++)
  {
  if (userfraans[i].up == fraans[i].up &amp;&amp; userfraans[i].down == fraans[i].down)grade++;
  }
  printf("grade=%d\n", grade);
  printf("是否继续做题?(Y/N)");
  cin &gt;&gt; f;
  if (f == 'Y')system("cls"); //清屏
  else return 0;
  }
  }
posted @ 2020-11-06 11:48  Aurora12345  阅读(332)  评论(0编辑  收藏  举报