FileSkeleton::FileSkeleton(const std::string &filename) { ifstream strm(filename.c_str()); if(!strm.is_open()) { std::cout << "Error opening file " << filename << endl; return; } while(!strm.eof()) { vector<string> line = readWords(strm); if(line.size() < 5) continue; //error Vector3 p; sscanf(line[1].c_str(), "%lf", &(p[0])); sscanf(line[2].c_str(), "%lf", &(p[1])); sscanf(line[3].c_str(), "%lf", &(p[2])); if(line[4] == "-1") line[4] = std::string(); makeJoint(line[0], p * 2., line[4]); } initCompressed(); }
void Mesh::readObj(istream &strm) { int i; int lineNum = 0; while(!strm.eof()) { ++lineNum; vector<string> words = readWords(strm); if(words.size() == 0) continue; if(words[0][0] == '#') //comment continue; if(words[0].size() != 1) //unknown line continue; //deal with the line based on the first word if(words[0][0] == 'v') { if(words.size() != 4) { Debugging::out() << "Error on line " << lineNum << endl; OUT; } double x, y, z; sscanf(words[1].c_str(), "%lf", &x); sscanf(words[2].c_str(), "%lf", &y); sscanf(words[3].c_str(), "%lf", &z); vertices.resize(vertices.size() + 1); vertices.back().pos = Vector3(x, y, z); } if(words[0][0] == 'f') { if(words.size() < 4 || words.size() > 15) { Debugging::out() << "Error on line " << lineNum << endl; OUT; } int a[16]; for(i = 0; i < (int)words.size() - 1; ++i) sscanf(words[i + 1].c_str(), "%d", a + i); //swap(a[1], a[2]); //TODO:remove for(int j = 2; j < (int)words.size() - 1; ++j) { int first = edges.size(); edges.resize(edges.size() + 3); edges[first].vertex = a[0] - 1; edges[first + 1].vertex = a[j - 1] - 1; edges[first + 2].vertex = a[j] - 1; } } //otherwise continue -- unrecognized line } }
int main() { int n ; char **wordArray ; wordArray = readWords( &n ) ; outputWords(wordArray, n) ; return 0; }
/* olusturulan thread e gonderilern void pointer fonksiyon */ void* threadOperation(void* data) { sem_wait(&sem);/*mutex locked*/ clock_t start; double duration; start = clock(); char *szFullName=(char*)data; int boyutX,boyutY,total; char **arr,**words; int fdx; /*total words sayisi bulunup toplama a eklenir*/ printf("Thread_id: %lu\n", pthread_self()); boyutX= getLineNum(data); //printf(" line %d\n",boyutX); boyutY = getBoyut(data); arr=Make2DintArray(boyutX+1,boyutY+1); /*dosya okunur */ readData(data,arr,boyutX); total=countWordsInArray(arr,boyutX); /* Thread resource lari geri birakilir */ totalWords=totalWords+total; fprintf(stderr,"%s inside total words %d\n",(char*)data,total); readData(data,arr,boyutX); words = readWords(arr,boyutX,total); addWordsFile(words,total); freeArray(arr,boyutX+1); freeArray(words,total); //pthread_detach(pthread_self()); duration = ( clock() - start ) / (double) CLOCKS_PER_SEC; printf("duration time is %f\n",duration); sem_post(&sem);/*mutex acilir*/ //pthread_exit(NULL); return NULL; }
int main(void) { int word_len[WORD_MAX_LENGTH]; int overflow; overflow = 0; zeroArray(word_len, WORD_MAX_LENGTH); readWords(word_len, WORD_MAX_LENGTH, &overflow); draw_horizontal_histogram(word_len, WORD_MAX_LENGTH); printf("\n -------------------------------- \n\n"); draw_vertical_histogram(word_len, WORD_MAX_LENGTH); printf("num of overflow: %d\n", overflow); return 0; }
int parallelPalindrome(char * fileName) { char words [MAX_WORDS][MAX_WORD_LENGTH]; int wordsNumber = readWords(fileName, words); int i; for(i = 0; i < wordsNumber; i++) { launchWordHandler(i, words[i]); } for(i = 0; i < wordsNumber; i++) { fd_set readfds; FD_ZERO(&readfds); FD_SET(pipefdHandlersOutput[i], &readfds); struct timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 0; int selectResult = select(pipefdHandlersOutput[i] + 1, &readfds, NULL, NULL, &timeout); if (selectResult == -1) { printf("Select failed\n"); exit(1); } FILE * fp = fdopen(pipefdHandlersOutput[i], "r"); int answer; fscanf(fp, "%d", &answer); if (answer == 1) { printf("%s is palindrome\n", words[i]); } else { printf("%s is not palindrome\n", words[i]); } } }
int main(int argc, char *argv[]) { // Usage message if (argc < 3) { usage_msg(); return EXIT_FAILURE; } int mode; // mode of output char *a, *b; if (strcmp("components",argv[2]) == 0) { mode = MODE_COMPONENTS; } else if (strcmp("degrees",argv[2]) == 0) { mode = MODE_DEGREES; } else if (strcmp("path",argv[2]) == 0) { mode = MODE_PATH; } else if (strcmp("stats",argv[2]) == 0) { mode = MODE_STATS; } else if (strcmp("diameter",argv[2]) == 0) { mode = MODE_DIAMETER; } else { usage_msg(); } if (mode & MODE_PATH) { if (argc < 5 || strlen(argv[3]) < 3 || strlen(argv[4]) < 3) { fprintf(stderr, "Please specify two 3-letter words\nExample: 3words path bus car\n"); return EXIT_FAILURE; } a = argv[3]; b = argv[4]; if (a[0]<'a'||a[0]>'z'||a[1]<'a'||a[1]>'z'||a[2]<'a'||a[2]>'z'||b[0]<'a'||b[0]>'z'||b[1]<'a'||b[1]>'z'||b[2]<'a'||b[2]>'z') { fprintf(stderr, "Please only use lower case 3-letter words\nExample: 3words path bus car\n"); return EXIT_FAILURE; } } // Open file FILE *f_input = fopen(argv[1], "r"); if (!f_input) { fprintf(stderr, "File could not be opened: %s\n", argv[1]); return EXIT_FAILURE; } NODE nodes[LEN3]; init(nodes); // Read lines INT wordCount = readWords(nodes, f_input); fclose(f_input); // Init navigation structure on the nodes initNav(nodes, 0); initNav(nodes, 1); initNav(nodes, 2); INT componentCount = findComponents(nodes, mode & MODE_COMPONENTLIST); if (mode & MODE_COMPONENTSTAT) printf("%d components\n", componentCount - 1); calcDegrees(nodes, mode & MODE_DEGREESTAT, mode & MODE_DEGREELIST); if (mode & MODE_PATH) shortestPath(nodes, a[0]-'a',a[1]-'a',a[2]-'a',b[0]-'a',b[1]-'a',b[2]-'a', TRUE); if (mode & MODE_DIAMETER) diameter(fopen(argv[1], "r"), wordCount, nodes); }
/** Read single word from a 16-bit device register. * @param useSPI true : use SPI * @param devAddr I2C slave device address * @param regAddr Register regAddr to read from * @param data Container for word value read from device * @return Status of read operation (true = success) */ mraa_result_t I2Cdev::readWord (uint8_t devAddr, uint8_t regAddr, uint16_t *data) { return readWords (devAddr, regAddr, 1, data); }
/** Read single word from a 16-bit device register. * @param useSPI true : use SPI * @param devAddr I2C slave device address * @param regAddr Register regAddr to read from * @param data Container for word value read from device * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ int8_t I2Cdev::readWord(bool useSPI, uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) { return readWords(useSPI, devAddr, regAddr, 1, data, timeout); }
/* Pobiera tekst ze standardowego wejscia, justuje w dwoch kolumnach i wysyla na standardowe wejscie. Wymaga 4 parametrow: 1: dlugosc lewego marginesu, 2: dlugosc lewej kolumny (niezerowa), 3: dlugosc odstepu miedzy kolumnami, 4: dlugosc prawej kolumny (niezerowa). Suma wartosci parametrow nie moze byc wieksza niz ustawiona dlugosc wiersza konsoli (stala LineLength). Slowa musza miescic sie w przydzielonych im kolumnach. W przeciwnym razie wykonywanie programu zostaje przerwane. Wyrazy pobierane sa az do napotkania znaku konca pliku (EOF). */ int main (int argc, char *argv[]) { /* Lista wierszy. */ LinesHandler *Lines; int /* Zrzutowane wartosci parametrow programu. */ Margin, S1, Distance, S2, /* Wynik testu poprawnosci sprawdzanej liczby wierszy. */ LinesNoCorrectness, /* Okresla czy kolumny sie "spotkaly" (podczas wypelniania ich wyrazami). */ LinesMet = 0, /* Ostateczna liczba kolumn <= MaxLinesNo - warunek poprawnosci bedzie spr. dla l. wierszy = MaxLinesNo i mniejszych. */ MaxLinesNo = 0, Iterator; /* Lista wyrazow. */ WordsHandler *Words; /* Alokowanie pamieci na liste wyrazow. */ Words = (WordsHandler*) malloc (sizeof (WordsHandler)); if (Words == NULL) { printf ("Memory allocation error!\n"); return 0; } Words->First = NULL; Words->Last = NULL; /* Alokowanie pamieci na liste wierszy. */ Lines = (LinesHandler*) malloc (sizeof (LinesHandler)); if (Lines == NULL) { printf ("Memory allocation error!\n"); free (Words); return 0; } Lines->First = NULL; /* Sprawdzenie czy podano wymagana liczbe paramentrow. */ if (argc != 5) { printf ("Invalid number of parameters!\n"); return 0; } /* Przypisanie wartosci parametrcw do zmiennych. */ Margin = (int) atoi (argv [1]); S1 = (int) atoi (argv [2]); Distance = (int) atoi (argv [3]); S2 = (int) atoi (argv [4]); /* Sprawdzenie czy wartosci parametrow spelniaja przypisane im wymagania. */ if (S1 == 0 || S2 == 0) { printf ("Lines' length mustn't be egual to 0!\n"); return 0; } else if (Margin < 0 || S1 < 0 || Distance < 0 || S2 < 0) { printf ("None parameter can be less than 0!\n"); return 0; } else if (Margin + S1 + Distance + S2 > LineLength) { printf ("Margin + Col 1st length + Distance + Col 2nd length must be less or equal to %d!\n", LineLength); return 0; } /* Wczytuje slowa i sprawdza, czy mozliwe jest dla nich wykonanie kodu (slowa mieszcza sie w kolumnach etc.). */ if (!readWords (Words, S1, S2)) { freeMemory (Lines, Words); return 0; } /* Obsluga sytuacji gdy wprowadzony tekst to pojednyczy wyraz. */ if (Words->First->Next == NULL) { if (Words->First->Length <= S1) { for (Iterator = 0; Iterator < Margin; Iterator++) printf (" "); printf ("%s\n", Words->First->Content); } else printf ("The word is too long!"); freeMemory (Lines, Words); return 0; } /* Obliczanie gornego ograniczenia dla szukanej liczby wierszy. */ MaxLinesNo = calculateMaxLinesNo (Words, S1, S2); do { LinesNoCorrectness = checkColumnsCorrectness (Words, S1, S2, MaxLinesNo); switch (LinesNoCorrectness) { /* Dla podanych danych co najmniej jeden z wyrazow nie miesci sie w wyznaczonym miejscu. */ case -2: printf ("At least one of words was too long!\n"); freeMemory (Lines, Words); return 0; /* Nie istnieje rozwiazanie spelniajace podane zalozenia. */ case -1: printf ("Proper justification wasn't possible!\n"); freeMemory (Lines, Words); return 0; /* Podana liczba wierszy jest zbyt wysoka - nalezy sprawdzix nizsze wartosci. */ case 0: MaxLinesNo--; continue; /* Znaleziono szukano liczbe wierszy - przygotowanie kolumn i wyswietleniee wynikow. */ case 1: /* Czasem zachodzi sytuacja gdy funkcja sprawdzajaca daje poprawne wyniki dla wiecej niz 1 wartosci. Wtedy wybierana jest najmniejsza. */ while (checkColumnsCorrectness (Words, S1, S2, MaxLinesNo-1) == 1) MaxLinesNo--; if (makeColumns (Lines, Words, S1, S2, MaxLinesNo)) displayResults (Lines, Margin, Distance); else printf ("Memory allocation error!\n"); break; } } while (LinesNoCorrectness == 0); /* Poki liczba wierszy jest zbyt duza, nalezy ja zmniejszac. Znalezienie rozwiazania (lub stwierdzenie jego braku) przeywa petle. */ /* Zwalnia pamiec przed zakonczeniem programu. */ freeMemory (Lines, Words); return 0; }
void Mesh::readGts(istream &strm) { int i; int lineNum = 0; bool outOfHeader = false; int vertsLeft = -1; int edgesLeft = -1; vector<pair<int, int> > fedges; while(!strm.eof()) { ++lineNum; vector<string> words = readWords(strm); if(words.size() == 0) continue; if(words[0][0] == '#') //comment continue; if(!outOfHeader) { //look for number of verts if(words.size() < 3) //not "vertices faces 0" continue; sscanf(words[0].c_str(), "%d", &vertsLeft); sscanf(words[1].c_str(), "%d", &edgesLeft); outOfHeader = true; continue; } //if there are verts left, current line is a vertex if(vertsLeft > 0) { --vertsLeft; if(words.size() < 3) { Debugging::out() << "Error on line " << lineNum << endl; OUT; } double x, y, z; sscanf(words[0].c_str(), "%lf", &x); sscanf(words[1].c_str(), "%lf", &y); sscanf(words[2].c_str(), "%lf", &z); vertices.resize(vertices.size() + 1); vertices.back().pos = Vector3(-x, z, y); continue; } if(edgesLeft > 0) { --edgesLeft; if(words.size() != 2) { Debugging::out() << "Error (edge) on line " << lineNum << endl; OUT; } int e1, e2; sscanf(words[0].c_str(), "%d", &e1); sscanf(words[1].c_str(), "%d", &e2); fedges.push_back(make_pair(e1 - 1, e2 - 1)); continue; } //otherwise it's a face if(words.size() != 3) { Debugging::out() << "Error on line " << lineNum << endl; OUT; } int a[3]; for(i = 0; i < 3; ++i) { sscanf(words[i].c_str(), "%d", a + i); --(a[i]); //indices in file are 1-based } int first = edges.size(); edges.resize(edges.size() + 3); for(i = 0; i < 3; ++i) { int ni = (i + 1) % 3; if(fedges[a[i]].first == fedges[a[ni]].first) edges[first + i].vertex = fedges[a[i]].first; else if(fedges[a[i]].first == fedges[a[ni]].second) edges[first + i].vertex = fedges[a[i]].first; else if(fedges[a[i]].second == fedges[a[ni]].first) edges[first + i].vertex = fedges[a[i]].second; else if(fedges[a[i]].second == fedges[a[ni]].second) edges[first + i].vertex = fedges[a[i]].second; } //otherwise continue -- unrecognized line } }
void Mesh::readOff(istream &strm) { int i; int lineNum = 0; bool outOfHeader = false; int vertsLeft = -1; while(!strm.eof()) { ++lineNum; vector<string> words = readWords(strm); if(words.size() == 0) continue; if(words[0][0] == '#') //comment continue; if(!outOfHeader) { //look for number of verts if(words.size() < 3) //not "vertices faces 0" continue; sscanf(words[0].c_str(), "%d", &vertsLeft); outOfHeader = true; continue; } //if there are verts left, current line is a vertex if(vertsLeft > 0) { --vertsLeft; if(words.size() < 3) { Debugging::out() << "Error on line " << lineNum << endl; OUT; } double x, y, z; sscanf(words[0].c_str(), "%lf", &x); sscanf(words[1].c_str(), "%lf", &y); sscanf(words[2].c_str(), "%lf", &z); vertices.resize(vertices.size() + 1); vertices.back().pos = Vector3(x, y, z); continue; } //otherwise it's a face if(words.size() != 4) { Debugging::out() << "Error on line " << lineNum << endl; OUT; } int a[3]; for(i = 0; i < 3; ++i) sscanf(words[i + 1].c_str(), "%d", a + i); int first = edges.size(); edges.resize(edges.size() + 3); for(i = 0; i < 3; ++i) { edges[first + i].vertex = a[i]; //indices in file are 0-based } //otherwise continue -- unrecognized line } }
/** Read single word from a 16-bit device register. * @param devAddr I2C slave device address * @param regAddr Register regAddr to read from * @param data Container for word value read from device * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) { return readWords(devAddr, regAddr, 1, data, timeout); }