Exemple #1
0
int main()
{
  try {
    double a = 0;
    double b = 0;
    double c = 0;

    cout << "I solve quadratic equations (ax^2+bx+c)\n";
    cout << "Enter a: ";
    cin >> a;
    cout << "Enter b: ";
    cin >> b;
    cout << "Enter c: ";
    cin >> c;
    if (root1(a,b,c) == root2(a,b,c)) {
      cout << "There is only one root, x = " << root1(a,b,c) << '\n';
    } else {
      cout << "x = " << root1(a,b,c) << " or x = " << root2(a,b,c) << '\n';
    }
    return 0;
  } catch (exception &e) {
    cerr << "Exception Caught: " << e.what() << '\n';
    return 1;
  } catch (...) {
    cerr << "Unknown Exception\n";
    return 2;
  }
}
Exemple #2
0
int main() {
    TreeNode root1(1);
    TreeNode root2(1);
	TreeNode t2(2);
	TreeNode t3(3);
	TreeNode t4(4);
	TreeNode t5(5);
	TreeNode t6(6);
	TreeNode t7(7);
	TreeNode t8(8);
	TreeNode t9(9);
	
	root1.left = &t2;
	root1.right = &t3;
	root2.left = &t2;
	root2.right = &t3;
	t2.left = &t4;
	t2.right = &t5;
	t3.right = &t6;
	t4.left = &t7;
	t4.right = &t8;
	t8.left = &t9;
	
	Solution s;
	std::cout << s.isSameTree(&root1, &root2) << std::endl;
    
	return 0;
}
Exemple #3
0
    void
    run ()
    {
        uint128 seed1, seed2;
        seed1.SetHex ("71ED064155FFADFA38782C5E0158CB26");
        seed2.SetHex ("CF0C3BE4485961858C4198515AE5B965");
        CKey root1 (seed1), root2 (seed2);

        uint256 priv1, priv2;
        root1.GetPrivateKeyU (priv1);
        root2.GetPrivateKeyU (priv2);

        unexpected (to_string (priv1) != "7CFBA64F771E93E817E15039215430B53F7401C34931D111EAB3510B22DBB0D8",
            "Incorrect private key for generator");

        unexpected (to_string (priv2) != "98BC2EACB26EB021D1A6293C044D88BA2F0B6729A2772DEEBF2E21A263C1740B",
            "Incorrect private key for generator");

        RippleAddress nSeed;
        nSeed.setSeed (seed1);

        unexpected (nSeed.humanSeed () != "shHM53KPZ87Gwdqarm1bAmPeXg8Tn",
            "Incorrect human seed");

        unexpected (nSeed.humanSeed1751 () != "MAD BODY ACE MINT OKAY HUB WHAT DATA SACK FLAT DANA MATH",
            "Incorrect 1751 seed");
    }
Exemple #4
0
TEST_F(ExpressionTests, EqualityTest) {
  // First tree operator_expr(-) -> (tup_expr(A.a), const_expr(2))
  std::tuple<oid_t, oid_t, oid_t> bound_oid1(1, 1, 1);
  auto left1 = new expression::TupleValueExpression("a", "A");
  left1->SetBoundOid(bound_oid1);
  auto right1 = new expression::ConstantValueExpression(
      type::ValueFactory::GetIntegerValue(2));
  std::unique_ptr<expression::OperatorExpression> root1(
      new expression::OperatorExpression(
      ExpressionType::OPERATOR_MINUS, type::TypeId::INVALID, left1, right1));
  // Second tree operator_expr(-) -> (tup_expr(A.b), const_expr(2))
  std::tuple<oid_t, oid_t, oid_t> bound_oid2(1, 1, 0);
  auto left2 = new expression::TupleValueExpression("b", "A");
  left2->SetBoundOid(bound_oid2);
  auto right2 = new expression::ConstantValueExpression(
      type::ValueFactory::GetIntegerValue(2));
  std::unique_ptr<expression::OperatorExpression> root2(
      new expression::OperatorExpression(
      ExpressionType::OPERATOR_MINUS, type::TypeId::INVALID, left2, right2));
  EXPECT_FALSE(root1->Equals(root2.get()));

  // Third tree operator_expr(-) -> (tup_expr(a.a), const_expr(2))
  auto left3 = new expression::TupleValueExpression("a", "a");
  left3->SetBoundOid(bound_oid1);
  auto right3 = new expression::ConstantValueExpression(
      type::ValueFactory::GetIntegerValue(2));
  std::unique_ptr<expression::OperatorExpression> root3(
      new expression::OperatorExpression(
      ExpressionType::OPERATOR_MINUS, type::TypeId::INVALID, left3, right3));
  EXPECT_TRUE(root1->Equals(root3.get()));
}
Exemple #5
0
void movePlanets( struct planet * thePlanets , double t , double dt ){

   double TOL = 1e-8;

   double r0   = thePlanets[0].r + thePlanets[1].r; 
   double phi0 = thePlanets[1].phi;

   double vr = thePlanets[0].vr + thePlanets[1].vr; 
   double omega = thePlanets[1].omega; 

   double l = r0*r0*omega;
   double en = 0.5*vr*vr - 1./r0 + 0.5*l*l/r0/r0;

   double a = 1./2./fabs(en);
   double b = l/sqrt(2.*fabs(en));
   double f = sqrt(fabs(a*a-b*b));
   double e = f/a; 

   double x0 = r0*cos(phi0);
   double y0 = r0*sin(phi0);

   double E0 = atan2( y0/b , (x0+f)/a );
   double M0 = E0 - e*sin(E0);
   double M = M0 + l*dt/a/b;

//Newton-Rapheson to solve M = E - e*sin(E)
      double E = M;  //Guess value for E is M.
      double ff = root0( E , e ) - M; 
      while( fabs(ff) > TOL ){
         double dfdE = root1( E , e ); 
         double dE = -ff/dfdE;
         E += dE;
         ff = root0( E , e ) - M; 
      } 
      double x = a*cos(E)-f;
      double y = b*sin(E);
      double R   = sqrt(x*x+y*y);
      double phi = atan2(y,x);

   vr = sqrt( fabs( 2.*en + 2./R - l*l/R/R ) );
   if( y<0.0 ) vr *= -1.;

   double mu = q_planet/(1.+q_planet);

   thePlanets[1].r   = R*(1.-mu);
   thePlanets[1].phi = phi;
   thePlanets[1].omega = l/R/R; 
   thePlanets[1].vr = vr*(1.-mu);

   thePlanets[0].r   = R*mu;
   thePlanets[0].phi = phi+M_PI;
   thePlanets[0].omega = l/R/R;
   thePlanets[0].vr  = vr*mu;

}
Exemple #6
0
TEST(IntNode, saveLoadAndSkip) {
    DefinitionNode root1(NULL, "root");
    IntNode testa1(&root1, "testa", 1);
    IntNode testb1(&root1, "testb", 4);

    PackStream stream1;
    root1.save(&stream1);

    DefinitionNode root2(NULL, "root");
    IntNode testb2(&root2, "testb");

    PackStream stream2(&stream1);
    root2.load(&stream2);

    EXPECT_DOUBLE_EQ(4, testb2.getValue());
}
Exemple #7
0
TEST_F(ExpressionTests, HashTest) {
  // First tree operator_expr(-) -> (tup_expr(A.a), const_expr(2))
  auto left1 = new expression::TupleValueExpression("a", "A");
  auto oids1 = std::make_tuple<oid_t, oid_t, oid_t>(0,0,0);
  left1->SetBoundOid(oids1);
  auto right1 = new expression::ConstantValueExpression(
      type::ValueFactory::GetIntegerValue(2));
  std::unique_ptr<expression::OperatorExpression> root1(
      new expression::OperatorExpression(
      ExpressionType::OPERATOR_MINUS, type::TypeId::INVALID, left1, right1));
  LOG_INFO("Hash(tree1)=%ld", root1->Hash());

  // Second tree operator_expr(-) -> (tup_expr(A.b), const_expr(2))
  auto left2 = new expression::TupleValueExpression("b", "A");
  auto oids2 = std::make_tuple<oid_t, oid_t, oid_t>(0,0,1);
  left2->SetBoundOid(oids2);
  auto right2 = new expression::ConstantValueExpression(
      type::ValueFactory::GetIntegerValue(2));
  std::unique_ptr<expression::OperatorExpression> root2(
      new expression::OperatorExpression(
      ExpressionType::OPERATOR_MINUS, type::TypeId::INVALID, left2, right2));
  LOG_INFO("Hash(tree2)=%ld", root2->Hash());

  EXPECT_NE(root1->Hash(), root2->Hash());

  // Third tree operator_expr(-) -> (tup_expr(A.a), const_expr(2))
  auto left3 = new expression::TupleValueExpression("a", "A");
  auto oids3 = oids1;
  left3->SetBoundOid(oids3);
  auto right3 = new expression::ConstantValueExpression(
      type::ValueFactory::GetIntegerValue(2));
  std::unique_ptr<expression::OperatorExpression> root3(
      new expression::OperatorExpression(
      ExpressionType::OPERATOR_MINUS, type::TypeId::INVALID, left3, right3));
  LOG_INFO("Hash(tree3)=%ld", root3->Hash());

  EXPECT_EQ(root1->Hash(), root3->Hash());
}
Exemple #8
0
static Tree root1(Tree p) {
	if (p == NULL)
		return p;
	if (p->type == voidtype)
		warn++;
	switch (generic(p->op)) {
	case COND: {
		Tree q = p->kids[1];
		assert(q && q->op == RIGHT);
		if (p->u.sym && q->kids[0] && generic(q->kids[0]->op) == ASGN)
			q->kids[0] = root1(q->kids[0]->kids[1]);
		else
			q->kids[0] = root1(q->kids[0]);
		if (p->u.sym && q->kids[1] && generic(q->kids[1]->op) == ASGN)
			q->kids[1] = root1(q->kids[1]->kids[1]);
		else
			q->kids[1] = root1(q->kids[1]);
		p->u.sym = 0;
		if (q->kids[0] == 0 && q->kids[1] == 0)
			p = root1(p->kids[0]);
		}
		break;
	case AND: case OR:
		if ((p->kids[1] = root1(p->kids[1])) == 0)
			p = root1(p->kids[0]);
		break;
	case NOT:
		if (warn++ == 0)
			warning("expression with no effect elided\n");
		return root1(p->kids[0]);
	case RIGHT:
		if (p->kids[1] == 0)
			return root1(p->kids[0]);
		if (p->kids[0] && p->kids[0]->op == CALL+B
		&&  p->kids[1] && p->kids[1]->op == INDIR+B)
			/* avoid premature release of the CALL+B temporary */
			return p->kids[0];
		if (p->kids[0] && p->kids[0]->op == RIGHT
		&&  p->kids[1] == p->kids[0]->kids[0])
			/* de-construct e++ construction */
			return p->kids[0]->kids[1];
		p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
		return p->kids[0] || p->kids[1] ? p : (Tree)0;
	case EQ:  case NE:  case GT:   case GE:  case LE:  case LT: 
	case ADD: case SUB: case MUL:  case DIV: case MOD:
	case LSH: case RSH: case BAND: case BOR: case BXOR:
		if (warn++ == 0)
			warning("expression with no effect elided\n");
		p = tree(RIGHT, p->type, root1(p->kids[0]), root1(p->kids[1]));
		return p->kids[0] || p->kids[1] ? p : (Tree)0;
	case INDIR:
		if (p->type->size == 0 && unqual(p->type) != voidtype)
			warning("reference to `%t' elided\n", p->type);
		if (isptr(p->kids[0]->type) && isvolatile(p->kids[0]->type->type))
			warning("reference to `volatile %t' elided\n", p->type);
		/* fall thru */
	case CVI: case CVF: case CVU: case CVP:
	case NEG: case BCOM: case FIELD:
		if (warn++ == 0)
			warning("expression with no effect elided\n");
		return root1(p->kids[0]);
	case ADDRL: case ADDRG: case ADDRF: case CNST:
		if (needconst)
			return p;
		if (warn++ == 0)
			warning("expression with no effect elided\n");
		return NULL;
	case ARG: case ASGN: case CALL: case JUMP: case LABEL:
		break;
	default: assert(0);
	}
	return p;
}
Exemple #9
0
Tree root(Tree p) {
	warn = 0;
	return root1(p);
}