154 lines
3.1 KiB
Go
154 lines
3.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
type TestObject struct {
|
||
|
Numbers []int
|
||
|
Symbols []string
|
||
|
Res int
|
||
|
}
|
||
|
|
||
|
func Handle_multiplications(symbols []string, numbers []int) int {
|
||
|
ret := 0
|
||
|
|
||
|
for i, s := range symbols {
|
||
|
n1 := numbers[i]
|
||
|
n2 := numbers[i+1]
|
||
|
if s == "*" {
|
||
|
ret = n1 * n2
|
||
|
} else {
|
||
|
ret = n1 / n2
|
||
|
}
|
||
|
}
|
||
|
return ret
|
||
|
}
|
||
|
|
||
|
func Calculate1(symbols []string, numbers []int) int {
|
||
|
next_iter_symb_list := []string{}
|
||
|
next_iter_numbers_list := []int{}
|
||
|
|
||
|
fmt.Printf("init %v %v\n", symbols, numbers)
|
||
|
|
||
|
for i := 0; i < len(symbols); i++ {
|
||
|
s := symbols[i]
|
||
|
|
||
|
// }
|
||
|
// for i, s := range symbols {
|
||
|
n1 := numbers[i]
|
||
|
n2 := numbers[i+1]
|
||
|
|
||
|
if s == "-" || s == "+" {
|
||
|
next_iter_numbers_list = append(next_iter_numbers_list, n1)
|
||
|
next_iter_symb_list = append(next_iter_symb_list, s)
|
||
|
// fmt.Println("will add n SUM", n1)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
newN := 0
|
||
|
if s == "*" {
|
||
|
newN = n1 * n2
|
||
|
} else if s == "/" {
|
||
|
newN = n1 / n2
|
||
|
}
|
||
|
|
||
|
j := i
|
||
|
for {
|
||
|
if j == len(symbols) {
|
||
|
next_iter_numbers_list = append(next_iter_numbers_list, newN)
|
||
|
break
|
||
|
}
|
||
|
|
||
|
if symbols[j+1] == "-" || symbols[j+1] == "+" {
|
||
|
i = j
|
||
|
next_iter_numbers_list = append(next_iter_numbers_list, newN)
|
||
|
break
|
||
|
} else {
|
||
|
n2 := numbers[j+1]
|
||
|
if symbols[j+1] == "/" {
|
||
|
newN = newN / n2
|
||
|
} else {
|
||
|
newN = newN * n2
|
||
|
}
|
||
|
}
|
||
|
j += 1
|
||
|
}
|
||
|
|
||
|
// fmt.Println("new v:", newN)
|
||
|
|
||
|
// if i == len(symbols)-1 || symbols[i+1] == "+" || symbols[i+1] == "-" {
|
||
|
// if i == len(symbols)-1 {
|
||
|
// // fmt.Println("1st loop last")
|
||
|
// }
|
||
|
// // fmt.Println("will add n or Next LAST or SUM", newN)
|
||
|
// next_iter_numbers_list = append(next_iter_numbers_list, newN)
|
||
|
// } else {
|
||
|
// numbers[i+1] = newN
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
fmt.Printf("inter %v %v\n", next_iter_symb_list, next_iter_numbers_list)
|
||
|
|
||
|
res := 0
|
||
|
for i, s := range next_iter_symb_list {
|
||
|
n1 := next_iter_numbers_list[i]
|
||
|
n2 := next_iter_numbers_list[i+1]
|
||
|
|
||
|
if s == "-" {
|
||
|
res = n1 - n2
|
||
|
} else if s == "+" {
|
||
|
res = n1 + n2
|
||
|
}
|
||
|
|
||
|
// fmt.Println("1", res)
|
||
|
|
||
|
next_iter_numbers_list[i+1] = res
|
||
|
}
|
||
|
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
// func Calculate(symbols []string, numbers []int) int {
|
||
|
// res := 0
|
||
|
|
||
|
// nxt_symb := []string{}
|
||
|
// nxt_numb := []int{}
|
||
|
|
||
|
// for i := 0; i < len(symbols); i++ {
|
||
|
// s := symbols[i]
|
||
|
// n1 := numbers[i]
|
||
|
// n2 := numbers[i+1]
|
||
|
|
||
|
// if s == "" {
|
||
|
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
// return res
|
||
|
// }
|
||
|
|
||
|
// TestCalculate calls greetings.Hello with a name, checking
|
||
|
// for a valid return value.
|
||
|
func TestCalculate() {
|
||
|
tests := []*TestObject{
|
||
|
{Numbers: []int{-4, 14, 2, 5}, Symbols: []string{"-", "+", "*"}, Res: -8}, // -4-14+2*5
|
||
|
{Numbers: []int{4, 4, 2, 2}, Symbols: []string{"*", "+", "/"}, Res: 17}, // 4*4+2/2
|
||
|
{Numbers: []int{2, 5, 2, 5}, Symbols: []string{"*", "*", "-"}, Res: 15}, // 2*5*2-5
|
||
|
{Numbers: []int{-4, 14, 2}, Symbols: []string{"-", "+"}, Res: -16}, // -4-14+2
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
res := Calculate1(test.Symbols, test.Numbers)
|
||
|
if test.Res != res {
|
||
|
fmt.Println("FAILED: the expected result was supposed to be", test.Res, "but we got", res, test.Numbers)
|
||
|
} else {
|
||
|
fmt.Println("GOOD:", res, test.Numbers)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
TestCalculate()
|
||
|
}
|