Beispiel #1
0
int main()
{
    // printf("hello, world");

    char result[BUFFER_SIZE];
    char remainder[BUFFER_SIZE];

    // please make sure the bit length is enough before calculate
    // especially when you do a large power operation
    // you can change it by define: BIG_INT_BIT_LEN
    // the default bit length for BigInt is 1024

    // routine test
    puts(Add("2010", "4", result));
    puts(Sub("0", "2014", result));
    puts(Mul("2", "43", result));
    puts(Div("86", "10", result, remainder));
    puts(remainder);
    puts(Mod("-86", "10", result));
    puts(PowMod("7", "80", "86", result));

    // BigInt test
    puts(Sub("233333333333333333333333333333333333333333333333", "33", result));
    puts(Mul("2333333333333333333333333333333", "2333333333333333333", result));
    puts(Div("2333333333333333333333333333333", "2333333333333333332", result, remainder));
    puts(remainder);
    puts(Pow("8", "86", result));

    return 0;
}
Beispiel #2
0
/*
** This is based on the simple Fractal / Divide and Conquer / Karatsuba
** O(n^1.585) method.
**
** It's fairly simple.  a*b is: a1b1(B^2+B)+(a1-a2)(b2-b1)B+a2b2(B+1)
**
** You need 4*SIZE storage for the product and the working space.
*/
static void
FractalMul(BigInt Prod, BigInt Num1, BigInt Num2, size_t Len)
{size_t HLen;
 int Sign1, Sign2;
 BigInt offset;
 BigInt Work=Prod+2*Len;
 int OldNum1IsCached, OldNum2IsCached;
 int OldSaveNum1FFT, OldSaveNum2FFT;

  if (Len <= FFTLimit) {FFTMul(Prod,Num1,Num2,Len,Len*2,0);return;}

  HLen = Len/2;

  OldNum1IsCached=Num1IsCached;OldNum2IsCached=Num2IsCached;
  OldSaveNum1FFT=SaveNum1FFT;OldSaveNum2FFT=SaveNum2FFT;
  Num1IsCached=Num2IsCached=SaveNum1FFT=SaveNum2FFT=0;

  if (Num1==Num2)
    {
     Sign1 = Sub(Prod, Num1, Num1+HLen, HLen);
     if (Sign1) Negate(Prod, HLen);
     FractalMul(Work,Prod,Prod,HLen);
     ClearBigInt(Prod,Len*2);
     offset = Prod + HLen;
     RippleSub(Prod,offset,offset,Work,Len); /* square makes sign1 pos */
    }
  else
    {
     /* Do x=Right(Num1)-Left(Num1) y=Left(Num2)-Right(Num2) */
     Sign1 = Sub(Prod, Num1+HLen, Num1, HLen);
     if (Sign1) Negate(Prod, HLen);

     Sign2 = Sub(Prod+HLen, Num2, Num2+HLen, HLen);
     if (Sign2) Negate(Prod+HLen, HLen);

     FractalMul(Work, Prod, Prod+HLen, HLen);

     ClearBigInt(Prod,Len*2);
     offset = Prod + HLen;
     if (Sign1 == Sign2)  RippleAdd(Prod,offset,offset,Work,Len);
     else                 RippleSub(Prod,offset,offset,Work,Len);
    }

#if 1
/* Turn the FFT/NTT caching back on. */
  Num1IsCached=OldNum1IsCached;Num2IsCached=OldNum2IsCached;
  SaveNum1FFT=OldSaveNum1FFT;SaveNum2FFT=OldSaveNum2FFT;
#endif

  FractalMul(Work, Num1, Num2, HLen);
  offset = Prod + HLen;RippleAdd(Prod,offset,offset,Work,Len);
  Add(Prod, Prod, Work, Len);

  FractalMul(Work, Num1 + HLen, Num2 + HLen, HLen);
  offset = Prod + HLen;RippleAdd(Prod,offset,offset,Work,Len);
  offset = Prod + Len; RippleAdd(Prod,offset,offset,Work,Len);

  Num1IsCached=OldNum1IsCached;Num2IsCached=OldNum2IsCached;
  SaveNum1FFT=OldSaveNum1FFT;SaveNum2FFT=OldSaveNum2FFT;
}
Beispiel #3
0
QString Falling::FullName() const {
    TrString stoneNameString = QObject::tr("Masonry");
    switch ( Sub() ) {
    default:    return TrManager::SubNameUpper(Sub());
    case WATER: return TrManager::KindName(FALLING);
    case STONE: return stoneNameString;
    }
}
Beispiel #4
0
//==========================================================================*
// 2D: Result := CosAlpha(LHS-C,RHS-C)
//--------------------------------------------------------------------------*
Tdble CosAlpha(const TV2D &LHS, const TV2D &RHS, const TV2D &C)
{
  TV2D DLHS, DRHS;

  DLHS = Sub(LHS,C);
  DRHS = Sub(RHS,C);
  return Mult(DLHS,DRHS) / (Len(DLHS) * Len(DRHS));
};
dgFloat64 dgSymmetricBiconjugateGradientSolve::Solve (dgInt32 size, dgFloat64 tolerance, dgFloat64* const x, const dgFloat64* const b) const
{
	dgStack<dgFloat64> bufferR0(size);
	dgStack<dgFloat64> bufferP0(size);
	dgStack<dgFloat64> matrixTimesP0(size);
	dgStack<dgFloat64> bufferConditionerInverseTimesR0(size);

	dgFloat64* const r0 = &bufferR0[0];
	dgFloat64* const p0 = &bufferP0[0];
	dgFloat64* const MinvR0 = &bufferConditionerInverseTimesR0[0];
	dgFloat64* const matrixP0 = &matrixTimesP0[0];

	MatrixTimeVector (matrixP0, x);
	Sub(size, r0, b, matrixP0);
	bool continueExecution = InversePrecoditionerTimeVector (p0, r0);

	dgInt32 iter = 0;
	dgFloat64 num = DotProduct (size, r0, p0);
	dgFloat64 error2 = num;
	for (dgInt32 j = 0; (j < size) && (error2 > tolerance) && continueExecution; j ++) {

		MatrixTimeVector (matrixP0, p0);
		dgFloat64 den = DotProduct (size, p0, matrixP0);

		dgAssert (fabs(den) > dgFloat64 (0.0f));
		dgFloat64 alpha = num / den;

		ScaleAdd (size, x, x, alpha, p0);
        if ((j % 50) != 49) {
		    ScaleAdd (size, r0, r0, -alpha, matrixP0);
        } else {
            MatrixTimeVector (matrixP0, x);
            Sub(size, r0, b, matrixP0);
        }

//dgUnsigned64 xxx0 = dgGetTimeInMicrosenconds();
		continueExecution = InversePrecoditionerTimeVector (MinvR0, r0);
//xxx0 = dgGetTimeInMicrosenconds() - xxx0;
//dgTrace (("%d\n", dgUnsigned64 (xxx0)));


		dgFloat64 num1 = DotProduct (size, r0, MinvR0);
		dgFloat64 beta = num1 / num;
		ScaleAdd (size, p0, MinvR0, beta, p0);
		num = DotProduct (size, r0, MinvR0);
		iter ++;
		error2 = num;
		if (j > 10) {
			error2 = dgFloat64 (0.0f);
			for (dgInt32 i = 0; i < size; i ++) {
				error2 = dgMax (error2, r0[i] * r0[i]);
			}
		}
	}

	dgAssert (iter < size);
	return num;
}
Beispiel #6
0
//=============================================================================
/*virtual*/ double VectorMath::LinearSpline::ShortestDistanceToSpline( const Vector& point ) const
{
	Vector p_p0, p1_p0;
	Sub( p_p0, point, controlPoint[0] );
	Sub( p1_p0, controlPoint[1], controlPoint[0] );
	double t = Dot( p_p0, p1_p0 ) / Dot( p1_p0, p1_p0 );
	Vector vec;
	Evaluate( t, vec );
	return Length( vec );
}
Beispiel #7
0
    QString Box::FullName() const {
        TrString differentName = QObject::tr("Pile");
        TrString meatName      = QObject::tr("Corpse (%1)");

        switch ( Sub() ) {
        default:        return Block::FullName();
        case DIFFERENT: return differentName;
        case H_MEAT:
        case A_MEAT:    return meatName.arg(TrManager::SubName(Sub()));
        }
    }
Beispiel #8
0
 void Converter::InitDamageKinds() {
     switch ( Sub() ) {
     default:
         qDebug("%s: sub ?: %d.", Q_FUNC_INFO, Sub());
         // no break;
     case STONE:
         damageKindOn  = DAMAGE_HEAT;
         damageKindOff = DAMAGE_FREEZE;
         break;
     }
 }
dFloat64 dSymmetricBiconjugateGradientSolve::Solve (int size, dFloat64 tolerance, dFloat64* const x, const dFloat64* const b) const
{
	dFloat64* const r0 = new dFloat64 [size];
	dFloat64* const p0 = new dFloat64 [size];
	dFloat64* const MinvR0 = new dFloat64 [size];
	dFloat64* const matrixP0 = new dFloat64 [size];

	MatrixTimeVector (matrixP0, x);
	Sub(size, r0, b, matrixP0);
	bool continueExecution = InversePrecoditionerTimeVector (p0, r0);

	int iter = 0;
	dFloat64 num = DotProduct (size, r0, p0);
	dFloat64 error2 = num;
	for (int j = 0; (j < size) && (error2 > tolerance) && continueExecution; j ++) {

		MatrixTimeVector (matrixP0, p0);
		dFloat64 den = DotProduct (size, p0, matrixP0);

		dAssert (fabs(den) > dFloat64 (0.0f));
		dFloat64 alpha = num / den;

		ScaleAdd (size, x, x, alpha, p0);
        if ((j % 50) != 49) {
		    ScaleAdd (size, r0, r0, -alpha, matrixP0);
        } else {
            MatrixTimeVector (matrixP0, x);
            Sub(size, r0, b, matrixP0);
        }

		continueExecution = InversePrecoditionerTimeVector (MinvR0, r0);

		dFloat64 num1 = DotProduct (size, r0, MinvR0);
		dFloat64 beta = num1 / num;
		ScaleAdd (size, p0, MinvR0, beta, p0);
		num = DotProduct (size, r0, MinvR0);
		iter ++;
		error2 = num;
		if (j > 10) {
			error2 = dFloat64 (0.0f);
			for (int i = 0; i < size; i ++) {
				error2 = dMax (error2, r0[i] * r0[i]);
			}
		}
	}

	delete[] matrixP0;
	delete[] MinvR0;
	delete[] p0;
	delete[] r0;

	dAssert (iter <= size);
	return num;
}
Beispiel #10
0
QString Weapon::FullName() const {
    if ( Kind() != WEAPON ) return Block::FullName();

    TrString stoneName = QObject::tr("Pebble");
    TrString ironName  = QObject::tr("Spike");
    switch ( Sub() ) {
    case STONE: return stoneName;
    case IRON:  return ironName;
    case BONE:
    case SKY:
    case SUB_NUT: return TrManager::SubNameUpper(Sub());
    default:      return Block::FullName();
    }
}
Beispiel #11
0
QString Accumulator::FullName() const {
    QString name;
    if ( GetSubGroup(Sub()) == GROUP_METAL ) {
        TrString batteryString = QObject::tr("Battery (%1)");
        name = batteryString.arg(TrManager::SubName(Sub()));
    } else {
        TrString nameString = QObject::tr("Thermos");
        switch ( Sub() ) {
        case GLASS: name = nameString; break;
        default:    name = Block::FullName(); break;
        }
    }
    TrString chargeString = QObject::tr(" (charge: %1%)");
    return name.append(chargeString.arg(100*charge / MAX_CHARGE));
}
Beispiel #12
0
BigData BigData::operator+(BigData& bigData)
{
	if (!IsINT64Owerflow() && !bigData.IsINT64Owerflow())  //没有溢出
	{
		if (m_strData[0] != bigData.m_strData[0])   //异号
		{
			return BigData(m_llValue + bigData.m_llValue);
		}
		else
		{
			if (('+' == m_strData[0] && MAX_INT64 - m_llValue <= bigData.m_llValue) 
				|| ('-' == m_strData[0] && MIN_INT64 - m_llValue >= bigData.m_llValue))
			{
				return BigData(m_llValue + bigData.m_llValue);
			}
		}
	}
	std::string strRet;
	if (m_strData[0] == bigData.m_strData[0])
	{
		return BigData((char*)Add(m_strData, bigData.m_strData).c_str());

		//strRet = Add(m_strData, bigData.m_strData);
	}
	/*else
	{
	}*/
	return BigData((char*)Sub(m_strData, bigData.m_strData).c_str());
}
Beispiel #13
0
BigData BigData::operator-(const BigData& bigData)
{
	if (!IsINT64Owerflow() && !bigData.IsINT64Owerflow())
	{
		if (m_strData[0] == bigData.m_strData[0])
		{
			return BigData(m_llValue - bigData.m_llValue);
		}
		else
		{
			//10+(-8)
			if (('+' == m_strData[0] && MAX_INT64 + bigData.m_llValue >= m_llValue) || ('-' == m_strData[0] && MIN_INT64 + bigData.m_llValue <= m_llValue))
			{
				return BigData(m_llValue - bigData.m_llValue);
			}
		}
	}
	//1.至少有一个操作数溢出
	//2.相减的结果一定会溢出
	//9999999999   “-111111”
	std::string strRet;
	if (m_strData[0] != bigData.m_strData[0])
	{
		strRet = Add(m_strData, bigData.m_strData);
	}
	else
	{
		strRet = Sub(m_strData, bigData.m_strData);
	}
	return BigData(strRet.c_str());
}
Beispiel #14
0
 void Converter::DoRareAction() {
     if ( isOn && fuelLevel < World::SECONDS_IN_DAY/DamageLevel() ) {
         for (int i=GetSize()-1; i>=0; --i) {
             Block* const block = ShowBlock(i);
             if ( block ) {
                 const int add = ConvertRatio(block->Sub());
                 if ( add > 0 ) {
                     fuelLevel += add;
                     Pull(i);
                     delete block;
                     break;
                 }
             }
         }
     }
     World* const world = World::GetWorld();
     if ( fuelLevel <= 0
             || ( Sub() == STONE
                 && world->GetBlock(X(), Y(), Z()+1)->Sub() == WATER ) )
     {
         Damage(1, damageKindOff);
     } else {
         if (world->Damage(X(), Y(), Z()+1, DamageLevel(), damageKindOn)<=0)
         {
             world->DestroyAndReplace(X(), Y(), Z()+1);
         }
         fuelLevel -= DamageLevel();
     }
 }
Beispiel #15
0
int SpherePtFromLine(double p1[3], double p2[3], double center[3], double radius, double roots[2])
{
    // given a 3D line defined by point p1 and p2, compute roots such that
    // p1+root*(p2-p1) is a point of intersection with a sphere of given center and radius
    // whose distance to center (of the sphere) equals radius
    double d[3], q[3]; // two points on line, line vector, two roots for t
    Sub(p2, p1, d);            // set d (line vector)
    // vector from point on line, p+t*d, to center is p+td-center, or q+td (q = p-center)
    // magnitude-squared of this vector is (q+td)**2 = (q**2)+2tdq+(t**2)(d**2)
    // in terms of the quadratic equation, a is d**2, b is 2dq, and c is (q**2)-(radius**2)
    Sub(p1, center, q);
    double a = MagSq(d);
    double b = 2*DotProduct(d, q);
    double c = MagSq(q)-radius*radius;
    return QuadraticRoots(a, b, c, &roots[0], &roots[1]);
}
//=============================================================================
// We assume here that the given AABB is valid.  If it is not, then the
// results of this routine are undefined.  We assume that the given
// position is inside or on the given box.  The first of the returned
// AABBs will be on the back side of the split plane.  The second will
// be on the front side.  The front of the XY plane, for example is in
// the direction of the positive Z axis.  The split-plane is parallel
// to one of the XY, YZ or XZ planes and contains the given position.
void VectorMath::AabbSplit( const Aabb& aabb, const Vector& pos, Aabb::Plane splitPlane, Aabb& aabb0, Aabb& aabb1 )
{
	Vector delta;
	Sub( delta, aabb.max, aabb.min );

	Copy( aabb0.min, aabb.min );
	Copy( aabb1.max, aabb.max );

	switch( splitPlane )
	{
		case Aabb::XY_PLANE:
		{
			Set( aabb0.max, aabb.max.x, aabb.max.y, pos.z );
			Set( aabb1.min, aabb.min.x, aabb.min.y, pos.z );
			break;
		}
		case Aabb::YZ_PLANE:
		{
			Set( aabb0.max, pos.x, aabb.max.y, aabb.max.z );
			Set( aabb1.min, pos.x, aabb.min.y, aabb.min.z );
			break;
		}
		case Aabb::XZ_PLANE:
		{
			Set( aabb0.max, aabb.max.x, pos.y, aabb.max.z );
			Set( aabb1.min, aabb.min.x, pos.y, aabb.min.z );
			break;
		}
	}
}
Beispiel #17
0
HeapString HeapString::Sub(size_t startPos) const
{
	size_t stringLen = GetStringData()->stringLen;
	if (startPos >= stringLen)
		return HeapString();
	return Sub(startPos, stringLen - startPos);
}
Beispiel #18
0
void Intermediate ( Quaternion* q0, Quaternion* q1, Quaternion* q2,
   Quaternion* a, Quaternion* b)
{
   /* assert:  q0, q1, q2 are unit quaternions */
   /*Quaternion q0inv = q0.UnitInverse();
   Quaternion q1inv = q1.UnitInverse();
   Quaternion p0 = q0inv*q1;
   Quaternion p1 = q1inv*q2;
   Quaternion arg = 0.25*(p0.Log()-p1.Log());
   Quaternion marg = -arg;
 
   a = q1*arg.Exp();
   b = q1*marg.Exp();
   */

   Quaternion p0, p1;
   UnitInverse(&p0, q0);
   UnitInverse(&p1, q1);
   MulSelf(&p0, q1);
   MulSelf(&p1, q2);
   LogSelf(&p0);
   LogSelf(&p1);
   Sub(a,&p0, &p1);
   MulScalSelf(a, 0.25);
   Neg(b, a);

   ExpSelf(a);
   ExpSelf(b);
   Mul(a, q1, a);
   Mul(b, q1, b);
}
Beispiel #19
0
BigData BigData::operator-(const BigData& bigData)
{
	if (!IsINT64Overflow() && !bigData.IsINT64Overflow())
	{
		if (m_strData[0] == bigData.m_strData[0])
		{
			return BigData(m_llValue - bigData.m_llValue);
		}
		else
		{
			if ((m_strData[0] == '+' && MAX_INT64 + bigData.m_llValue >= m_llValue)
				|| (m_strData[0] == '-' && MIN_INT64 + bigData.m_llValue <= m_llValue))
			{
				return BigData(m_llValue - bigData.m_llValue);
			}
		}
	}
	std::string strret;
	if (m_strData[0] != bigData.m_strData[0])
	{
		strret = Add(m_strData, bigData.m_strData);
	}
	else
	{
		strret = Sub(m_strData, bigData.m_strData);
	}
	return BigData(strret.c_str());
}
Beispiel #20
0
void PrelucrareArbore(struct TNod *lel){

	char * a=lel->info;
	lel->rez = (double*)malloc(sizeof(double));  
	
	if(strcmp(a,"+")==0){
		Add(lel->st,lel->dr,lel);
	}else if(strcmp(a,"-")==0){
		Sub(lel->st,lel->dr,lel);
	}else if(strcmp(a,"*")==0){
		Mult(lel->st, lel->dr,lel);
	}else if(strcmp(a,"/")==0){
		Divi(lel->st, lel->dr,lel);
	}else if(strcmp(a,"sum")==0){
		Sum(lel);
		//printf("PrelArb %lf\n",*(lel->rez));
	}else if(strcmp(a,"prod")==0){
		Prod(lel);
	}else if(strcmp(a,"sqrt")==0){
		SqrtCalc(lel->st,lel->dr,lel);
	}else if(strcmp(a,"pow")==0){
		PowCalc(lel->st, lel->dr,lel);
	}else{
		*(lel->rez) = (double)atoi(a);
		//printf("ATOI %lf\n",*(lel->rez));
	}
}
Beispiel #21
0
//===========================================================================
double CMatrix::Det()
{
    if(Data == NULL   ) return -999;  // --- numeric_limits<double>::quiet_NaN();
    if(Rows != Columns) return -999;  // --- numeric_limits<double>::quiet_NaN();
  //
    if(Rows == 1) return  Data[0][0];
    if(Rows == 2) return (Data[0][0]*Data[1][1] - Data[1][0]*Data[0][1]);
  //
    CMatrix Sub(Rows - 1, Columns - 1);
   //
    double Result = 0.0;
   //
    for(int n = 0; n < Columns; n++) {
         for(int i = 1; i < Rows; i++) {
              int Counter = 0;
              for(int j = 0; j < Columns; j++) {
                  if(j == n) continue;
                  Sub.Data[i-1][Counter] = Data[i][j];
                  Counter++;
              }
         }
        //
         double det = Sub.Det();
         if ((n & 1) == 1) det = -det;
         Result += Data[0][n] * det;
    }
   //
    return Result;
}
Beispiel #22
0
status_t _GlBinaryOp2d::Process(	const GlPlanes* src, GlPlanes& dest,
									GlProcessStatus* status)
{
	if (dest.size < 1) return B_OK;

	GlAlgo2d*		lh = Algo2dAt(_LH_INDEX);
	GlAlgo2d*		rh = Algo2dAt(_RH_INDEX);

	if (lh && rh) {
		GlPlanes*	c = dest.Clone();
		if (c && c->size == dest.size) {
			lh->Process(src, dest, status);
			rh->Process(src, *c, status);
			if (mOp == GL_ADD_BINARY_SRF_KEY) Add(dest, *c);
			else if (mOp == GL_SUB_BINARY_SRF_KEY) Sub(dest, *c);
			else if (mOp == GL_MULT_BINARY_SRF_KEY) Mult(dest, *c);
			else if (mOp == GL_DIV_BINARY_SRF_KEY) Div(dest, *c);
			else if (mOp == GL_MIN_BINARY_SRF_KEY) Min(dest, *c);
			else if (mOp == GL_MAX_BINARY_SRF_KEY) Max(dest, *c);
			else ArpASSERT(false);
		}
		delete c;
	} else if (lh) {
		lh->Process(src, dest, status);
	} else if (rh) {
		rh->Process(src, dest, status);
	}
	return B_OK;
}
Beispiel #23
0
BigData BigData::operator + (const BigData& bigdata)
{
	if (!IsOverFlowINT64() && !bigdata.IsOverFlowINT64())
	{
		if (_pData[0] != bigdata._pData[0])
		{
			return BigData(_value + bigdata._value);
		}
		else
		{
			if ((_pData[0] == '+' && (LLONG_MAX - _value >= bigdata._value)) ||
				(_pData[0] == '-' && (LLONG_MIN - _value <= bigdata._value)))
			{
				return BigData(_value + bigdata._value);
			}
		}
	}

	if (_pData[0] == bigdata._pData[0])
	{
		return BigData( (char *) Add(_pData, bigdata._pData).c_str() );
	}

	return BigData((char *)Sub(_pData, bigdata._pData).c_str());
}
Beispiel #24
0
int  Weapon::DamageKind() const {
    switch ( Sub() ) {
    case IRON: return DAMAGE_THRUST;
    case SKY:  return DAMAGE_TIME;
    default:   return DAMAGE_CRUSH;
    }
}
Beispiel #25
0
void
_show(MENU *m)
{
	int r, c;
	WINDOW *us;

	if (Posted(m) || Indriver(m)) {
		(void) mvderwin(Sub(m), Top(m), 0);
		us = US(m);
		getmaxyx(us, r, c);
		r = min(Height(m), r);
		c = min(Width(m), c);
		(void) copywin(Sub(m), us, 0, 0, 0, 0, r-1, c-1, FALSE);
		_position_cursor(m);
	}
}
BigData BigData::operator-(const BigData& bigdata)  //- 减法运算符重载
{
	if (IsINT64OverFlow() && bigdata.IsINT64OverFlow())
	{
		if (_strData[0] == bigdata._strData[0])
		{
			return BigData(_value - bigdata._value);
		}
		else
		{
			INT64 temp_max = _value - MAX_INT64;
			INT64 temp_min = _value - MIN_INT64;

			//10 -3 =7  5
			//(-10)-(-3)=-7  -5
			if (((_value >= 0) && (temp_max <= bigdata._value)) ||
				((_value < 0) && (temp_min >= bigdata._value)))
			{
				return BigData(_value - bigdata._value);
			}
		}
	}
	if (_strData[0] == bigdata._strData[0])
	{
		return BigData(Sub(_strData, bigdata._strData).c_str());
	}
	else
	{
		string ptr(bigdata._strData);
		ptr[0] = _strData[0];
		return Add(_strData, ptr).c_str();
	}

}
Beispiel #27
0
int
post_form(FORM *f)
{
	int x, y, v;

	if (!f)
		return (E_BAD_ARGUMENT);

	if (Status(f, POSTED))
		return (E_POSTED);

	if (!f->field)
		return (E_NOT_CONNECTED);

	getmaxyx(Sub(f), y, x);

	if (f->rows > y || f->cols > x)
		return (E_NO_ROOM);

	v = _set_form_page(f, P(f), C(f));

	if (v != E_OK)
		return (v);

	Set(f, POSTED);
	init_form(f);
	init_field(f);
	(void) _update_current(f);
	return (E_OK);
}
Beispiel #28
0
int Test()
{
    std::cout << "Calling Add(2, 3)" << std::endl;
    int res = Add(2, 3);
    std::cout << "Calling Sub(5, 1)" << std::endl;
    return Sub(res, 1);
}
Beispiel #29
0
BigData BigData::operator-(const BigData& bigData)
{
	if (!IsINT64Owerflow() && !bigData.IsINT64Owerflow())
	{
		if (m_strData[0] == bigData.m_strData[0])
		{
			return BigData(m_llValue - bigData.m_llValue);
		}
		else
		{
			// 10 + (-8) = 2 > 1// 3 - (-8); 1 - (-8) 
			// -10  -8  3    -8  2  -10 + 3 = -7 <= 
			if (('+' == m_strData[0] && MAX_INT64 + bigData.m_llValue >= m_llValue) ||
				('-' == m_strData[0] && MIN_INT64 + bigData.m_llValue <= m_llValue))
			{
				return BigData(m_llValue - bigData.m_llValue);
			}
		}
	}

	// 1、至少有一个操作数溢出
	// 2、相减的结果一定会溢出
	// "999999999" "-111111"  "-9999999" "1111111"
	std::string strRet;
	if (m_strData[0] != bigData.m_strData[0])
	{
		strRet = Add(m_strData, bigData.m_strData);
	}
	else
	{
		strRet = Sub(m_strData, bigData.m_strData);
	}
	return BigData(strRet.c_str());
}
Beispiel #30
0
/****************************************************
	FFT()

	参数:

		TD为时域值
		FD为频域值
		power为2的幂数

	返回值:

		无

	说明:

		本函数实现快速傅立叶变换
****************************************************/
void FFT(COMPLEX * TD, COMPLEX * FD, int power)
{
	int count;
	int i,j,k,bfsize,p;
	double angle;
	COMPLEX *W,*X1,*X2,*X;

	/*计算傅立叶变换点数*/
	count=1<<power;
	
	/*分配运算所需存储器*/
	W=(COMPLEX *)malloc(sizeof(COMPLEX)*count/2);
	X1=(COMPLEX *)malloc(sizeof(COMPLEX)*count);
	X2=(COMPLEX *)malloc(sizeof(COMPLEX)*count);
	
	/*计算加权系数*/
	for(i=0;i<count/2;i++)
	{
		angle=-i*PI*2/count;
		W[i].re=cos(angle);
		W[i].im=sin(angle);
	}
	
	/*将时域点写入存储器*/
	memcpy(X1,TD,sizeof(COMPLEX)*count);
	
	/*蝶形运算*/
	for(k=0;k<power;k++)
	{
		for(j=0;j<1<<k;j++)
		{
			bfsize=1<<(power-k);
			for(i=0;i<bfsize/2;i++)
			{
				p=j*bfsize;
				X2[i+p]=Add(X1[i+p],X1[i+p+bfsize/2]);
				X2[i+p+bfsize/2]=Mul(Sub(X1[i+p],X1[i+p+bfsize/2]),W[i*(1<<k)]);
			}
		}
		X=X1;
		X1=X2;
		X2=X;
	}
	
	/*重新排序*/
	for(j=0;j<count;j++)
	{
		p=0;
		for(i=0;i<power;i++)
		{
			if (j&(1<<i)) p+=1<<(power-i-1);
		}
		FD[j]=X1[p];
	}
	
	/*释放存储器*/
	free(W);
	free(X1);
	free(X2);
}