1. 程式人生 > >M語言,Cache資料庫(第一個計算器演算法,哭)

M語言,Cache資料庫(第一個計算器演算法,哭)

我的計算器(calculateV1

main ; 主迴圈函式
	do {
		do input()
		quit:(num1 = "")
	}
	while num1'=""
	quit

///輸入
input() [num1,num2,operator1,operator2,result,flag,ArrPri] {
	for{
		for{
		read !, "請輸入數字:", num1
		quit:num1=""
		set result = num1
		set flag = $$isNum(num1) ;判定算式格式是否正確
		quit:flag=1
		}
		quit:num1=""
		for{
		read !, "請輸入運算子:", operator1
		quit:operator1=""
		set flag = $$isOperator(operator1) ;判定算式格式是否正確
		quit:flag=1
		}
		if (operator1?1"="){
			write !, "運算結果為:"
			write result
			kill
			set num1 = ""
			;quit
		}else{
			quit
		}
	}	
	for{
		for{
		read !, "請輸入數字:", num2
		;quit:num2=""
		set flag = $$isNum(num2) ;判定算式格式是否正確
		quit:flag=1
		}
		;quit:num2="" 
		for{
		read !, "請輸入運算子:", operator2
		set flag = $$isOperator(operator2) ;判定算式格式是否正確
		quit:flag=1
		}
		
		do prior
		
		if ArrPri(operator1,operator2) = "^"{
			do cal1
			do display
			kill
			set num1 = 1 ;防止迴圈崩潰
			quit
		}elseif ArrPri(operator1,operator2) = 1{
			do cal1
			set operator1 = operator2
			;kill ArrPri
		}elseif ArrPri(operator1,operator2) = 2{
			do cal2
			///此時運算子O1應該不變,O2應等於O3
			;set operator1 = operator2
			;kill ArrPri
			;set num2 = "" ;防止迴圈崩潰
		}
		
		;set operator1 = operator2
		
	}
		
}

///按順序運算
cal1()[num1,num2,operator1,operator2,result]{
	if (operator1?1"+"){
		set result = result +num2
	}elseif (operator1?1"-"){
		set result = result -num2
	}elseif (operator1?1"*"){
		set result = result *num2
	}elseif (operator1?1"/"){
		set result = result /num2
	}
	kill ArrPri(operator1,operator2)
}

///先算後面*,/,後算前面+,-
cal2()[num1,num2,operator1,operator2,result,flag,ArrPri]{
	set result = num2 ; 後面要把result還給num1,本次計算的結果賦給num2
	for{
		read !, "請輸入數字:", num2
		set flag = $$isNum(num2) ;判定算式格式是否正確
		quit:flag=1
	}
	if (operator2?1"*"){
		set num2 = result *num2
	}elseif (operator2?1"/"){
		set num2 = result /num2
	}
	kill ArrPri(operator1,operator2)
	set result = num1 		
	for{
		read !, "請輸入運算子:", operator2
		set flag = $$isOperator(operator2) ;判定算式格式是否正確
		quit:flag=1
	}
	do prior
	if ArrPri(operator1,operator2) = "^"{
		do cal1
		do display
		kill
		set num1 = 1
		quit
	}elseif ArrPri(operator1,operator2) = 1{
		do cal1
		set operator1 = operator2
		;kill ArrPri
	}elseif ArrPri(operator1,operator2) = 2{
		do cal2
		///此時運算子O1應該不變,O2應等於O3
		;set operator1 = operator2
		;kill ArrPri
	}
	
}

///輸出
display()[num1,num2,operator1,operator2,result]{
	;kill
	write !, "運算結果為:", result
}

///設定相鄰運算子優先順序
prior()[operator1,operator2,ArrPri]{
	if (operator2?1"="){
			if (operator1?1"+"){ ;為相近的兩個運算子設定優先順序
				set ArrPri("+","=") = "^" ;下標儲存
				///接下來應為直接運算然後輸出結果
			}elseif (operator1?1"-"){
				set ArrPri("-","=") = "^"
			}elseif (operator1?1"*"){
				set ArrPri("*","=") = "^"
			}elseif (operator1?1"/"){
				set ArrPri("/","=") = "^"
			}
		}elseif(operator1?1"+")
		{
		
			if (operator2?1"+"){ ;為相近的兩個運算子設定優先順序
				set ArrPri("+","+") = 1 ;下標儲存
				///接下來應為直接運算得到result
			}elseif (operator2?1"-"){
				set ArrPri("+","-") = 1
			}elseif (operator2?1"*"){
				set ArrPri("+","*") = 2
				///接下來應為先算*結果存到num2,但是沒有num3,不能計算,需要再次輸入num和ope
			}elseif (operator2?1"/"){
				set ArrPri("+","/") = 2
				///接下來應為先算*結果存到num2
			}
		}elseif(operator1?1"-")
		{
		
			if (operator2?1"+"){ ;為相近的兩個運算子設定優先順序
				set ArrPri("-","+") = 1 ;下標儲存
				///接下來應為直接運算得到result
			}elseif (operator2?1"-"){
				set ArrPri("-","-") = 1
			}elseif (operator2?1"*"){
				set ArrPri("-","*") = 2
				///接下來應為先算*結果存到num2
			}elseif (operator2?1"/"){
				set ArrPri("-","/") = 2
				///接下來應為先算*結果存到num2
			}
		}elseif(operator1?1"*")
		{
		
			if (operator2?1"+"){ ;為相近的兩個運算子設定優先順序
				set ArrPri("*","+") = 1 ;下標儲存
				///接下來應為直接運算得到result
			}elseif (operator2?1"-"){
				set ArrPri("*","-") = 1
			}elseif (operator2?1"*"){
				set ArrPri("*","*") = 1
			}elseif (operator2?1"/"){
				set ArrPri("*","/") = 1
			}
		}elseif(operator1?1"/")
		{
		
			if (operator2?1"+"){ ;為相近的兩個運算子設定優先順序
				set ArrPri("/","+") = 1 ;下標儲存
				///接下來應為直接運算得到result
			}elseif (operator2?1"-"){
				set ArrPri("/","-") = 1
			}elseif (operator2?1"*"){
				set ArrPri("/","*") = 1
			}elseif (operator2?1"/"){
				set ArrPri("/","/") = 1
			}
		}
}

///判斷輸入的是否為數字
isNum(num){
	if (num?1(1.n,1"-".n,1"+".n,1.n1".".n,1"-".n1".".n,1"+".n1".".n))
	{
		write "正確輸入"
		quit 1
	}else{
		write "請輸入正確格式""""n,-n,+n,n.n,-n.n,+n.n"""
		quit 0
	}
}

///判斷輸入的是否為運算子
isOperator(operator){
	if (operator?1(1"+",1"-",1"*",1"/",1"="))
	{
		write "正確輸入"
		quit 1
	}else{
		write "請輸入正確格式""""+,-,*,/"""
		quit 0
	}
}