duandong7980 2016-07-01 06:14
浏览 61
已采纳

解析数字字符串数字

I am trying to calculate the multiplication result of a few digits which are part of a long digits string. Here is my code:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    inputNum := "73167176531330624919225119"
    mult := getMult(3, inputNum)

    fmt.Printf("Mult = %d", mult)

}

func getMult(startIndex int, inputNum string) int {
    mult := 0
    for i := 0; i < 10; i++ {

        digit, err := strconv.Atoi(string(inputNum[startIndex+i]))

        if err != nil {
            mult *= int(digit)
        } else {
            fmt.Printf("Error converting %s to int  : %s
", string(inputNum[startIndex+i]), err.Error())
        }

    }
    return mult
}

The result I want to get is 6*7*1*7*6*5*3*1*3*3 = 238140

But I an getting a runtime error:

panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x20 pc=0x40130e]

goroutine 1 [running]: main.getMult(0x3, 0x534d40, 0x1a, 0x4d2701) test.go:25 +0x17e main.main() test.go:10 +0x55 exit status 2

  • 写回答

2条回答 默认 最新

  • drygauost253590142 2016-07-01 07:16
    关注

    your code will work with fixing these two typos:
    change mult := 0 to mult := 1
    and change err != nil to err == nil like this:

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        inputNum := "73167176531330624919225119"
        mult := getMult(3, inputNum)
        fmt.Printf("Mult = %d", mult)
    }
    
    func getMult(startIndex int, inputNum string) int {
        mult := 1
        for i := 0; i < 10; i++ {
            digit, err := strconv.Atoi(string(inputNum[startIndex+i]))
            if err == nil {
                mult *= int(digit)
            } else {
                fmt.Printf("Error converting %s to int  : %s
    ", string(inputNum[startIndex+i]), err.Error())
            }
        }
        return mult
    }
    

    also you may use inputNum[3:13] to get new string from index 3 with length 10,
    and you may use int(v - '0') to convert one character to integer number,
    then use for range like this:

    package main
    
    import "fmt"
    
    func main() {
        inputNum := "73167176531330624919225119"
        mult := getMult(inputNum[3:13])
        fmt.Printf("Mult = %d 
    ", mult) // Mult = 238140
    }
    
    func getMult(str string) int {
        m := 1
        for _, v := range str {
            if v >= '0' && v <= '9' {
                m *= int(v - '0')
            } else {
                fmt.Printf("Error converting %q to int
    ", v)
                break
            }
        }
        return m
    }
    

    output:

    Mult = 238140
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作