zoukankan      html  css  js  c++  java
  • 四则运算

    题目:自动生成四则运算题目 主要功能:可自动生成小学四则运算题目的程序

    程序设计思想  

          本程序的设计主要基于对C/C++中产生随机数的应用,在产生算数符号部分主要采用了switch语句,通过随机产生0-3的数值来随机产生"+-*/"字符,

          并进行相应的处理;在处理整数操作部分时,使用try/throw语句,避免除法分母为0,语句采用一维数组,记录答案结果;在处理真分数部分,

         采用while语句产生合理的真分数,采用二维数组记录答案结果




    #include<iostream.h> #include<stdlib.h> #include<conio.h> void DealFenshu(int m, int a[][2]) { for(int p=0;p<m;p++) { int i=(int)rand()%10; int j=(int)rand()%10; while(j==0||i>=j) { i=(int)rand()%10; j=(int)rand()%10; } int x=(int)rand()%10; int y=(int)rand()%10; while(y==0||x>=y) { x=(int)rand()%10; y=(int)rand()%10; } int k=(int)rand()%100/25; switch(k) { case 0: cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"="; a[p][0]=i*y+x*j; a[p][1]=j*y; break; case 1: cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"="; a[p][0]=i*y-x*j; a[p][1]=j*y; break; case 2: cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"="; a[p][0]=i*x; a[p][1]=j*y; break; case 3: a[p][0]=i*y; a[p][1]=j*x; cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"="; } if(p%5==4) { cout<<endl; } else { cout<<‘ ‘; } } } void DisplayFenshu(int a[][2],int w,int m) { if(w==1) { for(int q=0;q<m;q++) { if(a[q][0]==0) cout<<"0"<<‘ ‘; else cout<<a[q][0]<<"/"<<a[q][1]<<‘ ‘; if(q%5==4) { cout<<endl; } } } else {}; } void DealInt(int m,int a[]) { for(int p=0;p<m;p++) { int i=(int)rand()%10; int j=(int)rand()%10; int k=(int)rand()%100/25; switch(k) { case 0: cout<<i<<"+"<<j<<"="; a[p]=i+j; break; case 1: cout<<i<<"-"<<j<<"="; a[p]=i-j; break; case 2: cout<<i<<"*"<<j<<"="; a[p]=i*j; break; case 3: try { a[p]=i/j; cout<<i<<"/"<<j<<"="; } catch(...) { p--; } } if(p%5==4) { cout<<endl; } else { cout<<‘ ‘; } } } void DisplayInt(int a[],int w,int m) { if(w==1) { for(int q=0;q<m;q++) { cout<<a[q]<<‘ ‘; if(q%5==4) { cout<<endl; } } } else {}; } void main() { int p; do { system("cls"); int a[1000],b[1000][2]; int m,n,w; cout<<"请输入生成的四则运算题个数:"; cin>>m; cout<<endl; cout<<"请输入要生成的四则运算种类(输入1为整数,否则为真分数):"; cin>>n; cout<<endl; if(n==1) { DealInt(m,a); cout<<endl; } else { DealFenshu(m,b); cout<<endl; } cout<<"是否输出答案(输入1则输出答案否则不输出答案)"<<endl; cin>>w; if(n==1) { DisplayInt(a,w,m); } else { DisplayFenshu(b,w,m); } cout<<endl; cout<<"是否继续生成运算题(输入1则生成否则不生成)"<<endl; cin>>p; cout<<endl; }while(1==p); }

    总结     

         因为自己基础比较薄弱,并不能自主完成这个程序。我通过自己看书,参考网上的源代码的基本构架和同学的代码,自己慢慢的研究了出来。 通过这次程序的制作,对switch语句有的用法,和使用的思想有了新的认识。还有随机函数,原来可能有学过可是自己并不掌握,这次理解了随机函数的用法。包括一些细节和程序整体的构造有了大致的认识。有的能够看懂在今后的学习应用中可以自己做出来,有些的语句用法或者思想还是不够了解,在今后需要多实践,从实践中慢慢习惯。

     

  • 相关阅读:
    vb代码控制 Excel锁定单元格
    SendMessage
    vb 中Treeview控件的一些问题!
    NGWS runtime C# 开始学习 第一天 (2006.6.7)
    DTS Transform Data Task的使用
    GetTickCount
    ASP.NET 2.0 中Login控件的使用
    core dump解析(2)
    tcp滑动窗口机制
    linux 查看文件夹大小 du命令
  • 原文地址:https://www.cnblogs.com/Alvin-D/p/5284838.html
Copyright © 2011-2022 走看看