📄 check_chang2.c
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define MAX 13
#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;
int number[2][4];
typedef struct sqlist{
int bol;//bol is 0 when num_ch is a number;bol is 1 when the num_ch is a oprater
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;
}*/
int check(sqlist l)
{
int right=1,find=0,i;
sqlist *q=&l;
q=q->next ;
for (;q->next!=NULL;q=q->next){
if(q->bol==1){
if(q->num_ch <=39||q->num_ch>57||q->num_ch==44||q->num_ch==46){
right=0;
printf("the %c is not allowed here!\n");
}
}
else {
find=0;
for(i=0;i<4;i++){
if(number[1][i]==0&&number[0][i]==q->num_ch ){
number[1][i]=1;
find=1;
break;
}
}
if(find==0){
right=0;
printf("the %d is not found!\n",q->num_ch );
}
}
}//end for
printf("the right is %d\n",right);
return right;
}
int 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 (check(*l));
}
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);
if(chang(s,&l)!=0){
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("have not this oper!\n");
return 0;
} // switch
}//else
} // while
result=gettop(&OPND);
return result;
}
else {
printf("your put is not right !\n");
return 0;
}
} // EvaluateExpression
int randomm()
{
int i=0;
srand((unsigned)time(NULL));
for (;i<4;i++){
number[0][i]=0;
number[0][i]=rand();
number[0][i]%=13;
number[0][i]++;
}
return number[2][4];
}
void main(){
char s[40],ch;
int result,t=1;
printf("/***************************************************/\n");
printf(" 24点游戏 dos 版本1.0\n");
printf(" 北京工商大学微机024班 欧阳锦林 版权所有\n");
printf(" 完成于2004年4月15日\n");
printf("/***************************************************/\n");
printf("When 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){
number[2][4]=randomm();
printf("When you input the formula,\n");
printf("you should look A as 1,J as 11,Q as 12,k as 13\n");
printf("The four numbers are %d ,%d ,%d ,%d\n",number[0][0],number[0][1],number[0][2],number[0][3]);
printf("please input the arithmetic(算术)\n");
scanf ("%s",s);
result=EvaluateExpression(s);
printf("Your result is %d \n",result);
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");
scanf(" %c",&ch);
printf("the choic is %c\n",ch);
if(ch=='n'||ch=='N'){
t=0;
}
else if (ch=='Y'||ch=='y') t=1;
// 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 + -