#include<stdio.h>
#include<math.h>
int e_postion = 0;
char buff[200];
int data[50];//存放数据
int data_buff[50];
int sign[50];//存放符号
int sign_buff[50];
int stack_buff[200];//顺序存储找出来的数据和符号
int stack_flag[200];//2是符号,1是数据
int chart[7][7]={
{-1,-1,-1,-1,+1,+1,+1},
{-1,-1,-1,-1,+1,+1,+1},
{+1,+1,-1,-1,+1,+1,+1},
{+1,+1,-1,-1,+1,+1,+1},
{+1,+1,+1,+1,+1,+0,+1},
{-1,-1,-1,-1,+0,+1,+1},
{-1,-1,-1,-1,+1,-1,+0}
};
/* int chart[7][7]={
{-1,-1,+1,+1,+1,-1,-1},
{-1,-1,+1,+1,+1,-1,-1},
{-1,-1,-1,-1,+1,-1,-1},
{-1,-1,-1,-1,+1,-1,-1},
{+1,+1,+1,+1,+1,+0,+1},
{+1,+1,+1,+1,+0,+1,-1},
{+1,+1,+1,+1,+1,+1,+0}
};
*/
int sign_check[7]={'+','-','*','/','(',')','#'};
int sign_postion[50];//存符号所在的位置
int data_postion[50];
int result;
int sign_counter=0;//符号的计数
int data_counter=0;//数据个数
int flag_error = 0;
void init()
{
int i;
for(i=0; i<50; i++)
{
data[i] = 0;
sign[i] = 0;
sign_postion[i] = 0;
data_postion[i] = 0;
sign_buff[i] = 0;
data_buff[i] = 0;
}
for(i=0; i<200; i++)
{
buff[i] = 0;
stack_flag[i] = 0;
stack_buff[i] = 0;
}
e_postion = 0;
sign_counter=0;
data_counter=0;
flag_error = 0;
result = 0;
}
//输入程序
void putin_char()
{
int k = 0;//存储时i为参数1的位标记,j为参数2的位标记,k为buff的位标记
int t=0;
int p=0;
printf("输入必须紧贴输入中间不能有空格,以'#'结尾\n");
while(1)
{
scanf("%c",&buff[k]);
if(buff[k] == '\n')
{
e_postion = k;
break;
}
k++;
}
///
for(t=0; t<e_postion; t++)
{
printf("%c",buff[t]);
if((buff[t] == '/')&&(buff[t+1] == '0'))
{
flag_error = 1;
}
if((buff[t]=='+')||(buff[t]=='-')||(buff[t]=='*')||(buff[t]=='/')||(buff[t]=='(')||(buff[t]==')')||(buff[t]=='#'))
{
sign[sign_counter] = buff[t];
sign_postion[sign_counter] = t;
sign_counter++;
}
}
printf("\n===符号总共的个数================\n");
printf("%d",sign_counter);
printf("\n");
printf("==式子中的符号=================\n");
for(t=0; t<sign_counter; t++)
{
printf("%c ",sign[t]);
}
printf("\n");
printf("====符号所在的位数============\n");
for(t=0; t<sign_counter; t++)
{
printf("%d ",sign_postion[t]);
}
printf("\n");
//
if(buff[0] != '(')
{
for(p=0; p<sign_postion[0]; p++)
{
data[0] = data[0]*10 + (buff[p]-48);
}
data_counter = 1;
data_postion[0] = 0;
}
for(t=0; t<sign_counter-1; t++)
{
if( (sign_postion[t+1]-sign_postion[t]) > 1 )
{
for(p=sign_postion[t]+1; p<sign_postion[t+1]; p++)
{
data[data_counter]= data[data_counter]*10 + (buff[p]-48);
}
data_postion[data_counter] = sign_postion[t]+1;
data_counter++;
}
}
printf("===从字符串识别出的数据===========\n");
for(t=0; t<data_counter; t++)
{
printf("%d ", data[t]);
}
printf("\n");
printf("====数据开始所在的位数============\n");
for(t=0; t<data_counter; t++)
{
printf("%d ", data_postion[t]);
}
printf("\n");
printf("====数据和符号摘取后重新排序和识别============\n");
stack_buff[0] = '#';
stack_flag[0] = 2;
for(t=0; t<sign_counter; t++)
{
stack_buff[sign_postion[t]+1] = sign[t];
stack_flag[sign_postion[t]+1] = 2;
}
for(t=0; t<data_counter; t++)
{
stack_buff[data_postion[t]+1] = data[t];
stack_flag[data_postion[t]+1] = 1;
}
for(t=0; t<200; t++)
{
if(stack_buff[t] != 0)
printf("%d ",stack_buff[t]);
}
printf("\n");
for(t=0; t<200; t++)
{
if(stack_flag[t] != 0)
printf("%d ",stack_flag[t]);
}
printf("\n");
}
int search(int temp)
{
int t;
for(t=0; t<7; t++)
{
if(temp == sign_check[t])
{
return t;
}
}
}
void calculate()
{
int t;
int check_no[50];
int i=0;
int j=0;
int tk=0;
int p=0;
int temp_data=0;
check_no[0] = 6;
for(t=0; t<sign_counter; t++)
{
check_no[t+1] = search(sign[t]);
}
/
for(t=0; t<sign_counter+1; t++)
{
printf("%d",check_no[t]);
}
printf("\n");
///
//data_buff[0] = 0;
//sign_buff[0] = 6;
do
{
for(t=tk;t<200;t++)
{
if(stack_flag[t] == 1)
{
data_buff[i] = stack_buff[t];
printf("数据装入:%d!\n",data_buff[i]);
i++;
tk = t+1;
break;
}
if(stack_flag[t] == 2)
{
sign_buff[j] = search(stack_buff[t]);
printf("符号装入:%c!\n",sign_check[sign_buff[j]]);
j++;
tk = t+1;
break;
}
}
while((j>=2)&&(chart[sign_buff[j-1]][sign_buff[j-2]] == -1))
{
printf("一次运算%c~\n",sign_check[sign_buff[j-2]]);
switch (sign_buff[j-2])
{
case 0: {temp_data = data_buff[i-2]+data_buff[i-1];break;}
case 1: {temp_data = data_buff[i-2]-data_buff[i-1];break;}
case 2: {temp_data = data_buff[i-2]*data_buff[i-1];break;}
case 3: {temp_data = data_buff[i-2]/data_buff[i-1];break;}
default: {printf("\n switch error");break;}
}
data_buff[i-2] = temp_data;
data_buff[i-1] = 0;
i = i-1;
sign_buff[j-2] = sign_buff[j-1];
sign_buff[j-1] = 0;
j = j-1;
printf("i=%d\n",i);
printf("j=%d\n",j);
printf("sign_buff[j-1]=%c\n",sign_check[sign_buff[j-1]]);
printf("sign_buff[j-2]=%c\n",sign_check[sign_buff[j-2]]);
printf("data_buff[i-1]=%d\n",data_buff[i-1]);
}
if((j>=2)&&(chart[sign_buff[j-1]][sign_buff[j-2]] == 0)&&(j>=1))
{
printf("()抵消一次\n");
if(sign_buff[j-1] != 6)
{
sign_buff[j-1] = 0;
sign_buff[j-2] = 0;
j=j-2;
}
if(sign_buff[1] == 6)
{
result = data_buff[0];
break;
}
}
}while(1);
}
void main()
{
while(1)
{
init();
putin_char();
if(flag_error == 0)
{
calculate();
printf("结果是:%d \n",result);
}
else
{
printf("错误!!!!除数不能为0\n");
}
printf("\n***************************************\n");
}
}
数组作为堆栈实现表达式整型计算器
最新推荐文章于 2023-01-29 18:21:31 发布