vector<string> findMissingRanges(int A[], int n, int lower, int upper) { vector<string> result; int prev = lower - 1; for (int i = 0; i < n; i++) { checkNum(A[i], prev, result); } // upper checkNum(upper + 1, prev, result); return result; }
int rearrangeArray(int A[], int n, bool (*checkNum)(int)) { int left = 0; int right = n - 1; while(left < right) { while(left < right && checkNum(A[left])) { left++; } while(left < right && !checkNum(A[right])) { right--; } swap(A[left], A[right]); } return 0; }
Token Parser::nextToken(void) { Token token; // Inicialmente marcamos como token no válido token.type = TK_NOTK; token.value = 0; if (m_tokenizer.hasMoreTokens()) { // De momento marcamos como cadena y obtenemos esta del tokenizador token.type = TK_STR; token.string = m_tokenizer.nextToken(); // Comprobamos si se trata de un número if (m_detect_numbers) { checkNum(token); } checkReservedWords(token); } else { // Si no quedan tokens, devolvemos el token fin de fichero token.type = TK_EOF; } return token; }
void Me7SegmentDisplay::display(float value) { int i=0; bool isStart = false; int index = 0; int8_t disp[]= {0,0,0,0}; bool isNeg = false; if(value<0) { isNeg = true; value = -value; disp[0] = 0x20; index++; } for(i=0; i<7; i++) { int n = checkNum(value,3-i); if(n>=1||i==3) { isStart=true; } if(isStart) { if(i==3) { disp[index]=n+0x10; } else { disp[index]=n; } index++; } if(index>3) { break; } } display(disp); }
int main(int argc, char* argv[]) { string num1("9"); string num2("0019"); if(argc == 3) { num1 = argv[1]; num2 = argv[2]; } eliminateHeadZeros(num1); eliminateHeadZeros(num2); cout << "num1: " << num1 << endl; cout << "num2: " << num2 << endl; if(num1 == "" || num2 == "" || num1 == "+" || num1 == "-" || num2 == "+" || num2 == "-") { cout << "num1 == \"\" or num2 == \"\"" << endl; return -1; } if(checkNum(num1) || checkNum(num2)) { cout << "Num1 or Num2 illegal" << endl; return -1; } //Add string addResult = addBigNum(num1, num2); cout << num1 << " + " << num2 << " = " << addResult << endl; //Compare cout << num1; int compare = compareBigNum(num1, num2); if(compare == 0) { cout << " = "; } else if(compare < 0) { cout << " < "; } else { cout << " > "; } cout << num2 << endl; //Sub string subResult = subBigNum(num1, num2); eliminateHeadZeros(subResult); cout << num1 << " - " << num2 << " = " << subResult << endl; return 0; }
//功能:除#define命令外逐条命令检测魔鬼数字 void ghostNum() { int i; string temp = ""; for(i=0; i<cmdCount; i++) { temp = command[i]; if(!checkDef(temp)) // 如果不是#define语句,则需要判断是否含有魔鬼数字 { checkNum(temp, i+1); // 检查该条命令中的魔鬼数字 } } }
int main(void) { int t, m, n, lim, isp, i, j, sum = 0, d, c = 0; populate(); i = 0; while(primes[i]<10)i++; for( ; i < np ; i++ ){ if(checkNum(primes[i])){ c++; sum+=primes[i]; //printf("%d\n", primes[i]); } } //printf("%d\n", c); printf("%d", sum); return 0; }
int main(void){ int i, j, x, m = 0, k, l; for(i = 1; i <= 9; i++) for( j = 1; j <= 9; j++) if(j != i) for( k = 1; k <= 9; k++) if(k!=i && k!= j ) for( l = 1; l <= 9; l++) if(l !=i && l!=j && l != k){ x = checkNum(1000*i + 100*j + 10*k + l); if(x){ printf("Hello! %d\n", 1000*i + 100*j + 10*k + l); m = max(m, getVal(1000*i + 100*j + 10*k + l, x)); } } printf("%d\n", m); return 0; }
int game() { int blow=0,hit=0,checknum=0,count=0, i=0; int check,blowh,hith=0,counth=0; char human[10],com[10]; srand((unsigned) time(NULL)); resetNum(); generateNumber(com); while (checknum == 0 && hith!=4) { if (count!=0) { if (trynum()) { printf("入力された値に誤りがあります.最初からやり直します.\n\n"); return 1; } } count++; printf("%d回目\n",count); printf("あなたの番です。数値を入力してください。\n"); do { printf(" 数値 : "); scanf("%s", human); check=checkString(human); } while (check==1); hith = countHit(human,com); blowh = countBlow(human,com); printf(" Hit : %d\n",hith); printf(" Blow : %d\n",blowh); if (hith==4) break; printf("わたしの番です。"); printf("%d%d%d%dのヒット数とブロー数を入力してください.\n",numex[0],numex[1],numex[2],numex[3]); do { printf(" Hit : "); scanf("%d",&hit); printf(" Blow : "); scanf("%d",&blow); } while (!(hit<5 && blow<5)); checkHandB(hit,blow); checknum = checkNum(); } if (checknum !=0){ if (checknum == 1) { printf("入力された値に誤りがあります.最初からやり直します.\n\n"); return 1; } else { printf("私の勝ちです。"); if (checknum < 1000) { printf("答えは0%dです。\n",checknum); } else { printf("答えは%dです。\n",checknum); } return 0; } } else { printf("あなたの勝ちです。%d回の試行で一致しました。\n",count); return 0; } }
void ZCsl::interpret (Function * aFunc) { ZFUNCTRACE_DEVELOP ("ZCsl::interpret(Function* aFunc)"); ZException *excpt = 0; PackedInstruction *loAddr = aFunc->iPcode; PackedInstruction *hiAddr = loAddr + aFunc->iPsize - 1; PackedInstruction *i = loAddr; ZString v2; Variable *v; double d2; long i2; ZBoolean head (zTrue); while (zTrue) { try { if (iTraceMode & traceCode) { if (head) { trace ('#'); trace ('#', funcHeader (aFunc).constBuffer ()); trace ('#'); trace ('#', "address opcode parameter tos tos-1"); trace ('#', "-------- ------ -------------------- -------------------- --------------------"); head = zFalse; } // if trace ('#', dumpInstr (i - aFunc->iPcode, i, zTrue).constBuffer ()); } switch (i->iCode) { case cdOpen: aFunc->openBlock (i->iText); i++; break; case cdClose: aFunc->closeBlock (); i++; break; case cdSize: push (aFunc->findVar (pop ())->iSize); i++; break; case cdRSize: { ZString varName (pop ()); aFunc->findVar (pureVarName (varName))->resize (varName); i++; break; } case cdTrace: if (iTraceMode & traceMsgs) trace ('>', pop ().constBuffer ()); else pop (); i++; break; case cdExist: { ZString ret (One); try { Variable *var = aFunc->findVar (pop (), zFalse); if (!var || var->iIsExtern || var->iIndex < 0 || var->iIndex >= var->size ()) ret = Zero; } // try catch (const ZException & exc) { ret = Zero; } // catch push (ret); i++; break; } case cdNeg: checkNum (*iTos); *iTos = ZString (-iTos->asDouble ()); i++; break; case cdNot: if (isNumber (iTos->constBuffer ())) *iTos = iTos->asDouble ()? Zero : One; else *iTos = *iTos == "" ? One : Zero; i++; break; case cdLoad: *iTos = aFunc->findVar (*iTos)->value (); i++; break; case cdStore: { Variable *var = aFunc->findVar (pop ()); var->set (pop ()); i++; break; } case cdStorC: { Variable *var = aFunc->findVar (pop ()); var->set (pop (), zTrue); i++; break; } case cdPush: push (decodeString (i->iText)); i++; break; case cdPop: pop (); i++; break; case cdDupl: push (*iTos); i++; break; case cdDivI: i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (*iTos); *iTos = ZString (iTos->asInt () / i2); i++; break; case cdMod: i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (*iTos); *iTos = ZString (iTos->asInt () % i2); i++; break; case cdMul: v2 = pop (); checkNum (v2); checkNum (*iTos); *iTos = ZString (iTos->asDouble () * v2.asDouble ()); i++; break; case cdDiv: d2 = pop ().asDouble (); if (!d2) throwExcept (msgDivZero); checkNum (*iTos); *iTos = ZString (iTos->asDouble () / d2); i++; break; case cdAdd: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos += v2; else *iTos = ZString (iTos->asDouble () + v2.asDouble ()); i++; break; case cdSub: v2 = pop (); checkNum (v2); checkNum (*iTos); *iTos = ZString (iTos->asDouble () - v2.asDouble ()); i++; break; case cdCat: v2 = pop (); *iTos += v2; i++; break; case cdMulV: v = aFunc->findVar (pop ()); v2 = pop (); checkNum (v2); checkNum (v->value ()); v->set (v->value ().asDouble () * v2.asDouble ()); i++; break; case cdDivV: v = aFunc->findVar (pop ()); d2 = pop ().asDouble (); if (!d2) throwExcept (msgDivZero); checkNum (v->value ()); v->set (v->value ().asDouble () / d2); i++; break; case cdDivIV: v = aFunc->findVar (pop ()); i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (v->value ()); v->set (v->value ().asInt () / i2); i++; break; case cdModV: v = aFunc->findVar (pop ()); i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (v->value ()); v->set (v->value ().asInt () % i2); i++; break; case cdAddV: v = aFunc->findVar (pop ()); v2 = pop (); if (!isNumber (v->value ().constBuffer ()) || !isNumber (v2.constBuffer ())) v->set (v->value () + v2); else v->set (v->value ().asDouble () + v2.asDouble ()); i++; break; case cdSubV: v = aFunc->findVar (pop ()); v2 = pop (); checkNum (v2); checkNum (v->value ()); v->set (v->value ().asDouble () - v2.asDouble ()); i++; break; case cdCatV: v = aFunc->findVar (pop ()); v2 = pop (); v->set (v->value () + v2); i++; break; case cdIncV: v = aFunc->findVar (pop ()); checkNum (v->value ()); v->set (v->value ().asDouble () + 1.0); i++; break; case cdDecV: v = aFunc->findVar (pop ()); checkNum (v->value ()); v->set (v->value ().asDouble () - 1.0); i++; break; case cdLss: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos < v2 ? One : Zero; else *iTos = iTos->asDouble () < v2.asDouble ()? One : Zero; i++; break; case cdLeq: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos <= v2 ? One : Zero; else *iTos = iTos->asDouble () <= v2.asDouble ()? One : Zero; i++; break; case cdGtr: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos > v2 ? One : Zero; else *iTos = iTos->asDouble () > v2.asDouble ()? One : Zero; i++; break; case cdGeq: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos >= v2 ? One : Zero; else *iTos = iTos->asDouble () >= v2.asDouble ()? One : Zero; i++; break; case cdEql: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos == v2 ? One : Zero; else *iTos = iTos->asDouble () == v2.asDouble ()? One : Zero; i++; break; case cdNeq: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos != v2 ? One : Zero; else *iTos = iTos->asDouble () != v2.asDouble ()? One : Zero; i++; break; case cdAnd: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = (iTos->size () > 0 && v2.size () > 0) ? One : Zero; else *iTos = (iTos->asDouble () != 0.0 && v2.asDouble () != 0.0) ? One : Zero; i++; break; case cdOr: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = (iTos->size () > 0 || v2.size () > 0) ? One : Zero; else *iTos = (iTos->asDouble () != 0.0 || v2.asDouble () != 0.0) ? One : Zero; i++; break; case cdJmp: i = loAddr + i->iAddr; break; case cdJF: { v2 = pop (); ZBoolean ok; if (isNumber (v2.constBuffer ())) ok = v2.asDouble () == 0.0; else ok = v2 == ""; i = ok ? loAddr + i->iAddr : i + 1; break; } case cdJT: { v2 = pop (); ZBoolean ok; if (isNumber (v2.constBuffer ())) ok = v2.asDouble () != 0.0; else ok = v2 != ""; i = ok ? loAddr + i->iAddr : i + 1; break; } case cdCall: exec ((Function *) i->iFunc); i++; head = zTrue; break; case cdRet: return; case cdTry: aFunc->iBlocks->iCatch = i->iAddr; aFunc->iBlocks->iTos = iTos; i++; break; case cdThrow: throw ZException (pop ()); case cdThroV: { v = aFunc->findVar (pop ()); int cnt (v->iSize); ZException exc (v->value ()); for (int i = 1; i < cnt; i++) exc.addAsLast (v->iVals[v->iIndex + i]); throw exc; } case cdAllTV: case cdAllTC: { if (!excpt) throw ZException (msgInvCatch); int cnt (excpt->count ()); Variable *var = aFunc->iBlocks->addVar (decodeString (i->iText) + "[" + ZString (cnt ? cnt : 1) + "]", "", i->iCode == cdAllTC); for (int c = 0; c < cnt; c++) var->iVals[c] = (*excpt)[c]; delete excpt; excpt = 0; i++; break; } case cdAllV: v2 = pop (); aFunc->iBlocks->addVar (v2, pop ()); i++; break; case cdAllC: v2 = pop (); aFunc->iBlocks->addVar (v2, pop (), zTrue); i++; break; case cdAllVR: v2 = pop (); aFunc->iBlocks->addVar (v2, pop (), zFalse, zTrue); i++; break; case cdAllCR: v2 = pop (); aFunc->iBlocks->addVar (v2, pop (), zTrue, zTrue); i++; break; case cdNop: i++; break; default: throwExcept (msgIllgInstr); } // switch if (i < loAddr || i > hiAddr) throwExcept (msgNoReturn); } // try catch (const ZException & exc) { while (aFunc->iBlocks->iPrev && aFunc->iBlocks->iCatch < 0) aFunc->closeBlock (); if (aFunc->iBlocks->iPrev) { i = loAddr + aFunc->iBlocks->iCatch; iTos = aFunc->iBlocks->iTos; aFunc->closeBlock (); if (excpt) delete excpt; excpt = new ZException (exc); } else throw; } // catch } // while } // interpret
void seg::control() { //ifstream in("dev.txt"); ifstream in("test.txt"); size_t i, len, lineLen; string container; char tmp[3]; tmp[2] = 0; Unicode one; vector<char> state; vector<Unicode> words; while (!in.eof()) { getline(in, container); lineLen = container.size(); for (i = 0; i < lineLen;) { tmp[0] = container[i]; tmp[1] = container[i + 1]; one = charToUni(tmp); len = findSymbol(one); if (len) { if (!words.empty()) { eachWords(words, state); } if (checkNum(one)) //handle with the number, decimal, time { do{ segOut << container[i] << container[i + 1]; i += 2; if (i >= lineLen || !findSymbol(one)) break; tmp[0] = container[i]; tmp[1] = container[i + 1]; one = charToUni(tmp); } while (checkNum(one)); segOut << endl; } else if (one == special) //handle with the specail symbol '-' { do{ segOut << container[i] << container[i + 1]; i += 2; if (i >= lineLen) break; tmp[0] = container[i]; tmp[1] = container[i + 1]; one = charToUni(tmp); } while (one == special); segOut << endl; } else { #ifdef GET for (size_t j = 0; j < (len * 2) && (i + j) < lineLen; j++) segOut << container[i + j]; segOut << endl; #endif i += len * 2; } } else { words.push_back(one); i += 2; } } if (!words.empty()) { eachWords(words, state); } } in.close(); }
U2DataId UdrRecord::getDataId(int fieldNum, U2OpStatus &os) const { CHECK(checkNum(fieldNum, os), ""); return data[fieldNum].getDataId(os); }
QString UdrRecord::getString(int fieldNum, U2OpStatus &os) const { CHECK(checkNum(fieldNum, os), ""); return data[fieldNum].getString(os); }
double UdrRecord::getDouble(int fieldNum, U2OpStatus &os) const { CHECK(checkNum(fieldNum, os), 0.0); return data[fieldNum].getDouble(os); }
qint64 UdrRecord::getInt(int fieldNum, U2OpStatus &os) const { CHECK(checkNum(fieldNum, os), 0); return data[fieldNum].getInt(os); }
int main(int numargs, char *args[]){ //should have either 4, 5, 6, or 7 arguments //if 7, [1] must be -r, [2,3,4] must be ints, initial and goal are letters //if 6, must not include -r, include all else //if 5 must include -r and not height OR width //if 4, must have int for maxlength and letters for other, and nothing else //height and width are ints between 2 and 5 (Default is 3) //maxlength is non-negative int (can be 0) //INITIAL and GOAL have the necessary number of letters for the size of the tray //letters between only A and L (duplicates ok) int width = 3; //width of the tray, default is 3 int height = 3; //height of the tray, default is 3 int maxlength = -1; //maxlength of the pattern int tilelen; //number of tiles char *temp; bool flag; //true is -r is specified bool used; char *initial; //initial position char *goal; //goal position //if all the command-line args follow the rules for 4 args if((numargs == 4) && checkNum(args[1]) && checkTiles(args[2], args[3])){ maxlength = (int)strtol(args[1], &temp, 10); tilelen = strlen(args[2]); flag = false; //no flag present initial = args[2]; goal = args[3]; } //if all the command-line args follow the rules for 5 args else if((numargs == 5) && checkFlag(args[1]) && checkNum(args[2]) && checkTiles(args[3], args[4])){ maxlength = (int)strtol(args[2], &temp, 10); tilelen = strlen(args[3]); flag = true; //flag present initial = args[3]; goal = args[4]; } //if all the command-line args follow the rules for 6 args else if((numargs == 6) && checkNum(args[1]) && checkNum(args[2]) && checkNum(args[3]) && checkTiles(args[4], args[5])){ height = (int)strtol(args[1], &temp, 10); width = (int)strtol(args[2], &temp, 10); maxlength = (int)strtol(args[3], &temp, 10); tilelen = strlen(args[4]); flag = false; //no flag present initial = args[4]; goal = args[5]; } //if all the command-line args follow the rules for 7 args else if((numargs == 7) && checkFlag(args[1]) && checkNum(args[2]) && checkNum(args[3]) && checkNum(args[4]) && checkTiles(args[5], args[6])){ height = (int)strtol(args[2], &temp, 10); width = (int)strtol(args[3], &temp, 10); maxlength = (int)strtol(args[4], &temp, 10); tilelen = strlen(args[5]); flag = true; //flag present initial = args[5]; goal = args[6]; } else{//invalid command line args fprintf(stderr, "Invalid command line arguments\n"); exit(1); } if((height >= 2) && (height <= 5) && (width >= 2) && (width <= 5) && (tilelen == height*width)){} else{//if the height and width aren't right, then quit fprintf(stderr, "Invalid command line arguments\n"); exit(1); } Stack q1; //create the two stacks for the queue Stack q2; createS(&q1); createS(&q2); char *nextStr; //holds what is popped off queue 'P' int nextLen; //holds the length of P Trie root; //root of the trie dictionary root.len = -1; root.from = NULL; for(int k = 0; k < 12; k++) root.children[k] = NULL; insert(&root, goal, NULL, 0); //insert root in queue & dictionary pushS(&q1, goal); if(!(strcmp(goal, initial))){ //if goal is initial printf("%s\n", initial); destroyS(&q1); destroyS(&q2); return 0; } while(!(isEmptyS(&q1) && isEmptyS(&q2))){ //while queue not empty popQ(&q1, &q2, &nextStr); nextLen = lenInTrie(&root, nextStr); used = false; if(nextLen < maxlength){ //generate all the possible tiles for(int c = 0; c < width; c++){ //for all column shifts if(flag){//-r, then do it for all possible shifts for(int sh = 1; sh < width; sh++){ char *nextTiles = shiftC(nextStr, height, width, c, height - sh); //next tile pattern generated by a shift if(!strcmp(nextTiles, initial)){ insert(&root, nextTiles, nextStr, nextLen + 1); printSteps(root, nextTiles); destroyS(&q1); destroyS(&q2); return 0; } if(insert(&root, nextTiles, nextStr, nextLen + 1)){ pushS(&q1, nextTiles); //add it to the queue and dictionary used = true; } else free(nextTiles); } } else{//else do it for shift is one step only char *nextTiles = shiftC(nextStr, height, width, c, height - 1); //next tile pattern generated by a shift if(!strcmp(nextTiles, initial)){ insert(&root, nextTiles, nextStr, nextLen + 1); printSteps(root, nextTiles); destroyS(&q1); destroyS(&q2); return 0; } if(insert(&root, nextTiles, nextStr, nextLen + 1)){ pushS(&q1, nextTiles); //add it to the queue and dictionary used = true; } else free(nextTiles); } } for(int r = 0; r < height; r++){//for all possible row shifts if(flag){//-r, then do it for all possible shifts for(int sh = 1; sh < height; sh++){ char *nextTiles = shiftR(nextStr, height, width, r, width - sh); //next tile pattern generated by a shift if(!strcmp(nextTiles, initial)){ insert(&root, nextTiles, nextStr, nextLen + 1); printSteps(root, nextTiles); destroyS(&q1); destroyS(&q2); return 0; } if(insert(&root, nextTiles, nextStr, nextLen + 1)){ pushS(&q1, nextTiles); //add it to the queue and dictionary used = true; } else free(nextTiles); } } else{//else do it for shift is one step only char *nextTiles = shiftR(nextStr, height, width, r, width - 1); //next tile pattern generated by a shift if(!strcmp(nextTiles, initial)){ insert(&root, nextTiles, nextStr, nextLen + 1); printSteps(root, nextTiles); destroyS(&q1); destroyS(&q2); return 0; } if(insert(&root, nextTiles, nextStr, nextLen + 1)){ pushS(&q1, nextTiles); //add it to the queue and dictionary used = true; } else free(nextTiles); } } } if(!used) free(nextStr); }//end of while queue is not empty destroyS(&q1); destroyS(&q2); return 0; }