Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 //вычисление корня
 void sqrt()
 {
     if(!Data.isempty())
     {
         double x = Data.pop();
         Data.push(std::sqrt(x));
         it++;
     }
     else error("cann't execute sqrt, stack is empty");
 }
Ejemplo n.º 4
0
 //умножение
 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");
 }
Ejemplo n.º 5
0
 //разность
 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");
 }
Ejemplo n.º 6
0
 //запуск виртуальной машины
 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;
         }
     }
 }
Ejemplo n.º 7
0
 //деление
 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");
 }
Ejemplo n.º 8
0
void CStackTraceImpl::Expand(CStackTrace::TStack& stack)
{
    char** syms = backtrace_symbols(&m_Stack[0], m_Stack.size());
    for (size_t i = 0;  i < m_Stack.size();  ++i) {
        string sym = syms[i];

        CStackTrace::SStackFrameInfo info;
        info.func = sym.empty() ? "???" : sym;
        info.file = "???";
        info.offs = 0;
        info.line = 0;

        string::size_type pos = sym.find_first_of("(");
        if (pos != string::npos) {
            info.module = sym.substr(0, pos);
            sym.erase(0, pos + 1);
        }

        pos = sym.find_first_of(")");
        if (pos != string::npos) {
            sym.erase(pos);
            pos = sym.find_last_of("+");
            if (pos != string::npos) {
                string sub = sym.substr(pos + 1, sym.length() - pos);
                info.func = sym.substr(0, pos);
                info.offs = NStr::StringToInt(sub, 0, 16);
            }
        }

        //
        // name demangling
        //
        if ( !info.func.empty()  &&  info.func[0] == '_') {
#if NCBI_COMPILER_VERSION >= 310
            // use abi::__cxa_demangle
            size_t len = 0;
            char* buf = 0;
            int status = 0;
            buf = abi::__cxa_demangle(info.func.c_str(),
                                      buf, &len, &status);
            if ( !status ) {
                info.func = buf;
                free(buf);
            }
#endif
        }

        stack.push_back(info);
    }

    free(syms);
}
Ejemplo n.º 9
0
int main() {
    srand(time(NULL)); // Seed random number generator
    TStack<Trash> bin; // Default to ownership
    // Fill up the Trash bin:
    for (int i = 0; i < 30; i++) {
        switch (rand() % 3) {
        case 0:
            bin.push(new Aluminum(rand() % 100));
            break;
        case 1:
            bin.push(new Paper(rand() % 100));
            break;
        case 2:
            bin.push(new Glass(rand() % 100));
            break;
        }

        // Bins to sort into:
        TStack<Trash> glassBin(0); // No ownership
        TStack<Trash> paperBin(0);
        TStack<Trash> alBin(0);
        TStackIterator<Trash> sorter(bin);

        // Sort the Trash:
        // (RTTI offers a nicer solution)
        while (sorter) {
            // Smart pointer call:
            switch(sorter->trashType()) {
            case AluminumT:
                alBin.push(sorter.current());
                break;
            case PaperT:
                paperBin.push(sorter.current());
                break;
            case GlassT:
                glassBin.push(sorter.current());
                break;
            }

            sorter++;
        }

        SumValue(alBin, out);
        SumValue(paperBin, out);
        SumValue(glassBin, out);
        SumValue(bin, out);

        return 0;
    }
} ///:~
Ejemplo n.º 10
0
int main() {
    TStack<Gromit> dogs;

    for (int i = 0; i < 5; i++) {
        dogs.push(new Gromit(i));
    }

    applist(dogs, &Gromit::speak, 1);
    applist(dogs, &Gromit::eat, 2.0f);
    applist(dogs, &Gromit::sleep, 'z', 3.0);
    applist(dogs, &Gromit::sit);

    return 0;
} ///:~
Ejemplo n.º 11
0
int main() {
  TStack s;
  int i, n, m;
  
  do {
    printf("Digite um numero interio positivo : ");
    scanf("%d", &n);
  } while (n < 0); 
  
  m = n;

  while (n) {
    s.push(n % 2);
    n = n / 2;
  }
  
  printf("O numero %d na base2 eh :", m);
  s.print("");
  return 0;
}
Ejemplo n.º 12
0
void CErrorStack::Pop()
{
    TStack *cStack = m_cErrors.get();
    if (cStack->size() > 1)
        cStack->pop();
}
Ejemplo n.º 13
0
		MacroRecord* GetTopMacro() { return m_StateStack.empty()?nullptr:(*m_StateStack.Peek())->GetCurMacro(); }
Ejemplo n.º 14
0
void add_rigid_body(OBJMESH *objmesh, float mass)
{
    /* Create a new Box collision shape for the current mesh. */
    /* Use half of the dimension XYZ to represent the extent of the box
     * relative to its pivot point, which is already centered in the middle of
     * its bounding box.
     */
    btCollisionShape *btcollisionshape = new btBoxShape(btVector3(objmesh->dimension->x * 0.5f,
                                                                  objmesh->dimension->y * 0.5f,
                                                                  objmesh->dimension->z * 0.5f));

    /* Declare a btTransform variable to be able to contain the transformation
     * matrix of the object in a form that Bullet will understand.
     */
    btTransform bttransform;

    TStack  l;
    l.loadTranslation(objmesh->location);
    // Convert angles to radians & divide by 2.
    float   alpha = objmesh->rotation->z*DEG_TO_RAD_DIV_2;
    float   cosAlpha(cosf(alpha)), sinAlpha(sinf(alpha));
    float   beta  = objmesh->rotation->y*DEG_TO_RAD_DIV_2;
    float   cosBeta(cosf(beta)), sinBeta(sinf(beta));
    float   gamma = objmesh->rotation->x*DEG_TO_RAD_DIV_2;
    float   cosGamma(cosf(gamma)), sinGamma(sinf(gamma));
    float   cAcB(cosAlpha*cosBeta);
    float   sAsB(sinAlpha*sinBeta);
    float   cAsB(cosAlpha*sinBeta);
    float   sAcB(sinAlpha*cosBeta);
    l.rotate(quaternion(cAcB*cosGamma+sAsB*sinGamma,
                        cAcB*sinGamma-sAsB*cosGamma,
                        cAsB*cosGamma+sAcB*sinGamma,
                        sAcB*cosGamma-cAsB*sinGamma));

    /* Assign the current transformation matrix that you create using the
     * standard "OpenGL way" and send it over to the Bullet transform variable.
     */
    bttransform.setFromOpenGLMatrix(l.back().m());

    /* Create a new motion state in order for Bullet to be able to
     * maintain and interpolate the object transformation.
     */
    btDefaultMotionState *btdefaultmotionstate = NULL;

    btdefaultmotionstate = new btDefaultMotionState(bttransform);

    /* Create a Bullet vector to be able to hold the local inertia of
     * the object.
     */
    btVector3 localinertia(0.0f, 0.0f, 0.0f);

    /* If a mass greater than 0 is passed in as a parameter to the function,
     * use it to calculate the local inertia.  If a mass is equal to 0, it means
     * that the object is static and you do not need to execute this calculation.
     */
    if (mass > 0.0f)
        btcollisionshape->calculateLocalInertia(mass, localinertia);

    /* Create a new rigid body and link the information that you have
     * calculated above.  Note that you are using the btRigidBody pointer already
     * contained in the OBJMESH structure to initialize the class.  This way, when
     * you're drawing, you can easily query the pointer in order to gain access to
     * its transformation matrix, which is from now on maintained by Bullet
     * internally.
     */
    objmesh->btrigidbody = new btRigidBody(mass,
                                           btdefaultmotionstate,
                                           btcollisionshape,
                                           localinertia);

    /* Built into the btRigidBody class is a "void *" variable that
     * allows you to associate a user-defined pointer with the rigid
     * body.  By associating the current objmesh pointer to this
     * variable, you will have direct access to the OBJMESH structure
     * at any time inside any Bullet-driven functions and callbacks.
     */
    objmesh->btrigidbody->setUserPointer(objmesh);

    /* Only mark the object named "Cube" to receive a contact-added callback. */
    if (!strcmp(objmesh->name, "Cube")) {
        /* Adjust the collision flags for this body by adding
         * CF_CUSTOM_MATERIAL_CALLBACK to the current flags.
         */
        objmesh->btrigidbody->setCollisionFlags(objmesh->btrigidbody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
    }

    /* Add the new rigid body to your physical world. */
    dynamicsworld->addRigidBody(objmesh->btrigidbody);
}
Ejemplo n.º 15
0
// 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;
}
Ejemplo n.º 16
0
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;*/

}
Ejemplo n.º 17
0
unsigned int CErrorStack::Count()
{
    TStack *cStack = m_cErrors.get();
    return cStack->size() - 1;
}
Ejemplo n.º 18
0
const CErrorStack::SError &CErrorStack::Check()
{
    TStack *cStack = m_cErrors.get();
    return cStack->top();
}
Ejemplo n.º 19
0
void CErrorStack::Clear()
{
    TStack *cStack = m_cErrors.get();
    while (cStack->size() > 1)
        cStack->pop();
}
Ejemplo n.º 20
0
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;
}
Ejemplo n.º 21
0
void CErrorStack::Push(const CErrorStack::ECode eCode, const CString &cMessage)
{
    TStack *cStack = m_cErrors.get();
    cStack->push(SError(eCode, cMessage));
}