bool isNumber(const char *s) { trimSpace(s); int ret = intOrFloat(s); if (ret == 2) { return false; } if (*s == 'e') { s++; if (intOrFloat(s) != 0) { return false; } } trimSpace(s); return *s == 0; }
void Option2() { char String[200]; int i; int count = 0; printf("Your String: "); gets(String); trimSpace(String); for (i=0; i< strlen(String); i++) { if (String[i] == ' ') count++; } count++; printf("There are %d Words", count); }
/** @internal @brief Mine the statements inside a function's body. Look for return statements, and extract their return value so that they can be used to generate \@retval tags later. @param[in,out] buf the tBuffer to process */ static void parseStatement(tBuffer *buf) { char *s,*e,*p; int count; s = buf->statementStart; if (s != NULL) { if (strncmp(s,"return",6) == 0) { s = skipSpace(s+6, buf->ptr); e = trimSpace(buf->ptr, s); /* if the value is surrounded by brackets, remove them */ if (*s == '(') { /* check for situations like (a)+(b) */ p = s; count = 0; while (p < e) { if (*p++ == '(') ++count; } if (count == 1 && e[-1] == ')') { s = skipSpace(&s[1], e); e = trimSpace(&e[-1], s); } } addString(&(buf->retvals),s,e); } buf->statementStart = NULL; } }
bool isNumber(const char *s) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. trimSpace(s); bool f = lastPosition(s); //trimSpace(s); if(!f || (*s != 'e' && *s != '\0' && *s != ' ')) return false; if(*s == 'e') { //trimSpace(++s); const char *temp = ++s; f = lastPosition(s, true); if(!f || *s == *temp) return false; } while(*s) { if(*s != ' ') return false; s++; } return true; }
//Downloads the data (train, validation, test sets) into memory following specifications //in the attr file. //filenames may be empty strings, if correspondent data is not provided INDdata::INDdata(const char* trainFName, const char* validFName, const char* testFName, const char* attrFName, bool doOut) { LogStream clog; //read attr file, collect info about boolean attributes and attrN clog << "Reading the attribute file: \"" << attrFName << "\"\n"; fstream fattr; fattr.open(attrFName, ios_base::in); if(!fattr) throw OPEN_ATTR_ERR; char buf[LINE_LEN]; //buffer for reading from input files getLineExt(fattr, buf); //read list of attributes, collect information about them int attrId, colNo; // counters string tarName; //name of the response attribute bool foundClass = false; //response found flag weightColNo = -1; for(attrId = 0, colNo = 0; fattr.gcount(); attrId++, colNo++) { string attrStr(buf); //a line of an attr file (corresponds to 1 attribute) //check for response attribute if(attrStr.find("(class)") != string::npos) { if(foundClass) throw MULT_CLASS_ERR; tarColNo = colNo; attrId--; foundClass = true; string::size_type nameLen = attrStr.find(":"); tarName = attrStr.substr(0, nameLen); getLineExt(fattr, buf); continue; } if(attrStr.find("(weight)") != string::npos) { weightColNo = colNo; attrId--; getLineExt(fattr, buf); continue; } //parse attr name string::size_type nameLen = attrStr.find(":"); if((attrStr.find("contexts") != -1) || (nameLen == -1)) break; //end of listed attributes string attrName = attrStr.substr(0, nameLen); if(attrName.find_first_of("\\/*?\"<>|:") != string::npos) throw ATTR_NAME_DEF_ERR; attrNames.push_back(trimSpace(attrName)); //parse attr type string::size_type endType = attrStr.find("."); string typeStr = attrStr.substr(nameLen + 1, endType - nameLen - 1); typeStr = trimSpace(typeStr); if(typeStr.compare("0,1") == 0) boolAttrs.insert(attrId); else if(typeStr.compare("nom") == 0) nomAttrs.insert(attrId); else if(attrStr.find("cont") == string::npos) throw ATTR_TYPE_ERR; getLineExt(fattr, buf); } attrN = attrId; colN = colNo; if(!foundClass) throw NO_CLASS_ERR; //read contexts part (if any), add unused attributes into ignoreattrs while(fattr.gcount()) { string attrStr(buf); if(attrStr.find(" never") != string::npos) {//extract name of the attribute, find its number, insert it into ignoreattrs int nameLen = (int)attrStr.find(" "); string attrName = attrStr.substr(0, nameLen); attrName = trimSpace(attrName); int neverAttrId = getAttrId(attrName); if (neverAttrId == -1) clog << "\nWARNING: trying to exclude \"" << attrName << "\" - this is not a valid feature\n\n"; else ignoreAttrs.insert(neverAttrId); } getLineExt(fattr, buf); } fattr.close(); int activeAttrN = attrN - (int)ignoreAttrs.size(); clog << attrN << " attributes\n" << activeAttrN << " active attributes\n\n"; if(!isSubset(nomAttrs, ignoreAttrs)) throw NOM_ACTIVE_ERR; //Read data if(string(trainFName).compare("") != 0) {//Read train set clog << "Reading the train set: \"" << trainFName << "\"\n"; fstream fin; fin.open(trainFName, ios_base::in); if(fin.fail()) throw OPEN_TRAIN_ERR; hasMV = false; getLineExt(fin, buf); int caseNo; for(caseNo = 0; fin.gcount(); caseNo++) {//read one line of data file, save class value in targets, attribute values in data if(doOut && ((caseNo + 1)% 100000 == 0)) cout << "\tRead " << caseNo + 1 << " lines..." << endl; floatv item; //single data point try { readData(buf, fin.gcount(), item, colN); } catch (TE_ERROR err) { cerr << "\nLine " << caseNo + 1 << "\n"; throw err; } trainTar.push_back(item[tarColNo]); if(weightColNo != -1) trainW.push_back(item[weightColNo]); item.erase(item.begin() + max(tarColNo, weightColNo)); if(weightColNo != -1) item.erase(item.begin() + min(tarColNo, weightColNo)); for(intset::iterator boolIt = boolAttrs.begin(); boolIt != boolAttrs.end(); boolIt++) if((item[*boolIt] != 0) && (item[*boolIt] != 1) && !wxisNaN(item[*boolIt])) throw ATTR_NOT_BOOL_ERR; train.push_back(item); getLineExt(fin, buf); } trainN = caseNo; trainV = trainN; if(trainN == 0) throw TRAIN_EMPTY_ERR; if(weightColNo != -1) { double trainSum = 0; trainR.resize(trainN); for(int itemNo = 0; itemNo < trainN; itemNo++) trainSum += trainW[itemNo]; double trCoef = trainN / trainSum; for(int itemNo = 0; itemNo < trainN; itemNo++) { trainW[itemNo] *= trCoef; trainR[itemNo] = (itemNo == 0) ? trainW[itemNo] : trainW[itemNo] + trainR[itemNo - 1]; } } double trainStD = getTarStD(TRAIN); clog << trainN << " points in the train set, std. dev. of " << tarName << " values = " << trainStD << "\n\n"; fin.close(); //initialize bootstrap (bag of data) bootstrap.resize(trainN); newBag(); } else //no train set trainN = 0; if(string(validFName).compare("") != 0) {//Read validation set clog << "Reading the validation set: \"" << validFName << "\"\n"; fstream fvalid; fvalid.open(validFName, ios_base::in); if(fvalid.fail()) throw OPEN_VALID_ERR; getLineExt(fvalid, buf); int caseNo; for(caseNo=0; fvalid.gcount(); caseNo++) {//read one line of data file, save response value in validtar, attributes values in valid if (doOut && ((caseNo + 1) % 100000 == 0)) cout << "\tRead " << caseNo + 1 << " lines..." << endl; floatv item; //single data point try { readData(buf, fvalid.gcount(), item, colN); } catch (TE_ERROR err) { cerr << "\nLine " << caseNo + 1 << "\n"; throw err; } validTar.push_back(item[tarColNo]); if(weightColNo != -1) validW.push_back(item[weightColNo]); item.erase(item.begin() + max(tarColNo, weightColNo)); if(weightColNo != -1) item.erase(item.begin() + min(tarColNo, weightColNo)); valid.push_back(item); getLineExt(fvalid, buf); } validN = caseNo; if(validN == 0) throw VALID_EMPTY_ERR; double validStD = getTarStD(VALID); clog << validN << " points in the validation set, std. dev. of " << tarName << " values = " << validStD << "\n\n"; fvalid.close(); } else //no validation set validN = 0; if(string(testFName).compare("") != 0) {//Read test set clog << "Reading the test set: \"" << testFName << "\"\n"; fstream ftest; ftest.open(testFName, ios_base::in); if(ftest.fail()) throw OPEN_TEST_ERR; getLineExt(ftest, buf); int caseNo; for(caseNo=0; ftest.gcount(); caseNo++) {//read one line of data file, save response value in testtar, attributes in test if (doOut && ((caseNo + 1) % 100000 == 0)) cout << "\tRead " << caseNo + 1 << " lines...\n"; floatv item; //single data point try { readData(buf, ftest.gcount(), item, colN); } catch (TE_ERROR err) { cerr << "\nLine " << caseNo + 1 << "\n"; throw err; } testTar.push_back(item[tarColNo]); if(weightColNo != -1) testW.push_back(item[weightColNo]); item.erase(item.begin() + max(tarColNo, weightColNo)); if(weightColNo != -1) item.erase(item.begin() + min(tarColNo, weightColNo)); test.push_back(item); getLineExt(ftest, buf); } testN = caseNo; double testStD = getTarStD(TEST); clog << testN << " points in the test set, std. dev. of " << tarName << " values = " << testStD << "\n\n"; ftest.close(); } else //no test set testN = 0; }
/** @internal @brief Turns a type declaration into a name and descriptive type string. This function is used on both the function name and each of its arguments. It also makes an educated guess about whether the type is input only, or may be used to modify the argument within the function. @note isStaticP and inOnlyP will only be set if type is non-NULL @param[out] type a buffer to fill with the type description @param[in] size space available in the type buffer @param[out] isStaticP set according to whether the type is static @param[out] inOnlyP set according to educated guess about an argument's direction (true if pass by value or const, false otherwise). @param[in,out] name On input, the range containing the type declaration to process. On output, shrunk to only contain the identifier. @see processArgList() @see processFunction() @todo doesn't handle C++–style references */ static void processTyped( char *type, size_t size, bool *isStaticP, bool *inOnlyP, tRange *name ) { char *p; char *s = skipSpace(name->start,name->end); char *e = trimSpace(name->end, name->start); int ptrCount = 0; size_t remaining = size; bool loop = true; bool isArray = false; bool isStatic = false; bool isConst = false; bool inOnly; /* if [] is present, flag it and remove */ --e; if (*e == ']') { isArray = true; do { --e; } while (e >= s && *e != '['); if (e > s) --e; } p = e; ++e; /* isolate the actual parameter name */ while (p >= s && (isalnum(*p) || *p == '_')) { --p; } p += 1; name->start = p; name->end = e; /* only do the following if asked */ if (type != NULL && size > 0) { *type = '\0'; while (loop) { /* strip off 'static' if it's present */ if (strncmp(s,"static",6) == 0) { isStatic = true; s = skipSpace(s+6,e); } else if (strncmp(s,"const",5) == 0) { isConst = true; s = skipSpace(s+5,e); } else loop = false; } if (isStaticP != NULL) *isStaticP = isStatic; e = trimSpace(p,s); --e; /* because it normally points after the actual character */ while (e > s && *e == '*') { ++ptrCount; --e; while (e > s && isspace(*e)) { --e; } } ++e; /* put it back to normal */ /* guess if the parameter cannot be modified by the function (basically if it's const, or pass by value). Very easily fooled by typdefs and #defines */ inOnly = (bool)(isConst || (ptrCount == 0 && !isArray)); if (inOnlyP != NULL) *inOnlyP = inOnly; while (ptrCount--) { strncat(type, "a pointer to ", remaining); remaining -= 13; /* length of "pointer to " */ } if (isArray) { strncat(type, "an array of ", remaining); remaining -= 12; /* length of "an array of " */ } #ifdef qTypePrefixedWithA else { if (!isConst && strchr("aeiouAEIOU",*s) != NULL) { strncat(type, "an ", remaining); remaining -= 3; } else { strncat(type, "a ", remaining); remaining -= 2; } } #endif if (isConst) { strncat(type, "const ", remaining); remaining -= 6; /* length of "const " */ } /* output the type */ if ((size_t)(e - s) < remaining) { remaining = (e - s); } strncat(type, s, remaining); } }
int main(void) { char buf[1024]; char* ptr; SGSet* set = sgSetCreate(_setCmp, NULL); printf("Example of SIEGE sets"); printf("----------------------------------------\n"); int option; do { printf("Select a set operation:\n"); printf("+<elem> -- Add a new element\n"); printf("-<elem> -- Delete an element\n"); printf("?<elem> -- Query whether an element is present\n"); printf("p -- Print all elements in (strcmp) order\n"); printf("pp -- Pretty-print the set's search tree\n"); printf("s -- Print the set's tree in a compact s-expression form\n"); // yes, I was bored. printf("ss -- Pretty print the set's tree in a compact s-expression form\n"); // ...very bored. printf("\n"); printf("q -- Quit the program\n"); printf("----------------------------------------\n"); printf("Selection: "); fflush(stdout); fgets(buf, sizeof(buf), stdin); printf("\n"); trimNL(buf); if(strstr(buf, "+") == buf) option = '+'; else if(strstr(buf, "-") == buf) option = '-'; else if(strstr(buf, "?") == buf) option = '?'; else if(!strcmp(buf, "p") || !strcmp(buf, "P")) option = 'p'; else if(!strcmp(buf, "pp") || !strcmp(buf, "pP") || !strcmp(buf, "Pp") || !strcmp(buf, "PP")) option = 'P'; else if(!strcmp(buf, "s") || !strcmp(buf, "S")) option = 's'; else if(!strcmp(buf, "ss") || !strcmp(buf, "sS") || !strcmp(buf, "Ss") || !strcmp(buf, "SS")) option = 'S'; else if(!strcmp(buf, "q") || !strcmp(buf, "Q")) option = 'q'; else option = 0; ptr = skipSpace(buf + 1); switch(option) { case '+': if(!strlen(ptr)) { printf("Error -- element must be a non-empty string!\n"); break; } switch(insertItem(set, ptr)) { case 0: printf("Element '%s' already exists in the set!\n", ptr); break; case 1: printf("Element '%s' successfully added!\n", ptr); break; default: printf("Error adding element '%s' -- probably out of memory!\n", ptr); } break; case '-': if(!strlen(ptr)) { printf("Error -- element must be a non-empty string!\n"); break; } switch(removeItem(set, ptr)) { case 0: printf("There is no element '%s' in the set!\n", ptr); break; case 1: printf("Element '%s' successfully removed!\n", ptr); break; default: printf("Error removing element '%s'!\n", ptr); } break; case '?': if(!strlen(ptr)) { printf("Error -- element must be a non-empty string!\n"); break; } if(sgSetSearch(set, ptr)) printf("Element '%s' is present in the set!\n", ptr); else printf("Element '%s' is *NOT* present in the set!\n", ptr); break; case 'p': printSet(set); break; case 'P': pprintSet(set); break; case 's': sprintSet(set, 0); break; case 'S': sprintSet(set, 1); break; case 'q': break; /* handled by the while() */ default: printf("Invalid option '%s'!\n", trimSpace(buf)); break; } printf("\n"); fflush(stdout); } while(option != 'q'); while(set->root) removeItem(set, set->root->item); sgSetDestroy(set); return 0; }