C语言计算器

运用了堆栈、链表等数据结构

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//堆栈

//1符号栈 
typedef struct n1{
	char val;
	struct n1* next;
}node1;

//2数字栈 
typedef struct n2{
	int val;
	struct n2* next;
}node2;

//新建符号栈 
node1* node1create(){
	node1* head=(node1 *)malloc(sizeof(node1));
	head->val='\0';//头指针,值为空 
	head->next=NULL; 
	return head;//返回头指针 
}
//新建数字栈 
node2* node2create(){
	node2* head=(node2 *)malloc(sizeof(node2));
	head->val=0;//头指针,值为0 
	head->next=NULL; 
	return head;//返回头指针 
}

//符号栈压栈 
void push1(node1* head,char v){
	node1 * p=(node1 *)malloc(sizeof(node1));
	p->val=v;
	//strcpy(p->val,v);
	p->next=head->next;
	head->next=p;
	return ;
}
//数字栈压栈 
void push2(node2* head,int x){
	node2 * p=(node2 *)malloc(sizeof(node2));
	p->val=x;
	p->next=head->next;
	head->next=p;
	return ;
}

//符号栈出栈 
char pop1(node1* head){
	if(head->next==NULL){
		return NULL ;
	}
	char v=head->next->val;
	node1* p=head->next;
	head->next=head->next->next;
	free(p);
	return v;
}
//数字栈出栈 
int pop2(node2* head){
	if(head->next==NULL){
		return NULL ;
	}
	int x=head->next->val;
	node2* p=head->next;
	head->next=head->next->next;
	free(p);
	return x;
}

//读符号栈栈顶 
char top1(node1* head){
	return head->next->val;
}
//读数字栈栈顶 
int  top2(node2* head){
	return head->next->val;
}

//优先级判断 
int check(char c){
	switch(c){
		case '+':return 3;break;
		case '-':return 3;break;
		case '*':return 2;break;
		case '/':return 2;break;
		case '^':return 1;break;
		case '(':return 4;break;
		case ')':return 4;break;
		default:return 0;
	}
} 

//计算
int calculate(char c,int a,int b){
	switch(c){
		case '+':return b+a;break;
		case '-':return a-b;break;
		case '*':return b*a;break;
		case '/':return a/b;break;
		case '^':return pow(a,b);break;
		default:return 0;
	}
}

//输出当前链表2 
void print2(node2* n){
	node2* ppp=n->next;
	printf("\n当前数字栈:"); 
	while(ppp){
		printf(">%d",ppp->val);
		ppp=ppp->next;
	}
} 
//输出当前链表1
void print1(node1* n){
	node1* ppp=n->next;
	printf("\n当前符号栈:"); 
	while(ppp){
		printf(">%c",ppp->val);
		ppp=ppp->next;
	}
} 

/*递归的构想 
int read(){
	
	node1* head1=node1create();//创建符号栈 
	node2* head2=node2create();//创建数字栈 
	int y=0;char c;
	if( (c=getchar())=='(' ){
		push2(head2,read());
	}else{
		while((c=getchar())!=')'&&(c=getchar())!='='){//开始读入内容并计算 
			if(c>='0' && c<='9'){
				y=y*10+c-'0';
			}else{
				push2(head2,y);//把前面读取到的的数字入栈 
				//print2(head2);print1(head1); 
				y=0;
				if(head1->next==NULL || head1->next->val=='('){//符号栈顶空 或 为左括号 
					push1(head1,c);//入栈 
				}else if(check(c)<check(head1->next->val)){//优先级高于栈顶,入栈 
					push1(head1,c);			
				}else{	//如果优先级比栈顶操作符低或者相等,则弹出栈顶元素,直至比栈顶元素优先级高 
					char e;
					int x;
					while(check(c)>=check(top1(head1))){//当前读入的运算符c的优先级比栈顶运算符低或相等 
						e=pop1(head1); //弹出运算符 
						x=calculate(e,pop2(head2),pop2(head2));//把数字栈顶两个元素算掉 
						push2(head2,x);//将计算结果压回数字栈 
						//print2(head2);print1(head1);
						if(head1->next==NULL)break;//如果符号栈空了,就退出循环 
					}
					push1(head1,c);
				}
			}
		}
		push2(head2,y);
		//若符号栈仍有剩余符号,则依次弹出并计算
		if(head1->next->val){
			char e;int x;
			while(1){
				e=pop1(head1); //弹出运算符 
				x=calculate(e,pop2(head2),pop2(head2));//把数字栈顶两个元素算掉 
				push2(head2,x);//将计算结果压回数字栈 
				if(head1->next==NULL)break;//如果符号栈空了,就退出循环 
			}
		} 
	return head2->next->val;
	}
}
*/




int main(){
	node1* head1=node1create();//创建符号栈 
	node2* head2=node2create();//创建数字栈 
	int y=0;char c;
	int flag=0;
	while((c=getchar())!='='){//开始读入内容并计算 
		if(c>='0' && c<='9'){
			y=y*10+c-'0';
			flag=1;//表示当前读入了数字 
		}else{
			if(flag==1){push2(head2,y);y=0;flag=0;}//把前面读取到的的数字y入栈 ///
			//print2(head2);print1(head1);过程显示 
			
			if(head1->next==NULL || c=='('){//符号栈顶空 或 为左括号 
				push1(head1,c);//符号入栈 
				//print1(head1);//过程显示 
			}else if(check(c)<check(head1->next->val)){//优先级高于栈顶,入栈 
				push1(head1,c);			
				//print1(head1);//
			}else{	//如果c优先级比栈顶操作符低或者相等,则弹出栈顶元素,直至c比栈顶元素优先级高 
				char e;
				int x;
				while(check(c)>=check(top1(head1))){//当前读入的运算符c的优先级比栈顶运算符低或相等 
					e=pop1(head1); //弹出栈顶运算符 
					if (e=='(')break;
					x=calculate(e,pop2(head2),pop2(head2));//把数字栈顶两个元素算掉 
					push2(head2,x);//将计算结果压回数字栈 
					//print2(head2);print1(head1);/过程显示 
					if(head1->next==NULL)break;//如果符号栈空了,就退出循环 
				}
				if( c!=')' ) push1(head1,c);
				//print1(head1);//过程显示 
			}
		}
	}
	push2(head2,y);
	//若符号栈仍有剩余符号,则依次弹出并计算
	if(head1->next->val){
		char e;int x;
		while(1){
			e=pop1(head1); //弹出运算符 
			//print1(head1);//过程显示 
			x=calculate(e,pop2(head2),pop2(head2));//把数字栈顶两个元素算掉 
			push2(head2,x);//将计算结果压回数字栈 
			if(head1->next==NULL)break;//如果符号栈空了,就退出循环 
		}
	} 
	int result=head2->next->val;
	printf("\nresult=%d",result);

	
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值