Beispiel #1
0
void Gameplay::pushNumber(const Number &n, bool pushToVec = true) {
    if (pushToVec) {
        numbers.push_back(n);
    }
    for (int i = sumOfNumbers; i >= 0; i--)
        if (reachable[i]) {
            reachable[i + n.getValue()] = true;
    }
    sumOfNumbers += n.getValue();
    correctSum = findCorrectSum();
}
Beispiel #2
0
void BehaviourPatterns::cMemento() {
  int i;
  cout << "Integer: ";
  cin >> i;
  Number* object = new Number( i );

  CommandM* commands[3];
  commands[1] = new CommandM( object, &Number::dubble );
  commands[2] = new CommandM( object, &Number::half );

  cout << "Exit[0], Double[1], Half[2], Undo[3], Redo[4]: ";
  cin >> i;
  while( i ) {

    if( i == 3 ) {
      CommandM::undo();
    }
    else if( i == 4 )
      CommandM::redo();
    else
      commands[i]->execute();
    cout << "  " << object->getValue() <<  endl;
    cout << "Exit[0], Double[1], Half[2], Undo[3], Redo[4]: ";
    cin >> i;
  }
}
int main()
{
  int i;
  cout << "Integer: ";
  cin >> i;
  Number *object = new Number(i);

  Command *commands[3];
  commands[1] = new Command(object, &Number::dubble);
  commands[2] = new Command(object, &Number::half);

  cout << "Exit[0], Double[1], Half[2], Undo[3], Redo[4]: ";
  cin >> i;

  while (i)
  {
    if (i == 3)
      Command::undo();
    else if (i == 4)
      Command::redo();
    else
      commands[i]->execute();
    cout << "   " << object->getValue() << endl;
    cout << "Exit[0], Double[1], Half[2], Undo[3], Redo[4]: ";
    cin >> i;
  }
}
int main()
{
	int i;
	cout << "Please enter an integer: ";
	cin >> i;

	Number * object = new Number(i);
 
	Command * commands[5];
	commands[1] = new Command(object, &Number::doubleValue);
	commands[2] = new Command(object, &Number::halfValue);
	commands[3] = new Command(object, &Number::increaseByOne);
	commands[4] = new Command(object, &Number::decreaseByOne);
 
	cout << "[0]Exit, [1]Double, [2]Half, [3]Increase, [4]Decrease, [5]Undo, [6]Redo: ";
	cin >> i;
 
	while (i != Command::Operation::Exit)
	{
		if (i == Command::Operation::Undo)
		{
			Command::undo();
		}
		else if (i == Command::Operation::Redo)
		{
			Command::redo();
		}
		else if (i > Command::Operation::Exit && i < Command::Operation::Undo)
		{
			commands[i]->execute();
		}
		else
		{
			cout << "Enter a proper choice: ";
			cin >> i;
			continue;
		}

		cout << "   " << object->getValue() << "  " << object->getName() << "  " << object->getDecimal() << endl;
		cout << "[0]Exit, [1]Double, [2]Half, [3]Increase, [4]Decrease, [5]Undo, [6]Redo: ";
		cin >> i;
	}
}
Beispiel #5
0
void Printer::operator()(const SmartPointer<Block> &block) {
  if (!block->isEmpty() || !removeBlankLines) {
    if (addComments) {
      string comments;

      Block::const_iterator it;
      for (it = block->begin(); it != block->end(); it++) {
        Word *word = dynamic_cast<Word *>(it->get());

        if (word) {
          char type = word->getType();
          Number *number = dynamic_cast<Number *>(word->getExpression().get());

          if (number) {
            double value = number->getValue();
            const Code *code = Codes::find(type, value);

            if (code) comments += SSTR(" (" << code->description << ')');
          }
        }
      }

      if (!comments.empty()) {
        string line = SSTR(*block);
        stream << line;

        int pad = 80 - (line.length() + comments.length());
        if (0 < pad) stream << string(pad, ' ');

        stream << comments << '\n';

        return;
      }
    }

    stream << *block << '\n';
  }
}
Beispiel #6
0
double Evaluator::eval(Number &e) {
  return e.getValue();
}
Beispiel #7
0
long operator-(long val, const Number &num)
{
    return num.getValue() - val;
}
Beispiel #8
0
long operator-(const Number &num, long val)
{
    return num.getValue() - val;
}
Beispiel #9
0
int Number::operator>=(const Number &num)
{
    return getValue() >= num.getValue();
}
Beispiel #10
0
int Number::operator<(const Number &num)
{
    return getValue() < num.getValue();
}
Beispiel #11
0
Number operator+ (const Number& lv,const Number& rv){
	string lvs = lv.getValue();
	int lv_length = lvs.size();
	string rvs = rv.getValue();
	int rv_length = rvs.size();

	unsigned short int flag(0); //进位
	char result[202];
	char temp[202];
	unsigned short int res;

	string max_length_str;
	string min_length_str;
	int min_length;
	int length;

	if(lv_length>rv_length){
		length = lv_length;
		min_length = rv_length;
		max_length_str = lvs;
		min_length_str = rvs;
	}else{
		length = rv_length;
		min_length = lv_length;
		max_length_str = rvs;
		min_length_str = lvs;
	}

	strcpy(result,max_length_str.c_str());
	strcpy(temp+(length-min_length),min_length_str.c_str());

	for(int i=length-1,j=0;i>=0;i--,j++){
		if(i == length-3){
			result[i] = '.';
			continue;
		}
		if(j< min_length){
			res =  (unsigned short int)result[i]-48 + (unsigned short int)temp[i]-48+flag;
			if(res>=10){flag = 1;res-=10;}else{flag = 0;}
			// cout<<res<<endl;
			result[i] = res+48;
		}else{
			if(flag>0){
				unsigned short int tem = (unsigned short int)result[i]-48 + flag;
				if(tem>10){
					flag = 1;
					tem-=10;
				}else{
					flag = 0;
				}
				result[i] = tem+48;

			}else{
				break;
			}
		}
	}
	char sum_number[202];

	if(flag>0){
		sum_number[0] = '1';
		strcpy(sum_number+1,result);

		return Number(sum_number);
	}else{
		return Number(result);
	}
}