| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 979 人关注过本帖
标题:初学C,照着书上写了个小程序,无奈编译通过,链接OK,运行出错。。
只看楼主 加入收藏
扮猪吃老虎
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-22
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
初学C,照着书上写了个小程序,无奈编译通过,链接OK,运行出错。。
本来想自己多看看书,多百毒下,哎。。。调了几天完全没进展,所以来贵论坛求助额。。第一次发帖不懂规矩请见谅。。。。。。

书上给的程序是在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;
    }
}

[ 本帖最后由 扮猪吃老虎 于 2010-11-22 17:01 编辑 ]
搜索更多相关主题的帖子: 链接 编译 初学 运行 
2010-11-22 16:45
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:10 
VC能用啊? 什么错误,难道是程序逻辑错误 还是?
2010-11-22 17:48
扮猪吃老虎
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-22
收藏
得分:0 
...我在VC下编译和链接能通过啊  没问题  结果出现

Debug assertion failed
 _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

然后就自动关闭了。。。。
好无解啊。。。。。
2010-11-22 20:58
cacker
该用户已被删除
收藏
得分:10 
提示: 作者被禁止或删除 内容自动屏蔽
2010-11-22 21:18
扮猪吃老虎
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-22
收藏
得分:0 
重复释放?  我好想记得只写了1句释放内存额。。。。
2010-11-22 21:28
扮猪吃老虎
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-22
收藏
得分:0 
....求解啊。。。。。头痛。。。。
2010-11-23 09:33
快速回复:初学C,照着书上写了个小程序,无奈编译通过,链接OK,运行出错。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013480 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved