分享
 
 
 

表达式,转换和计算,用C语言描述--Part4(源代码)

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Program #1

To convert Infix expression to Prefix and Postfix form using Stack

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <ctype.h>

#define MAX 50

char output[MAX] ;

char stack[MAX] ;

char input[MAX] ;

char *s, *t ; /*pointers to input and output strings*/

char ch; /*choice*/

int top; /*Stack top*/

int l ; /*length of infix string*/

/*Function Prototypes*/

void Initialize (void);

void SetExpression (char *);

char PopFromStack (void );

void PushOnStack (char);

int priority (char);

void ConvertToPrefix (void);

void ConvertToPostfix (void);

void main( )

{

clrscr( ) ;

Initialize ( ) ;

printf ( "\nEnter an expression in infix form: " ) ;

gets ( input ) ;

printf ( "\nSpecify output expression type, 1)Prefix 2)Postfix " ) ;

ch=getch();

SetExpression (input) ;

if(ch=='1') /*Infix->Prefix*/

{

strrev ( s );

ConvertToPrefix ( ) ;

strrev ( output ) ;

printf ( "\nThe Prefix expression is: " ) ;

}

else

{

ConvertToPostfix ( ) ;

printf ( "\nThe Postfix expression is: " ) ;

}

puts(output);

getch( ) ;

}

void Initialize (void)

{

top = -1 ;/*Make stack empty*/

strcpy ( output, "" ) ;

strcpy ( stack, "" ) ;

l = 0 ;

}

void SetExpression ( char *str )

{

s = str ;

l = strlen ( s ) ;

*( output + l ) = '\0' ;

t = output;

}

/* adds operator to the stack */

void PushOnStack ( char c )

{

if ( top == MAX - 1 )

printf ( "\nStack is full.\n" ) ;

else

{

top++ ;

stack[top] = c ;

}

}

/* pops an operator from the stack */

char PopFromStack (void )

{

if ( top == -1 ) /* Stack is empty*/

return -1 ;

else

{

char item = stack[top] ;

top-- ;

return item ;

}

}

/* returns the priotity of the operator */

int priority ( char c )

{

if ( c == '^' ) return 3 ;/*Exponential operator*/

if ( c == '*' || c == '/' || c == '%' ) return 2 ;

else if ( c == '+' || c == '-' ) return 1 ;

else return 0 ;

}

/* converts the Infix expression to Prefix form */

void ConvertToPrefix (void)

{

char opr ;

while ( *( s ) )

{

/*Skip white spaces, if any*/

if ( *( s ) == ' ' || *( s ) == '\t' )

{

s++ ;

continue ;

}

if ( isdigit ( *( s ) ) || isalpha ( *(

s ) ) )/*operands*/

{

while ( isdigit ( *( s ) ) || isalpha ( *

( s ) ) )

{

*( t ) = *( s ) ;

s++ ;

t++ ;

}

}

if ( *( s ) == ')' )/*Closing Parenthesis*/

{

PushOnStack ( *( s ) ) ;

s++ ;

}

if ( *( s ) == '*' || *( s ) == '+' || *( s )

== '/' || *( s ) == '%' || *( s ) == '-' || *( s )

== '^' )

{

if ( top != -1 )

{

opr = PopFromStack ( ) ;

while ( priority ( opr ) >

priority ( *( s ) ) )

{

*( t ) = opr ;

t++ ;

opr =

PopFromStack ( ) ;

}

PushOnStack ( opr ) ;

PushOnStack ( *( s ) ) ;

}

else PushOnStack ( *( s ) ) ;

s++ ;

}

if ( *( s ) == '(' )/*Opening Parenthesis*/

{

opr = PopFromStack ( ) ;

while ( opr != ')' )

{

*( t ) = opr ;

t++ ;

opr = PopFromStack ( ) ;

}

s++ ;

}

}

while ( top != -1 )/*While Stack is not empty*/

{

opr = PopFromStack ( ) ;

*( t ) = opr ;

t++ ;

}

t++ ;

}

/* converts the infix expr. to postfix form */

void ConvertToPostfix (void)

{

char opr ;

while ( *( s ) )

{ /*Skip white spaces, if any*/

if ( *( s ) == ' ' || *( s ) == '\t' )

{

s++ ;

continue ;

}

if ( isdigit ( *( s ) ) || isalpha ( *(

s ) ) )/*Operands*/

{

while ( isdigit ( *( s ) ) || isalpha ( *

( s ) ) )

{

*( t ) = *( s ) ;

s++ ;

t++ ;

}

}

if ( *( s ) == '(' )/*Opening Parenthesis*/

{

PushOnStack ( *( s ) ) ;

s++ ;

}

if ( *( s ) == '*' || *( s ) == '+' || *( s )

== '/' || *( s ) == '%' || *( s ) == '-' || *( s )

== '^' )

{

if ( top != -1 )

{

opr = PopFromStack ( ) ;

while ( priority ( opr ) >=

priority ( *( s ) ) )

{

*( t ) = opr ;

t++ ;

opr =

PopFromStack ( ) ;

}

PushOnStack ( opr ) ;

PushOnStack ( *( s ) ) ;

}

else PushOnStack ( *( s ) ) ;

s++ ;

}

if ( *( s ) == ')' )/*Closing Parenthesis*/

{

opr = PopFromStack ( ) ;

while ( opr != '(' )

{

*( t ) = opr ;

t++ ;

opr = PopFromStack ( ) ;

}

s++ ;

}

}

while ( top != -1 )/*While stack is not empty*/

{

opr = PopFromStack ( ) ;

*( t ) = opr ;

t++ ;

}

t++ ;

}

END OF PROGRAM #1

PROGRAM #2

/*Expression tree: Creation, Conversion and Evaluation.*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

#define MAX 50

struct node

{

struct node* left_child;

char x;

struct node* right_child;

} * stack[50];

int top = -1;

struct node* CreateExpTreePostfix(char*);

struct node* CreateExpTreePrefix(char*);

void preorder(struct node* sr);

void inorder(struct node* sr);

void postorder(struct node* sr);

int Evaluate(struct node* sr);

void push(struct node**, struct node*);

struct node* pop(struct node**);

void Delete_Tree(struct node*);

main()

{

struct node* root;

char str[50];

int z;

char ch;

clrscr();

printf("Input expression is:\n1)Prefix\n2)Postfix ");

ch=getche();

if(ch=='1')

{

printf("\nEnter Prefix Expression:");

gets(str);

root = CreateExpTreePrefix(str);

}

else

{

printf("\nEnter Postfix Expression:");

gets(str);

root = CreateExpTreePostfix(str);

}

printf("\nPrefix Exp. :");

preorder(root);

printf("\nInfix Exp. :");

inorder(root);

printf("\nPostfix Exp. :");

postorder(root);

z=Evaluate(root);

printf("\nExpression Evaluated to: %d", z);

Delete_Tree(root);

getch();

}

struct node* CreateExpTreePostfix(char* str)

{

struct node* nleft, * nright, * nodeptr;

while (*str)

{

nodeptr = (struct node *) malloc(sizeof(struct node));

nodeptr->x = *str;

if (*str == '+' || *str == '-' || *str == '/' ||

*str == '*' || *str == '^')

{

nright = pop(stack);

nleft = pop(stack);

nodeptr->left_child = nleft;

nodeptr->right_child = nright;

}

else

{

nodeptr->left_child = NULL;

nodeptr->right_child = NULL;

}

push(stack, nodeptr);

str++;

}

return pop(stack);

}

struct node* CreateExpTreePrefix(char* str)

{

struct node* nleft, * nright, * nodeptr;

strrev(str);

while (*str)

{

nodeptr = (struct node *) malloc(sizeof(struct node));

nodeptr->x=*str;

if (*str == '+' || *str == '-' || *str == '/' || *str

== '*' || *str == '^')

{

nleft = pop(stack);

nright = pop(stack);

nodeptr->left_child = nleft;

nodeptr->right_child = nright;

}

else

{

nodeptr->left_child = NULL;

nodeptr->right_child = NULL;

}

push(stack, nodeptr);

str++;

}

return pop(stack);

}

void inorder(struct node* sr)

{

if (sr != NULL)

{

inorder(sr -> left_child) ;

/* print the data of the node whose left child is NULL or the path

has already been traversed */

printf("%c", sr ->x) ;

inorder(sr -> right_child) ;

}

}

void preorder(struct node* sr)

{

if (sr != NULL)

{

/* print the data of a node */

printf("%c", sr -> x) ;

/* traverse till left child is not NULL */

preorder(sr -> left_child) ;

/* traverse till right child is not NULL */

preorder(sr -> right_child) ;

}

}

void postorder(struct node* sr)

{

if (sr != NULL)

{

postorder(sr -> left_child) ;

postorder(sr -> right_child) ;

printf("%c", sr -> x) ;

}

}

void push(struct node** stack, struct node* ptr)

{

if (top == MAX - 1)

printf("\nStack is full.\n") ;

else

{

top++ ;

stack[top] = ptr ;

}

}

/* pops an operator from the stack */

struct node* pop(struct node** stack)

{

if (top == -1)

{

printf("Stack is empty\n") ;

return -1 ;

}

else

{

struct node* ptr = stack[top] ;

top-- ;

return ptr ;

}

}

int Evaluate(struct node* sr)

{

int x,y,z;

if (sr != NULL)

{

if ( sr->x == '+' || sr->x == '-' || sr-

>x == '/' || sr->x == '*' || sr->x == '^' )

{

x = Evaluate(sr -> left_child) ;

y = Evaluate(sr -> right_child) ;

switch(sr->x)

{

case '+' : z=x+y; break;

case '-' : z=x-y; break;

case '*' : z=x*y; break;

case '/' : z=x/y; break;

case '^' : z=pow(x,y); break;

}

return z;

}

return (sr -> x - 48) ;

}

}

void Delete_Tree(struct node * root)

{

if(root!=NULL)

{

Delete_Tree(root->left_child);

Delete_Tree(root->right_child);

free(root);

}

}

END OF PROGRAM #2

全文完。

作者:Pradeep P. Chandiramani.

联系方式:pradeepchandiramani@yahoo.co.in

译者:liudows

联系方式:mysea000@163.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有