| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 496 人关注过本帖
标题:請教:簡單的表達式運算為什麽不能寫入內存?
只看楼主 加入收藏
maizijiankan
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-9-22
收藏
 问题点数:0 回复次数:1 
請教:簡單的表達式運算為什麽不能寫入內存?
#include "stdio.h"
#include "string.h"
#include "conio.h"
#include "stdlib.h"
#define STACK_INIT_SIZE 100
#define STACKINREMENT 20
#define FALSE 0
#define TRUE 1
typedef struct{
     int *pBase;
     int *pTop;
     int StackSize;
}Stack;

typedef int BOOLEAN;
char Operator[8]={"+-*/()#"};
char Optr;
int Opnd=-1;
int Result;

char PriorityTable[7][7]=
{
    {'>','>','<','<','<','>','>'},
    {'>','>','<','<','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'<','<','<','<','<','=','0'},
    {'>','>','>','>','0','>','>'},
    {'<','<','<','<','<','0','='},

};

Stack InitStack()
{
Stack S;
S.pBase=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
   if(!S.pBase)
{
printf("失敗\n");
exit(-1);
}
   else
{
     S.pTop=S.pBase;
     S.StackSize=STACK_INIT_SIZE;
   }
    return S;
}

void DestoryStack(Stack *S)
{
    if(S->pBase)
    {
        free(S->pBase);
        S->pTop=S->pBase=NULL;
    }  
}
int GetTop(Stack S)
{
    return *(S.pTop-1);
}
int Push(Stack *S,int e)
{
    if (S->pTop-S->pBase==S->StackSize) {
    
        S->pBase=(int*)realloc(S->pBase,S->StackSize+STACKINREMENT*sizeof(int));
    if(!S->pBase)
    return 0;
    S->pTop=S->pBase+S->StackSize;
    S->StackSize+=STACKINREMENT;
    }
    *(S->pTop++)=e;
    return 1;
}

int Pop(Stack *S,int *e) {
    if(S->pTop==S->pBase)
        return 0;
    *e=*--(S->pTop);
        return 1;
}

char CheckPriority(char Operator_1,char Operator_2) {
int i,j;

    i=strchr(Operator,Operator_1)-Operator;
    j=strchr(Operator,Operator_2)-Operator;
        return PriorityTable[i][j];
}

BOOLEAN IsOperator(char ch) {
    if(strchr(Operator,ch))
        return TRUE;
    else
        return FALSE;
}
void GetInput(void) {
     char ch;
     char Buffer[100];
     int index;
         index=0;
         ch=getch();
         while (ch!=13&&IsOperator(ch)) {
             if(ch>='0'&&ch<='9') {
             printf("%c",ch);
             Buffer[index]=ch;
             index++;
             }
             ch=getch();
         }
         if(ch==13)
             Optr='#';
         else{
         Optr=ch;
         printf("%c",ch);
         }
         if(index>0) {
         Buffer[index]='\0';
         Opnd=atoi((Buffer));
         }
         else
             Opnd=-1;
}

int Calc (int a ,char theta, int b) {
    switch(theta){

     case '+':
         return a+b;
     case '-':
         return a-b;
     case '*':
         return a*b;
     default:
         if(b==0){
         printf("除數不能為零\n");
         return 0;
         }
         else
         return a/b;
}
}

BOOLEAN EvaluateExpression() {

int temp;
char theta;
int itheta;
int a,b;

Stack OPTR=InitStack();
Stack OPND=InitStack();
GetInput();

while(Optr!='#'||GetTop(OPTR)!='#') {
if (Opnd>=0) Push(&OPND,Opnd);
switch(CheckPriority(GetTop(OPTR),Optr)) {
   case '=':
         Pop(&OPTR,&temp);GetInput();
   break;
   case '<':
       if(!Push(&OPTR,Optr))
           return FALSE;
       GetInput();
       break;
   case '>':
       Pop(&OPTR,&itheta);
       Pop(&OPND,&b);
       Pop(&OPND,&a);
       theta=(char)(itheta);
       Push(&OPND,Calc(a,itheta,b));
       break;
}
}

if(OPND.pTop==OPND.pBase&&Opnd<0) {
    printf("\n\n謝謝\n");

}
else if (OPND.pTop==OPND.pBase) {
Result=GetTop(OPND);
DestoryStack(&OPND);
DestoryStack(&OPTR);
}
return TRUE;
}
void main(void) {

   if(EvaluateExpression())
        printf("=%d\n",Result);
   else
        printf("計算有錯\n");
}



編譯沒有問題但是運行的時候就會出現:

“0x00401355”指令引用的“0xfe30684c”內存。內存不能為“read”。的錯誤.
有哪位大蝦辦一下我的 小弟無限感激!!!

截图00.jpg (14.73 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册


截图01.jpg (27.92 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: int define include char 
2008-04-22 14:47
maizijiankan
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-9-22
收藏
得分:0 
哪位大蝦幫幫我啊 我等到花兒也謝啦!
2008-04-22 19:10
快速回复:請教:簡單的表達式運算為什麽不能寫入內存?
数据加载中...
 
   



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

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