哈工大2017级数据结构实验1C语言

源代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define null 0
#define MAXLEN 100

int is_operation(char op){
switch(op){
case ‘+’:
case ‘-’:
case ‘*’:
case ‘/’:return 1;
default:return 0;
}
}

int Youxianji(char c2)
{
if(c2==’+’||c2==’-’)
return 1;
else if(c2==’*’||c2==’/’)
return 2;
else if(c2==’)’)
return 3;
else if(c2==’(’)
return 0;
else
return -1;
}

void show1(char c[])
{
int i;
for(i=0;c[i]!=’\0’;i++)
printf("%c ",c[i]);
}

double readNumber(char f[],int *i){
double x = 0.0;
int k = 0;
while(f[*i] >= ‘0’ && f[i] <= ‘9’){
x = x
10 + (f[*i] - ‘0’);
(*i)++;
}
if(f[*i] == ‘.’){
(*i)++;
while(f[*i] >= ‘0’ && f[i] <= ‘9’){
x = x
10 + (f[*i] - ‘0’);
(*i)++;
k++;
}
}
while(k!=0){
x = x/10.0;
k–;
}
return (x);
}

void postfix(char e[],char f[]){

int i = 1, j = 0,k;
char opst[100];
int top,t;
top = 0;
opst[top] = '#';top++;
while(e[i] != '#'){
	if((e[i] >= '0' && e[i] <= '9') || e[i] == '.'){
		f[j++] = e[i];
		printf("栈:");
        for(k=0;k<j;k++)
        printf("%c",f[k]);
        printf("\n");
	}
	else if(e[i] == '('){
		opst[top] = e[i]; top++;
	}
	else if(e[i] == ')'){
		t = top - 1;
		while(opst[t] != '('){
			f[j++] = opst[--top];
			t = top - 1;
			printf("栈:");
        for(k=0;k<j;k++)
        printf("%c",f[k]);
        printf("\n");
		}
		top-- ;
	}
	else if(is_operation(e[i])){
		f[j++] = ' ';
		while(Youxianji(e[i]) <= Youxianji(opst[top-1])){
			f[j++] = opst[--top];
			printf("zhan:");
        for(k=0;k<j;k++)
        printf("%c",f[k]);
        printf("\n");
		}
		opst[top] = e[i]; top++;
	}
	i++;

}
while(top) f[j++] = opst[--top];
f[j]='\0';

}

double compute(char output[]){
double lf[100];
int top =0;
int i = 0,j;
double x1,x2;
while(output[i] != ‘#’){
if(output[i] >=‘0’&&output[i]<=‘9’){
lf[top] = readNumber(output,&i);
top++;
}
else if(output[i]’ ')
{
i++;
continue;
}
else if(output[i]
’+’){
x1 = lf[–top];
x2 = lf[–top];
lf[top] = x1 + x2;
top++; i++;
}
else if(output[i] == ‘-’&&output[i-1]>=‘0’&&output[i-1]<=‘9’){
x1 = lf[–top];
x2 = lf[–top];
lf[top] = x2- x1;
top++; i++;
}
else if(output[i] == ‘*’){
x1 = lf[–top];
x2 = lf[–top];
lf[top] = x1 * x2;
top++; i++;
}
else if(output[i] == ‘/’){
x1 = lf[–top];
x2 = lf[–top];
lf[top] = x2 / x1;
top++; i++;
}
printf(“栈:”);
for(j=top-1;j>=0;j–)
printf("%lf\t",lf[j]);
printf("\n");
}
return lf[0];
}

void FUHAO(char c[],char b[])
{
int i,j=0,k;
for(i=0;i<strlen©;i++)
{
if(c[i]!=’-’)
{
b[j]=c[i];
j++;
}
else if(i>0&&((c[i-1]>=‘0’&&c[i-1]<=‘9’)||c[i-1]==’)’))
{
b[j]=c[i];
j++;
}
else
{
b[j]=’(’;
j++;
b[j]=‘0’;
j++;
b[j]=’-’;
j++;
i++;
while(c[i]<‘9’&&c[i]>‘0’)
{
b[j]=c[i];
j++;
i++;
}
b[j]=’)’;
j++;
i–;
}
}
b[j]=’\0’;
}

void print(char c[])
{
int i=0;
while(c[i]!=’#’)
{
printf("%c",c[i]);
i++;
}
printf("\n");
}

int main()
{
char c[MAXLEN],c1[MAXLEN],b[MAXLEN];
char output[MAXLEN];
output[0]=’\0’;
double m;
int i=0,j=0,k;
printf(“输入中缀表达式,前面与后面加上#:”);
scanf("%s",c1);
printf(“你输入的是%s\n”,c1);
FUHAO(c1,b);
for(k=0;b[k]!=’\0’;k++)
{
if(b[k]!=’+’&&b[k]!=’-’&&b[k]!=’*’&&b[k]!=’/’&&b[k]!=’(’&&b[k]!=’)’)
{
c[i]=b[k];
i++;
}
else
{
c[i]=’ ‘;
c[i+1]=b[k];
c[i+2]=’ ';
i=i+3;
}
}
postfix(c,output);
printf(“后缀表达式为:\n\n\n”);
print(output);
m = compute(output);
printf(“计算后缀表达式的值:\n”);
printf("%lf\n",m);
return 0;
}

扫描二维码关注公众号,回复: 4327342 查看本文章

猜你喜欢

转载自blog.csdn.net/cobracanary/article/details/84699453