check_chang.c
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:7k
源码类别:
控制台编程
开发平台:
C/C++
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define OK 1
- #define TRUE 1
- #define FALSE 0
- #define ERROR 0
- #define OVERFLOW -2
- #define OPSETSIZE 7
- #define STACK_INIF_SIZE 50
- #define STACKINCREMENT 10
- typedef int status;
- typedef struct sqlist{
- int bol;
- int num_ch;
- struct sqlist *next;
- }sqlist;
- typedef struct sqstack{
- int *base;
- int *top;
- int stacksize;
- }sqstack;
- unsigned char Prior[7][7] = { // 表3.1 算符间的优先关系
- '>','>','<','<','<','>','>',
- '>','>','<','<','<','>','>',
- '>','>','>','>','<','>','>',
- '>','>','>','>','<','>','>',
- '<','<','<','<','<','=',' ',
- '>','>','>','>',' ','>','>',
- '<','<','<','<','<',' ','='
- };
- char OPSET[OPSETSIZE]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'};
- status init_sq(sqlist *l){
- // sqlist *l;
- l=(sqlist*)malloc(sizeof(sqlist));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next=NULL;
- return OK;
- }
- status insert_sq(sqlist **p,int e,int bl){
- sqlist *q;
- q=(sqlist*)malloc(sizeof(sqlist));
- q->num_ch=e;
- q->bol=bl;
- q->next=NULL;
- (*p)->next=q;
- (*p)=(*p)->next;
- printf("insert %d,%dis succssed!n",e,bl);
- return OK;
- }
- /*status chang2(char *s , sqlist *l)
- {
- unsigned int t;
- //data da;
- sqlist *p=l;
- int a,bl;
- //char *s=ss;
- for(t=0;t<strlen(s);t++){
- if(s[t]>47&&s[t]<58){
- a=s[t]-48;
- bl=0;
- insert_sq(&p,a,bl);
- }
- else
- {
- a=s[t];
- bl=1;
- insert_sq(&p,a,bl);
- }
- }
- return OK;
- }*/
- status chang(char *s,sqlist *l){
- int t=0;
- unsigned int i=0;
- int bl,ch;
- int a1,a2,a;
- sqlist *p=l;
- for (;i<strlen(s);i++){
- if(s[i]>47&&s[i]<58&&t==0){
- a1=(int)s[i]-48;
- t++;
- }
- else if(s[i]>47&&s[i]<58&&t==1){
- a2=(int)s[i]-48;
- a=a1*10+a2;
- t++;
- }
- else if(s[i]<48&&s[i]>39&&s[i]!=44&&s[i]!=46){
- if(t==1){
- bl=0;
- insert_sq(&p,a1,bl);
- t=0;
- }
- else if(t==2){
- bl=0;
- insert_sq(&p,a,bl);
- t=0;
- }
- bl=1;
- ch=(int)s[i];
- insert_sq(&p,ch,bl);
- t=0;
- }
- printf("the i is %dn",i);
- } //end for
- i=strlen(s)-1;
- if(s[i]>47&&s[i]<58){
- if(s[i-1]>47&&s[i-1]<58){
- bl=0;
- insert_sq(&p,a,bl);
- }
- else {
- bl=0;
- insert_sq(&p,a1,bl);
- }
- }
- bl=1;
- a=35;
- insert_sq(&p,a,bl);
- printf("chang is OKn");
- return OK;
- }
- status print_sq(sqlist *l)
- {
- sqlist *p=l;
- if(l){
- for (p=p->next ;p!=NULL;p=p->next){
- printf("%d,%dn",p->num_ch,p->bol );
- //printf("OK!");
- }
- }
- else printf("空!");
- return OK;
- }
- int Operate(int a,int theta, int b) {
- printf("a=%d,theta=%c,b=%dn",a,theta,b);
- switch(theta) {
- case 43: return a+b;
- case 45: return a-b;
- case 42: return a*b;
- case 47:
- {if(b==0){
- printf("除数不能为0!n");
- exit(ERROR);
- }
- else return a/b;
- }
- default : return 0;
- }
- }
- int ReturnOpOrd(char op,char* TestOp) {
- int i;
- for(i=0; i< OPSETSIZE; i++) {
- if (op == TestOp[i]) return i;
- }
- return 0;
- }
- char precede(char Aop, char Bop) {
- printf("%cn",Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)]);
- printf("%c, %cn",Aop,Bop);
- return Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)];
- }
- status initstack(sqstack *s){
- (s)->base = (int*)malloc(STACK_INIF_SIZE*sizeof(int));
- if((s)->base==NULL) exit(OVERFLOW);
- (s)->top=(s)->base;
- (s)->stacksize = STACK_INIF_SIZE;
- printf("initstack is finished!n");
- return OK;
- }
- int gettop(sqstack *s){
- int e;
- if(s->top==s->base){
- printf("NULL1n");
- return 0;
- }
- e=*(s->top-1);
- printf("gettop %d is OK!n",e);
- return e;
- }
- status push(sqstack *s,int e){
- if(s->top-s->base>=s->stacksize){
- s->base=(int*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(int));
- if(!s->base) exit(OVERFLOW);
- s->stacksize+= STACKINCREMENT;
- }
- *(s->top++)=e;
- printf("push %d is OKn",e);
- return OK;
- }
- status pop(sqstack *s,int *e){
- if(s->top==s->base){
- printf("NULL2n");
- return ERROR;
- }
- *e=*(--s->top);
- printf("pop %d is OK!n",*e);
- return OK;
- }
- int EvaluateExpression(char* MyExpression) { // 算法3.4
- // 算术表达式求值的算符优先算法。
- // 设OPTR和&&OPND分别为运算符栈和运算数栈,OP为运算符集合。
- int result;
- sqstack OPTR; // 运算符栈,字符元素
- sqstack OPND; // 运算数栈,实数元素
- // char TempData[30];
- int c,bl,a,b,theta,top;
- //char ch;
- sqlist *q,l;
- char *s=MyExpression;
- init_sq(&l);
- chang(s,&l);
- q=&l;
- initstack(&OPTR);
- push(&OPTR, 35);
- initstack (&OPND);
- q=q->next;
- c=q->num_ch;
- bl=q->bol;
- // strcpy(TempData,"