Compare commits

..

No commits in common. "fa33d861bedd990137fb587f4faee6a90477e96a" and "06aa0e6813dded3a936d77dc980085d81221be32" have entirely different histories.

2 changed files with 82 additions and 199 deletions

View File

@ -295,56 +295,92 @@
// console.log(chiffre_num) // console.log(chiffre_num)
// console.log(symb) // console.log(symb)
let n1 = chiffre_num[0]
for (let i = 0; i < symb.length; i++) { for (let i = 0; i < symb.length; i++) {
let s = symb[i] let s = symb[i]
let n2 = chiffre_num[i+1]
if (s === "x" || s === "/") { if (s === "x" || s === "/") {
console.log("prio", s, n1, n2)
}
}
let symbb_2 = []
let chiffre_num_2 = []
let previous = chiffre_num[0]
for (let i = 0; i < symb.length; i++) {
const s = symb[i]
const n = chiffre_num[i + 1]
if (s === "x") { if (s === "x") {
n1 = n1 * n2 console.log(previous, s, n)
} else { previous = previous * n
n1 = n1 / n2 } else if (s === "/") {
} console.log(previous, s, n)
} else { previous = previous / n
console.log("no prio", s, n1, n2)
let nn1 = n2
// if (s2 === "x" || s2 === "/") {
for (let index = i+1; index < symb.length; index++) {
let nn2 = chiffre_num[index+1]
let s2 = symb[index]
if (s2 === "x" || s2 === "/") {
if (s2 === "x" ) {
nn1 = nn1 * nn2
} else {
nn1 = nn1 / nn2
}
n2=nn1
} else { } else {
if (i === symb.length) {
console.log("n", n)
chiffre_num_2.push(n)
break break
}
i = index
console.log("XXX",n2)
}
// }
if (s === "+" ) {
n1 = n1 + n2
} else { } else {
n1 = n1 - n2 console.log("val prev2", previous)
} chiffre_num_2.push(previous)
} }
console.log("resultat", n1) if (i === 0) {
console.log("toto2", previous)
chiffre_num_2.push(previous)
} else {
console.log(symb[i + 1] === "-", symb[i + 1] === "+", symb[i + 1])
if (symb[i + 1] === "-" || symb[i + 1] === "+")
console.log("valeur si +&-", n)
chiffre_num_2.push(n)
}
previous = n
symbb_2.push(s)
continue
}
console.log("val previous", previous)
chiffre_num_2.push(previous)
// console.log(previous)
}
console.log(chiffre_num_2)
console.log(symbb_2)
previous = chiffre_num_2[0]
for (let i = 0; i < symbb_2.length; i++) {
const s = symbb_2[i]
const n = chiffre_num_2[i + 1]
for (let i = 0; i < symbb_2.length; i++) {
const s = symbb_2[i]
// const n = chiffre_num_2[i + 1]
// if (s === "+") {
// console.log(previous, s, n)
// previous = previous + n
// } else if (s === "-") {
// console.log(previous, s, n)
// previous = previous - n
// }
}
// console.log(previous)
// info : les mult &div sont fonctionnels, les prios aussi. Mais la liste ne
// prend pas en compte la derniere valeur de la liste !
// let val_int = parseFloat(val)
// console.log(val_int)
// element_oui.value = ""
// document.getElementById("memoir1").value = val_int
return
} }

View File

@ -1,153 +0,0 @@
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()
}