js实现简易计算器(含中间过程)

由链表栈和数组实现

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="text" id="str"/>
		<button id="btn">计算</button>
		<script>
			class Data{
				constructor(ch) {
				    this.ch=ch;
					this.next=null;
				}
			}
			class LinkedListStack{
				constructor() {
				    this.head = {
						next : null
					}
				}
				pushObj(obj){
					if(obj.ch!=undefined){
						let temp = this.head;
						while(temp.next!=null){
							temp=temp.next;
						}
						temp.next=obj;
					}
				}
				popObj(){
					let temp = this.head;
					if(temp.next==null){
						// document.write("链表为空还未有数据");
						return;
					}
					while(temp.next.next!=null){
						temp=temp.next;
					}
					let obj = temp.next;
					temp.next=null;
					return obj;
				}
				show(){
					document.write("<br />");
					let temp = this.head.next;
					while(temp!=null){
						document.write(temp.ch+"<br />");
						temp=temp.next;
					}
				}
			}
			//定义+-*/的优先级
			var arr = new Array(4);
			for(let i=0;i<4;i++){
				arr[i]= new Array(2);
			}
			arr[0][0]="+";
			arr[1][0]="-";
			arr[2][0]="*";
			arr[3][0]="/";
			arr[0][1]=0;
			arr[1][1]=0;
			arr[2][1]=1;
			arr[3][1]=1;
			
			document.getElementById("btn").onclick=function(){
				//计算式
				var str = document.getElementById("str").value;
				//开始计算
				var numList =[];
				var chList =[];
				var resultList = new LinkedListStack();
				if(isNaN(str[0])){
					alert("输入算式不合法");
					return;
				}
				//将所有数字符号进栈
				for(let i=0;i<str.length;i++){
					let ch = str[i];
					if(!isNaN(ch)){//是数字
						var n = ch;
						while(!isNaN(str[i+1])){
							i++;
							n = n*10 + parseInt(str[i]);
						}
						// let obj = new Data(n);
						numList.push(n)
					}else{
						// let obj = new Data(ch);
						chList.push(ch);
						if(isNaN(str[i+1])){
							alert("输入算式不合法");
							return;
						}
					}
				}
				document.write("符号有:"+"<br />");
				for(let i=0;i<chList.length;i++){
					document.write(chList[i]+"<br />");
				}
				document.write("数字有:"+"<br />");
				for(let i=0;i<numList.length;i++){
					document.write(numList[i]+"<br />");
				}
				
				//计算乘除
				for(let i=0;i<chList.length;i++){
					let ch = chList[i];
					if(ch=='*'||ch=='/'){//优先级较高
						let num1 = numList[i];
						let num2 = numList[i+1];
						var result ;
						if(ch=='*'){result = num1 * num2;}
						if(ch=='/'){result = num1 / num2;}
						while(chList[i+1]=='*'||chList[i+1]=='/'){
							i++;
							let num2 = numList[i+1];
							if(chList[i]=='*'){result = result * num2;}
							if(chList[i]=='/'){result = result / num2;}
						}
						let obj = new Data(result);
						resultList.pushObj(obj);
						let chobj = new Data(chList[i+1]);
						resultList.pushObj(chobj);
						i+=2;
						if(i==chList.length){
							let num = numList[i];
							let obj = new Data(num);
							resultList.pushObj(obj);
						}
					}else{
						let num = numList[i];
						let obj = new Data(num);
						resultList.pushObj(obj);
						let chobj = new Data(ch);
						resultList.pushObj(chobj);
						if(i==chList.length-1){
							let num = numList[i+1];
							let obj = new Data(num);
							resultList.pushObj(obj);
						}
					}
				}
				document.write("计算过程为:"+"<br />");
				resultList.show();
				//计算加减
				document.write("<br />");document.write("<br />");
				var temp = resultList.popObj();
				// console.log(temp);
				if(temp.ch==undefined){
					temp = resultList.popObj();
				}
				// resultList.show();
				var sum = parseInt(temp.ch);
				// console.log(sum);
				// console.log( resultList.popObj().ch);
				
				while(temp!=null){
					var ch = temp.ch;
					console.log(ch)
					if(isNaN(ch)){
						var aa= resultList.popObj().ch;
						console.log(aa)
						if(ch=='+'){
							sum+=parseInt(aa);
						}else{
							sum-=parseInt(aa);
						}
					}
					temp=resultList.popObj();
				}
				document.write("结果为:"+sum+"<br />");
			}
		</script>
	</body>
</html>

测试:1+235/6+5

在这里插入图片描述
在这里插入图片描述
另一种方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="text" id="str" />
		<button id="btn">计算</button>
		<script>
			class Data{
				constructor(ch) {
					this.ch=ch;
					this.next=null;
				}
			}
			class LinkedListStack{
				constructor() {
					this.head = {
						next : null
					};
					this.top=null;
				}
				pushObj(obj){
					let temp = this.head;
					while(temp.next!=null){
						temp=temp.next;
					}
					temp.next=obj;
					this.top=obj;
				}
				popObj(){
					let temp = this.head;
					if(temp.next==null){
						// document.write("链表为空还未有数据");
						return;
					}
					while(temp.next.next!=null){
						temp=temp.next;
					}
					let obj = temp.next;
					this.top=temp;
					temp.next=null;
					return obj;
				}
				show(){
					document.write("<br />");
					let temp = this.head.next;
					while(temp!=null){
						document.write(temp.ch+"<br />");
						temp=temp.next;
					}
				}
			}
			//判断谁的优先级高
			function judge(ch1,ch2){//ch为当前运算符
			
				if(ch1==ch2){
					return 1;
				}else if((ch1=='+'||ch1=='-')&&(ch2=='*'||ch2=='/')){
					return 1;
				}else if((ch1=='*'||ch1=='/')&&(ch2=='*'||ch2=='/')){
					return 1;
				}else{
					return 0;
				}
			}
			//给数字和符号,进行运算
			function operate(num1,num2,ch){
				// console.log(ch)
				num1=parseInt(num1);
				num2=parseInt(num2);
				if(ch=='+'){return num1+num2;}
				if(ch=='-'){console.log(num2+"-"+num1); return num2-num1;}
				if(ch=='*'){return num1*num2;}
				if(ch=='/'){console.log(num2+"/"+num1); return num2/num1;}
			}
			document.getElementById("btn").onclick=function(){
				var numList = new LinkedListStack();//数字栈
				var chList = new LinkedListStack();//符号栈
				var ch = chList.popObj();
				var str = document.getElementById("str").value;
				// alert(str)
				for(let i=0;i<str.length;i++){
					if(isNaN(str[i])){//是个符号
						var ch = chList.popObj();
						if(ch==undefined){//栈空
							chList.pushObj(new Data(str[i]));
							// chList.show();
						}else if(judge(str[i],ch.ch)==1){
							let num1=numList.popObj().ch;
							let num2=numList.popObj().ch;
							let result=operate(num1,num2,ch.ch);
							numList.pushObj(new Data(result));
							chList.pushObj(new Data(str[i]));
						}else{
							chList.pushObj(new Data(ch.ch));
							chList.pushObj(new Data(str[i]));
						}
					}else{
						var n=str[i];
						while(!isNaN(str[i+1])){
							i++;
							n = n*10 + parseInt(str[i]);
							
						}
						numList.pushObj(new Data(n));
						// numList.pushObj(new Data(str[i]));
					}
				}
				var ch = chList.popObj();
				while(ch){
					ch=ch.ch;
					var num1 = numList.popObj().ch;
					var num2 = numList.popObj().ch;
					// if(ch=='/'){
					// 	result = operate(num2,num1,ch);
					// }else{
					// 	result = operate(num1,num2,ch);
					// }
					result = operate(num1,num2,ch);
					numList.pushObj(new Data(result));
					ch=chList.popObj();
				}
				document.getElementById("str").value=document.getElementById("str").value+"="+numList.popObj().ch;
			}
		</script>
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值