zoukankan      html  css  js  c++  java
  • 结对编程总结

    1.结对编程题目是:面向小学生,随机生成30个100以内的四则运算,并且用户可以录入结果,检查用户录入的结果是否是正确的。

    2.结对编程对象:叶小娟,代码共计:326行

    3.

    4.代码

    #include <stdio.h>  //计算器
    #include <malloc.h>
    #include <stdlib.h>
    
    #define STACK_SIZE        100    //max size of the stack
    #define STACK_INCREMENT    10    //realloc size
    
    typedef struct FStack  //stack of int
    {
        float* base;
        float* top;
        int stacksize;
    }FStack;
    
    void Init(FStack* s)
    {
        s->base = (float*)malloc(STACK_SIZE * sizeof(FStack));
        if (!s->base)
        {
            printf("overflow!
    ");
            return;
        }
        s->top = s->base;
        s->stacksize = STACK_SIZE;
    }
    
    bool isEmpty(FStack* s)
    {
        if (s->top == s->base)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void Push(FStack* s, float e)
    {
        if (s->top - s->base >= s->stacksize)
        {
            printf("stack is full!
    realloc %d
    ", STACK_INCREMENT);
            s->base = (float*)realloc(s->base, (s->stacksize + sizeof(FStack)));
            if (!s->base)
            {
                printf("overflow!
    ");
                return;
            }
            s->top = s->base + s->stacksize;
            s->stacksize += STACK_INCREMENT;
        }
        *(s->top) = e;
        (s->top)++;
    }
    
    float GetTop(FStack* s)
    {
        if (s->top == s->base)
        {
            printf("stack is empty!
    ");
            return 0;
        }
        float e = *(s->top - 1);
        return e;
    }
    
    void Pop(FStack* s)
    {
        if (s->top == s->base)
        {
            printf("stack is empty!
    ");
            return;
        }
        s->top--;
    }
    
    typedef struct CStack  //stack of char
    {
        char* base;
        char* top;
        int stacksize;
    }CStack;
    
    void Init(CStack* s)
    {
        s->base = (char*)malloc(STACK_SIZE * sizeof(CStack));
        if (!s->base)
        {
            printf("overflow!
    ");
            return;
        }
        s->top = s->base;
        s->stacksize = STACK_SIZE;
    }
    
    bool isEmpty(CStack* s)
    {
        if (s->top == s->base)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void Push(CStack* s, int e)
    {
        if (s->top - s->base >= s->stacksize)
        {
            printf("stack is full!
    realloc %d
    ", STACK_INCREMENT);
            s->base = (char*)realloc(s->base, (s->stacksize + sizeof(CStack)));
            if (!s->base)
            {
                printf("overflow!
    ");
                return;
            }
            s->top = s->base + s->stacksize;
            s->stacksize += STACK_INCREMENT;
        }
        *(s->top) = e;
        (s->top)++;
    }
    
    char GetTop(CStack* s)
    {
        if (s->top == s->base)
        {
            printf("stack is empty!
    ");
            return 0;
        }
        char e = *(s->top - 1);
        return e;
    }
    
    void Pop(CStack* s)
    {
        if (s->top == s->base)
        {
            printf("stack is empty!
    ");
            return;
        }
        s->top--;
    }
    
    bool isOper(char ch)
    {
        if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%')
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    int Priority(char ch)
    {
        int p;
        switch (ch)
        {
        case '(':
            p = 0;
            break;
        case '+':
        case '-':
            p = 1;
            break;
        case '*':
        case '/':
        case '%':
            p = 2;
            break;
        }
        return p;
    }
    
    float Calculate(float f1, float f2, char oper)
    {
        float f3;
        switch (oper)
        {
        case '+':
            f3 = f1 + f2;
            break;
        case '-':
            f3 = f1 - f2;
            break;
        case '*':
            f3 = f1 * f2;
            break;
        case '%':
            f3 = (float)((int)f1 % (int)f2);
            break;
        case '/':
            if (f2 == 0)
            {
                printf("
    Devided by zero!");
                exit(1);
            }
            else
            {
                f3 = f1 / f2;
            }
            break;
        }
        return f3;
    }
    
    float StrtoFloat(char* str, int* pos)
    {
        float fRes;
        int i = *pos;
        int k;
        char n[50];
        for (k = 0; str[i] >= '0' && str[i] <= '9' || str[i] == '.'; i++, k++)
        {
            n[k] = str[i];
        }
        n[k] = '';
        *pos = i;
        fRes = atof(n);
        return fRes;
    }
    
    bool Check(char* str)
    {
        int i = 0;
        while (str[i] != '')
        {
            if (str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '%' && str[i] != '.' && str[i] != '(' && str[i] != ')' && (str[i] < '0' || str[i] > '9'))
            {
                return false;
            }
            i++;
        }
        return true;
    }
    
    void main()
    {
        char exp[100];
        int i;
        float f, f1, f2;
        char oper;
        FStack fstack;
        CStack cstack;
        Init(&fstack);
        Init(&cstack);
        printf("The expression is:");
        gets(exp);
        if (!Check(exp))
        {
            printf("input error! exit now!
    ");
            exit(1);
        }
        for (i = 0; exp[i] != '' && exp[i] != -52; i++)
        {
            if (!isOper(exp[i]))
            {
                f = StrtoFloat(exp, &i);
                Push(&fstack, f);
            }
            if (isOper(exp[i]))
            {
                if (!isEmpty(&cstack))
                {
                    while (!isEmpty(&cstack) && Priority(exp[i]) <= Priority(GetTop(&cstack)))
                    {
                        oper = GetTop(&cstack);
                        Pop(&cstack);
                        f2 = GetTop(&fstack);
                        Pop(&fstack);
                        f1 = GetTop(&fstack);
                        Pop(&fstack);
                        f = Calculate(f1, f2, oper);
                        Push(&fstack, f);
                    }
                    Push(&cstack, exp[i]);
                }
                else
                {
                    Push(&cstack, exp[i]);
                }
            }
            else if (exp[i] == '(')
            {
                Push(&cstack, exp[i]);
            }
            else if (exp[i] == ')')
            {
                while (GetTop(&cstack) != '(' && !isEmpty(&cstack))
                {
                    oper = GetTop(&cstack);
                    Pop(&cstack);
                    f2 = GetTop(&fstack);
                    Pop(&fstack);
                    f1 = GetTop(&fstack);
                    Pop(&fstack);
                    f = Calculate(f1, f2, oper);
                    Push(&fstack, f);
                }
                Pop(&cstack);
            }
        }
        while (!isEmpty(&cstack))
        {
            oper = GetTop(&cstack);
            Pop(&cstack);
            f2 = GetTop(&fstack);
            Pop(&fstack);
            f1 = GetTop(&fstack);
            Pop(&fstack);
            f = Calculate(f1, f2, oper);
            Push(&fstack, f);
        }
        printf("
    The result is:%f
    ", GetTop(&fstack));
        Pop(&fstack);
    
        getchar();
        getchar();
        getchar();
    }

    5.总结:

    通过这次项目的实践,我改变了对结对编程的想法。

  • 相关阅读:
    简单有效 四招教你保护好WiFi网络安全
    WP8手机安装《神庙逃亡》的教程
    如何关闭iOS7中的iPhone广告跟踪系统
    如何隐藏任务栏图标
    MVC中Controller和Action讲解上篇
    node.js
    express
    node.js+socket.io安装
    自定义view文字垂直居中
    AndroidTouch事件总结
  • 原文地址:https://www.cnblogs.com/handsonboy/p/5376301.html
Copyright © 2011-2022 走看看