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

    一.题目及设计思路

      程序能接受用户输入答案,并判定对错。最后给出总共对/错 的数量。

      在输出链表中的数据的时候,增加一个用户输入答案的模块,检查对错并且直接显示对错,将对的个数存储,最后显示对错的个数。

      之前的程序就是把生成的算式存储在链表里了,所以实现这一项比较容易,直接在加一个模块,比较用户输入的结果和链表两个操作数运算的结果是否一致即可。

    二.源代码

         

    // 四则运算3.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include<iostream.h>
    #include<time.h>
    #include<stdlib.h>
    typedef struct
    {
        int first;
        int second;
        char ope;
    }equation;
    //***********链表的数据结构***********//
    typedef struct LNode
    {
        int first_operand;
        int second_operand;
        char operat;
        struct LNode *next;
    }LNode,*LinkList;
    //**********链表初始化***********//
    void InitList_L(LinkList &L)
    {
        L=new LNode;
        L->next=NULL;
    }
    //**********链表数据的插入*************//
    void InsertList_L(LinkList &L,equation N)
    {
        LNode *temp,*head;
        head=L;
        temp=new LNode;
    
        temp->first_operand=N.first;
        temp->second_operand=N.second;
        temp->operat=N.ope;
        temp->next=NULL;
    
        while(head->next!=NULL)
        {
            head=head->next;
        }
        head->next=temp;
    
    }
    //*********用户要求的输入***********//
    void require(int &num,int a[])
    {
        cout<<"请输入四则运算的个数:";
        cin>>num;
        cout<<"请输入数的最小值:";
        cin>>a[1];
        cout<<"请输入数的最大值:";
        cin>>a[2];
        cout<<"是否有乘除法(有(1)/无(0)):";
        cin>>a[0];
    }
    //***********检查是否有重复的式子***********//
    int repeat(LinkList L,equation N)     //a是算式的个数,函数返回值是1的话有重复,返回值是0的话无重复
    {
        int flag=0;                //如果有重复的则flag=1,否则为0
        LNode *temp;
        temp=L->next;
    
        while(temp!=NULL)
        {
            if((temp->first_operand==N.first)&&(temp->second_operand==N.second)&&(temp->operat==N.ope))//如果有重复的话
            {
                flag=1;
            }
            temp=temp->next;
        }
    
        return flag;
    }
    //***********链表的输出***********//
    int input_L(LinkList L)               //m,是每行的列数,n是每行的间隔
    {
        LNode *head;
        head=L->next;
        int temp=0;
        int sum=0;                 //存放计算正确的个数
        int consult,remainder;          //前者是商,后者是余数
        cout<<"请在下列算式后面写出结果(若是除法则先写商再写余数):"<<endl;
        while(head!=NULL)
        {
            cout<<head->first_operand<<head->operat<<head->second_operand<<"=";//数据的输出
            switch(head->operat)
            {
                case '+':cin>>consult;
                         if(consult==head->first_operand+head->second_operand)
                         {
                             sum++;
                             cout<<""<<endl;
                         }
                         else cout<<"×"<<endl;
                         break;
                case '-':cin>>consult;
                         if(consult==head->first_operand-head->second_operand)
                         {
                             sum++;
                             cout<<""<<endl;
                         }
                         else cout<<"×"<<endl;
                         break;
                case '*':cin>>consult;
                         if(consult==head->first_operand*head->second_operand)
                         {
                             sum++;
                             cout<<""<<endl;
                         }
                         else cout<<"×"<<endl;
                         break;
                case '/':cin>>consult;
                         cout<<"  ";
                         cin>>remainder;
                         if((consult==head->first_operand/head->second_operand)&&(remainder==head->first_operand%head->second_operand))
                         {
                             sum++;
                             cout<<""<<endl;
                         }
                         else cout<<"×"<<endl;
                         break;
                default:cout<<"ERROR!!!"<<endl;
            }
            temp++;
            head=head->next;
        }
        return sum;
    }
    int main()
    {
        int i,j,num,a[3];                //num是式子的个数
        int c,b,temp,sum;
        int flag1=1,flag2;                //判断是否运行继续生成算式,flag2表示是否有重复的式子
        char op[4]={'+','-','*','/'};         //运算符
        LinkList L;
        equation N;
        srand(time(NULL));
        
        for(i=0;i<5;i++)                 //对数组a初始化
        {
            a[i]=0;
        }
        while(flag1==1)
        {
            require(num,a);
    
            InitList_L(L);
            if(a[0]==0)                 //判断是否有乘除
            {
                op[2]='+';
                op[3]='-';
            }
    
            c=a[1];
            b=a[2]+1-a[1];
    
            for(i=0;i<num;i++)
            {
    XH:            N.second=c+rand()%b;        //式子的生成
                N.first=c+rand()%b;
                j=rand()%4;
                N.ope=op[j];
                
                if(N.ope=='-')              //如果是减法的话,则两个操作数调换位置
                {
                    if(N.first<N.second)
                    {
                        temp=N.first;
                        N.first=N.second;
                        N.second=temp;
                    }
                }
                flag2=repeat(L,N);
                InsertList_L(L,N);            //数据进入链表当中
                if(flag2==1)                //判断是否有重复,若有则重新生成算式
                {
                    goto XH;
                }
            }
            sum=input_L(L);
            cout<<sum<<"道题正确"<<endl;
            cout<<endl<<"是否继续(是(1)/否(0)):";    //判断是否继续运行
            cin>>flag1;
            if(flag1==1)
            {
                system("cls");               //清屏
            }
            else
            {
                cout<<"感谢您的使用!"<<endl;
            }
        }
        return 0;
    }

    三.运行结果截图

      

      

      

    四.PSP0级 

    项目计划总结:

    周活动总结表

    姓名:于海洋                    日期:2015/3/21

    日期       任务

    听课

    编写程序

    阅读课本

    准备考试

    日总计

    周日

    周一

    周二

         100

       100

    周三

        152

        

       152

    周四

        

        

       

    周五

         100

       100

    周六

       

       

    周总结

        200

        152

      

       352

    以前各周的累计时间                                                                                    

    总计

     200

     152

     352

    平均

     200

     152

     352

    最大

     200

     152

     352

    最小

     200

     152

     352

     

     

     

     

     

     

    时间记录表:

    学生        于海洋                                             日期       2015/3/21 

    教师        王建民                                             课程           PSP       

    日期

    开始时间

    结束时间

    中断时间

    净时间

    活动

    备注

     3/18

     14:00

    16:20

     80

     编程

     编写结对开发的作业

     21:00

     10:12

     72

     编程

     四则运算3

  • 相关阅读:
    [CFNews] Ediscovery: 4 scenarios that call for computer forensics
    [CFNews] MacForensicsLab发布MacLockPick 3.0
    [CFNews] Logicube也欲分“山寨机”取证一杯羹
    [CFNews] Tableau 发布全功能只读接口T35689iu
    [转载] Windows 8 TypedURLsTime
    [CFNews] Guidance发布EnCase V7.04.01中文及英文版
    [CFNews] AIS Inc. 发布苹果取证产品MacResponse LE
    [CFNews] Office 20072010、PGP全盘加密快速破解,Passware Kit 11.7发布
    [CFNews] GSI发布EnCase v7.04
    [CFNews] Paraben发布ProjectAPhone新型号ICD8000
  • 原文地址:https://www.cnblogs.com/menglikanhualuo/p/4356038.html
Copyright © 2011-2022 走看看