Exemplo n.º 1
0
struct node *add(struct node *number1, struct node *number2){
	struct node *result = NULL;
	struct node *head = NULL;
	struct node *tail = NULL;
	struct node *num1ptr = number1;
	struct node *num2ptr = number2;
	unsigned int leftover = 0;
	unsigned int carry = 0;
	unsigned int total = 0;
	if (num1ptr->num == 0 && num2ptr->num == 0){
		return result = changeSign(add(changeSign(num1ptr), changeSign(num2ptr)));
	}
	else if(num1ptr->num == 0 && num2ptr->num != 0){
		return result = subtract(num2ptr, changeSign(num1ptr));
	}
	else if(num1ptr->num != 0 && num2ptr->num == 0){
		return result = subtract(num1ptr, changeSign(num2ptr));
	}
	while(num1ptr != NULL && num2ptr != NULL){
		total = num1ptr->num + num2ptr->num;
		if(total < num1ptr->num || total < num2ptr->num){ /* overflow case */
			if(num1ptr->num > num2ptr->num)	leftover = num1ptr->num - (UINT_MAX - num2ptr->num);
			else leftover = num2ptr->num - (UINT_MAX - num1ptr->num);
			result = (struct node *)malloc(sizeof(struct node));
			result->num = leftover + carry;
			carry = 1; /* carry 1 over to the next significant node */
			if (tail == NULL) head = result;
			else tail->next = result;
			tail = result;
			num1ptr = num1ptr->next;
			num2ptr = num2ptr->next;
			continue;
		}
		else{
			result = (struct node *)malloc(sizeof(struct node));
			result->num = total + carry;
			carry = 0;
			if (tail == NULL) head = result;
			else tail->next = result;
			tail = result;
			num1ptr = num1ptr->next;
			num2ptr = num2ptr->next;
			continue;
		}
	}



	return result;
}
void BigReal::init(const Double80 &x) {
  DEFINEMETHODNAME;

  init();

  if(!isnormal(x)) {
    if(isnan(x)) {
      setToNan();
    } else if(isinf(x)) {
      setToInf();
      if(x.isNegative()) {
        changeSign();
      }
    }
    return;
  }
  // x is normal and != 0
  const int expo2 = getExpo2(x) - 63;
  if(expo2 == 0) {
    init(getSignificand(x));
  } else {
    DigitPool *pool = getDigitPool();
    const BigReal significand(getSignificand(x), pool);
    const bool isConstPool = pool->getId() == CONST_DIGITPOOL_ID;
    if(isConstPool) ConstDigitPool::releaseInstance(); // unlock it or we will get a deadlock
    const BigReal &p2 = pow2(expo2, CONVERSION_POW2DIGITCOUNT);
    if(isConstPool) ConstDigitPool::requestInstance();
    shortProductNoZeroCheck(significand, p2, 5).rRound(22);
  }
  if(x.isNegative()) {
    m_negative = true;
  }
}
Exemplo n.º 3
0
struct node *quicksum(struct node *number1, struct node *number2){
	struct node *result;
	if(iszero(number1) && iszero(number2)) return NULL;
	if(iszero(number1)) return number2;
	if(iszero(number2)) return number1;
	if (number1->num == 0 && number2->num == 0){
		return result = changeSign(quicksum(changeSign(number1), changeSign(number2)));
	}
	else if(number1->num == 0 && number2->num != 0){
		return result = quicksub(number2, changeSign(number1));
	}
	else if(number1->num != 0 && number2->num == 0){
		return result = quicksub(number1, changeSign(number2));
	}
	result = (struct node *)malloc(sizeof(struct node));
	result->num = number1->num + number2->num;
	result->next = NULL;
	result->prev = NULL;
	return result;
}
Exemplo n.º 4
0
struct node *subtract(struct node *number1, struct node *number2){
	struct node *result = NULL;
	struct node *head = NULL;
	struct node *tail = NULL;
	int total = 0;
	int negative = 0;
	struct node *num1ptr = number1;
	struct node *num2ptr = number2;
	if (num1ptr->num == 0 && num2ptr->num == 0){
		return result = add(number1, changeSign(number2));
	}
	else if (num1ptr->num != 0 && num2ptr->num == 0){
		return result = add(number1, changeSign(number2));
	}
	else if (num1ptr->num == 0 && num2ptr->num != 0){
		return changeSign(result = add(changeSign(number1), number2));
	}
	while(num1ptr != NULL && num2ptr != NULL){

		if (num1ptr->num > num2ptr->num){
			total = num1ptr->num - num2ptr->num;
			negative = 0;
		}
		else{
			total = num2ptr->num - num1ptr->num;
			negative = 1;
		}

		result = (struct node *)malloc(sizeof(struct node));
		result->num = total;
		if(tail == NULL) head = result;
		else tail->next = result;
		tail = result;
		num1ptr = num1ptr->next;
		num2ptr = num2ptr->next;
		continue;
	}

	if(negative == 1) result = changeSign(result);
	return result;
}
void inputWidget::on_sign2Btn_toggled(bool checked)
{
    isSign2 =checked;
    if(isSign2)
    {
        if(isCapsLock)
           this->capslockBtn->setChecked(!isCapsLock);
        if(isSign)
           this->signBtn->setChecked(!isSign);
        if(!isEnglish)
        {
            this->ecBtn->setChecked(isEnglish);
        }
        changeSign(!isSign2);
        this->labelInputname->setText(QObject::tr("ÌØÊâ·ûºÅ"));
    }
    else
    {
        changeTextCaps(!isSign2);
        this->labelInputname->setText(QObject::tr("СдÊäÈë"));
    }
}
Exemplo n.º 6
0
/** Append text to Expression. Overwrites a trailing operator with the new one, if a sequence of
  * two operators is input.
  * Recognizes and executes C, AC and = commands.
  */
void Calculator::addToExpressionText(const QString & buttonText)
{
    QString text = buttonText;
    try{
        if(text == "C")
            clearLast();
        else if(text == "AC")
            clearAll();
        else if(text == "+/-")
            changeSign();
        else if(text == m_TextRad)
            setAngleMode(text);
        else if(text == m_TextDeg)
            setAngleMode(text);
        else if(text == "=")
        {
            if(isPlotExpression())
                createPlot();
            else parseExpression();
        }
        else if(text=="x")
            addVariable(text);
        else if(text.startsWith("Plot"))
                plotter.processPlotCommand(text);
        else{ //we have a digit, operator or function
            //no clear or evaluate command
            if(text == "x^y") //convert to operator
                text = "^";
            if(isNumber(text)){ //No parenthesis, we have a digit or a decimal separator
                addDigit(text);
                emit guiStateSuggested(STATE_STANDARD); //return to std page, if on Fn Page for entering E
            }
            else if(isOperator(text)) //operator or digit
            {
                addOperator(text);
            }
            else if(isParenthesis(text)) //we have a parenthesis
            {
                addParenthesis(text);
            }
            else if(isFunction(text)) //we have a function
            {
                addFunctionCall(text);
            }
            else if(isVariable(text)) //we have a variable
            {
                addVariable(text);
            }
            else throw ExExpressionError(tr("Unknown command:") + text);

        } //end digit operator or function

        m_ExpressionText = m_ExpressionParts.join("");
        if(isPlotExpression())
            emit expressionTextChanged("y=" + m_ExpressionText);
        else emit expressionTextChanged(m_ExpressionText);
    } //end try
    catch(ExExpressionError e)
    {
        emit guiStateSuggested(STATE_STANDARD);
        emit errorMessage(tr("Input Error"),e.what());
    }
    catch(std::exception e)
    {
        emit guiStateSuggested(STATE_STANDARD);
        emit errorMessage(tr("Unknown Input Error"),e.what());
    }
}
Exemplo n.º 7
0
QrCalculatorImpl::QrCalculatorImpl()
{
    desktop_ = new QDesktopWidget();

    dialog_ = new QDialog( 0, Qt::FramelessWindowHint);
    dialog_->setMinimumSize( width(), height());
    dialog_->setMaximumSize( width(), height());
    dialog_->installEventFilter( this);

    result_line_ = new QLineEdit( dialog_);
    result_line_->setGeometry(QRect(0, 0, 121, 26));
    result_line_->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    result_line_->setText( "");
    result_line_->setReadOnly( true);
    result_line_->installEventFilter( this);

    button_sign_ = new QPushButton( dialog_);
    button_sign_->setGeometry(QRect(0, 20, 31, 31));
    button_sign_->setText( "x");
    button_sign_->setAutoDefault( false);
    connect( button_sign_, SIGNAL( clicked()), this, SLOT( cancelCalculator()));

    button_minus2_ = new QPushButton( dialog_);
    button_minus2_->setGeometry(QRect(30, 20, 31, 31));
    button_minus2_->setText( "+/-");
    button_minus2_->setAutoDefault( false);
    connect( button_minus2_, SIGNAL( clicked()), this, SLOT( changeSign()));

    button_inc_ = new QPushButton( dialog_);
    button_inc_->setGeometry(QRect(60, 20, 31, 31));
    button_inc_->setText( "Add");
    button_inc_->setAutoDefault( false);
    connect( button_inc_, SIGNAL( clicked()), this, SLOT( increment()));

    button_dec_ = new QPushButton( dialog_);
    button_dec_->setGeometry(QRect(90, 20, 31, 31));
    button_dec_->setText( "Sub");
    button_dec_->setAutoDefault( false);
    connect( button_dec_, SIGNAL( clicked()), this, SLOT( decrement()));

    button_div_ = new QPushButton( dialog_);
    button_div_->setGeometry(QRect(90, 50, 31, 31));
    button_div_->setText( "/");
    button_div_->setShortcut( QKeySequence( "/"));
    button_div_->setAutoDefault( false);
    connect( button_div_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button9_ = new QPushButton( dialog_);
    button9_->setGeometry(QRect(60, 50, 31, 31));
    button9_->setText( "9");
    button9_->setShortcut( QKeySequence( "9"));
    button9_->setAutoDefault( false);
    connect( button9_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button8_ = new QPushButton( dialog_);
    button8_->setGeometry(QRect(30, 50, 31, 31));
    button8_->setText( "8");
    button8_->setShortcut( QKeySequence( "8"));
    button8_->setAutoDefault( false);
    connect( button8_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button7_ = new QPushButton( dialog_);
    button7_->setGeometry(QRect(0, 50, 31, 31));
    button7_->setText( "7");
    button7_->setShortcut( QKeySequence( "7"));
    button7_->setAutoDefault( false);
    connect( button7_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button_times_ = new QPushButton( dialog_);
    button_times_->setGeometry(QRect(90, 80, 31, 31));
    button_times_->setText( "*");
    button_times_->setShortcut( QKeySequence( "*"));
    button_times_->setAutoDefault( false);
    connect( button_times_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button6_ = new QPushButton( dialog_);
    button6_->setGeometry(QRect(60, 80, 31, 31));
    button6_->setText( "6");
    button6_->setShortcut( QKeySequence( "6"));
    button6_->setAutoDefault( false);
    connect( button6_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button5_ = new QPushButton( dialog_);
    button5_->setGeometry(QRect(30, 80, 31, 31));
    button5_->setText( "5");
    button5_->setShortcut( QKeySequence( "5"));
    button5_->setAutoDefault( false);
    connect( button5_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button4_ = new QPushButton( dialog_);
    button4_->setGeometry(QRect(0, 80, 31, 31));
    button4_->setText( "4");
    button4_->setShortcut( QKeySequence( "4"));
    button4_->setAutoDefault( false);
    connect( button4_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button_minus_ = new QPushButton( dialog_);
    button_minus_->setGeometry(QRect(90, 110, 31, 31));
    button_minus_->setText( "-");
    button_minus_->setShortcut( QKeySequence( "-"));
    button_minus_->setAutoDefault( false);
    connect( button_minus_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button3_ = new QPushButton( dialog_);
    button3_->setGeometry(QRect(60, 110, 31, 31));
    button3_->setText( "3");
    button3_->setShortcut( QKeySequence( "3"));
    button3_->setAutoDefault( false);
    connect( button3_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button1_ = new QPushButton( dialog_);
    button1_->setGeometry(QRect(0, 110, 31, 31));
    button1_->setText( "1");
    button1_->setShortcut( QKeySequence( "1"));
    button1_->setAutoDefault( false);
    connect( button1_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button2_ = new QPushButton( dialog_);
    button2_->setGeometry(QRect(30, 110, 31, 31));
    button2_->setText( "2");
    button2_->setShortcut( QKeySequence( "2"));
    button2_->setAutoDefault( false);
    connect( button2_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button_plus_ = new QPushButton( dialog_);
    button_plus_->setGeometry(QRect(90, 140, 31, 31));
    button_plus_->setText( "+");
    button_plus_->setShortcut( QKeySequence( "+"));
    button_plus_->setAutoDefault( false);
    connect( button_plus_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button_dot_ = new QPushButton( dialog_);
    button_dot_->setGeometry(QRect(30, 140, 31, 31));
    button_dot_->setText( ".");
    button_dot_->setShortcut( QKeySequence( "."));
    button_dot_->setAutoDefault( false);
    connect( button_dot_, SIGNAL( clicked()), this, SLOT( buttonClicked()));

    button_clear_ = new QPushButton( dialog_);
    button_clear_->setGeometry(QRect(60, 140, 31, 31));
    button_clear_->setText( "C");
    button_clear_->setShortcut( QKeySequence( "C"));
    button_clear_->setAutoDefault( false);
    connect( button_clear_, SIGNAL( clicked()), this, SLOT( clearCalculator()));

    button0_ = new QPushButton( dialog_);
    button0_->setGeometry(QRect(0, 140, 31, 31));
    button0_->setText( "0");
    button0_->setShortcut( QKeySequence( "0"));
    button0_->setAutoDefault( false);
    connect( button0_, SIGNAL( clicked()), this, SLOT( buttonClicked()));
}
Exemplo n.º 8
0
/*
Configures server and loops, processing connections 1 by 1 until quit_signal is received
*/
void* server_thread(void* p){
//Server config implementation
      //get info regarding server config details from Main method
      server_info* info = (server_info*)p;
      int PORT_NUMBER = info->port_num;
      // structs to represent the server and client
      struct sockaddr_in server_addr,client_addr;
      int sock; // socket descriptor
      // 1. socket: creates a socket descriptor that you later use to make other system calls
      if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("Socket");
      exit(1);
      }
      int temp;
      if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&temp,sizeof(int)) == -1) {
      perror("Setsockopt");
      exit(1);
      }
      // configure the server
      server_addr.sin_port = htons(PORT_NUMBER); // specify port number
      server_addr.sin_family = AF_INET;
      server_addr.sin_addr.s_addr = INADDR_ANY;
      bzero(&(server_addr.sin_zero),8);
      // 2. bind: use the socket and associate it with the port number
      if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
      perror("Unable to bind");
      exit(1);
      }
      // 3. listen: indicates that we want to listen to the port to which we bound; second arg is number of allowed connections
      if (listen(sock, 1) == -1) {
      perror("Listen");
      exit(1);
      }
      // once you get here, the server is set up and about to start listening
      printf("\nServer configured to listen on port %d\n", PORT_NUMBER);
      fflush(stdout);

//Request Processing
      //loops, waiting for requests until quit_signal is activated
      while (quit_signal == 0){
            // 4. accept: wait here until we get a connection on that port
            int sin_size = sizeof(struct sockaddr_in);
            int fd2 = accept(sock, (struct sockaddr *)&client_addr,(socklen_t *)&sin_size);
            printf("Server got a connection from (%s, %d)\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

            // buffer to read data into
            char request[1024];
            // 5. recv: read incoming request message into buffer
            int bytes_received = recv(fd2,request,1024,0);
            request[bytes_received] = '\0';
            printf("%s\n", request);
            //this is a request for the most recent temperature 
            switch (request[5]){
                  case 'a':
                        changeSign(fd2);
                        break;
                  case 'b':
                        mostRecentTemp(fd2);
                        break;
                  case 'd':
                        highLowAverage(fd2); 
                        break;
                  case 'm':
                        requestMessage(fd2);
                        break;
                  case 'r':
                        resetAlarm(fd2);
                        break;
                  case 's':
                        toggleStandby(fd2);
                        break;
                  case 't':
                        checkTripped(fd2);
                        printf("%s\n\n", "in t");
                        break;
            }  
            close(fd2);
      }
      // 7. close: close the socket connection
      close(sock);
      printf("Server closed connection\n");
      return 0;
}