📄 check_chang.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 %d\n",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 OK\n");
return OK;
}
status print_sq(sqlist *l)
{
sqlist *p=l;
if(l){
for (p=p->next ;p!=NULL;p=p->next){
printf("%d,%d\n",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=%d\n",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("%c\n",Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)]);
printf("%c, %c\n",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("NULL1\n");
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 OK\n",e);
return OK;
}
status pop(sqstack *s,int *e){
if(s->top==s->base){
printf("NULL2\n");
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,"\0");
while ((c!= 35 || gettop(&OPTR)!=35)){
printf("while start!\n");
if (bl!=1) {
push(&OPND, c);
q=q->next;
c=q->num_ch;
bl=q->bol;
} // 不是运算符则进栈
else {
// ch=(char)c;
top=gettop(&OPTR);
// topch=(char)top;
printf("top %c",top);
switch (precede(top, c)) {
// c2=c;
case '<':
printf("case <!\n"); // 栈顶元素优先权低
push(&OPTR, c);
// if(q=q->next!=NULL){
q=q->next;
c=q->num_ch;
bl=q->bol;
// }
break;
case '=': // 脱括号并接收下一字符
pop(&OPTR, &c);
// if(q=q->next!=NULL){
q=q->next;
c=q->num_ch;
bl=q->bol;
// }
break;
case '>': // 退栈并将运算结果入栈
pop(&OPTR, &theta);
pop(&OPND, &b);
pop(&OPND, &a);
//thetach=(char)theta;
printf("q->num_ch is %d\n",q->num_ch);
push(&OPND, Operate(a, theta, b));
printf("%d\n",q);
/* if (q->num_ch !=35){
q=q->next;
c=q->num_ch;
bl=q->bol;
printf("%d\n",c);*/
// }
break;
default :
printf("NO this oper!\n");
return 0;
} // switch
}//else
} // while
result=gettop(&OPND);
return result;
} // EvaluateExpression
void main(){
char *s="5*(7-1)-6";
int result,t=1;
int numer[4];
printf("/***************************************************/\n");
printf(" 24点游戏 dos 版本\n");
printf(" 北京工商大学微机024班 欧阳锦林 版权所有\n");
printf(" 完成于2004年4月15日\n");
printf("/***************************************************/\n");
printf("The four numbers are 3,4,A,Q\nWhen you input the formula,\n");
printf("you should look A as 1,J as 11,Q as 12,k as 13\n");
while(t!=0){
// gets(s);
result=EvaluateExpression(s);
if(result==24){
printf("Very good!!It's right.\n");
}
else {
printf("sorry!You are wrong!Thank you!\n");
}
printf("Contine?Or not??please input Y/N??\n");
/* ch=getchar();
if(ch=='n'||ch=='N')
t=0;
else if (ch=='Y'||ch=='y') continue;
else{
printf("your choic is not exact!!");
t=0;
}*/
t=0;
}//end while
//printf("all finished!the result is %d\n",result);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -