首页 > 代码库 > 2nd 简单四则运算更新

2nd 简单四则运算更新

简单四则运算更新

 

功能:由随机数决定出题为10个以内的数字,并确定是否出现括号(仅限一对),顺序输出表达式,并用栈的方式进行计算,判断正误。其他功能有待进一步实现。

 

头文件

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <math.h>

 

栈的结构体

typedef struct stack{    double * e;    int zd;}bds_stack;

 

声明函数

void InitStack(bds_stack ** s,int n);int Push_stack(bds_stack * s,double t);void Pop_stack(bds_stack * s,double * t);double Opt_num();void Cal(int n);void yxj_table();

 

定义全局变量

int yxj[7][7];bds_stack * dz,* tz;

 

主函数

int main(){    int num;    int c = 0,p,s;    double r,ans,r1,r2,r3;    int * d,* t;    int p1,p2;       //存储出现括号的下标int m,n,f;       //分别记录数组d、t的下标int i,j,k;    yxj_table();     //初始化优先级数组    scanf("%d",&num);    srand(time(NULL));       //随机数种子    for(i = 0;i < num;i++)    {        m = 0;n = 0;f = 0;        while(c == 1||c == 0)        {            c = rand()%10;  //数字字符        }        s = rand()%2;     //是否出现括号        if(s == 1)         //确定字符总数        {            p = c + 1;        }        else        {            p = c - 1;        }        printf("%d %d %d\n",c,p,s);        d = (int * )malloc(sizeof(int)*c);        t = (int * )malloc(sizeof(int)*p);        InitStack(&dz,c);        InitStack(&tz,p);        Push_stack(tz,0);       //0为‘#’         for(j = 0;j < c;j++)        {            d[j] = rand()%100;             //数字为两位数        }        if(s == 1)        {            p1 = rand()%(p-2)+1;             p2 = rand()%(p-p1-1)+1+p1+1;            p1--; p2--;                    //下标            t[p1] = 5;            t[p2] = 6;        }        for(j = 0;j < p;j++)        {            if(s == 1)            {                if(j == p1||j == p2)                    continue;            }            t[j] = rand()%4+1;        }        j = 0;        if(t[n] == 5)        {            printf("(");            Push_stack(tz,t[n]);            n++;            j++;        }        for(;j < (c+p);)              //输出表达式        {            if(f == 0)            {                printf("%d",d[m]);                Push_stack(dz,d[m]);                m++; j++;                if(t[n] == 6)                {                    printf(")");                    while(yxj[(int)tz->e[tz->zd-1]][t[n]] != 2)                    {                        if(yxj[(int)tz->e[tz->zd-1]][t[n]] == 1)                        {                            r = Opt_num();                            Push_stack(dz,r);                        }                        else                        {                            printf("出错!\n");                            return 1;                        }                    }                    Pop_stack(tz,&r);                    n++; j++;                }                f = 1;            }            else            {                if(t[n] == 1)                {                    printf("+");                    Cal(t[n]);                }                if(t[n] == 2)                {                    printf("-");                    Cal(t[n]);                }                if(t[n] == 3)                {                    printf("*");                    Cal(t[n]);                }                if(t[n] == 4)                {                    printf("/");                    Cal(t[n]);                }                n++;j++;                if(t[n] == 5)                {                    printf("(");                    Push_stack(tz,t[n]);                    n++; j++;                }                f = 0;            }        }        printf(" =  ?\n");        while((yxj[(int)(tz->e[tz->zd-1])][0]) != 2)        {            r = Opt_num();            Push_stack(dz,r);        }        scanf("%lf",&ans);        if(ceil(fabs(r-ans)) == 0)        {            printf("right!!\n");        }        else        {            printf("false!!\n");        }    }    return 0;}

 

初始化优先级表

void yxj_table(){    int i,j;    yxj[0][0] = 2;    yxj[0][6] = 3;  yxj[5][0] = 3;    for(i = 0,j = 1;j <= 5;j++)    {        yxj[i][j] = 0;    }    yxj[6][0] = 1;    for(i = 1;i <= 4 ;i++)    {        for(j = 0;j <= 2;j++)        {            yxj[i][j] = 1;        }    }    for(i = 5,j = 1;j <= 4;j++)        yxj[i][j] = 0;    for(i = 6,j = 1;j <= 4;j++)        yxj[i][j] = 1;    for(i = 1;i <= 2;i++)    {        for(j = 3;j <= 5;j++)        {            yxj[i][j] = 0;        }    }    for(i = 3;i <= 4;i++)    {        for(j = 3;j <= 4;j++)        {            yxj[i][j] = 1;        }    }    yxj[3][5] = 0; yxj[4][5] = 0;    for(j = 6,i = 1;i <= 4;i++)    {        yxj[i][j] = 1;    }    yxj[5][6] = 2;    yxj[5][5] = 3;  yxj[6][5] = 3; yxj[6][6] = 3;}

 

初始化栈

void InitStack(bds_stack ** s,int n){    *s = (bds_stack*)malloc(sizeof(bds_stack));    (*s)->e = (double*)malloc(sizeof(double)*n);    (*s)->zd = 0;}

 

入栈

int Push_stack(bds_stack * s,double t){    int q;    s->e[s->zd] = t;    q = s->zd;    s->zd++;    return q;}

 

出栈

void Pop_stack(bds_stack * s,double * t){    (*t) = s->e[--(s->zd)];}

 

字符出栈计算

double Opt_num(){    double num1,num2,opt;    Pop_stack(dz,&num2);    Pop_stack(tz,&opt);    Pop_stack(dz,&num1);    if(opt == 1)    {        return num1 + num2;    }    if(opt == 2)    {        return num1 - num2;    }    if(opt == 3)    {        return num1 * num2;    }    if(opt == 4)    {        return num1 / num2;    }    return 0;}

 

优先级比较

void Cal(int n){    double r;    int k;    while(yxj[(int)(tz->e[tz->zd-1])][n] != 0)    {        if(yxj[(int)(tz->e[tz->zd-1])][n] == 1)        {            r = Opt_num();            Push_stack(dz,r);        }        else        {            printf("出错!\n");        }    }    Push_stack(tz,(double)n);}

 

ssh://git@git.coding.net:amberpass/szysgx.git

 

https://git.coding.net/amberpass/szysgx.git

 

结对编程体会:

  通过这几次结对编程,让我对合作有了更深的了解,也深刻意识到合作在软件开发中的重要作用,它不仅能够增加编程效率,让1+1不仅仅等于2,而且也增进了队友之间的感情,让我们在学习知识的同时也收获友谊。

2nd 简单四则运算更新