本来想自己多看看书,多百毒下,哎。。。调了几天完全没进展,所以来贵论坛求助额。。第一次发帖不懂规矩请见谅。。。。。。

书上给的程序是在TC下运行的,我实在VC6.0下写的,一些在TB下能用但是VC下不能用的东西我都改了下

程序大致的意思是,随机给出4个数,算24。功能是检查用户输入的格式合法,然后把中缀式转换为后缀式储存在一个数组中,然后计算后缀式判断对错。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>//用时间来当种子
//#include <alloc.h> random函数,alloc.h只能在TC下使用
#include <ctype.h>//包含几个判断类型函数(isdigit(),isspace(),isalpha()等)
#define BUFFSIZE 32    
/*堆栈函数*/
typedef struct node
{
    int data;
    struct node *next;
}stack;
stack *Push(stack *top,int x)
{
    stack *p;
    p=(stack *)malloc(sizeof(stack));
    top=p;
    if(p==NULL)
    {
        printf("ERROR\n!");
        exit(0);
    }
    p->data=x;
    p->next=top;
    top=p;

    return top;
}//入栈函数
stack *Pop(stack *top)
{
    stack *q;
    q=top;
    top=top->next;
    free(q);
    return top;
}//出栈函数
int  Gettop(stack *top)
{
    if(top==NULL)
    {
        printf("The stack is null\n");
        return 0;
    }
    return top->data;
}//读栈顶元素
stack *Getdeltop(stack *top,int *x)
{
    *x=Gettop(top);
    top=Pop(top);
    return top;
}//取栈顶元素,并删除栈顶元素
int Empty(stack *top)
{
    if(top==NULL)
        return 1;
    return 0;
}//判断栈是否为空
/*中缀式转换为后缀式*/
void ExpressTrasform(char *expMiddle,char *expBack)
{
    int Tras[100];
    stack *top=NULL;
    int i=0,j=0;
    int ch;
    while(expMiddle[i]!='\0')
    {
        if(isdigit(expMiddle[i]))
        {
            do
            {
                expBack[j]=expMiddle[i];
                j++;
                i++;
            }
            while(expMiddle[i]!='.');//点的算法
            expBack[j]='.';//点的算法
            j++;
        }
            if(expMiddle[i]=='(')
                top=Push(top,expMiddle[i]);
            if(expMiddle[i]==')')
            {
                top=Getdeltop(top,&ch);
                if(ch!='(')
                {
                    expBack[j]=ch;
                    j++;
                    top=Getdeltop(top,&ch);
                }
            }
        if(expMiddle[i]=='+'||expMiddle[i]=='-')
        {
            if(!Empty(top))
            {
                ch=Gettop(top);
                while(ch!='(')
                {
                    expBack[j]=ch;
                    j++;
                    top=Pop(top);
                    if(Empty(top))
                        break;
                    else
                        ch=Gettop(top);
                }
            }
            top=Push(top,expMiddle[i]);
        }
        if(expMiddle[i]=='*'||expMiddle[i]=='/')
        {
            if(!Empty(top))
            {
                ch=Gettop(top);
                while(ch=='*'||ch=='/')
                {
                    expBack[j]=ch;
                    j++;
                    top=Pop(top);
                    if(Empty(top))
                        break;
                    else
                        ch=Gettop(top);
                }
            }
            top=Push(top,expMiddle[i]);
        }
        i++;
        }
        
    while(!Empty(top))
         
    for(i=0;i<=j;i++)
    {
        Tras[i]=(int)expBack[j];
        j++;
    }
        top=Getdeltop(top,&Tras[i++]);
        expBack[j]='\0';
}
/*后缀式的计算*/
int ExpressComputer(char *s)
{
    stack *top=NULL;
    int i,k,num1,num2,result;
    i=0;
    while(s[i]!='\0')
    {
        if(isdigit(s[i]))
        {
            k=0;
            do
            {
                k=10*k+s[i]-48;//字符0的ASCLL码为48
                    i++;
            }while(s[i]!='.');
            top=Push(top,k);
        }
        if(s[i]=='-')
        {
            top=Getdeltop(top,&num2);
            top=Getdeltop(top,&num1);
            result=num1-num2;
            top=Push(top,result);
        }
        if(s[i]=='*')
        {
            top=Getdeltop(top,&num2);
            top=Getdeltop(top,&num1);
            result=num2*num1;
            top=Push(top,result);
        }
        if(s[i]=='/')
        {
            top=Getdeltop(top,&num2);
            top=Getdeltop(top,&num1);
            result=num1/num2;
            top=Push(top,result);
        }
        if(s[i]=='+')
        {
            top=Getdeltop(top,&num2);
            top=Getdeltop(top,&num1);
            result=num2+num1;
            top=Push(top,result);
        }
        i++;
    }
    top=Getdeltop(top,&result);
    return result;
}
/*随机发4张牌,产生4个随机数*/
void Gencard()
{
    int num,i;
    srand(time(0));
        for(i=0;i<4;i++)
        {
            num=rand()%13+1;
            printf("%d  ",num);
        }
}
/*检查输入的表达式是否正确*/
int CheckExpression(char *expMiddle)
{
    int i=0;
    while(expMiddle[i]!='\0')
    {
        if(isdigit(expMiddle[i]))
        {    if(isdigit(expMiddle[i+1]))
            {
                i++;
                continue;
            }
            if(expMiddle[i+1]!='.')
            {
                printf("\n错误的格式!!");
                return 0;
            }
            i++;
    }
    i++;
}
return 1;
}
void main()
{
    char expMiddle[BUFFSIZE],expBack[BUFFSIZE],ch;
    int result;
    system("cls");
    printf("****************************************************************\n");
    printf("|                      欢迎参加24点游戏                        |\n");
    printf("|                   请按照以下格式输入算式                     |\n");
    printf("|                       (13.-4.)*2.+6.                         |\n");
    printf("****************************************************************\n");
    while(1)
    {
        printf("随机产生4个数\n");
        Gencard();
        printf("\n");
        do
        {
            printf("请按照输入算式:\n");
            scanf("%s%c",expMiddle,&ch);
        }while(!CheckExpression(expMiddle));
        ExpressTrasform(expMiddle,expBack);
        result=ExpressComputer(expBack);
        printf("%s=%d",expMiddle,result);
        if(result==24)
            printf("恭喜,回答正确!\n");
        else
            printf("对不起,计算错误。\n");
            printf("想再尝试下吗?\n");
            scanf("%c",&ch);
            if(ch=='n'||ch=='N')
                break;
    }
}