int main(int argc, char** argv) { TStack stack; stack.push(Triangle(1, 1, 1)); stack.push(Triangle(2, 2, 2)); stack.push(Triangle(3, 3, 3)); std::cout << stack; Triangle t, tmp1, tmp2; t = stack.pop(); std::cout << t; t = stack.pop(); std::cout << t; t = stack.pop(); std::cout << t; t = Triangle(15, 15, 15); tmp1 = Triangle(20, 15, 15); isEqual(t, tmp1); tmp1 = t; isEqual(t, tmp1); return 0; }
//вывод на экран void print() { if(!Data.isempty()) std::cout << "Result is: " << Data.pop() << std::endl; else error("cann't res, stack is empty"); it++; //так можно вывести значение переменной x1 //cout << VM::table.at("x1") << endl; }
//вычисление корня void sqrt() { if(!Data.isempty()) { double x = Data.pop(); Data.push(std::sqrt(x)); it++; } else error("cann't execute sqrt, stack is empty"); }
//умножение void mul() { if(Data.size()>1) { double x = Data.pop(); double y = Data.pop(); Data.push(x*y); it++; } else error("cann't execute mul, stack is empty"); }
//разность void sub() { if(Data.size()>1) { double y = Data.pop(); double x = Data.pop(); Data.push(x-y); it++; } else error("cann't execute add, stack is empty"); }
//запуск виртуальной машины void start() { if(Code == nullptr) { error("Not found code"); return; } while(1) { char cur = Code[it]; int curit = it; switch (Code[it]) { //в зависимости от кода функции выбираем действие case CNVAR: it++; getname(); add_var(string(NameVar)); break; case CSVAR: it++; getname(); set_var(NameVar, Data.pop()); break; case CPUSH: it++; Data.push(*((double*)(Code+it))); it+=sizeof(double); break; case CMULT: mul(); break; case CADD: add(); break; case CSUB: sub(); break; case CDIV: div(); break; case CSQRT: sqrt(); break; case COUT: print(); break; case CHALT: return; default: error("Cann't understand command."); break; } } }
//деление void div() { if(Data.size()>1) { double y = Data.pop(); double x = Data.pop(); if(y) Data.push(x/y); else error("devide by zero"); it++; } else error("cann't execute add, stack is empty"); }
// Traverses ray through KDTree and intersects ray with all of the objects along // the way. Returns the first intersecting object // if there is one. // Entry: // ray - the ray being traced // KDTree - the KD tree enclosing the entire environment // Exit: // obj - the first object that intersects the ray bool KDTree::findFirstIntersection( const TRay &ray, const float maxDist2, TObject *&obj ) const { TStack *stack; BinNode *currentNode, *nearChild, *farChild; float dist, _min, _max; TPoint3 p; #ifdef __DEBUG__ ray.origin.print("ray origin"); ray.direction.print("ray direction"); ray.reverseDirection.print("ray reverse direction"); #endif // test if the whole KD tree is missed by the input ray if ( !rayBoxIntersect(ray, min, max, _min, _max ) ) #ifdef __DEBUG__ { printf("whole KD tree is missed by ray\n"); #endif return false; #ifdef __DEBUG__ } #endif #ifdef __DEBUG__ printf("rayBoxIntersect: %5.5f %5.5f\n", _min, _max ); #endif stack = new TStack; stack->init(); currentNode = root; while ( currentNode != NULL ) { while ( !currentNode->leaf() ) { #ifdef __DEBUG__ currentNode->min.print("current node min"); currentNode->max.print("current node max"); printf("is leaf: %d\n", currentNode->leaf() ); currentNode->child[0]->max.print("cut plane"); #endif dist = currentNode->distanceToDivisionPlane( currentNode->child[0]->max, ray ); currentNode->getChildren( currentNode, ray.origin, nearChild, farChild ); #ifdef __DEBUG__ printf("distance to plane: %5.5f\n", dist ); nearChild->min.print("near min"); nearChild->max.print("near max"); printf("is near leaf: %d\n", nearChild->leaf() ); farChild->min.print("far min"); farChild->max.print("far max"); printf("is far leaf: %d\n", farChild->leaf() ); #endif if ( ( dist > _max ) || ( dist < 0 ) ) { #ifdef __DEBUG__ printf("using near child\n"); #endif currentNode = nearChild; } else if ( dist < _min ) { #ifdef __DEBUG__ printf("using far child\n"); #endif currentNode = farChild; } else { stack->push( farChild, dist, _max ); #ifdef __DEBUG__ printf("-->PUSH far keep near\n"); #endif currentNode = nearChild; _max = dist; } } #ifdef __DEBUG__ printf("rayObjIntersect:\n"); currentNode->min.print("currentNode min"); currentNode->max.print("currentNode max"); #endif if ( rayObjIntersect( ray, currentNode->members, maxDist2, obj ) ) return true; stack->pop( currentNode, _min,_max ); #ifdef __DEBUG__ printf("-->POP\n"); if ( currentNode ) { currentNode->min.print("currentNode min"); currentNode->max.print("currentNode max"); printf("is leaf: %d\n", currentNode->leaf() ); } #endif } return false; }
void CErrorStack::Clear() { TStack *cStack = m_cErrors.get(); while (cStack->size() > 1) cStack->pop(); }
void CErrorStack::Pop() { TStack *cStack = m_cErrors.get(); if (cStack->size() > 1) cStack->pop(); }
bool do_command(char command, TStack &numbers) /*Pre: The first parameter specifies a valid calculator command Post: The command specified by the first parameter has been applied to the stack of numbers given by the second parameter. A result of true is returned unless commnad == 'q'.*/ { stack_entry p,q; switch(command) { case '?': cout<<"Enter a real number:"<<flush; cin>>p; if(numbers.push(p) == overflow) cout<<"Warning: Stack full, lost number"<<endl; break; case '=': if(numbers.top(p) == underflow) cout<<"Stack empty"<<endl; else cout<<p<<endl; break; case '+': if(numbers.top(p) == underflow) cout<<"Stack empty"<<endl; else{ numbers.pop(); if(numbers.top(q) == underflow){ cout<<"Stack has just one entry"<<endl; numbers.push(p); } else { numbers.pop(); if(numbers.push(p+q) == overflow) cout<<"Warning: Stack full, lost result"<<endl; } } break; case '-': if(numbers.top(p) == underflow) cout<<"Stack empty"<<endl; else{ numbers.pop(); if(numbers.top(q) == underflow){ cout<<"Stack has just one entry"<<endl; numbers.push(p); } else { numbers.pop(); if(numbers.push(p-q) == overflow) cout<<"Warning: Stack full, lost result"<<endl; } } break; case '*': if(numbers.top(p) == underflow) cout<<"Stack empty"<<endl; else{ numbers.pop(); if(numbers.top(q) == underflow){ cout<<"Stack has just one entry"<<endl; numbers.push(p); } else { numbers.pop(); if(numbers.push(p*q) == overflow) cout<<"Warning: Stack full, lost result"<<endl; } } break; case '/': if(numbers.top(p) == underflow) cout<<"Stack empty"<<endl; else{ numbers.pop(); if(numbers.top(q) == underflow){ cout<<"Stack has just one entry"<<endl; numbers.push(p); } else { numbers.pop(); if(numbers.push(p/q) == overflow) cout<<"Warning: Stack full, lost result"<<endl; } } break; case 'q': cout<<"Calculation finished.\n"; return false; } return true; }
int main(int argc, char** argv) { TStack<Figure> stack; int state = start; while (1) { switch (state) { case start: { Tips(); cin >> state; break; } case add: { PrintLine(); cout << "Which figure you want to add?" << endl; TipsForFirgurs(); int fig = 0; cin >> fig; PrintStars(); if (fig == 1) { stack.push(shared_ptr<Figure>(new Triangle(cin))); PrintStars(); break; } if (fig == 2) { stack.push(shared_ptr<Figure>(new Quadro(cin))); PrintStars(); break; } if (fig == 3) { stack.push(shared_ptr<Figure>(new Rectangle(cin))); PrintStars(); break; } if (fig == 0) { state = start; PrintStars(); break; } cout << "Wrong number" << endl; PrintStars(); break; } case del: { PrintStars(); stack.pop(); PrintStars(); state = start; break; } case print: { PrintStars(); for (auto i : stack) i->Print(); PrintStars(); state = start; break; } case srt: { clock_t time; double duration; time = clock(); cout << "Sort -------------" << endl; stack.sort(); cout << "Done -------------" << endl; duration = (clock() - time) / (double)CLOCKS_PER_SEC; cout << "Time of sort: " << duration << endl; state = start; break; } case par_sort: { clock_t time; double duration; time = clock(); cout << "Parallel Sort ----" << endl; stack.sort_parallel(); cout << "Done -------------" << endl; duration = (clock() - time) / (double)CLOCKS_PER_SEC; cout << "Time of parallel sort: " << duration << endl; state = start; break; } case fin: return 0; } } /*TStack<Figure> stack; std::default_random_engine generator; uniform_int_distribution<int> distribution(1, 14); for (int i = 0; i < 14; i++) { int side = distribution(generator); stack.push(shared_ptr<Figure>(new Triangle(side, side, side))); stack.push(shared_ptr<Figure>(new Quadro(side))); stack.push(shared_ptr<Figure>(new Rectangle(side, side + 1))); } TStack<Figure> stack1 = stack; std::cout << "Sort -------------" << std::endl; stack1.sort(); stack.sort_parallel(); std::cout << "Done -------------" << std::endl; std::cout << stack << std::endl; std::cout << stack1 << std::endl;*/ }