码迷,mamicode.com
首页 > 其他好文 > 详细

结对编程总结

时间:2016-04-10 22:36:51      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

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!\n");
        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!\nrealloc %d\n", STACK_INCREMENT);
        s->base = (float*)realloc(s->base, (s->stacksize + sizeof(FStack)));
        if (!s->base)
        {
            printf("overflow!\n");
            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!\n");
        return 0;
    }
    float e = *(s->top - 1);
    return e;
}

void Pop(FStack* s)
{
    if (s->top == s->base)
    {
        printf("stack is empty!\n");
        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!\n");
        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!\nrealloc %d\n", STACK_INCREMENT);
        s->base = (char*)realloc(s->base, (s->stacksize + sizeof(CStack)));
        if (!s->base)
        {
            printf("overflow!\n");
            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!\n");
        return 0;
    }
    char e = *(s->top - 1);
    return e;
}

void Pop(CStack* s)
{
    if (s->top == s->base)
    {
        printf("stack is empty!\n");
        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("\nDevided 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] = \0;
    *pos = i;
    fRes = atof(n);
    return fRes;
}

bool Check(char* str)
{
    int i = 0;
    while (str[i] != \0)
    {
        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!\n");
        exit(1);
    }
    for (i = 0; exp[i] != \0 && 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("\nThe result is:%f\n", GetTop(&fstack));
    Pop(&fstack);

    getchar();
    getchar();
    getchar();
}

 

5.总结:

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

结对编程总结

标签:

原文地址:http://www.cnblogs.com/handsonboy/p/5376301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!