int GraphColoring(FILE* f, Vertex* &v){// output: "Vertex* v" chua cac dinh da duoc to mau hop ly // Creat data from input char* buffer = new char[100]; init(buffer,'\0',100); fgets(buffer,100,f); sscanf(buffer, "%d", &numberEdge); VertexInfo *vertexMap = new VertexInfo[numberEdge]; for(int i = 0; i < numberEdge; i ++){ fgets(buffer,100,f); int current = char2Int(buffer[0] - 'A'); int j = 0; int level = 0; vertexMap[current].name = buffer[0]; while(buffer[j] != '\n') { if(buffer[j] != ' ' && buffer[j] != '\r' && char2Int(buffer[j] - 'A')!=i){ vertexMap[current].connection[char2Int(buffer[j] - 'A')] = true; ++level; } j++; } vertexMap[current].vertexlevel = level; // display(vertexMap[current].connection); } // Coloring graph int current = 0; for(int i = 0 ; i < numberEdge ; i++){ int max = -1; for(int i = 0 ; i < numberEdge ; i++){ if(vertexMap[i].color == -1){ int temp = ArroundColor(vertexMap,i); if( temp > max){ max = temp; current = i; } if( temp == max){ if(vertexMap[i].vertexlevel > vertexMap[current].vertexlevel) current = i; } } } color(vertexMap,current); add(vertexMap[current],v); } // cout<<maxColor; return colorcount; }
int HuffmanCode::unzip(char* inputFile, char* outputFile) { CStopWatch time; time.startTimer(); cout << "unzipping..."<<endl; // Declare variable Writer *writer = new Writer(outputFile); // Writer ref object Reader *reader = new Reader(inputFile); // Reader ref object HCZHeader *header = new HCZHeader; char **codemap; // Map of chareacter code Node* huffmantree = new Node; // huffmantree int totalChar; int totalBit; header->getTotal(totalChar, totalBit); // Check if file is right format if( header->read(reader) != -2){ // Initialize the table of character code codemap = new char*[256]; for(int i = 0; i < 256; i++){ codemap[i] = new char[16]; memset(codemap[i],'\0',16); } for(int i = 0, nbit; i < 256; i++){ header->get((char)i,nbit,codemap[char2Int(i)]); } for(int i = 0; i< 256; i++){ if(codemap[i][0] != '\0'){ convertMaptoHuffman(huffmantree, codemap, (char)i, totalChar); } } int bodysize = header->getBodySize(); while(bodysize){ deCode(reader, writer, huffmantree, bodysize); } } delete reader; delete writer; delete header; cout<<"done!"<<endl; time.stopTimer(); cout<<"Excution time: "<<time.getElapsedTime()<<"s"<<endl; return UN_IMPLEMENT; }
char myModbusRead(char * modbus, char * cmdName0) { if (strlen(modbus) > 8) // checks if Modbus frame is at least as long as the minimum length { if (modbus[0] == ':') // checks if Modbus frame has start character { if ( (modbus[1] == myStationAddress[0]) && (modbus[2] == myStationAddress[1]) ) // checks if Station Address is Correct { int cdl; cdl = 16*char2Int( modbus[strlen(modbus)-4])+char2Int( modbus[strlen(modbus)-3]); // strip length of DATAFIELD from MODBUS frame // myStringPut(outstr); ***DELETE THIS LINE myCharPut(0x0D); myCharPut(0x0A); if ( (strlen(modbus)-9) == cdl) // check if DATAFIELD length is correct { int k1; myStringPut("Modbus is formated correctly"); myNLPut(); for(k1=0;k1<cdl;k1++) // iterate through each character of the DATAFIELD { cmdName0[k1]=modbus[k1+5]; // copy DATAFIELD to string } cmdName0[k1]='\0'; // add NULL terminator at the end myNLPut(); myStringPut("Data Field:"); // Echo command out myStringPut(cmdName0); myNLPut(); // verify checksum bytes if( (checkSumDigit0(checkSum( cmdName0)) == modbus[strlen(modbus)-1]) && (checkSumDigit1(checkSum( cmdName0)) == modbus[strlen(modbus)-2]) ) { return 1; // return 1 on properly formated Modbus frame } } } } } return 0; // return 0 on any violations of Modbus frame }
long long findStartRadix(const char * oriValue,const long long referenceValue){ long long ret; long long num=findDigit(oriValue); if (num <= 1){ ret = -1; } else { long long firstDig = char2Int(oriValue[0]); ret = pow(double(referenceValue)/(firstDig+1),1.0/(num-1)); } return ret; }
long long change(const char * oriValue,const long long myRadix){ long long num = findDigit(oriValue); long long ret=0; long long tmpValue; int tmpChar; for(long long i = 0;i<num;i++){ tmpChar = oriValue[i]; tmpValue = char2Int(tmpChar); ret+= tmpValue * pow(double(myRadix),int(num-i-1)); } return ret; }
long long findMinRadix(const char * oriValue){ long long num=findDigit(oriValue); long long ret = 2; long long tmpValue; long long tmpChar; for(long long i = 0;i<num;i++){ tmpChar = oriValue[i]; tmpValue = char2Int(tmpChar); ret = std::max(tmpValue+1,ret); } return ret; }
static void buildin_tonumber(const vector<LuaValue>& args, vector<LuaValue>& rets) { auto &v = args[0]; if (v.isTypeOf(LVT_Number)) rets.push_back(v); else if (v.isTypeOf(LVT_String)) { int base = 10; if (args.size() > 1) base = (int)args[1].getNumber(); NumberType n = 0; for (const char *str = v.getString()->buf(); *str; ++str) { n *= base; n += char2Int(*str); } rets.push_back(LuaValue(n)); } else rets.push_back(LuaValue::NIL); }
void convertMaptoHuffman(Node*& root, char** codemap,char cha, int totalChar){ static int level = -1; if(totalChar == 1 || codemap[char2Int(cha)][level+1] == '\0' ){ ((ChaNode*)root)->symbol = cha; return; } else{ ++level; if(codemap[char2Int(cha)][level] == '0'){ if(root->left == NULL){ root->left = new ChaNode; } convertMaptoHuffman(root->left , codemap , cha,totalChar); } else{ if(root->right == NULL){ root->right = new ChaNode; } convertMaptoHuffman(root->right, codemap , cha,totalChar); } --level; } }
long long findEndRadix(const char * oriValue,const long long referenceValue){ long long ret; long long num=findDigit(oriValue); long long firstDigNum=0; long long firstDig; for(;firstDigNum<num;firstDigNum++){ firstDig = char2Int(oriValue[firstDigNum]); if (firstDig!=0){ break; } } if (num-firstDigNum <= 1){ ret = -1; } else { ret = pow(double(referenceValue)/(firstDig),1.0/(num-firstDigNum-1)) + 1; } return ret; }
int HuffmanCode::zip(char* inputFile, char* outputFile) { CStopWatch time; time.startTimer(); cout<<"Zipping.."<<endl; // Declare variable Reader *reader = new Reader(inputFile); // Readfile object Writer *writer = new Writer(outputFile); // Writefile object HCZHeader header; // Header object Queue priQueue; // Creat a Queue Node* huffmantree = NULL; // root of Huffman tree char **codemap;// Map of chareacter code char *code; // temp of code int bodysize = 0; // Sum of bit in body int totalcharater = 0; // Sum of character in text int usedcharacter = 0; // Number of character used in txt int totalbit = 0; // Total bit of coding map // Initialize the table of character code codemap = new char*[256]; for(int i = 0; i < 256; i++){ codemap[i] = new char[16]; memset(codemap[i],'\0',strlen(codemap[i])); } // Initialize the temp variable to save encode code = new char[16]; memset(code,'\0',strlen(code)); code[0]='0'; // Count character and add to the Queue while(reader->isHasNext()){ priQueue.freq(reader->readChar()); } delete reader; // Get number of used character usedcharacter = priQueue.getCount(); // Sort the Queue priQueue.sort(); // Get the huffman tree huffmantree = priQueue.creatHuffmanTree(); // Conver huffman tree to code map and get nessessary information convertHuffmanToMap(codemap,huffmantree,code,bodysize,totalcharater,totalbit); // Set important info to Header header.setBodySize(bodysize); header.setTotal(usedcharacter,totalbit); // Set codelist to header for(int i = 0 ; i < 256 ; i++){ if(codemap[i][0] != '\0'){ header.set(i,strlen(codemap[i]),codemap[i]); } } // Writer header to file header.write(writer); // Encode file reader = new Reader(inputFile); char k; int limit; while(reader->isHasNext()){ k = reader->readChar(); limit = strlen(codemap[char2Int(k)]); for(char j = 0 ; j < limit ; j++){ writer->writeBit(codemap[char2Int(k)][char2Int(j)] - '0'); } } delete reader; delete writer; cout<< "Total character encoded: "<<totalcharater<<endl; cout<<"Done!..."<<endl; time.stopTimer(); cout<<"Excution time: "<<time.getElapsedTime()<<"s"<<endl; return SUCCESS; }