Skip to content

Instantly share code, notes, and snippets.

@GZShi
Created September 29, 2012 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GZShi/3803883 to your computer and use it in GitHub Desktop.
Save GZShi/3803883 to your computer and use it in GitHub Desktop.
// 这个版本不支持赋值运算
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void preproccess(char *buf, int a, int b, int c, int d, int e);
void replace(char *buf, int start, int end, int number);
void mult_analytic(char *buf);
void remove_bracket(char *buf);
void analytic(char *buf, char *op);
int calculate(char *buf, int n);
void preproccess(char *buf, int a, int b, int c, int d, int e)
{
int i = 0;
while(buf[i] != 0)
{
if(buf[i] == ' ')
strcpy(buf+i, buf+i+1);
else if(buf[i] == '>' && buf[i+1] == '>')
strcpy(buf+i, buf+i+1);
else if(buf[i] == '<' && buf[i+1] == '<')
strcpy(buf+i, buf+i+1);
else
i++;
}
}
void replace(char *buf, int start, int end, int number)
{
char t_buf_1[20];
char t_buf_2[20];
int i = 0;
itoa(number, t_buf_1, 10);
strcpy(t_buf_2, buf+end+1);
strcpy(buf+start, t_buf_1);
strcat(buf+start+strlen(t_buf_1), t_buf_2);
}
void mult_analytic(char *buf)
{
char op[6][3] = {
{'*', '/', '%'},
{'+', '-', '-'},
{'<', '>', '>'},
{'&', '&', '&'},
{'^', '^', '^'},
{'|', '|', '|'}
};
int i = 0;
remove_bracket(buf);
while(i < 6)
{
analytic(buf, op[i++]);
}
}
void remove_bracket(char *buf)
{
int i = 0;
int j = 0;
int k = 0;
char t_buf[20];
while(buf[i] != 0)
{
if(buf[i] == '(')
++j;
else if(buf[i] == ')')
++k;
i++;
}
if(j != k) // 判断括号配对
return;
else if(j == 0) // 判断有无括号
return;
j = k = -1;
i = 0;
while(buf[i] != 0) // 寻找最内层括号
{
if(buf[i] == '(')
j = i;
else if(buf[i] == ')')
{
k = i;
break;
}
i++;
}
if(k != -1 && j == -1)
return;
// 分析括号里面的式子,存入t_buf
strcpy(t_buf, buf + j + 1);
t_buf[k-j-1] = '\0';
mult_analytic(t_buf); // 分析并计算式子
replace(buf, j, k, atoi(t_buf));
remove_bracket(buf);
}
void analytic(char *buf, char* op)
{
int i = 0;
int j = 0;
int k = 0;
if(buf[i] == '-' && isdigit(buf[i+1]))
i++;
while(buf[i] != 0)
{
if(buf[i] == op[0] || buf[i] == op[1] || buf[i] == op[2])
{
break;
}
i++;
}
if(i >= strlen(buf))
return;
j = k = i;
while(isdigit(buf[j-1]))
--j;
if(j == 1 && buf[0] == '-' && isdigit(buf[1]))
--j;
if(buf[k+1] == '-' && isdigit(buf[k+2]))
++k;
while(isdigit(buf[k+1]))
++k;
replace(buf, j, k, calculate(buf + j, k-j+1));
analytic(buf, op);
}
int calculate(char *buf, int n)
{
char t_buf_1[20];
char t_buf_2[20];
char op;
int a = 0;
int b = 0;
int i = 0;
if(buf[i] == '-' && isdigit(buf[i+1]))
i++;
while(isdigit(buf[i]))
i++;
if(i >= strlen(buf))
return atoi(buf);
strncpy(t_buf_1, buf, i);
t_buf_1[i] = 0;
op = buf[i];
strncpy(t_buf_2, buf + i + 1, n-i-1);
t_buf_2[n-i-1] = '\0';
a = atoi(t_buf_1);
b = atoi(t_buf_2);
switch(op)
{
case '*': return (a*b);
case '/': return (a/b);
case '%': return (a%b);
case '+': return (a+b);
case '-': return (a-b);
case '<': return (a<<b);
case '>': return (a>>b);
case '^': return (a^b);
case '&': return (a&b);
case '|': return (a|b);
default: return 0;
}
return 0;
}
int main(void)
{
int a, b, c, d, e;
char buf[60];
puts("input a b c d e");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
fflush(stdin);
puts("input a expression");
gets(buf);
preproccess(buf, a, b, c, d, e);
mult_analytic(buf);
printf("= %d\n", atoi(buf));
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment