Пример #1
0
void main()
{
	int N = toDec(101101001, 2);
	int M = toDec(10101, 2);

	printf("\n after setting bits = %d\n", fromDec(setbitsitoj(N, M, 2, 6), 2));
}
Пример #2
0
void genCode()
{
	printProc("Generating code... ");
	printStatus("DONE",10);
	printProc("Writing on file... ");
	printStatus("DONE",10);
	writeHeader();
	int i, k;
	for(i=1; i<=charCount; i++)
	{
		k=mark[charSet[i]];
		while(k!=parent[k])
		{
			k=parent[k];
			if(k<0)
				k = -k, push(0);
			else
				push(1);
		}
		while((k=pop())> -1)
			writeOnFile(k);
	}
	if(dc<7)
	{
		for(i=dc+1; i<8; i++)
			s[i] = '0';
		toDec();
		fputc(7-dc+'0', fp);
	}
	else
	{
		fputc(0+'0', fp);
	}
	closeFile2Write();
}
string twos_complement(string s){
    string res = "";
    int a = atoi(s.c_str());
    while(a){
        res = toHex(15 - a%10) + res;
        a /= 10;
    }
    int l = 3-s.size();
    while(l>0) res = "F" + res, l--;
    res = toHex(toDec(res) + 1);
    return res;
}
Пример #4
0
uint32_t serial_getInt(uint8_t base){
    char str[33]; // Lowest base taken is 2, and we're dealing with 32 bit integers.
    char* ptr=str;
    uint32_t ret=0;

    if(base>36) return 0;
    if(!serial_initialized) serial_init(); //safety
    
    serial_gets(str, 33, '\r');
    while(*ptr){
       *ptr = toUpcase(*ptr);
       if((*ptr > 47 && *ptr < 58) || (*ptr > 64 && *ptr < 91))
            ret=toDec(*ptr, base)+ret*base;
       ptr++;
    }
    return ret;
}
Пример #5
0
int main()
{
    char str[20], line[]="___________";
    while(gets(str)){
        char Ans[5000]={'\0'};
        int index = 0;
        while(gets(str) && strcmp(str, line)){
            char temp[20]={'\0'};
            int top = 0;
            for(int i = 0; str[i]; i++){
                if(str[i] == '.')   continue;
                if(str[i] == ' ')   temp[top++] = '0';
                if(str[i] == 'o')   temp[top++] = '1';
            }
            Ans[index++] = toDec(atoi(temp));
        }
        printf("%s", Ans);
    }
    return 0;
}
Пример #6
0
int main(int argc, char *argv[]) {
    /*Check if it is a '-'
     * in first, if not take the char it is form and go from +1 of that to end */
    /*Convert to binary*/
    /*Repeat for 2nd guy*/
    /*Add if add, or sub if sub(two's complement and then add)*/
    /*Output*/
    LLBin *list1 = (LLBin *)malloc(sizeof(LLBin));
    LLBin *list2 = (LLBin *)malloc(sizeof(LLBin));

    /*Check if argv < 5*/
    if (argc < 5) {
        fprintf(stderr, "Not enough arguments passed.\n");
        exit(EXIT_FAILURE);        
    }
    if (argc > 6) {
        fprintf(stderr, "Too many arguments passed.\n");
        exit(EXIT_FAILURE);        
    }


    char *setFirst = argv[2];
    char *setTwo = argv[3];
    char *op = argv[1];
    char *outForm = argv[4];

    if (!strcmp(op, "+") || !strcmp(op, "-")) {
        fprintf(stderr, "Invalid operation!\n");
        exit(EXIT_FAILURE);  
    }

    if (!strcmp(outForm, "b")  || !strcmp(outForm, "d")  || !strcmp(outForm, "x") ||  !strcmp(outForm, "o")) {
        fprintf(stderr, "Invalid output base!\n");
        exit(EXIT_FAILURE);
    }

    if (setFirst[0] == '-') {
        char base = setFirst[1];
        strncpy(setFirst, setFirst+2, strlen(setFirst));
        if (base == 'b') {
            char *str = strrev(setFirst);
            place(list1, str);
        }
        else if (base == 'x')
            toDec(setFirst, 'x', HEX_BASE, &list1);
        else if (base == 'd')
            toDec(setFirst, 'd', DEC_BASE, &list1);
        else if (base == 'o')
            toDec(setFirst, 'o', OCT_BASE, &list1);
    }
    else {
        char base = setFirst[0];
        strncpy(setFirst, setFirst+1, strlen(setFirst));
        if (base == 'b') {
            char *str = strrev(setFirst);
            place(list1, str);
        }
        else if (base == 'x')
            toDec(setFirst, 'x', HEX_BASE, &list1);
        else if (base == 'd')
            toDec(setFirst, 'd', DEC_BASE, &list1);
        else if (base == 'o')
            toDec(setFirst, 'o', OCT_BASE, &list1);
    }

    /*Second input*/
    if (setTwo[0] == '-') {
        /*Two's! and add to list*/
        char base = setTwo[1];
        strncpy(setTwo, setTwo+2, strlen(setTwo));
        if (setTwo[1] == 'b') {
            char *str = strrev(setTwo);
            place(list2, str);
        }
        else if (base == 'x')
            toDec(setTwo, 'x', HEX_BASE, &list2);
        else if (base == 'd')
            toDec(setTwo, 'd', DEC_BASE, &list2);
        else if (base == 'o')
            toDec(setTwo, 'o', OCT_BASE, &list2);


    }
    else {
        char base = setTwo[0];
        strncpy(setTwo, setTwo+1, strlen(setTwo));
        if (base == 'b') {
            char *str = strrev(setTwo);
            place(list2, str);
        }
        else if (base == 'x')
            toDec(setTwo, 'x', HEX_BASE, &list2);
        else if (base == 'd')
            toDec(setTwo, 'd', DEC_BASE, &list2);
        else if (base == 'o')
            toDec(setTwo, 'o', OCT_BASE, &list2);


    }

    LLBin *out = (LLBin*)malloc(sizeof(LLBin));
    if (strcmp(op, "+")) {
        out = add(list1, list2);
    }
    else if (strcmp(op, "-")) {
        list2 = twos(list2);
        out = add(list1, list2);
    }
    print(out, outForm);
    

    return 0;
}
Пример #7
0
void CompassPort::on()// метод для чтения из порта и его открытия, если не открыт
{

    emit timerStop();
    if(!portSensor->isOpen())
    {       

        if (portSensor->open(QIODevice::ReadWrite))// открываем порт если он еще не открыт
        {

            QSerialPortInfo *info = new QSerialPortInfo(*portSensor);//информация о порте для отладки
            m_state=1;// порт открыт


            delete info;
        }
        else
        {
            if(portSensor->isOpen())// если что-то пошло не так, закрываем порт
                portSensor->close();
        }
    }

    if(portSensor->isOpen() && portSensor->waitForReadyRead(100))// работа с открытым портом
    {

        QString data;
        QByteArray ByteArray,ByteArrayStart,ByteArrayFinish;
        bool startFinded = false;
        m_state = 1;
        while(m_state)// пока порт открыт
        {
            //if(portSensor->waitForReadyRead(1))
            {
                qint64 byteAvail = portSensor->bytesAvailable();// просматриваем кол-во доступных байн для чтения
                qApp->processEvents();
                QThread::msleep(10);//усыпляем поток, чтобы не занимал времени( данные раз в 10 секунд)
                if(byteAvail >=23)// проверка кол-ва байт, для их обработки
                {
                    ByteArray = portSensor->readAll();// чтение из буфера порта
                    data = data.fromLocal8Bit(ByteArray).trimmed();
                    if(ByteArray[3]=='p')//то ли сообщение пришло(смотри даташит хоневеловского датчика)
                    {
                        QBitArray bitdata(184),two_bytes(16);
                        for(int i = 0,j; i < 184; ++i)//формирование массива бит для парсинга сообщения
                        {
                            j=i/8;
                            if(j<=18)
                                bitdata[i] = ByteArray[j] & (1 << i%8);
                            else

                                break;
                        }

//                            //m_roll = Round(toDec(two_bytes,1)*1.41,1);
                            m_roll = QString::number((short)((ByteArray.at(6)<<8) + (ByteArray.at(5)))*360.0/65536.0,10,1).toDouble();
                            emit rollChanged(m_roll);
//                            for(int i=56,j=15;i<72&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Pitch

//                            m_pitch = Round(toDec(two_bytes,1)*1.41,1);
                            m_pitch = QString::number((short)((ByteArray.at(8)<<8) + (ByteArray.at(7)))*360.0/65536.0,10,1).toDouble();
                            emit pitchChanged(m_pitch);
                            for(int i=72,j=15;i<88&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Azimuth

                           // m_angle = Round(toDec(two_bytes,0)*1.41,1);
                            for(int i=72,j=15;i<88&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Azimuth
                            m_angle = Round(toDec(two_bytes,0)*1.41,1);
                            static int i=0;
                            if(i<=1)
                            {
                                i++;
                            }
                            else
                            {
                                emit angleChanged(m_angle);
                                i=0;
                            }


                            m_B= QString::number((short)((ByteArray.at(18)<<8) + (ByteArray.at(17)))*750.0/65536.0,10,1).toDouble();
                            emit BChanged(m_B);

//                            for(int i=152,j=15;i<168&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //coef C
//                            m_C = Round(toDec(two_bytes,1)*3,1);
                            m_C= QString::number((short)((ByteArray.at(20)<<8) + (ByteArray.at(19)))*750.0/65536.0,10,1).toDouble();

                            emit CChanged(m_C);

//                            for(int i=168,j=15;i<184&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //coef Z
//                            m_Z = Round(toDec(two_bytes,1)*1.41,1);
                            m_Z= QString::number((short)((ByteArray.at(22)<<8) + (ByteArray.at(21)))*750.0/65536.0,10,1).toDouble();
                            emit ZChanged(m_Z);


                        m_state=0;
                        qApp->processEvents();
                    }
                }
                // внимательно посмотреть этот код, кажется косяк с выбросами полей и курса в нем(!)
                else if(byteAvail >=4 && byteAvail <=23)// если сообщение не полное( разбито на два)
                {
                    ByteArray= portSensor->readAll();
                    data = data.fromLocal8Bit(ByteArray).trimmed();
                    if(ByteArray[3]=='p' && startFinded == false)
                    {
                        ByteArrayStart = ByteArray;
                        startFinded = true;

                    }
                    else if(startFinded == true)
                    {
                        ByteArrayFinish += ByteArray;
                        ByteArray = ByteArrayStart + ByteArrayFinish;
                        if(ByteArray.size() >= 23)
                        {
                            QBitArray bitdata(184),two_bytes(16);
                            for(int i = 0,j; i < 184; ++i)
                            {
                                j=i/8;
                                if(j<=23)
                                    bitdata[i] = ByteArray[j] & (1 << i%8);
                                else
                                    break;
                            }
//                            //m_roll = Round(toDec(two_bytes,1)*1.41,1);
                            m_roll = QString::number((short)((ByteArray.at(6)<<8) + (ByteArray.at(5)))*360.0/65536.0,10,1).toDouble();
                            emit rollChanged(m_roll);
//                            for(int i=56,j=15;i<72&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Pitch

//                            m_pitch = Round(toDec(two_bytes,1)*1.41,1);
                            m_pitch = QString::number((short)((ByteArray.at(8)<<8) + (ByteArray.at(7)))*360.0/65536.0,10,1).toDouble();
                            emit pitchChanged(m_pitch);
                            for(int i=72,j=15;i<88&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Azimuth

                           // m_angle = Round(toDec(two_bytes,0)*1.41,1);
                            for(int i=72,j=15;i<88&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Azimuth
                            m_angle = Round(toDec(two_bytes,0)*1.41,1);
                            static int i=0;
                            if(i<=1)
                            {
                                i++;
                            }
                            else
                            {
                                emit angleChanged(m_angle);
                                i=0;
                            }


                            m_B= QString::number((short)((ByteArray.at(18)<<8) + (ByteArray.at(17)))*750.0/65536.0,10,1).toDouble();
                            emit BChanged(m_B);

//                            for(int i=152,j=15;i<168&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //coef C
//                            m_C = Round(toDec(two_bytes,1)*3,1);
                            m_C= QString::number((short)((ByteArray.at(20)<<8) + (ByteArray.at(19)))*750.0/65536.0,10,1).toDouble();

                            emit CChanged(m_C);

//                            for(int i=168,j=15;i<184&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //coef Z
//                            m_Z = Round(toDec(two_bytes,1)*1.41,1);
                            m_Z= QString::number((short)((ByteArray.at(22)<<8) + (ByteArray.at(21)))*750.0/65536.0,10,1).toDouble();
                            emit ZChanged(m_Z);

                            m_state=0;
                            startFinded = false;
                        }
                    }
                }
            }
        }
    }
    else
    {

    }
    emit timerStart(10);
}
Пример #8
0
void Kompas::on()
{

    timer->stop();
    QSerialPortInfo *info = new QSerialPortInfo(*port);
    if(!(port->isOpen() && info->portName() == settings->m_name_COM))
    {
        if(port->isOpen())
            port->close();

        qDebug()<<settings->m_name_COM;
        port->setPortName(settings->m_name_COM);
        if (port->open(QIODevice::ReadWrite))
        {
            QFile file("dataRead.dat");
            if (file.open(QIODevice::ReadOnly | QIODevice::Text))
            {
               QTextStream stream(&file);
               m_skl = stream.readLine().toInt();
               m_coef_A = stream.readLine().toInt();
               file.close();
            }

            updateSettings();
            QSerialPortInfo *info = new QSerialPortInfo(*port);
            qDebug() << "Name        : " << info->portName();
            qDebug() << "Description : " << info->description();
            qDebug() << "Manufacturer: " << info->manufacturer();
            qDebug() << "BaudRate: " << port->baudRate();
            qDebug() << "Parity: " << port->parity();
            qDebug() << "Data bits: " << port->dataBits();
            qDebug() << "Stop Bits: " << port->stopBits();
            delete info;
            m_state=1;
            emit stateChanged();
            m_connect_state=1;
            emit connect_stateChanged();
            qDebug()<<"state = 1 ON";
            qDebug()<<settings->m_name_COM<<"opened";
        }
        else
        {
            if(port->isOpen())
                port->close();
            qDebug()<<"Error while opening";
        }
    }
    if(port->isOpen() && port->waitForReadyRead(1000))
    {
        QString data;
        QByteArray ByteArray;
        m_state = 1;
        while(m_state)
        {
            qint64 byteAvail = port->bytesAvailable();
            qApp->processEvents();
            if(byteAvail >=13)
            {
                ByteArray = port->readAll();
                data = data.fromLocal8Bit(ByteArray).trimmed();
                if(ByteArray[3]=='p')
                {
                    QBitArray bitdata(104),two_bytes(16);
                    for(int i = 0,j; i < 104; ++i)
                    {
                        j=i/8;
                        if(j<=13)
                            bitdata[i] = ByteArray[j] & (1 << i%8);
                        else
                            break;
                    }

                    for(int i=40,j=15;i<56&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Roll
                    setRoll(Round(toDec(two_bytes,1)*1.41,1));
                    for(int i=56,j=15;i<72&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Pitch
                    setPitch(Round(toDec(two_bytes,1)*1.41,1));
                    for(int i=72,j=15;i<88&&j>=0;i++,j--){two_bytes[j]=bitdata[i];} //Azimuth

                    setAngle(Round(toDec(two_bytes,0)*1.41,1));
                    m_state=0;
                    qApp->processEvents();
                }
            }
        }
    }
    else
    {
        qDebug()<<"WaitForReadyRead failed";
        qDebug()<<port->error();
    }
    kompasThread1->quit();
    timer->start(10);
}
Пример #9
0
void run()
{
    create();
    string s,word[5];
    int count=0;
    fin1.open("input_fibonacci.txt", fstream::out);

    
    fout1.open("intermediate.txt", fstream::in);
    error.open("error.txt", fstream::in);
    line=5;
    getline(fin1,s);
    extract(s,word,count);
    while(count==0)
    {
        fout1<<line<<endl;
        fout1<<"$";
        fout1<<s<<endl;
        fout1<<""<<endl;
        fout1<<""<<endl;
        line+=5;
        cout<<"s: "<<s<<endl;
    }
    pc="0";
    BLOCK["DEFAULT"].num=0;
    BLOCK["DEFAULT"].address=pc;
    BLOCK["DEFAULT"].length="0";
    BLOCK["DEFAULT"].exist='y';
    currBlock="DEFAULT";
    blockNum=1;
    line=5;
    if(word[0]=="START")
    {
        pc=word[1];
        fout1<<line<<endl;
        fout1<<""<<endl;
        fout1<<"START"<<endl;
        fout1<<pc<<endl;
        fout1<<pc<<endl;
        fout1<<""<<endl;
        cout<<"0 is start!"<<endl;
    }
    else if(word[1]=="START")
    {
        pc=word[2];
        fout1<<line<<endl;
        fout1<<word[0]<<endl;
        fout1<<"START"<<endl;
        fout1<<pc<<endl;
        fout1<<pc<<endl;
        fout1<<""<<endl;
        cout<<"1 is start!"<<endl;
    }
    else
        execute(word,count);
    while(true)
    {
        getline(fin1,s);
        extract(s,word,count);
        line+=5;
        cout<<"s: "<<s<<endl;
        fout1<<line<<endl;
        if(count==0)
        {
            cout<<"Comment detected!"<<endl;
            fout1<<"$"<<endl;
            fout1<<s<<endl;
            fout1<<""<<endl;
            fout1<<""<<endl;
            fout1<<""<<endl;
            continue;
        }
        if(word[0]=="END")
        {
            BLOCK[currBlock].length=pc;
            fout1<<""<<endl;
            fout1<<word[0]<<endl;
            fout1<<word[1]<<endl;
            fout1<<pc<<endl;
            fout1<<""<<endl;
            break;
        }
        execute(word,count);
        // cin>>ch;
    }
    hexa addr,len;
    string temp=find_block(0);
    addr=BLOCK[temp].address;
    len=BLOCK[temp].length;
    for(int i=1;i<blockNum;++i)
    {
        temp=find_block(i);
        BLOCK[temp].address=toHex(toDec(addr)+toDec(len));
        addr=BLOCK[temp].address;
        len=BLOCK[temp].length;
        
    }
}
Пример #10
0
void execute(string word[],int count)
{
    cout<<"word[0]: "<<word[0]<<" pc: "<<pc<<endl;
    if(word[0]=="USE")
    {
        cout<<"USE detected!"<<endl;
        BLOCK[currBlock].length=pc;
        if(word[1]=="")
        {
            currBlock="DEFAULT";
            pc=BLOCK["DEFAULT"].length;
        }
        else if(BLOCK[word[1]].exist=='y')
        {
            currBlock=word[1];
            pc=BLOCK[word[1]].length;
        }
        else
        {
            BLOCK[word[1]].num=blockNum;
            BLOCK[word[1]].exist='y';
            BLOCK[word[1]].length="0";
            currBlock=word[1];
            pc="0";
            ++blockNum;
        }
        fout1<<""<<endl;
        fout1<<word[0]<<endl;
        fout1<<word[1]<<endl;
        fout1<<pc<<endl;
        fout1<<""<<endl;
        return;
    }
    if(word[0][0]=='+')
    {
        cout<<"Format 4"<<endl;
        fout1<<""<<endl;
        fout1<<word[0]<<endl;
        fout1<<word[1]<<endl;
        fout1<<pc<<endl;
        pc=toHex(toDec(pc)+4);
        fout1<<pc<<endl;
        return;
    }
    if(OPTAB[word[0]].exist=='y')
    {
        cout<<"0 is opcode!"<<endl;
        fout1<<""<<endl;
        fout1<<word[0]<<endl;
        fout1<<word[1]<<endl;
        fout1<<pc<<endl;
        pc=toHex(toDec(pc)+OPTAB[word[0]].format);
        fout1<<pc<<endl;
        return;
    }
    if(OPTAB[word[0]].exist=='n')
    {
        if(SYMTAB[word[0]].exist=='y')
        {
            error<<"Line "<<line<<": Duplicate Symbol"<<endl;
            errorFlag=1;
        }
        else
        {
            SYMTAB[word[0]].address=pc;
            SYMTAB[word[0]].block=currBlock;
            SYMTAB[word[0]].exist='y';
            fout1<<word[0]<<endl;
            fout1<<word[1]<<endl;
            fout1<<word[2]<<endl;
            fout1<<pc<<endl;
            if(word[1][0]=='+')
                pc=toHex(toDec(pc)+4);
            else if(OPTAB[word[1]].exist=='y')
                pc=toHex(toDec(pc)+OPTAB[word[1]].format);
            else if(word[1]=="WORD")      pc=toHex(toDec(pc)+3);
            else if(word[1]=="RESW")      pc=toHex(toDec(pc)+(atoi(word[2].c_str())*3));
            else if(word[1]=="RESB")      pc=toHex(toDec(pc)+atoi(word[2].c_str()));
            else if(word[1]=="BYTE")
            {
                int len=word[1].length()-3;
                if(word[1][0]=='X') len/=2;
                pc=toHex(toDec(pc)+len);
            }
            else
            {
                error<<"Line "<<line<<": Opcode not found"<<endl;
                errorFlag=1;
            }
            fout1<<pc<<endl;
        }
    }
}
Пример #11
0
void writeOnFile(int ch)
{
	int v, i; s[8] = 0; s[++dc] = ch + '0';
	if(dc==7)
		toDec(), dc = -1;
}
Пример #12
0
int main(int argc, char *argv[]) {
    /*Check if argc < 5*/
    if (argc < 5) {
        fprintf(stderr, "Not enough arguments passed.\n");
        exit(EXIT_FAILURE);        
    }
    if (argc > 6) {
        fprintf(stderr, "Too many arguments passed.\n");
        exit(EXIT_FAILURE);        
    }


    char *op = (char *)malloc(strlen(argv[1]));
    strcpy(op, argv[1]);
    char *setFirst = (char *)malloc(strlen(argv[2]));
    strcpy(setFirst, argv[2]);
    char *setTwo = (char *)malloc(strlen(argv[3]));
    strcpy(setTwo, argv[3]);
    char *outForm = (char *)malloc(strlen(argv[4]));
    strcpy(outForm, argv[4]);
    char *ans = (char*)malloc(sizeof(char));

    int numO, numT, res;
    numO = 0;
    numT = 0;
    res = 0;

    int negO, negT;
    negO = 1;
    negT = 1;

    /*if (strcmp(op, "+") != 0 || strcmp(op, "-") != 0) {
        fprintf(stderr, "Invalid operation!\n");
        exit(EXIT_FAILURE);  
    }*/

    if (setFirst[0] == '-') {
        negO = -1;
        char base = setFirst[1];
        int val = getBase(base);
        char *numOne = (char *)malloc(strlen(setFirst)-2);
        numOne = strncpy(numOne, setFirst+2, strlen(setFirst));
        numO = toDec(numOne, val);
        numO *= negO;
    }
    else {
        char base = setFirst[0];
        int val = getBase(base);
        char *numOne = (char *)malloc(strlen(setFirst)-1);
        numOne = strncpy(numOne, setFirst+1, strlen(setFirst));
        numO = toDec(numOne, val);
    }

    if (setTwo[0] == '-') {
        negT = -1;
        char base = setTwo[1];
        int val = getBase(base);
        char *numTwo = (char *)malloc(strlen(setTwo)-2);
        numTwo = strncpy(numTwo, setTwo+2, strlen(setTwo));
        numT = toDec(numTwo, val);
        numT *= negT;
    }
    else {
        char base = setTwo[0];
        int val = getBase(base);
        char *numTwo = (char *)malloc(strlen(setTwo)-1);
        numTwo = strncpy(numTwo, setTwo+1, strlen(setTwo));
        numT = toDec(numTwo, val);
    }

    if (strcmp(op,"+") == 0) {
        res = add(numO,numT);
    }
    else if (strcmp(op,"-") == 0) {
        res = sub(numO,numT);
    }


    ans = fromDec(res, (*outForm));
    printf("%s\n", ans);
    
    
    return 0;
}