Example #1
0
	bignum& bignum::sum(const bignum& value, bool same_sign)
	{
		if(same_sign)
		{
			if(value_ == value.value_)
			{
				value_.reset();
				sign_ = false;
			}
			else if(value_ < value.value_)
			{
				sign_ = !sign_;
				value_ = substract(value.value_, value_);
			}
			else
				value_ = substract(value_, value.value_);
		}
		else
		{
			if(value_.check_adding_overflow(value.value_))
				throw_with_nested(overflow_error("number is too big"));
			value_ = add(value_, value.value_);
		}
		
		return *this;
	}
Example #2
0
	pair<bits_value, bits_value> bignum::bits_divide(const bits_value& dividend, const bits_value& divisor)
	{
		bits_size_t cursor = dividend.bits_span() - divisor.bits_span();
		bits_value divided;
		bits_value remainder = dividend >> cursor;
		
		while(cursor > 0)
		{
			if(remainder < divisor)
			{
				divided <<= 1;
				remainder <<= 1;
				remainder.set(0, dividend.test(--cursor));
			}
			else
			{
				remainder = substract(remainder, divisor);
				divided.set(0);
			}
		}
			
		if(!(remainder < divisor))
		{
			divided.set(0);
			remainder = substract(remainder, divisor);
		}
			
		return make_pair(move(divided), move(remainder));
	}
int main() {
    fixed_16_16_t a = double_to_fixed_16_16(12.6798d);
    fixed_16_16_t b = double_to_fixed_16_16(1.2d);
    fixed_16_16_t c = double_to_fixed_16_16(0.6669776578d);
    fixed_16_16_t d = double_to_fixed_16_16(-3.1d);
    fixed_16_16_t e = double_to_fixed_16_16(55.0d);
    fixed_16_16_t f = double_to_fixed_16_16(0.2d);

    printf("initial a=%.4f\n", fixed_16_16_to_double(a));
    printf("initial b=%.2f\n", fixed_16_16_to_double(b));
    printf("initial c=%.2f\n", fixed_16_16_to_double(c));
    printf("initial d=%.2f\n", fixed_16_16_to_double(d));
    printf("initial e=%.2f\n", fixed_16_16_to_double(e));
    printf("initial f=%.16f\n", fixed_16_16_to_double(f));

    fixed_16_16_t adding_value = add(a, b);

    printf("adding result=%.4f\n", fixed_16_16_to_double(adding_value));

    fixed_16_16_t substraction_value = substract(d, f);
    printf("substraction result=%.2f\n", fixed_16_16_to_double(substraction_value));

    fixed_16_16_t multiplication_value = multiply(e, d);
    printf("multiplication result=%.4f\n", fixed_16_16_to_double(multiplication_value));

    fixed_16_16_t division_value = divide(b, f);
    printf("division result=%.16f\n", fixed_16_16_to_double(division_value));

    return 0;
}
char *getDivider(int portion, char *n1, const char *n2)
{
  char  *multiplier = strdup("0");
  char  *tmp = 0;
  int   ret;

  do {
      free(tmp);
      tmp = multiplier;
      multiplier = add(tmp, "1");
      free(tmp);
      tmp = mult(multiplier, n2);
    } while (!(ret = isBigger2(tmp, n1, strlen(tmp), portion)));
  if (!strncmp(tmp, n1, portion)) {
    free(tmp);
    return multiplier;
  }
  free(tmp);
  tmp = substract(multiplier, "1");
  free(multiplier);
  if (!strcmp(tmp, "0") && portion + 1 <= strlen(n1)) {
    free(tmp);
    return getDivider(portion + 1, n1, n2);
  }
  return tmp;
}
Example #5
0
	bignum bignum::mod_inverse_of(const bignum& mod)const
	{
		pair<bignum, bignum> t = make_pair<bignum, bignum>(0x00, 0x01U);
		pair<bits_value, bits_value> r(mod.value_, value_);
		
		while(r.second.any())
		{
			auto d = bits_divide(r.first, r.second);
			r.first = r.second;
			r.second = d.second;
			
			//t.first = t.second, t.second = first - second*d.quotient
			auto transit = t.second;
			t.second *= bignum(move(d.first));
			t.first -= t.second;
			t.second = t.first;
			t.first = move(transit);
		}
		
		if(r.first.test(0) && r.first.count() == 1)
		{
			if(t.first.sign_)
				t.first.value_ = substract(mod.value_, t.first.value_);
			
			t.first.sign_ = sign_;
			
			return move(t.first);
		}
		
		throw_with_nested(coprime_error("given two nums are not co-prime"));
		
	}
Example #6
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->clear,SIGNAL(released()), this,SLOT(clear()));
    connect(ui->add,SIGNAL(released()), this,SLOT(add()));
    connect(ui->sub,SIGNAL(released()),this,SLOT(substract()));
    connect(ui->mul,SIGNAL(released()),this,SLOT(multiply()));
    connect(ui->div,SIGNAL(released()),this,SLOT(divide()));
    connect(ui->push_but_0,SIGNAL(released()),this,SLOT(zero()));
    connect(ui->push_but_1,SIGNAL(released()),this,SLOT(one()));
    connect(ui->push_but_2,SIGNAL(released()),this,SLOT(two()));
    connect(ui->push_but_3,SIGNAL(released()),this,SLOT(tree()));
    connect(ui->push_but_4,SIGNAL(released()),this,SLOT(four()));
    connect(ui->push_but_5,SIGNAL(released()),this,SLOT(five()));
    connect(ui->push_but_6,SIGNAL(released()),this,SLOT(six()));
    connect(ui->push_but_7,SIGNAL(released()),this,SLOT(seven()));
    connect(ui->push_but_8,SIGNAL(released()),this,SLOT(eight()));
    connect(ui->push_but_9,SIGNAL(released()),this,SLOT(nine()));
    connect(ui->equal,SIGNAL(released()),this,SLOT(equal()));
    connect(ui->point,SIGNAL(released()),this,SLOT(point()));
    connect(ui->convertingto8,SIGNAL(released()),this,SLOT(converting8()));
    connect(ui->point,SIGNAL(released()),this,SLOT(converting10()));
}
Example #7
0
/**
 * Remove one given item
 */
bool ItemStorage::remove(int item) {
	for (int i=0; i<slot_number; i++) {
		if (storage[i].item == item) {
			substract(i, 1);
			return true;
		}
	}
	return false;
}
Example #8
0
int main()
{
  int tab[10];
  int *point=tab;

  getValues(point);
  printf("Wynik: %d\n",substract(point));
 
}
Example #9
0
main()
{
     int a,b,c;
     a=10;
     b=23;
     c=add(a,b);
     printf("%d",c);
     c=substract(a,b);
     printf("%d",c);
}
bool ScienceTheft::play(Player* owner, Player* opponent){
	if(!substract(owner)){
		return false;
	}

	int stolenScience = std::min(opponent->getScience(), 6);
	owner->setScience(owner->getScience() + stolenScience);
	opponent->setScience(opponent->getScience() - stolenScience);

	return true;
}
Example #11
0
ItemStack MenuItemStorage::click(InputState * input) {
	ItemStack item;
	drag_prev_slot = slotOver(input->mouse);
	if( drag_prev_slot > -1) { 
		item = storage[drag_prev_slot];
		if( input->pressing[SHIFT]) {
			item.quantity = 1;
		}
		substract( drag_prev_slot, item.quantity);
		return item;
	}
	else {
		item.item = 0;
		item.quantity = 0;
		return item;
	}
}
Example #12
0
int main()
{
    printf("Hello world!\n");
        memset(a,48,10);
    memset(b,48,10);
    scanf("%s %s",a,b);
    reverse(a,strlen(a));
   // printf("After Reverse a=%s\n",a);
    reverse(b,strlen(b));
    //printf("After Reverse b=%s\n",b);
    len=strlen(a);
   // a[len]=48;
    substract();
        reverse(a,len);
    printf("%s",a);
  return 0;
}
char    *substract(const char *n1, const char *n2)
{
  int size1, size2, ori;
  char  *ret;

  if (!n1 || !n2)
    return strdup("0");
  size1 = strlen(n1);
  size2 = strlen(n2);
  switch (check_biggest(size1, size2, n1, n2)) {
    case 1:
      return substract(n2, n1);
    case 2:
      return strdup("0");
    default:
      break;
    }
  if (!(ret = malloc(sizeof(*ret) * (size1 + 1)))) {
      return 0;
  }
  memset(ret, '0', size1);
  ret[size1] = 0;
  ori = size1;
  --size1;
  --size2;
  while (size2 >= 0) {
      ret[size1] += (n1[size1] - '0') - (n2[size2] - '0');
      if (ret[size1] < '0') {
          ret[size1 - 1] -= 1;
          ret[size1] += 10;
        }
      --size1;
      --size2;
    }
  while (size1 >= 0) {
      ret[size1] += (n1[size1] - '0');
      --size1;
    }
  while (ret[++size1] == '0');
  if (size1)
    memmove(ret, ret + size1, ori - size1 + 1);
  return ret;
}
Example #14
0
/**
 * Compute an addition or a substraction of polynomial
 *
 * @param poly1 The first polynomial
 * @param poly2 The second polynomial
 * @param operation Type of computation
 * @return Result of the computation
 */
polynomial addSubPoly(polynomial poly1, polynomial poly2, int operation) {
    // We work on dev form
    if (!poly1 || !poly2 || !poly1->dev || !poly2->dev)
        return NULL;

    list firstPoly = poly1->dev;
    list secondPoly = poly2->dev;
    polynomial newPoly = malloc(sizeof (polynomialElem));

    while (firstPoly || secondPoly) {
        /**
         * We try to get the value of the exponent of the monomial.
         * If the monomial is not defined (we are at the end of the polynomial)
         * we set the exponent to a infinit value so that the monomial of the
         * other polynomial will automatically be added/substracted at the end
         * of the new polynomial.
         */
        int firstExponent = firstPoly ? firstPoly->exponent : INT_MAX;
        int secondExponent = secondPoly ? secondPoly->exponent : INT_MAX;

        if (firstExponent < secondExponent) {
            addNode(newPoly, firstPoly->number, firstPoly->exponent, DEV_FORM);
            firstPoly = firstPoly->succ;
        } else if (secondExponent < firstExponent) {
            complexNumber tmpCplx = secondPoly->number;
            if (operation == SUB_OP) {
                tmpCplx.real = -getRealPart(secondPoly->number);
                tmpCplx.imaginary = getImaginaryPart(secondPoly->number);
            }
            addNode(newPoly, tmpCplx, secondPoly->exponent, DEV_FORM);
            secondPoly = secondPoly->succ;
        } else {
            if (operation == ADD_OP)
                addNode(newPoly, add(firstPoly->number, secondPoly->number), firstPoly->exponent, DEV_FORM);
            else
                addNode(newPoly, substract(firstPoly->number, secondPoly->number), firstPoly->exponent, DEV_FORM);
            firstPoly = firstPoly->succ;
            secondPoly = secondPoly->succ;
        }
    }

    return newPoly;
}
char  *modulo(const char *n1, const char *n2)
{
  int   size1, size2, sup;
  char  *tmp, *tmp2, *multi;

  if (!n1 || !n2)
    return 0;
  if (!strcmp(n2, "0")) {
      return strdup("null");
    }
  if (!strcmp(n2, "1")) {
    return strdup("0");
  }
  size1 = strlen(n1);
  size2 = strlen(n2);
  if (size1 < size2) {
      return strdup(n1);
    }
  if (!strcmp(n1, n2)) {
      return strdup("0");
    }
  tmp = strdup(n1);
  while (strcmp(tmp, "0") && isBigger2(tmp, n2, size1, size2)) {
      sup = getSuperior(size1, size2, tmp, n2);
      multi = getDivider(sup, tmp, n2);
      tmp2 = mult(multi, n2);
      free(multi);
      multi = getSubstractNumber(tmp, tmp2);
      free(tmp2);
      tmp2 = multi;
      multi = substract(tmp, tmp2);
      free(tmp);
      free(tmp2);
      tmp = multi;
      size1 = strlen(tmp);
      if (!strcmp(tmp, n2)) {
	free(tmp);
	return strdup("0");
      }
    }
  return tmp;
}
Example #16
0
//Parse and translate a Math Expression
void expression(){
    if (isAddop(look)) {
        emitln("xor\t%eax,%eax"); //unary - or + so first operand is zero
    }else{
        term();
    }
    while (isAddop(look)) {
        emitln("push\t%eax"); //push eax to top of stack
        switch (look) {
            case '+':
                add();
                break;
            case '-':
                substract();
                break;
            default:
                expected("Addop");
                break;
        }
    }
}
Example #17
0
File: factor.cpp Project: qbolec/c
int divide(char * a,int al ,char * b ,int bl){
	//written division a/b - whitout result : only testing the divisibility
	int i;
	for(i=0;i<al;i++)
		todiv[i+1]=a[i];
	todiv[0]=al;
	for(i=0;i<bl;i++)
		divby[i+al-bl+1]=b[i];
	for(i=1;i<=al-bl;i++)
		divby[i]=0;
	divby[0]=al;
	while(1){
		while(cmp(divby,todiv)>0)
			if(divby[0]>bl)div10(divby);
			else return 0;

		while(cmp(todiv,divby)>=0)
			substract(todiv,divby);

		if(todiv[0]==0)return 1;
	}

}
Example #18
0
bool Interpreter::assign()
{
    if (lookIsAddop()) {
        currentStatement->createRightChild(new Literal(MyVariant(NUMBER, new real_type(0))));
    }
    else {
        if (!term()) return false;
    }

     while (look!='\0' && look!=')' && look!=',')
     {
         if (look == '+') {
             if (!add()) return false;
         }
         else if (look =='-') {
             if (!substract()) return false;
         }
         else {
             reportExpected("Addop");
             return false;
         }
     }
}
 void NobracketString::calculating(){

   	bool havesametype=false;
   	//check for mutipositio
   	int temporarySize=0;
   if(op.size()==0){

   }else{
   	for(int i = 0;i<=op.size();++i){							//check if op contains '*'

   		if(temporarySize != op.size()){
   		  		i = 0;
   		  	}
   		temporarySize= op.size();
   		if(op[i]=='/'){
   			if(i==0){
   				divide(somenumbs[0],type[0],somenumbs[1],type[1]);
   				if(isReturnOneNumb){									//if the answer == is return one value example: log_3:4;

   					somenumbs[i]=opAnswer;								//set the element i to the opAnser,
   				  	somenumbs.erase(somenumbs.begin()+1);			//erase the second element
   				  	op.erase(op.begin());							//erase the '*'
   				}else{
   					////cout<<"the '*' sign in the index 0 and the answer return more then one value"<<endl;
   				}
   			}else{

   				divide(somenumbs[i],type[i],somenumbs[i+1],type[i+1]);
   				if(isReturnOneNumb){									//if the answer == is return one value example: log_3:4;
   					somenumbs[i]=opAnswer;								//set the element i to the opAnser,
   					somenumbs.erase(somenumbs.begin()+(i+1));			//erase the second element
   					op.erase(op.begin()+(i));							//erase the '*'
 //  					cout<<"the mutip() get answer is now "<<somenumbs[i]<<endl;
 //  					cout<<"im in the calculating function delect '*' sign DOES NOT in the index 0"<<endl;
   				}
   				else{

   		}
   		}
   		if(op[i]=='*'){

   			if(i==0){

   				Multip(somenumbs[0],type[0],somenumbs[1],type[1]);		//do the mulip()
   				if(isReturnOneNumb){									//if the answer == is return one value example: log_3:4;
   					somenumbs[i]=opAnswer;								//set the element i to the opAnser,
   					somenumbs.erase(somenumbs.begin()+1);			//erase the second element
   					op.erase(op.begin());							//erase the '*'
 //
   				}
   				/*
   				 * for 3*sqrt:3+7*sqrt:3 this case;
   				 */
   				else{
   					//cout<<"the '*' sign in the index 0 and the answer return more then one value"<<endl;
   				}								// if the answer return more then one value, example 5*log_3:4;
 //  					somenumbs[i]=opAnswer;
 //  					somenumbs.erase(somenumbs.begin()+1);			//erase the second element
 //  					op.erase(op.begin());	//do the mulitify everything as it is.
   			}
   			else{
   				//cout<<"do the mulitify."<<endl;
   				Multip(somenumbs[i],type[i],somenumbs[i+1],type[i+1]);
   				if(isReturnOneNumb){									//if the answer == is return one value example: log_3:4;
   					somenumbs[i]=opAnswer;								//set the element i to the opAnser,
   					somenumbs.erase(somenumbs.begin()+(i+1));			//erase the second element
   					op.erase(op.begin()+(i));							//erase the '*'
 //
   				}
   				else{}												// if the answer return more then one value, example 5*log_3:4;
   																	//keep everything as it is.
   			}

   		}
   		else{}	//if(op[i]!='*', do nothing;
   	//if(temporarySize != op.size()){
   	//	i = 0;
   	//}
   	}					//end of checking '*'
   }



 for(int i=0;type.size()-1>=somenumbs.size();i++){
 	type.pop_back();

 }
 int tempSize=0;
   	for(int i=0;i<op.size();i++){				//start to check if they have the same type; ad do the calculation
   		for(int j=i+1;j<op.size()+1;j++)
   		{
   			tempSize=op.size();

   				if((type[i]==type[j]&&op[j]=='*')||(type[i]==type[j]&&op[i]=='*')){		//if the op is a *, skip
   					//do nothing;

   				}
   				else if(type[i]==type[j]&&op[j-1]!='*'&&op[j-1]!='*'){				//if it has same type, and op does not have *,check for operator

   					havesametype = true;
   					if(op[j-1]=='+')
   					{				//only have two case +,-
   						add(somenumbs[j],type[j],somenumbs[i],type[i]);

   						if(isReturnOneNumb)
   						{

   							somenumbs[i]=opAnswer;
   							if(somenumbs.size()==2&&type.size()==2&&type[1]!=type[0])//set the element i to the opAnser,
   							{
   								isReturnOneNumb=false;
   								havesametype = false;

   							}else{
   								somenumbs.erase(somenumbs.begin()+(j));	//erase the second element
   								type.erase(type.begin()+j);
   								op.erase(op.begin()+(j-1));				//erase the op

   							}


   						}
   						else
   						{
   							//don't change anything.
   						}
   								// here need to do something with vectors somenumb and type
   								// 3 + 7 = 10, 10 will replace 3 in somenumb vector and delete 7 and + from somenumb and type;
   								// log_3:8 + log_3:7 will return as it is, so the vectors does not change, keep as it is.
   					}
   					else					//containts "-"l
   					{
   						substract(somenumbs[j],type[j],somenumbs[i],type[i]);
   						if(isReturnOneNumb)
   						{
   							somenumbs[i]=opAnswer;							//set the element i to the opAnser,
   							somenumbs.erase(somenumbs.begin()+(j));			//erase the second element
   						 	op.erase(op.begin()+(j-1));				//erase the op

   						}
   						else
   						{
   							 //don't change anything.
   						}
   					}
   				}
   				else if((type[i]=="frac"&&type[j]=="int") || (type[j]=="frac"&&type[i]=="int"))
   				{

   					// handle one numb is fraction, one numb is integer
   					havesametype =true;
   					if(op[j-1]=='+')
   					{			//only have two case +,-

   						add(somenumbs[j],"frac",somenumbs[i],"frac");
   						if(isReturnOneNumb)
   						   	{
   						   		somenumbs[i]=opAnswer;							//set the element i to the opAnser,
   						   		somenumbs.erase(somenumbs.begin()+(j));			//erase the second element
   						   		op.erase(op.begin()+(j-1));				//erase the op
   						   		   						   	}
   						   	else
   						   	{
   						   		 //don't change anything.
   						   	}
   					}
   					else
   					{
   						substract(somenumbs[j],"frac",somenumbs[i],"frac");
   						if(isReturnOneNumb)
   		   				{
   		   					somenumbs[i]=opAnswer;							//set the element i to the opAnser,
   		   					somenumbs.erase(somenumbs.begin()+(j));			//erase the second element
   		   					op.erase(op.begin()+(j-1));				//erase the op

   		   				}
   		   				else
   		   				{
   		   					 //don't change anything.
   		   				}
   					}
   				}							//check continue comparing the next type[i+1];

   				if(tempSize != op.size()){
   					j=0;
   				}
   				//after loop if we cannot find the same type;
   			}//end of the int j loop
 //  					//cout<<"end check for type here"<<j<<endl;
   		}//end of the int i loop
   }
 }
Example #20
0
vec3& vec3::operator-=(const vec3& other)
{
    return substract(other);
}
void pratica25exercise6(int argc, const char * argv[]) {
    printf("\n\nExercise 6:\n");
    
    struct rational r = promptUserForRational();
    printf("Simplified it's %d/%d\n\n",r.numerator,r.denominator);
    
    struct rational r1;
    struct rational r2;
    r1.numerator = 8;
    r1.denominator = 4;
    r2.numerator = 3;
    r2.denominator = 6;
    
    r1 = simplifyRational(r1);
    test(r1.numerator==2,"Test simplify numerator");
    test(r1.denominator==1,"Test simplify denominator");
    
    r2 = simplifyRational(r2);
    test(r2.numerator==1,"Test simplify numerator");
    test(r2.denominator==2,"Test simplify denominator");
    
    struct rational r3 = add(r1,r2);
    test(r3.numerator==5, "Check add");
    test(r3.denominator==2, "Check add");
    
    r2.numerator = -2;
    r3 = add(r1,r2);
    test(r3.numerator==1, "Check add");
    test(r3.denominator==1, "Check add");
    
    r2.numerator = -2;
    r3 = substract(r1,r2);
    test(r3.numerator==3, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    r2.numerator = 2;
    r3 = substract(r1,r2);
    test(r3.numerator==1, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    // 2/1 * -2/2 = -4/2 = -2/1
    r2.numerator = -2;
    r3 = product(r1,r2);
    test(r3.numerator==-2, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    // 2/1 * -2/2 = 2/1 * 2/-2 = 4/-2 = -2/1
    r3 = divide(r1,r2);
    test(r3.numerator==-2, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    // 2/2 * -2/6 = -4/12= -1/3
    r2.numerator = -2;
    r1.denominator = 2;
    r2.denominator = 6;
    r3 = product(r1,r2);
    test(r3.numerator==-1, "Check substraction");
    test(r3.denominator==3, "Check substraction");
    
    // 2/2 * -2/6 = 2/2 * 6/-2 = 12/-4= -3/1
    r2.numerator = -2;
    r1.denominator = 2;
    r2.denominator = 6;
    r3 = divide(r1,r2);
    test(r3.numerator==-3, "Check substraction");
    test(r3.denominator==1, "Check substraction");
}
void  do_substract(const char *n, const char *n2)
{
  char *s = substract(n, n2);
  printf("%s - %s = %s\n", n, n2, s);
  free(s);
}
char  *divide(const char *n1, const char *n2)
{
  int   size1, size2, pos = 0, sup, x, tmp_size, old_size;
  char  *ret, *tmp, *tmp2, *multi;

  if (!n1 || !n2)
    return 0;
  if (!strcmp(n2, "0")) {
      return strdup("null");
    }
  if (!strcmp(n2, "1")) {
      return strdup(n1);
    }
  size1 = strlen(n1);
  size2 = strlen(n2);
  if (size1 < size2) {
      return strdup("0");
    }
  if (!strcmp(n1, n2)) {
      return strdup("1");
    }
  ret = malloc(sizeof(*ret) * (size1 + 2));
  memset(ret, 0, size1 + 2 * sizeof(*ret));
  tmp = strdup(n1);

  while (strcmp(tmp, "0") && isBigger2(tmp, n2, size1, size2)) {
      sup = getSuperior(size1, size2, tmp, n2);
      multi = getDivider(sup, tmp, n2);
      x = 0;
      while (multi[x])
	ret[pos++] = multi[x++];
      tmp2 = mult(multi, n2);
      tmp_size = strlen(tmp2) - 1;
      free(multi);
      multi = getSubstractNumber(tmp, tmp2);
      free(tmp2);
      tmp2 = multi;
      multi = substract(tmp, tmp2);

      sup = strlen(multi);
      old_size = size1;
      x = 0;
      while (x++ < old_size - sup - size2 - tmp_size) {
          ret[pos++] = '0';
        }

      free(tmp);
      free(tmp2);
      tmp = multi;
      size1 = strlen(tmp);
    }
  free(tmp);
  tmp = mult(ret, n2);
  sup = strlen(n1) - strlen(tmp);
  while (sup-- > 0)
    ret[pos++] = '0';
  free(tmp);

  --pos;
  while (pos >= 0) {
      if (ret[pos] > '9') {
          if (!pos) {
              memcpy(ret + 1, ret, size1 + 1);
              ret[0] = '0';
              pos += 1;
            }
          ret[pos - 1] += 1;
          ret[pos] -= 10;
        }
      --pos;
    }
  if (!ret[0])
    ret[0] = '0';
  return ret;
}
int main()
{
    int flag = 0; //To decide when to quit the application
    int command;//response on what user wants to do
    int counter; //size of array

    struct customer list[10];

    list[0].age = 69;
    strcpy(list[0].name,"Nick");
    list[0].info.retire = 4;


    list[1].age = 14;
    strcpy(list[1].name,"Matt");
    strcpy (list[1].info.array, "Burlingame");

    list[2].age = 30;
    strcpy(list[2].name,"Sean");
    list[2].info.salary = 3000;

    printf("Welcome to Yuya's Restaurant what would you like to do?\n\n");
    printf("Type 1 to add data for a person\n");
    printf("Type 2 to eliminate an entry of a person\n");
    printf("Type 3 to show all list\n");
    printf("Type 4 to quit\n");
    printf("----------------------------\n\n\n");

    counter = 3;//take age of array in the group

    while (flag == 0) {


    scanf("%d", &command); // read in the user input

        if (command == 1)//add one entry
        {

            if (counter < 10){//run while under 10 list
                add(list, &counter);

            }
            else if (counter >= 10)
            {
                printf("Que is full."); // if the que is up to 10, do not add to list
            }
            printf("\n\nWhat would you like to do?\n");
        }
        else if (command == 2)//delete one of the list
        {
            substract(list, &counter);
            printf("\n\nWhat would you like to do?\n");
        }
        else if (command == 3)//show all array
        {
            stats(list, counter);

        printf("\n\nWhat would you like to do?\n");
        }
        else if (command == 4){ //quit
        flag = 1;
        }
    }
    return 0;
}
Example #25
0
descriptor34::descriptor34(int n_maximum, int l_maximum, double j_maximum, double r_cut_in)
{
    n_max = n_maximum;
    l_max = l_maximum;
    r_cut = r_cut_in;
    j_max = j_maximum;

    q = new radial_function[n_max + 1];
    for (int i = 1; i <= n_max; i = i + 1){
        q[i].n = n_max;
        q[i].w = new double[n_max + 1];
    }


    factorial = new double[(n_max + l_max + lrint(2 * j_maximum)) * 10 + 11];
    factorial[0] = 1;
    for (int i = 1; i <= (n_max + l_max + lrint(2 * j_maximum)) * 10 + 10; i = i + 1){
        factorial[i] = factorial[i - 1] * i;
    }


    double_factorial = new double [(n_max + l_max + lrint(2 * j_maximum)) * 10 + 11];
    double_factorial[0] = 1;
    double_factorial[1] = 1;

    for (int i = 2; i <= (n_max + l_max + lrint(2 * j_maximum) ) * 10 + 10; i = i + 1){
        double_factorial[i] = double_factorial[i - 2] * i;
    }

    for (int i = 1; i <= n_max; i = i + 1){
        for (int j = 1; j <= n_max; j = j + 1){
            if (i == j) q[i].w[j] = 1;
            else q[i].w[j] = 0;
        }
    }

    radial_function z;
    z.n = n_max;
    z.w = new double[n_max + 1];

    double k;
    for (int i = 1; i <= n_max; i = i + 1){
        for (int j = 1; j < i; j = j + 1){
            k = dot_product(q[i], q[j]) / dot_product(q[j], q[j]);
            z = mul(q[j], k);
            q[i] = substract(q[i], z);
        }
        k = dot_product(q[i], q[i]);
        q[i] = mul(q[i], 1/sqrt(k));
    }




    spherical_coef = new complex**[n_max + 1];

    for (int i = 1; i <= n_max; i = i + 1){
        spherical_coef[i] = new complex*[l_max + 1];
    }

    for (int i = 1; i <= n_max; i = i + 1){
        for (int j = 0; j <= l_max; j = j + 1){
            spherical_coef[i][j] = new complex[2 * l_max + 1];
            spherical_coef[i][j] = spherical_coef[i][j] + l_max;
        }
    }



    hyperspherical_coef = new complex**[lrint(j_maximum * 2) + 1];

    for (int i = 0; i <= lrint(j_maximum * 2); i = i + 1){
        hyperspherical_coef[i] = new complex*[2 * lrint(j_maximum * 2) + 1];
        hyperspherical_coef[i] = hyperspherical_coef[i] + lrint(j_maximum * 2);
    }


    for (int i = 0; i <= lrint(j_maximum * 2); i = i + 1){
        for (int j = -i; j <= i; j = j + 1){
            hyperspherical_coef[i][j] = new complex[2 * lrint(j_maximum * 2) + 1];
            hyperspherical_coef[i][j] = hyperspherical_coef[i][j] + lrint(j_maximum * 2);
        }
    }


    /*FILE *f = fopen("input.txt", "r");
    int num_atoms;
    fscanf(f, "%d", &num_atoms);

    vector *vectors;
    vectors = new vector[num_atoms + 1];

    for (int i = 1; i <= num_atoms; i = i + 1){
        fscanf(f, "%lf %lf %lf", &vectors[i].x, &vectors[i].y, &vectors[i].z);
    }
    fclose(f);

    FILE *g = fopen("output.txt", "w");
    calculate_and_write_power_spectrum(num_atoms, vectors, g);
    calculate_and_write_bispectrum(num_atoms, vectors, g);

    fclose(g);
    */
   /* complex co = integral(1, 1, -1,1, 1, -1);
    co.print();
    */

}
void ScalarOperatorNode::run(ExecutionNode* previous)
{
    m_previousNode= previous;
    if(nullptr != m_internalNode)
    {
        m_internalNode->run(this);
    }
    if(nullptr != previous)
    {
        auto previousResult= previous->getResult();

        if(nullptr != previousResult)
        {
            ExecutionNode* internal= m_internalNode;
            if(nullptr != internal)
            {
                while(nullptr != internal->getNextNode())
                {
                    internal= internal->getNextNode();
                }

                Result* internalResult= internal->getResult();
                m_result->setPrevious(internalResult);
                if(nullptr != m_internalNode->getResult())
                {
                    m_internalNode->getResult()->setPrevious(previousResult);
                }

                switch(m_arithmeticOperator)
                {
                case Die::PLUS:
                    m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(),
                        internalResult->getResult(Result::SCALAR).toReal()));
                    break;
                case Die::MINUS:
                    m_scalarResult->setValue(substract(previousResult->getResult(Result::SCALAR).toReal(),
                        internalResult->getResult(Result::SCALAR).toReal()));
                    break;
                case Die::MULTIPLICATION:
                    m_scalarResult->setValue(multiple(previousResult->getResult(Result::SCALAR).toReal(),
                        internalResult->getResult(Result::SCALAR).toReal()));
                    break;
                case Die::DIVIDE:
                    m_scalarResult->setValue(divide(previousResult->getResult(Result::SCALAR).toReal(),
                        internalResult->getResult(Result::SCALAR).toReal()));
                    break;
                case Die::INTEGER_DIVIDE:
                    m_scalarResult->setValue(static_cast<int>(divide(previousResult->getResult(Result::SCALAR).toReal(),
                        internalResult->getResult(Result::SCALAR).toReal())));
                    break;
                case Die::POW:
                    m_scalarResult->setValue(pow(previousResult->getResult(Result::SCALAR).toReal(),
                        internalResult->getResult(Result::SCALAR).toReal()));
                    break;
                }
            }

            if(nullptr != m_nextNode)
            {
                m_nextNode->run(this);
            }
        }
    }
}
int main()
{
        slist *a = NULL;
        slist *b = NULL;
        int negative = 0;
        int first_valid_digit = 0;
        int first = 1;
        char c;

        while ((c = getchar()) != '\n') {
                if ((c == '-') && (negative == 0) && first)
                        negative = 1;
                else {
                        unsigned int x = c - '0';
                        if (x >= 1 && x <= 9) {
                                first_valid_digit = 1;
                                a = insert(a, x);
                        } else if (x == 0 && first_valid_digit) {
                                a = insert(a, x);
                        }
                }
                first = 0;
        }
        if (negative && length(a))
                a = push_back(a, -1);

        negative = 0;
        first_valid_digit = 0;
        first = 1;
        
        while ((c = getchar()) != '\n') {
                if ((c == '-') && (negative == 0) && first)
                        negative = 1;
                else {
                        unsigned int x = c - '0';
                        if (x >= 0 && x <= 9) {
                                first_valid_digit = 1;
                                b = insert(b, x);
                        } else if (x == 0 && first_valid_digit) {
                                b = insert(b, x);
                        }
                }
        }
        if (negative && length(b))
                b = push_back(b, -1);

        print(a);
        print(b);

        slist *sum = add(a, b);
        slist *diff = substract(a, b);

        print(sum);
        print(diff);

        printf("Sum:\n");
        print_num(sum);
        printf("\n");

        printf("Diff:\n");
        print_num(diff);
        printf("\n");

        cleanup(&a);
        cleanup(&b);
        cleanup(&sum);
        cleanup(&diff);

        return 0;
}