Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
c++ eval - double numbers v1.0
#include <iostream> #include <string> #include <cmath> using namespace std; template<class T> class Node { public: T data; Node<T> *next; Node() { data = 0; next = 0; } Node(T value, Node<T> *nxt = 0) { data = value; next = nxt; } }; template<class T> class List { private: Node<T> *head, *tail; public: List() { head = tail = 0; } int Count() { int s = 0; Node<T> *temp = head; while (temp != 0) { temp = temp->next; s++; } return s; } void Add2Head(T value) { if (head == 0) { head = tail = new Node<T>(value, 0); } else { Node<T> *temp = new Node<T>(value, 0); temp->next = head; head = temp; } } void DeleteHead(T &n) { if (head == 0) { return; } else if (head == tail) { n = (head->data); head = tail = 0; } else { n = (head->data); Node<T> *temp = head->next; delete head; head = temp; } } void DeleteHead() { if (head == 0) { return; } else if (head == tail) { head = tail = 0; } else { Node<T> *temp = head->next; delete head; head = temp; } } T GetHeadValue() { return (head->data); } bool IsEmpty() { return (head == 0); } void Print() { cout << "*****" << endl; Node<T> *temp=head; while (temp!=0) { cout << temp->data << endl; temp=temp->next; } cout << "*****" << endl; } }; template<class T> class Stack { private: List<T> l; public: void Push(T n) { l.Add2Head(n); } bool Pop(T &n) { if (l.IsEmpty()) { return false; } else { l.DeleteHead(n); return true; } } bool IsEmpty() { return l.IsEmpty(); } bool Flush() { if (l.IsEmpty()) { return false; } else { while (!l.IsEmpty()) { l.DeleteHead(); } return true; } } T Top() { return (l.GetHeadValue()); } void Print() { l.Print(); } }; double eval(string str) { char d; double m,n; Stack<double> s; int length=0; double result=0; bool is_right_point=false; for (int i=0;i<str.size();i++) { d=str[i]; if (d>='0' && d<='9') { result += (d-'0')*pow(10,(is_right_point=true) ? (length--) : (length++)); } else if (d=='.') { length=-1; is_right_point=true; } else if (d==' ') { is_right_point=false; s.Push(result); length=0; result=0; } else if (d=='+') { s.Pop(m); s.Pop(n); s.Push(m+n); } else if (d=='-') { s.Pop(m); s.Pop(n); s.Push(n-m); } else if (d=='*') { s.Pop(m); s.Pop(n); s.Push(m*n); } else if (d=='/') { s.Pop(m); s.Pop(n); s.Push(n/m); } } s.Print(); return s.Top(); } void main() { /* char str[] = "Hello World!"; Stack s; cout << str << endl; for (int i = 0; i < sizeof(str); i++) { s.Push(str[i]); } cout << endl; cout << s.IsEmpty() << endl; cout << s.Top() << endl; */ //system("pause"); cout << eval("9.2 2 + 5 +") << endl; }
run
|
edit
|
history
|
help
0
#10.1
#30.3
iota_30-Seconds-of-C++
a parameter pack
Ελλάδα !!!!
Why is vsnprintf Not Writing the Same Number of Characters as strncpy Would?
multiply linked list numbers
Template float/int comparison
seh exception
DCapSurfaceDesc