golang切割字符串和字符串数字相互转化

问题源自leetcode 537
参考链接:https://studygolang.com/articles/19574?fr=sidebar

代码如下:

package main

import (
	"strconv"
	"strings"
)

func complexNumberMultiply(a string, b string) string {
	//strconv.Atoi()字符串数字转数值,strconv.Itoa()数值转字符串数字,
	// strings.split()按指定字符切,并返回切片
	x1, _ := strconv.Atoi(strings.Split(a, "+")[0])
	y1, _ := strconv.Atoi(strings.Split(a, "+")[1][:len(strings.Split(a, "+")[1]) - 1])
	x2, _ := strconv.Atoi(strings.Split(b, "+")[0])
	y2, _ := strconv.Atoi(strings.Split(b, "+")[1][:len(strings.Split(b, "+")[1]) - 1])
	x := x1 * x2 - y1 * y2
	y := x1 * y2 + x2 * y1
	res := strconv.Itoa(x) + "+" + strconv.Itoa(y) + "i"
	return res
}

func main() {
	a, b := "78+-76i", "-86+72i"
	complexNumberMultiply(a, b)
}

你可能感兴趣的:(go语言笔记)