go 矩阵乘法

结构体

rowlen 是第一行的元素个数
column 是第一列的元素个数
list []int是用来装元素的数组,采用切片的方发,之后append来添加元素

type Matrix struct{
        rowlen int
        columnlen int
        list []int
}
初始化

初始化函数用来给矩阵结构体赋值

func Matrix_initialization (object *Matrix)  {
        var a,b int
        fmt.Println("Please enter the number of first row and first column")
        fmt.Print("> ")
        fmt.Scan(&a,&b)
        object.rowlen=a
        object.columnlen=b
        for i:=0;i<object.rowlen*object.columnlen;i++{
                fmt.Scan(&a)
                object.list=append(object.list,a)
        }
}
关键乘法函数

3个for 循环足够了,Matrix 是我定义的结构体

func matrix_multiplication (first,second Matrix) Matrix{
        matrix_check(first,second)
        var result Matrix
        var tmp int
        result.rowlen=second.rowlen
        result.columnlen=first.columnlen
        for i:=0;i<first.columnlen;i++{
                for j:=0;j<second.rowlen;j++{
                        tmp=0
                        for k:=0;k<second.columnlen;k++{
                                tmp+=first.list[first.rowlen*i+k]*second.list[second.columnlen*k+j]
                        }
                        result.list=append(result.list,tmp)
                }
        }
        return result

}
总代码
package main

import (
        "fmt"
        "os"
)

type Matrix struct{
        rowlen int
        columnlen int
        list []int
}

func main() {
        var first,second,result Matrix
        Matrix_initialization(&first)
        Matrix_initialization(&second)
        result=matrix_multiplication(first,second)
        formate_print(result)
}


func Matrix_initialization (object *Matrix)  {
        var a,b int
        fmt.Println("Please enter the number of first row and first column")
        fmt.Print("> ")
        fmt.Scan(&a,&b)
        object.rowlen=a
        object.columnlen=b
        for i:=0;i<object.rowlen*object.columnlen;i++{
                fmt.Scan(&a)
                object.list=append(object.list,a)
        }
}

func matrix_check (first,second Matrix){
        if first.rowlen!=second.columnlen{
                fmt.Println("two matrix can't be multiplicated")
                os.Exit(0)
        }
}

func formate_print (j Matrix){
        fmt.Println("the result of two matrix your input")
        for i:=0;i<j.columnlen;i++{
                for p:=0;p<j.rowlen;p++{
                        fmt.Print(j.list[i*j.rowlen+p]," ")
                }
                fmt.Println()
        }
}

func matrix_multiplication (first,second Matrix) Matrix{
        matrix_check(first,second)
        var result Matrix
        var tmp int
        result.rowlen=second.rowlen
        result.columnlen=first.columnlen
        for i:=0;i<first.columnlen;i++{
                for j:=0;j<second.rowlen;j++{
                        tmp=0
                        for k:=0;k<second.columnlen;k++{
                                tmp+=first.list[first.rowlen*i+k]*second.list[second.columnlen*k+j]
                        }
                        result.list=append(result.list,tmp)
                }
        }
        return result

}

你可能感兴趣的:(go)