主题:谁能帮我看看这段代码是怎么运行的?
#define MAX 80
#define MAX1 40
#define MAX2 40
#define NULL 0
#include <stdio.h>
#include <math.h>
typedef struct
{
double A[ MAX1 ];
int top1;
}shuju;
typedef struct
{char B[ MAX2 ];
int top2;
}yunsuanfu;
void chushihua1(shuju *p)
{
p->top1 = 0;
}
void chushihua2(yunsuanfu *p)
{
p->top2 = 0;
}
int charu1(shuju *p,double x)
{
if(p->top1 >= (MAX1 - 1))
{
printf("you are wrong1\n");
return 0;
}
else
{
p->A[ p->top1 ] = x;
p->top1++;
return 1;
}
}
int charu2(yunsuanfu *p,char x)
{
if(p->top2 >= (MAX2 - 1))
{
printf("you are wrong2\n");
return 0;
}
else
{
p->B[ p->top2 ] = x;
p->top2++;
return 1;
}
}
int chuzhuan1(shuju *p,double *x)
{
if(p->top1 < 0)
{
printf("you are wrong11\n");
return 0;
}
else
{
p->top1--;
*x=p->A[ p->top1 ];
return 1;
}
}
int chuzhuan2(yunsuanfu *p,char *x)
{
if(p->top2 < 0)
{
printf("you are wrong22\n");
return 0;
}
else
{
p->top2--;
*x=p->B[ p->top2 ];
return 1;
}
}
int op(char f,char b)
{
if((f == '(' && b == ')') || (f == '\0' && b == '\0'))
return 2;
else if(((b == '*' || b == '/') && (f == '+' || f == '-'))||(b == '(' && f != ')') || ((f == '(' || f == '\0') && (b == '+' || b == '/' || b == '-' || b == '*')))
return -1;
else if((b == '\0' && f != '(') || (b == ')' && f != '\0') || b == f || ((f == '*' || f == '/' || f == ')') && (b == '+' || b == '/' || b == '-' || b == '*')))
return 1;
else
{
printf("\t\t\tyou are wrong\n");
return 0;
}
}
double zhuanhuan(int x,int y,char a[])
{
int i, j=0, k;
double n=0;
for(i=x; i<y; i++)
{
if(a[ i ] != '.')
n = n*10+a[ i ]-'0';
else j=i;
}
if(j == 0)
return n;
else
{
for(k=0; k < i-1-j; k++)
n = n/10;
return n;
}
}
double f1(shuju *a,yunsuanfu *b)
{
double m, m1, m2;
char x;
chuzhuan2(b, &x);
chuzhuan1(a, &m1);
chuzhuan1(a, &m2);
if(x == '*')
m = m1*m2;
else if(x == '+')
m = m1+m2;
else if(x == '-')
m = m2-m1;
else if(x == '/')
if(m1 == 0)
{
printf("\t\t\tyou are wrong\n");
jieshu();
}
else
m = m2/m1;
charu1(a, m);
return m;
}
void f2(int j, yunsuanfu *b, char k[])
{
charu2(b, k[ j ]);
}
void jiemian()
{
system("cls");
printf("\n");
printf("\t\t\t C O U N T E R\n");
textcolor(5);
cprintf("*******************************************************************************");
printf("\n\n");
printf("\t\t\tPlease input your exprssion\n");
printf("\t\t\t ");
}
jieshu()
{
char c;
printf("Are you contiune?(y/n) ");
while(1)
{
c = getch();
printf("\n");
if(c == 'y' || c == 'Y')
main();
else if(c == 'n' || c == 'N')
{
system("cls");
printf("\n\n\t\t\t Thank you use the counter\n");
printf("\t\t\t");
getch();
exit(0);
}
else
{
printf("\t\t\t");
cprintf("Please input y/n!");
printf("\n");
printf("\t\t\t");
continue;
}
}
}
main()
{
int i=0,j=0,bi,b1,B=0;
double n,m1,m2,m=0;
char k[ MAX ],ch='\0',x;
shuju a;
yunsuanfu b;
chushihua1( &a );
chushihua2( &b );
charu2( &b, '\0' );
jiemian();
gets(k);
while(k[ i ] != '\0')
{
if(k[ j ] == 's' && k[ j+2 ] == 'n')
{
j = j+3;
i = j;
B = 1;
}
else if(k[ j ] == 'c' && k[ j+2 ] == 's')
{
j = j+3;
i = j;
B = 2;
}
else if(k[ j ] == 't' && k[ j+2 ] == 'n')
{
j = j+3;
i = j;
B = 3;
}
else if(k[ j ] == 'l' && k[ j+2 ] == 'g')
{
j = j+3;
i = j;
B = 4;
}
else if(k[ j ] == 'l' && k[ j+1 ] == 'n')
{
j = j+2;
i = j;
B = 5;
}
while(k[ j ] == '.' || (k[ j ] >= '0' && k[ j ] <= '9' ))
j++;
n=zhuanhuan(i,j,k);
switch(B)
{
case 1: n=sin(n);break;
case 2: n=cos(n);break;
case 3: n=tan(n);break;
case 4: n=log10(n);break;
case 5: n=log(n);
}
charu1(&a, n);
b1 = 0;
while(b1 == 0)
{
bi = op(ch, k[j] );
if(bi == 0)
jieshu();
while(bi == 1)
{
m = f1(&a, &b);
chuzhuan2(&b, &ch);
bi = op(ch, k[ j ]);
charu2(&b, ch);
}
if(bi == -1)
{
do
{
f2(j, &b,k);
j++;
}while(k[j ] == '(');
j--;
}
else if(bi == 2)
{
if(ch == '(')
{
if(k[ j ] == ')')
j++;
chuzhuan2(&b, &ch);
chuzhuan2(&b, &ch);
charu2(&b, ch);
continue;
}
else chuzhuan1(&a, &m);
}
b1 = 1;
}
if(k[ j ] == '\0')
break;
else
{
ch = k[ j ];
j++;
}
i = j;
B = 0;
}
printf("\t\t\t");
textcolor(30);
cprintf("The answer is %f",m);
printf("\n");
jieshu();
}
我要画流程画和N-S画,所以希望能点出来,哪一步先执行,哪一步后执行@谢谢了,!
#define MAX1 40
#define MAX2 40
#define NULL 0
#include <stdio.h>
#include <math.h>
typedef struct
{
double A[ MAX1 ];
int top1;
}shuju;
typedef struct
{char B[ MAX2 ];
int top2;
}yunsuanfu;
void chushihua1(shuju *p)
{
p->top1 = 0;
}
void chushihua2(yunsuanfu *p)
{
p->top2 = 0;
}
int charu1(shuju *p,double x)
{
if(p->top1 >= (MAX1 - 1))
{
printf("you are wrong1\n");
return 0;
}
else
{
p->A[ p->top1 ] = x;
p->top1++;
return 1;
}
}
int charu2(yunsuanfu *p,char x)
{
if(p->top2 >= (MAX2 - 1))
{
printf("you are wrong2\n");
return 0;
}
else
{
p->B[ p->top2 ] = x;
p->top2++;
return 1;
}
}
int chuzhuan1(shuju *p,double *x)
{
if(p->top1 < 0)
{
printf("you are wrong11\n");
return 0;
}
else
{
p->top1--;
*x=p->A[ p->top1 ];
return 1;
}
}
int chuzhuan2(yunsuanfu *p,char *x)
{
if(p->top2 < 0)
{
printf("you are wrong22\n");
return 0;
}
else
{
p->top2--;
*x=p->B[ p->top2 ];
return 1;
}
}
int op(char f,char b)
{
if((f == '(' && b == ')') || (f == '\0' && b == '\0'))
return 2;
else if(((b == '*' || b == '/') && (f == '+' || f == '-'))||(b == '(' && f != ')') || ((f == '(' || f == '\0') && (b == '+' || b == '/' || b == '-' || b == '*')))
return -1;
else if((b == '\0' && f != '(') || (b == ')' && f != '\0') || b == f || ((f == '*' || f == '/' || f == ')') && (b == '+' || b == '/' || b == '-' || b == '*')))
return 1;
else
{
printf("\t\t\tyou are wrong\n");
return 0;
}
}
double zhuanhuan(int x,int y,char a[])
{
int i, j=0, k;
double n=0;
for(i=x; i<y; i++)
{
if(a[ i ] != '.')
n = n*10+a[ i ]-'0';
else j=i;
}
if(j == 0)
return n;
else
{
for(k=0; k < i-1-j; k++)
n = n/10;
return n;
}
}
double f1(shuju *a,yunsuanfu *b)
{
double m, m1, m2;
char x;
chuzhuan2(b, &x);
chuzhuan1(a, &m1);
chuzhuan1(a, &m2);
if(x == '*')
m = m1*m2;
else if(x == '+')
m = m1+m2;
else if(x == '-')
m = m2-m1;
else if(x == '/')
if(m1 == 0)
{
printf("\t\t\tyou are wrong\n");
jieshu();
}
else
m = m2/m1;
charu1(a, m);
return m;
}
void f2(int j, yunsuanfu *b, char k[])
{
charu2(b, k[ j ]);
}
void jiemian()
{
system("cls");
printf("\n");
printf("\t\t\t C O U N T E R\n");
textcolor(5);
cprintf("*******************************************************************************");
printf("\n\n");
printf("\t\t\tPlease input your exprssion\n");
printf("\t\t\t ");
}
jieshu()
{
char c;
printf("Are you contiune?(y/n) ");
while(1)
{
c = getch();
printf("\n");
if(c == 'y' || c == 'Y')
main();
else if(c == 'n' || c == 'N')
{
system("cls");
printf("\n\n\t\t\t Thank you use the counter\n");
printf("\t\t\t");
getch();
exit(0);
}
else
{
printf("\t\t\t");
cprintf("Please input y/n!");
printf("\n");
printf("\t\t\t");
continue;
}
}
}
main()
{
int i=0,j=0,bi,b1,B=0;
double n,m1,m2,m=0;
char k[ MAX ],ch='\0',x;
shuju a;
yunsuanfu b;
chushihua1( &a );
chushihua2( &b );
charu2( &b, '\0' );
jiemian();
gets(k);
while(k[ i ] != '\0')
{
if(k[ j ] == 's' && k[ j+2 ] == 'n')
{
j = j+3;
i = j;
B = 1;
}
else if(k[ j ] == 'c' && k[ j+2 ] == 's')
{
j = j+3;
i = j;
B = 2;
}
else if(k[ j ] == 't' && k[ j+2 ] == 'n')
{
j = j+3;
i = j;
B = 3;
}
else if(k[ j ] == 'l' && k[ j+2 ] == 'g')
{
j = j+3;
i = j;
B = 4;
}
else if(k[ j ] == 'l' && k[ j+1 ] == 'n')
{
j = j+2;
i = j;
B = 5;
}
while(k[ j ] == '.' || (k[ j ] >= '0' && k[ j ] <= '9' ))
j++;
n=zhuanhuan(i,j,k);
switch(B)
{
case 1: n=sin(n);break;
case 2: n=cos(n);break;
case 3: n=tan(n);break;
case 4: n=log10(n);break;
case 5: n=log(n);
}
charu1(&a, n);
b1 = 0;
while(b1 == 0)
{
bi = op(ch, k[j] );
if(bi == 0)
jieshu();
while(bi == 1)
{
m = f1(&a, &b);
chuzhuan2(&b, &ch);
bi = op(ch, k[ j ]);
charu2(&b, ch);
}
if(bi == -1)
{
do
{
f2(j, &b,k);
j++;
}while(k[j ] == '(');
j--;
}
else if(bi == 2)
{
if(ch == '(')
{
if(k[ j ] == ')')
j++;
chuzhuan2(&b, &ch);
chuzhuan2(&b, &ch);
charu2(&b, ch);
continue;
}
else chuzhuan1(&a, &m);
}
b1 = 1;
}
if(k[ j ] == '\0')
break;
else
{
ch = k[ j ];
j++;
}
i = j;
B = 0;
}
printf("\t\t\t");
textcolor(30);
cprintf("The answer is %f",m);
printf("\n");
jieshu();
}
我要画流程画和N-S画,所以希望能点出来,哪一步先执行,哪一步后执行@谢谢了,!