Esempio n. 1
0
  Eigen::Matrix3d CEMatrixEditor::validateEditor()
  {
    // Editing fractional matrix is not allowed. The widget is
    // disabled to ensure that this assertion passes.
    Q_ASSERT(m_ext->matrixCartFrac() != Fractional);

    // Clear selection, otherwise there is a crash on Qt 4.7.2.
    QTextCursor tc = ui.edit_matrix->textCursor();
    tc.clearSelection();
    ui.edit_matrix->setTextCursor(tc);

    QString text = ui.edit_matrix->document()->toPlainText();
    QStringList lines = text.split("\n",
                                   QString::SkipEmptyParts);
    if (lines.size() != 3) {
      emit invalidInput();
      return Eigen::Matrix3d::Zero();
    }

    QList<QStringList> stringVecs;
    Eigen::Matrix3d mat;
    for (int row = 0; row < 3; ++row) {
      stringVecs.append(lines.at(row).simplified()
                        .split(CE_PARSE_IGNORE_REGEXP,
                               QString::SkipEmptyParts));
      QStringList &stringVec = stringVecs[row];
      if (stringVec.size() != 3) {
        emit invalidInput();
        return Eigen::Matrix3d::Zero();
      }
      for (int col = 0; col < 3; ++col) {
        bool ok;
        double val = stringVec[col].toDouble(&ok);
        if (!ok) {
          emit invalidInput();
          return Eigen::Matrix3d::Zero();
        }
        mat(row,col) = val;
      }
    }

    // Transpose if needed
    if (m_ext->matrixVectorStyle() == ColumnVectors) {
      // Warning: mat = mat.transpose() will *not* work correctly with
      // Eigen matrices. Use transposeInPlace() instead.
      mat.transposeInPlace();
    }

    emit validInput();
    return mat;
  }
Esempio n. 2
0
void LexAnalysis::runLex()
{
	for (string::size_type ix = 0; ix < input.size();)
	{
		if (opTable.count(input[ix]))
		{
			elements.push_back(std::make_shared<OpElem>(OpElem(input[ix])));
			++ix;
		}
		else if (isdigit(input[ix]))
		{
			ix = stateGraph(ix, input);
		}
		else
			throw invalidInput("invalid input!");
	}
}
  CEAbstractEditor::CEAbstractEditor(CrystallographyExtension *ext)
    : CEAbstractDockWidget(ext),
      m_isLocked(false)
  {
    connect(this, SIGNAL(invalidInput()),
            this, SLOT(markAsInvalid()));
    connect(this, SIGNAL(validInput()),
            this, SLOT(markAsValid()));

    connect(m_ext, SIGNAL(cellChanged()),
            this, SLOT(refreshEditor()));

    connect(this, SIGNAL(visibilityChanged()),
            m_ext, SLOT(refreshActions()));

    connect(this, SIGNAL(editStarted()),
            m_ext, SLOT(lockEditors()));
    connect(this, SIGNAL(editAccepted()),
            m_ext, SLOT(unlockEditors()));
    connect(this, SIGNAL(editRejected()),
            m_ext, SLOT(unlockEditors()));

  }
Esempio n. 4
0
string::size_type 
LexAnalysis::stateGraph(string::size_type ix, const string& str)
{
	state = 13;
	string number;
	string::size_type ret = str.size();
	bool endFlag = false;
	for (string::size_type ie = ix; ie != str.size(); ++ie)
	{
		switch (state)
		{
		case 13:
		{
			if (isdigit(str[ie]))
			{
				number.push_back(str[ie]);
			}
			else if (str[ie] == '.')
			{
				number.push_back(str[ie]);
				state = 14;
			}
			else if (str[ie] == 'e')
			{
				number.push_back(str[ie]);
				state = 16;
			}
			else
			{
				endFlag = true;
			}
			break;
		}
		case 14:
		{
			if (isdigit(str[ie]))
			{
				number.push_back(str[ie]);
				state = 15;
			}
			else
				throw invalidInput("invalid input for a number after \'.\' ");
			break;
		}
		case 15:
		{
			if (isdigit(str[ie]))
				number.push_back(str[ie]);
			else if (str[ie] == 'e')
			{
				number.push_back(str[ie]);
				state = 16;
			}
			else
			{
				endFlag = true;
			}
			break;
		}
		case 16:
		{
			if (isdigit(str[ie]))
			{
				number.push_back(str[ie]);
				state = 18;
			}
			else if (str[ie] == '+' || str[ie] == '-')
			{
				number.push_back(str[ie]);
				state = 17;
			}
			else
				throw invalidInput("invalid input for a number after \'+\' or \'-\' ");
			break;
		}
		case 17:
		{
			if (isdigit(str[ie]))
			{
				number.push_back(str[ie]);
				state = 18;
			}
			else
				throw invalidInput("invalid input for a number");
			break;
		}
		case 18:
		{
			if (isdigit(str[ie]))
				number.push_back(str[ie]);
			else
				endFlag = true;
			break;
		}
		}
		if (endFlag)
		{
			ret = ie;
			break;
		}
	}
	state = 0;
	elements.push_back(std::make_shared<NumElem>(NumElem(std::stod(number))));
	return ret;
}
Esempio n. 5
0
int main(void)
{
    int length = 0, height = 0, i, k, lowVal = -1000, highVal = -1000;
    char entred[100] = "";

    do{
        drawField(COLS/4, ROWS/4, COLS/2, ROWS/2);
        k = setText(COLS/4 + 2, 3*ROWS/8, "Enter matrix length [1..9]\n& height [1..9]\nparting values with coma:", FIELD_COLOR);
        getText(COLS/4 + 2, 3*ROWS/8 + k + 1, entred, FIELD_BACKGROUND, FORE_BLACK);
        getPartiedInts(entred, &length, &height);
        if((length > 9 || length < 1) || (height > 9 || height< 1)){
            setTextLine(COLS/4 + 2, 3*ROWS/8 + k + 4, "Invalid input!", FIELD_BACKGROUND | FOREGROUND_RED);
            Sleep(1500);
        }
    }while((length > 9 || length < 1) || (height > 9 || height< 1));

    do{
        drawField(COLS/4, ROWS/4, COLS/2, ROWS/2);
        k = setText(COLS/4 + 2, 3*ROWS/8, "Enter matrix random values diapason\nparting left & right bounds with coma:", FIELD_COLOR);
        getText(COLS/4 + 2, 3*ROWS/8 + k + 1, entred, FIELD_BACKGROUND, FORE_BLACK);
        getPartiedInts(entred, &lowVal, &highVal);
        if((lowVal > highVal) || (highVal > 999) || (lowVal < -999)){
            setTextLine(COLS/4 + 2, 3*ROWS/8 + k + 4, "Invalid input!", FIELD_BACKGROUND | FOREGROUND_RED);
            Sleep(1500);
        }
    }while((lowVal > highVal) || (highVal > 999) || (lowVal < -999));


    int matr[height][length], mcpy[length][height], isTurned = 0, toSwitch = 0, firstToSwap, secondToSwap;
    int cX, cY, val, row;
    fillRand(length, height, matr, lowVal, highVal);

    int y = 2 + height*2;

    short color = FIELD_COLOR, toClean = 0, isSwaped = 0, help = 0, toPickOutCols = 0, toYellow = 0, toPickOutRow = 0;
    char * g, cpy[100];
    COORD swaped[2];
    clearScr();
    drawField(0, 0, COLS - 1, y);

    while(1){
        y = isTurned ? (2 + length*2) : (2 + height*2);
        if(toClean){
            toClean = 0;
            clean(y);
        }
        printMatr(length, height, matr, isTurned, toSwitch, color);
        color = FIELD_COLOR;

        if(isSwaped){
            pickOut(length, height, matr, isTurned, swaped[0], BACKGROUND_GREEN);
            pickOut(length, height, matr, isTurned, swaped[1], BACKGROUND_RED);
            isSwaped = 0;
        } else if(toYellow){
            printEl(length, height, matr, isTurned, cX - 1, cY - 1, FORE_YELLOW | FIELD_BACKGROUND | FOREGROUND_INTENSITY);
            toYellow = 0;
        } else if(toPickOutRow){
            pickOutRow(length, height, matr, isTurned, row - 1, BACKGROUND_GREEN);
            toPickOutRow = 0;
        } else if(toPickOutCols){
            pickOutCol(length, height, matr, isTurned, firstToSwap - 1, BACKGROUND_GREEN);
            pickOutCol(length, height, matr, isTurned, secondToSwap - 1, BACKGROUND_RED);
            toPickOutCols = 0;
        }

        toSwitch = 0;
        setTextLine(1, y + 2, "Enter your command:",  FORE_WHITE);
        do{
            getText(1, y + 3, entred, BACK_BLACK, FOREGROUND_GREEN);
        }while(entred[0] == '\0');

        strcpy(cpy, entred);
        g = strtok(cpy, " ");
        if(!g)
            g = "\n";
        for(i = 2; i < COLS; i++){
            moveCursor(i, y + 3);
            putchar(' ');
        }
        if(!strcmp(entred, "help")){
            helpComm(y + 5);
            help = 1;
        }else if(!strcmp(entred, "clear all")){
            clearMatr(length, height, matr);
            color = FIELD_BACKGROUND | FORE_YELLOW | FOREGROUND_INTENSITY;
        }else if(!strcmp(g, "random")){
            lowVal = -1000;
            highVal = -1000;
            char cpy1[100];
            for(i = 0; cpy[i + 7] != '\0'; i++){
                cpy1[i] = cpy[i + 7];
            }
            cpy1[i] = '\0';
            getPartiedInts(cpy1, &lowVal, &highVal);

            if((lowVal > highVal) || (highVal > 999) || (lowVal < -999)){
                invalidInput(y);
            }else{
                fillRand(length, height, matr, lowVal, highVal);
                color = FIELD_BACKGROUND | FORE_YELLOW | FOREGROUND_INTENSITY;
            }
        }else if(!strcmp(g, "set")){
            cX = -1;
            cY = -1;
            val = -1000;
            char cpy1[100], *l;
            for(i = 0; cpy[i + 4] != '\0'; i++){
                cpy1[i] = cpy[i + 4];
            }
            cpy1[i] = '\0';
            getPartiedInts(cpy1, &cX, &cY);
            l = strtok(cpy1, ":");
            l = strtok(NULL, ":");
            if(l != NULL){
                val = atoi(l);
            }

            if(!isTurned){
                if(cX > length || cX <= 0 || cY > height || cY <= 0 || val > 999 || val < -999){
                    invalidInput(y);
                }else{
                    matr[cY -  1][cX - 1] = val;
                    toYellow = 1;
                }
            }else{
                if(cX > height || cX <= 0 || cY > length || cY <= 0 || val > 999 || val < -999){
                    invalidInput(y);
                }else{
                    matr[cX - 1][cY - 1] = val;
                    toYellow = 1;
                }
            }
        }else if(!strcmp(entred, "print upright")){
            isTurned = !isTurned;
            toSwitch = 1;
        }else if(!strcmp(entred, "middle value")){
            double mid = countMiddle(length, height, matr);
            setTextColor(FORE_WHITE);
            moveCursor(5, y + 4);
            printf("middle value: ");
            setTextColor(FORE_YELLOW);
            moveCursor(22, y + 4);
            printf("%f                         ", mid);
            help = 1;
        }else if(!strcmp(g, "sum")){
            int sum;
            row = -1;
            g = strtok(NULL, " ");
            if(g != NULL && !strcmp(g, "in")){
                g = strtok(NULL, " ");
                if(g != NULL && !strcmp(g, "row")){
                    g = strtok(NULL, " ");
                    if(g != NULL)
                        row = atoi(g);
                }
            }
            if(row < 0 || row > (isTurned ? length : height))
                invalidInput(y);
            else{
                sum = sumInRow(length, height, matr, isTurned, row -1);
                toPickOutRow = 1;
                setTextColor(FORE_WHITE);
                moveCursor(5, y + 4);
                printf("sum in %dth row: ", row);
                setTextColor(FORE_YELLOW);
                moveCursor(22, y + 4);
                printf("%d                         ", sum);
                help = 1;
            }
        }else if(!strcmp(entred, "swap 1")){
            swapMinMax(length, height, matr, isTurned, 1, swaped);
            isSwaped = 1;
        }else if(!strcmp(entred, "swap 2")){
            swapMinMax(length, height, matr, isTurned, 0, swaped);
            isSwaped = 1;
        }else if(!strcmp(entred, "swap cols")){
            swapCols(length, height, matr, isTurned, &firstToSwap, &secondToSwap);
            toPickOutCols = 1;
        }else if(!strcmp(entred, "clean workspace") && help){
            toClean = 1;
            help = 0;
        }else if(!strcmp(entred, "f**k you")){
            clearScr();
            setTextLine(0, 0, "GO F**K YOURSELF!", FOREGROUND_RED | FOREGROUND_INTENSITY);
            setTextColor(FORE_WHITE);
            return EXIT_FAILURE;
        }else if(!strcmp(entred, "exit")){
            clearScr();
            setTextColor(FORE_WHITE);
            return EXIT_SUCCESS;
        }else{
            invalidInput(y);
        }
    }
    return EXIT_SUCCESS;
}
Esempio n. 6
0
/* main program*/
int main(){
	/* variables */
    char inName[128];
    char outName[128];
    ListHndl array[128];
    char tempLine[128];
    int customers;
    int purchases;
    int tempCust;
    unsigned long tempBook;
    FILE *in;
    FILE *out;
    
	/* greeting & prompt for input and output file names */
    printf("Welcome! Please enter your input file here: ");
    scanf("%s",inName);
    in = fopen(inName, "r");
    if(in == NULL){
        printf("Error: This input file does not exist.\n");
        exit(1);
    }
    printf("Please enter your output file here: ");
    scanf("%s",outName);
    out = fopen(outName, "w");
    
	/* check numbers of customer and purchased */
    do{
        if(fgets(tempLine, 128, in) == NULL) invalidInput();
    }while(strcmp(tempLine, "\n") == 0);
    if(sscanf(tempLine,"%d", &customers) != 1) invalidInput();
    do{
        if(fgets(tempLine, 128, in) == NULL) invalidInput();
    }while(strcmp(tempLine, "\n") == 0);
    if(sscanf(tempLine,"%d", &purchases) != 1) invalidInput();
    
    /*makes empty Lists and fills them up*/
    for(int i = 0; i<customers; i++) array[i] = newList();
    for(int i = 0; i<purchases; i++){
        do{
          if(fgets(tempLine, 128, in) == NULL){
            printf("Error: Number of purchases does not match (too little).\n");
            exit(1);
          }
        }while(strcmp(tempLine, "\n") == 0);
        if(sscanf(tempLine,"%d", &tempCust) != 1) invalidInput();
        if(sscanf(tempLine,"%*d%lu", &tempBook) != 1) invalidInput();
        if(tempCust > customers){
            printf("Error: Invalid customer number.\n");
            exit(1);
        }
        if(isEmpty(array[tempCust-1])){
            insertAtBack(array[tempCust-1],tempBook);
            continue;
        }
        moveFirst(array[tempCust-1]);
        while(!offEnd(array[tempCust-1])){
            if(tempBook < (unsigned)getCurrent(array[tempCust-1])){
                insertBeforeCurrent(array[tempCust-1], tempBook);
                break;
            }
            moveNext(array[tempCust-1]);
        }
        if(offEnd(array[tempCust-1])) insertAtBack(array[tempCust-1],tempBook);
    }
    
	/* checks for any lingering purchases */
    for(;;){
        if(fgets(tempLine, 128, in) == NULL) break;
        if(sscanf(tempLine,"%d", &tempCust) != 0){
            printf("Error: Number of purchases does not match (too many).\n");
            exit(1);
        }
    }
    
    /*printing store*/
    fprintf(out, "customer#   books purchased\n");
    for(int i = 0; i<customers; i++){
        fprintf(out, "%d   ",i+1);
        moveFirst(array[i]);
        while(!offEnd(array[i])){
            fprintf(out, "%lu ", getCurrent(array[i]));
            moveNext(array[i]);
        }
        fprintf(out, "\n");
    }
    
    /*free everythingggggggggggg*/
    for(int i = 0; i<customers; i++) freeList(&array[i]);
    fclose(in);
    fclose(out);
    
	/* end program */
    printf("Done.\n");
    return 0;
}