package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
var input string
var string1 string//储存括号内的算式
var string2 []string//储存计算乘除后的复数
func main(){
input := doInput()
if -1 != strings.LastIndex(input,")"){
string1 = doSelect1(input)
string2 = doSwitch1(strings.Fields(string1))
string2 = doSwitch2(string2)
}else{
string2 = doSwitch1(strings.Fields(input))
string2 = doSwitch2(string2)
}
fmt.Println(string2)
}
func doInput()string {
inputReader := bufio.NewReader(os.Stdin)
fmt.Println("请输入你想计算的算式(复数与运算符用空格隔开,以'|'为输入结束标志)例如:a+bi * c-di")
input,err := inputReader.ReadString('|')
if err == nil {
input = strings.Trim(input,"|")
return input
}
return input
}//将算式存入字符串中
func doSelect1(s1 string)string {
fn := func(c rune)bool {
return strings.ContainsRune("()",c)
}
return strings.TrimFunc(s1,fn)
}//将括号内的算式存入s1中
func doSwitch1(s []string) []string {
for i,value := range s {
//s := strings.Fields(value)
switch value{
case "*":
s1 := string(s[i-1])
s2 := string(s[i])
s3 := string(s[i+1])
trueS1,falseS2 := cangeNumber(handleString(s1,s2,s3))
endS1 := trueS1+"--"+falseS2+"i"
string2 = mutate(s, i, endS1)
case "/":
s1 := string(s[i-1])
s2 := string(s[i])
s3 := string(s[i+1])
trueS1,falseS2 := cangeNumber(handleString(s1,s2,s3))
endS1 := trueS1+"--"+falseS2+"i"
string2 = mutate(s, i, endS1)
case "+":
string2 = s
case "-":
string2 = s
default :
continue
}
}
return string2
}//扫描乘号和除号
func doSwitch2(s []string) []string {
for i,value := range string2{
switch value {
case "+":
s1 := string(s[i-1])
s2 := string(s[i])
s3 := string(s[i+1])
trueS1,falseS2 := cangeNumber(handleString(s1,s2,s3))
endS1 := trueS1+"--"+falseS2+"i"
string2 = mutate(s, i, endS1)
case "-":
s1 := string(s[i-1])
s2 := string(s[i])
s3 := string(s[i+1])
trueS1,falseS2 := cangeNumber(handleString(s1,s2,s3))
endS1 := trueS1+"--"+falseS2+"i"
string2 = mutate(s, i, endS1)
default:
continue
}
}
return string2
}//扫描加号和减号
func changeString(s1 string,s2 string)(float64,float64){
var a,b float64
a,_ = strconv.ParseFloat(s1, 64)
b,_ = strconv.ParseFloat(s2, 64)
return a,b
}//将字符转换成float64
func cangeNumber(m float64,n float64)(string,string) {
var s1,s2 string
s1 = strconv.FormatFloat(m,'E',-1, 64)
s2 = strconv.FormatFloat(n,'E', -1, 64)
return s1,s2
}//将float64转化为字符
func doSelect2(s string)(j string,k string) {
if -1 == strings.Index(s, "i") {
j := s[:]
return j, "0"
} else {
count1 := strings.Index(s, "i")
for i, value := range s {
switch value {
case '+':
j := s[:i]
k := s[i+1 : count1]
if k == "" {
k = "1"
}
return j, k
case '-':
j := s[:i]
k := s[i:count1]
if k == "" {
k = "1"
}
return j, k
default:
continue
}
}
}
return j, k
}//分离实部和虚部以字符串形式保存
func handleString(s1 string,s2 string,s3 string)(float64,float64){
var a,b,c,d,m,n float64
a,b = changeString(doSelect2(s1))
c,d = changeString(doSelect2(s3))
switch s2 {
case "*":
m = a*c - b*d
n = b*c - a*d
case "/":
if (c*c + d*d) == 0{
fmt.Println("虚部为零,无法进行除法运算:")
}else{
m = (a*c + b*d)/(c*c + d*d)
n = (b*c - a*d)/(c*c + d*d)
}
case "+":
m = a + c
n = b + d
case "-":
m = a - c
n = b - d
default:
fmt.Println("未知运算")
break
}
return m,n
}//处理运算符选择进行运算
func mutate(s []string,i int,m string)[]string {
slices := make([]string ,len(s))
slices = append(slices[:i-1],slices[i+2:]...)
slices = append(slices[:i],m)
return slices
}//删除原有字符,插入新的运算结果字符
go实现多值运算计算器
最新推荐文章于 2024-07-16 16:55:25 发布