数据结构课程设计表达式求值.docx

上传人:b****5 文档编号:2841214 上传时间:2022-11-15 格式:DOCX 页数:17 大小:18.84KB
下载 相关 举报
数据结构课程设计表达式求值.docx_第1页
第1页 / 共17页
数据结构课程设计表达式求值.docx_第2页
第2页 / 共17页
数据结构课程设计表达式求值.docx_第3页
第3页 / 共17页
数据结构课程设计表达式求值.docx_第4页
第4页 / 共17页
数据结构课程设计表达式求值.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

数据结构课程设计表达式求值.docx

《数据结构课程设计表达式求值.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计表达式求值.docx(17页珍藏版)》请在冰豆网上搜索。

数据结构课程设计表达式求值.docx

数据结构课程设计表达式求值

1.ch0201:

表达式求值,在VC++6.0环境下测试通过

●文件main.c:

案例源程序;

●文件input.txt:

案例测试输入数据文件;

●文件output.txt:

案例测试输出结果文件;

2源代码

#include

#include

#include

intPrintError=0;

/*全局变量,0代表正常,1代表表达式出错*/

/*char类型链表式堆栈,用来存放运算符号,以及用在中缀表达式转换等时候*/

typedefstructNode*PtrToNode;

typedefPtrToNodeStack;

intIsEmpty(StackS);

voidMakeEmpty(StackS);

voidPush(charX,StackS);

charTop(StackS);

voidPop(StackS);

typedefstructNode{

charElement;

PtrToNodeNext;

};

/*float类型链表式堆栈,用来存放操作数*/

typedefstructFNode*Ptr_Fn;

typedefPtr_FnFStack;

intFisEmpty(FStackS);

voidFPush(floatX,FStackS);

floatFTop(FStackS);

voidFPop(FStackS);

typedefstructFNode{

floatElement;

Ptr_FnNext;

};

voidConvertToPost(FILE*In,StackWhereat,FILE*Temp);

voidReverse(StackRev);

voidCalculate(FILE*Change,StackWhereat,FILE*Temp);

/******主函数******/

intmain()

{

FILE*InputFile,*OutputFile,*Temp;/*初始化变量*/

StackWhereat;

charsample;

InputFile=fopen("Input.txt","r");/*打开文件*/

OutputFile=fopen("Output.txt","w");

Whereat=malloc(sizeof(structNode));/*给Whereat分配空间*/

Whereat->Next=NULL;

if(!

InputFile||!

OutputFile){/*错误处理*/

printf("intputoroutputfile(s)donotexist.\n");

return

(1);

}

sample=getc(InputFile);

while(sample!

=EOF){

Temp=fopen("Temp.txt","w+");/*生成Temp文件*/

ungetc(sample,InputFile);/*putbacksample字符*/

ConvertToPost(InputFile,Whereat,Temp);/*中缀变后缀*/

if(PrintError){/*错误处理*/

fprintf(OutputFile,"Errorininfixnotation.");

fscanf(InputFile,"\n",&sample);

PrintError=0;

}

elseif(IsEmpty(Whereat)==1){/*跳过在input文件中的空格*/

}

elseif(IsEmpty(Whereat)!

=1){

Reverse(Whereat);

if(Top(Whereat)=='B'){/*错误处理,*/

/*A表示操作数B表示运算符*/

PrintError=1;/*后缀表达式第一个元素应是操作数而不是运算符号*/

}

fclose(Temp);

Temp=fopen("Temp.txt","r+");

Calculate(OutputFile,Whereat,Temp);/*计算结果*/

}

fclose(Temp);

MakeEmpty(Whereat);/*清空Whereat用来处理下一行*/

putc('\n',OutputFile);/*在输出文件中换行*/

sample=getc(InputFile);

}/*While循环结束*/

free(Whereat);

fclose(InputFile);

fclose(OutputFile);

remove("Temp.txt");/*删除Temp.txt*/

return1;

}

/******检查堆栈是否为空******/

intIsEmpty(StackS)

{

return(S->Next==NULL);

}

/******检查float堆栈是否为空******/

intFIsEmpty(FStackS)

{

return(S->Next==NULL);

}

/******弹出栈顶元素******/

voidPop(StackS)

{

PtrToNodeFirstCell;

if(IsEmpty(S))

perror("EmptyStack");

else{

FirstCell=S->Next;

S->Next=S->Next->Next;

free(FirstCell);

}

}

/******弹出float栈顶元素******/

voidFPop(FStackS)

{

Ptr_FnFirstCell;

if(FIsEmpty(S))

perror("EmptyStack");

else{

FirstCell=S->Next;

S->Next=S->Next->Next;

free(FirstCell);

}

}

/******将堆栈置空******/

voidMakeEmpty(StackS)

{

if(S==NULL)

perror("MustuseCreatestackfirst");

else

while(!

IsEmpty(S))

Pop(S);

}

/******将float堆栈置空******/

voidFMakeEmpty(FStackS)

{

if(S==NULL)

perror("MustuseCreatestackfirst");

else

while(!

IsEmpty(S))

Pop(S);

}

/******元素进栈******/

voidPush(charX,StackS)

{

PtrToNodeTmpCell;

TmpCell=(PtrToNode)malloc(sizeof(structNode));

if(TmpCell==NULL)

perror("OutofSpace!

");

else{

TmpCell->Element=X;

TmpCell->Next=S->Next;

S->Next=TmpCell;

}

}

/******float元素进栈******/

voidFPush(floatX,FStackS)

{

Ptr_FnTmpCell;

TmpCell=(Ptr_Fn)malloc(sizeof(structFNode));

if(TmpCell==NULL)

perror("OutofSpace!

");

else{

TmpCell->Element=X;

TmpCell->Next=S->Next;

S->Next=TmpCell;

}

}

/******返回栈顶元素******/

charTop(StackS)

{

if(!

IsEmpty(S))

returnS->Next->Element;

perror("EmptyStack");

exit

(1);

return0;

}

/******返回float栈顶元素******/

floatFTop(FStackS)

{

if(!

FIsEmpty(S))

returnS->Next->Element;

perror("EmptyStack");

exit

(1);

return0;

}

/******将堆栈元素倒置******/

voidReverse(StackRev)

{

StackTempstack;

Tempstack=malloc(sizeof(structNode));

Tempstack->Next=NULL;

while(!

IsEmpty(Rev)){

Push(Top(Rev),Tempstack);/*将元素压栈到一个临时堆栈*/

Pop(Rev);

}

Rev->Next=Tempstack->Next;/*指向新的堆栈*/

}

/*******

Whereat说明:

Whereat记录了操作数和运算符号的位置,用A和B区分。

A=operand,B=operator.

(例如1+2转换成12+,在whereat中的形式应该是AAB)

OpHolder说明:

Char类型的堆栈Opholder用来保存运算符号。

******/

/******将中缀表带式转换为后缀表达式******/

voidConvertToPost(FILE*In,StackWhereat,FILE*Temp)

{

StackOpHolder;

charholder;

charlastseen;

intdigitcounter=0;/*操作数的计数器*/

OpHolder=malloc(sizeof(structNode));/*初始化*/

OpHolder->Next=NULL;

holder=getc(In);

lastseen='@';/*用来防止输入格式错误,例如两个小数点*/

putc('',Temp);

while((holder!

='\n')&&(holder!

=EOF)){

if(holder==''){

digitcounter=0;

}

elseif(IsOperator(holder)==-1){/*如果holder不是操作数或运算符号*/

PrintError=1;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1