Beispiel #1
0
// Dijkstra's Two-Stack Algorithm for Expression Evaluation
// $ ./evaluate
//   ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
//   101.0
// $ ./evaluate
//   ( ( 1 + sqrt ( 5.0 ) ) / 2.0 )
//   1.618033988749895
func main() {
	ops := stack.NewLinked()
	vals := stack.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		s, _ := stdin.ReadString()

		if s == "(" {
			// ignore...
		} else if s == "+" {
			ops.Push(s)
		} else if s == "-" {
			ops.Push(s)
		} else if s == "*" {
			ops.Push(s)
		} else if s == "/" {
			ops.Push(s)
		} else if s == "sqrt" {
			ops.Push(s)
		} else if s == ")" {
			// Pop, evaluate, and push result if token is ")"
			var value float64
			op, _ := ops.Pop()
			v, _ := vals.Pop()

			if op == "+" {
				v1, _ := vals.Pop()
				value = v1.(float64) + v.(float64)
			} else if op == "-" {
				v1, _ := vals.Pop()
				value = v1.(float64) - v.(float64)
			} else if op == "*" {
				v1, _ := vals.Pop()
				value = v1.(float64) * v.(float64)
			} else if op == "/" {
				v1, _ := vals.Pop()
				value = v1.(float64) / v.(float64)
			} else if op == "sqrt" {
				value = math.Sqrt(v.(float64))
			}
			vals.Push(value)
		} else {
			// Token not operator or paren: push value
			value, _ := strconv.ParseFloat(s, 64)
			vals.Push(value)
		}
	}

	result, _ := vals.Pop()
	fmt.Println(result.(float64))
}
Beispiel #2
0
/*************************************************************************
 *  Execution: ./stack < input.txt
 *
 *  % more tobe.txt
 *  to be or not to - be - - that - - - is
 *
 *  % ./stack < tobe.txt
 *  to be not that or be (2 left on stack)
 *
 *************************************************************************/
func main() {
	s := stack.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			s.Push(item)
		} else if !s.IsEmpty() {
			item, _ := s.Pop()
			fmt.Printf("%s ", item)
		}
	}

	fmt.Printf("(%d left on stack)\n", s.Size())
}
Beispiel #3
0
// Execution: ./queue < input.txt
// Data files: http://algs4.cs.princeton.edu/13stacks/tobe.txt
// % ./queue < tobe.txt
//   to be or not to be (2 left on queue)
func main() {
	q := queue.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			q.Enqueue(item)
		} else if !q.IsEmpty() {
			item, _ := q.Dequeue()
			fmt.Print(item, " ")
		}
	}

	fmt.Printf("(%d left on queue)\n", q.Size())
}
Beispiel #4
0
/*************************************************************************
 *  Execution: ./bag < input.txt
 *
 *  % more tobe.txt
 *  to be or not to - be - - that - - - is
 *
 *  % ./bag < tobe.txt
 *  size of bag = 14
 *  is
 *  -
 *  -
 *  -
 *  that
 *  -
 *  -
 *  be
 *  -
 *  to
 *  not
 *  or
 *  be
 *  to
 *************************************************************************/
func main() {
	bag := bag.NewLinked()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		bag.Add(item)
	}

	fmt.Println("size of bag =", bag.Size())
	bag.Each(func(item interface{}) {
		fmt.Println(item)
	})
	// for e := range bag.Iter() {
	// 	fmt.Println(e)
	// }
}
Beispiel #5
0
func main() {
	q := queue.NewSliced()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			q.Enqueue(item)
		} else if !q.IsEmpty() {
			item, _ := q.Dequeue()
			fmt.Printf("%s ", item)
		}
	}

	fmt.Printf("(%d left on queue)\n", q.Size())
	q.Each(func(item interface{}) {
		fmt.Println(item)
	})
}
Beispiel #6
0
/*************************************************************************
 *  Execution:  ./slicedstack < input.txt
 *  Data files: http://algs4.cs.princeton.edu/13stacks/tobe.txt
 *
 *  % more tobe.txt
 *  to be or not to - be - - that - - - is
 *
 *  % ./slicedstack < tobe.txt
 *  to be not that or be (2 left on stack)
 *
 *************************************************************************/
func main() {
	s := stack.NewSliced()
	stdin := stdin.NewStdin()

	for !stdin.IsEmpty() {
		item, _ := stdin.ReadString()
		if item != "-" {
			s.Push(item)
		} else if !s.IsEmpty() {
			item, _ := s.Pop()
			fmt.Printf("%s ", item)
		}
	}

	fmt.Printf("(%d left on stack)\n", s.Size())
	s.Each(func(item interface{}) {
		fmt.Println(item)
	})
}