int main (int argc, char *argv[]) { srand(time(NULL)); FILE *arch=fopen("Jugadas.txt", "w"); int i=0,columna,size,length=0,jugadas=1; //i se usa para recorrer el for; columna es la variable donde se almacena char *cubo; printf("Ingrese la cantidad de Columnas \n"); scanf("%d", &size); //size es la cantidad de pilas que se deberan usar db *array[size]; for (i = 0; i < size; i += 1) array[i]=NULL; while(length<10) { columna=rancolumn(size); //pila al azar para que caiga un cubo al azar cubo=rancube(); //color al azar array[columna-1]=append(&array[columna-1], cubo); //le agrega un elemento a la pila que haya sido seleccionada al azar fprintf(arch, "Jugada %d:Cubo %s - Pila %d \n",jugadas,cubo,columna); //se imprime cada una de las jugadas al archivo de salida checkVertical(array,columna, arch); //se verifica la regla 1 con la funcion checkVertical checkHorizontal(array, size, arch, columna); //se verifica la regla 2 con la funcion checkHorizontal if(len(array[columna-1])>length) length=len(array[columna-1]); //aumenta la variable de longitud de la pila en 1 para provocar la salida del while. jugadas++; } fprintf(arch, "--JUEGO TERMINADO-- \n"); //sale del while cuando una pila tiene 10 cubos for (i = 0; i < size; i += 1) //y se hara un registro del estado final de las pilas, usando la funcion len { fprintf(arch, "Pila %d [%d]: " ,i+1, len(array[i])); writeList(array[i], arch); } return 0; }
/* syncDoWrite * returns -1 on error, 0 if not finished, 1 if finished * Note that, on a return of 0, some messages may have been sent, if * several were queued up. */ I pString_Connection::syncDoWrite(void) { I c,n=0; MSNodeItem *hp=writeList(); MSNodeItem *np; MSBuffer *bp; MSBoolean notdone=MSTrue; ipcWarn(wrnlvl(), "%t pString_Connection::syncDoWrite\n"); /* write queue to write channel */ while(notdone&&(hp!=(np=hp->next()))) { bp=(MSBuffer *)np->data(); c=bp->put()-bp->get(); while(c>0 && 0<(n=bp->write(fd(),c))) c-=n; if(bp->get() == bp->put()) { delete bp;delete np;turnInWriteOff(); } else { notdone=MSFalse;turnInWriteOn(); } if (0 > n) return syncFillError("buffwrite","buffwrite returned error %d",n); } if(hp == hp->next()) return 1; else return 0; }
void CFixPackage::DoFix() { if( !readOldData(m_szPkgName) ) return; fixSpaces(); writeList(); }
double MemorizeStorageSpace::onAction() { if (receiveObject && receiveStorageSpace){ incrementCleanUpCount(); writeList(); } return 0.1; }
void TelegramCache::insert(const QList<Document> &recentStickers) { const QString filePath = p->path + "/recent-stickers"; QVariantList list; Q_FOREACH(const Document &doc, recentStickers) list << doc.toMap(); writeList(filePath, list); }
void TelegramCache::insert(const QList<Dialog> &dialogs) { const QString filePath = p->path + "/dialogs"; QVariantList list; Q_FOREACH(const Dialog &dlg, dialogs) list << dlg.toMap(); writeList(filePath, list); }
/* command line args: threads, infile, outfile */ int main(int argc, char** argv){ listSize = 0; srand(time(NULL)); initQueue(); list = getList(argv[2]); numThreads = atoi(argv[1]); pthread_t pool[numThreads]; mailbox = malloc(sizeof(struct qnode) * numThreads); lock = malloc(sizeof(pthread_mutex_t) * numThreads); pthread_mutex_init(&qlock, NULL); int t = 0; int *args = malloc(sizeof(int) * numThreads); for(; t < numThreads; t++){ args[t] = t; pthread_mutex_init(&lock[t], NULL); mailbox[t].start = -1; mailbox[t].end = -1; pthread_create(&pool[t], NULL, &bootstrap, &args[t]); } for(t = 0; t < numThreads; t++){ pthread_join(pool[t], NULL); pthread_mutex_destroy(&lock[t]); } pthread_mutex_destroy(&qlock); writeList(argv[3], list); free(list); free(args); free(lock); free(mailbox); destroyQueue(); return 0; }
static void serializeUntypedTerm(A2PWriter writer, ATerm value){ int type = ATgetType(value); switch(type){ case AT_INT: writeInteger(writer, (ATermInt) value); break; case AT_REAL: writeDouble(writer, (ATermReal) value); break; case AT_APPL: { ATermAppl appl = (ATermAppl) value; AFun fun = ATgetAFun(appl); if(ATisQuoted(fun) == ATfalse){ A2PType expected = A2PnodeType(); ATermList annotations = (ATermList) ATgetAnnotations(value); if(annotations == NULL){ writeNode(writer, expected, appl); }else{ if(((A2PNodeType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Node term has annotations, but none are declared.\n"); exit(1); } writeAnnotatedNode(writer, expected, appl, annotations); } }else{ if(ATgetArity(fun) != 0){ fprintf(stderr, "Quoted appl (assumed to be a string) has a non-zero arity.\n"); exit(1);} writeString(writer, appl); } } break; case AT_LIST: writeList(writer, A2PlistType(A2PvalueType()), (ATermList) value); break; default: fprintf(stderr, "Encountered unwriteable type: %d.\n", type); exit(1); } }
static void doSerialize(A2PWriter writer, A2PType expected, ATerm value){ DKISIndexedSet sharedValues = writer->valueSharingMap; int valueHash = hashValue(value); int valueId = DKISget(sharedValues, (void*) value, (void*) expected, valueHash); /* TODO: Fix sharing (check types). */ if(valueId != -1){ writeByteToBuffer(writer->buffer, PDB_SHARED_FLAG); printInteger(writer->buffer, valueId); return; } switch(expected->id){ case PDB_VALUE_TYPE_HEADER: serializeUntypedTerm(writer, value); break; case PDB_BOOL_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Boolean didn't have AT_APPL type.\n"); exit(1); } writeBool(writer, (ATermAppl) value); break; case PDB_INTEGER_TYPE_HEADER: if(ATgetType(value) != AT_INT){ fprintf(stderr, "Integer didn't have AT_INT type.\n"); exit(1); } writeInteger(writer, (ATermInt) value); break; case PDB_DOUBLE_TYPE_HEADER: if(ATgetType(value) != AT_REAL){ fprintf(stderr, "Double didn't have AT_REAL type.\n"); exit(1); } writeDouble(writer, (ATermReal) value); break; case PDB_STRING_TYPE_HEADER: if(ATgetType(value) != AT_APPL || ATisQuoted(ATgetAFun((ATermAppl) value)) == ATfalse){ fprintf(stderr, "String didn't have 'quoted' AT_APPL type.\n"); ATabort(""); exit(1); } writeString(writer, (ATermAppl) value); break; case PDB_SOURCE_LOCATION_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Source location didn't have AT_APPL type.\n"); exit(1); } writeSourceLocation(writer, (ATermAppl) value); break; case PDB_NODE_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Node didn't have AT_APPL type.\n"); exit(1); } { ATermList annotations = (ATermList) ATgetAnnotations(value); if(annotations == NULL){ writeNode(writer, expected, (ATermAppl) value); }else{ if(((A2PNodeType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Node term has annotations, but none are declared.\n"); exit(1); } writeAnnotatedNode(writer, expected, (ATermAppl) value, annotations); } } break; case PDB_TUPLE_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Tuple didn't have AT_APPL type.\n"); exit(1); } writeTuple(writer, expected, (ATermAppl) value); break; case PDB_LIST_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "List didn't have AT_LIST type.\n"); exit(1); } writeList(writer, expected, (ATermList) value); break; case PDB_SET_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Set didn't have AT_LIST type.\n"); exit(1); } writeSet(writer, expected, (ATermList) value); break; case PDB_RELATION_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Relation didn't have AT_LIST type.\n"); exit(1); } writeRelation(writer, expected, (ATermList) value); break; case PDB_MAP_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Map didn't have AT_LIST type.\n"); exit(1); } writeMap(writer, expected, (ATermList) value); break; case PDB_CONSTRUCTOR_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Constructor didn't have AT_APPL type.\n"); exit(1); } { ATermList annotations = (ATermList) ATgetAnnotations(value); if(annotations == NULL){ writeConstructor(writer, expected, (ATermAppl) value); }else{ if(((A2PConstructorType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Constructor term has annotations, but none are declared.\n"); exit(1); } writeAnnotatedConstructor(writer, expected, (ATermAppl) value, annotations); } } break; case PDB_ADT_TYPE_HEADER: writeADT(writer, expected, value); break; default: fprintf(stderr, "Unserializable type: %d\n.", expected->id); exit(1); } DKISstore(sharedValues, (void*) value, (void*) expected, valueHash); }
int main() { // object read as an entry from the files graduate grad; // list holds graduates in two sections "BS" and "BA" list<graduate> gradBA_list, gradBS_list, diplomalist; // streams to input registrar's list and dean's list ifstream gradIn, noAttIn; // open registrar's list file gradIn.open("gradlist.dat"); if (!gradIn) { cerr << "Cannot open file 'gradlist.dat'" << endl; exit(1); } // read registrar's list to end-of-file and add to list while(true) { gradIn >> grad; if (!gradIn) break; if(grad.getDegree() == "BS") insertOrder(gradBS_list, grad); else insertOrder(gradBA_list, grad); } gradIn.close(); // open file of those not attending graduation noAttIn.open("noattend.dat"); if (!noAttIn) { cerr << "Cannot open file 'noattend.dat'" << endl; exit(1); } // read list of those not attending to end-of-file and // remove each graduate from the list while(true) { noAttIn >> grad; if (!noAttIn) break; if (grad.getDegree() == "BS") removeGraduate(gradBS_list,grad); else removeGraduate(gradBA_list, grad); } diplomalist = gradBS_list; splice<graduate>(diplomalist, diplomalist.end(), gradBA_list); // output list of graduates at ceremony by sections. use // a newline separator between names cout << "Students at Graduation" << endl << endl; writeList(diplomalist, "\n"); return 0; }
bool calculate(const char *in, double *result, Error* error) { List* nums = NULL; int start = -1; // on strlen + 1 is '\0' which can take care of input like "5" for(size_t i = 0, len = strlen(in) + 1; i < len; i++) { // is number or minus following a letter if(isNumber(in[i]) || (in[i] == '-' && (i + 1) < len && isNumber(in[i+1]))) { if(start == -1) { start = i; } } else if(start >= 0) { double num = strToInt(in + start, i - start); start = -1; writeList(&nums, num); } else if(in[i] == '+' || in[i] == '-' || in[i] == '*' || in[i] == '/' || in[i] == '^') { double a, b, result; if(!readList(&nums, &b) || !readList(&nums, &a)) { if(error) { error->code = MISSING_NUMBER; error->position = i-1; } destroyList(&nums); return false; } switch(in[i]) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a*b; break; case '/': result = a/b; break; case '^': result = pow(a, b); break; } writeList(&nums, result); } else { if(isMathFunction("sqrt") || isMathFunction("sin") || isMathFunction("cos") || isMathFunction("tg") || isMathFunction("cotg")) { double a, result; if(!readList(&nums, &a)) { if(error) { error->code = MISSING_NUMBER; error->position = i-1; } destroyList(&nums); return false; } if(isMathFunction("sqrt")) { result = sqrt(a); } else if(isMathFunction("sin")) { result = sin(a * M_PI / 180); } else if(isMathFunction("cos")) { result = cos(a * M_PI / 180); } else if(isMathFunction("tg")) { result = tan(a * M_PI / 180); } else if(isMathFunction("cotg")) { result = 1 / tan(a * M_PI / 180); } writeList(&nums, result); } } } if(result) { *result = 0; readList(&nums, result); } if(!isEmptyList(&nums)) { printf("Stack not empty!\n"); printList(&nums); destroyList(&nums); return false; } return true; }
bool convert(const char in[], char *result, Error *error) { List *ops = NULL; int r = 0; Token last = NOTHING; for(size_t i = 0, size = strlen(in); i < size; i++) { // if is number, push it to output if(isNumber(in[i]) || in[i] == '.' || (last == NOTHING && in[i] == '-')) { result[r++] = in[i]; last = OPERAND; } else { switch(in[i]) { // if its operator, push it to stack case '+': case '-': case '*': case '^': case '/': { // pop operators from stack when they have higher priority then actual operator while(ops && hasBiggerPriority(ops->num, in[i])) { //while(ops && ((ops->num == '*' || ops->num == '/') || ((in[i] == '+' || in[i] == '-') && (ops->num == '+' || ops->num == '-')))) { if(r > 1 && result[r - 1] != ' ') { result[r++] = ' '; } writeOperator(ops->num, result + r, &r); readList(&ops, NULL); // pop from queue } result[r++] = ' '; writeList(&ops, in[i]); last = OPERATOR; break; } // push starting bracket to stack case '(': { // support for a(a+b) = a*(a+b) notation if(last == OPERAND) { writeList(&ops, '*'); result[r++] = ' '; } writeList(&ops, in[i]); last = NOTHING; break; } // pop until '(' is found in stack case ')': { double op; while(readList(&ops, &op)) { if(op == '(') { break; } result[r++] = ' '; writeOperator(op, result + r, &r); } break; } // space character is allowed - just do nothing case ' ': { break; } default: { if(strncmp("sqrt", in + i, strlen("sqrt")) == 0) { i += strlen("sqrt"); writeList(&ops, OP_SQRT); } else if(strncmp("sin", in + i, strlen("sin")) == 0) { i += strlen("sin"); writeList(&ops, OP_SIN); } else if(strncmp("cos", in + i, strlen("cos")) == 0) { i += strlen("cos"); writeList(&ops, OP_COS); } else if(strncmp("tg", in + i, strlen("tg")) == 0) { i += strlen("tg"); writeList(&ops, OP_TG); } else if(strncmp("cotg", in + i, strlen("cotg")) == 0) { i += strlen("cotg"); writeList(&ops, OP_COTG); } else { error->code = UNEXPECTED_CHAR; error->position = i; destroyList(&ops); return false; } } } } } // pop all remaining operators from stack and put them to output double op; while(readList(&ops, &op)) { if(op == '(') { error->code = MISSING_END_BRACKET; error->position = strstr(in, "(") - in; destroyList(&ops); return false; } result[r++] = ' '; writeOperator(op, result + r, &r); } result[r] = '\0'; return true; }
MemorizeStorageSpace::~MemorizeStorageSpace() { writeList(); this->disconnect(); }
int main(int argc, char* argv[]) { t_Node *ptTree = NULL; FILE *ifp = NULL; t_Params tParams; t_Data tData; t_Map tMap; t_Node **aptSplit = (t_Node **) malloc(MAX_SPLIT*sizeof(t_Node *)); int nSplit = 0; double dMaxDepth = 0.0, dSplitDepth = 0.0, dDepth = 0.0; int iL = 0, nLast = 0, nCount = 0, i = 0, j = 0; int* anLast = NULL; char szDir[MAX_WORD_LENGTH]; char szTreeFile[MAX_WORD_LENGTH]; char szDatFile[MAX_WORD_LENGTH]; char szListFile[MAX_WORD_LENGTH]; FILE* tfp = NULL, *dfp = NULL, *lfp = NULL; getCommandLineParams(&tParams, argc, argv); readData(&tData, tParams.szDatFile); readMapFile(&tMap, tParams.szMapFile); ifp = fopen(tParams.szTreeFile, "r"); if(ifp){ addElement(&ptTree, ifp); fclose(ifp); } else{ printf("Failed to open tree file\n"); } setLeaves(ptTree); treeSplitEven(ptTree, tParams.nSplit, aptSplit, &nSplit); for(i = 0; i < nSplit; i++){ countLeaves(aptSplit[i],&(aptSplit[i]->nN)); if(aptSplit[i]->nN < tParams.nMinSize){ nLast += aptSplit[i]->nN; } aptSplit[i]->anLeaves = (int *) malloc(sizeof(int)*aptSplit[i]->nN); nCount = 0; getLeaves(aptSplit[i],aptSplit[i]->anLeaves, &nCount); } maxDepth(ptTree, &dMaxDepth); setDepth(ptTree, 0.0); /*sort on number of leaves*/ //void qsort(void* field, size_t nElements, size_t sizeOfAnElement, // int(_USERENTRY *cmpFunc)(const void*, const void*)); qsort(aptSplit,nSplit,sizeof(t_Node*),compNode); i = 0; while(i < nSplit && aptSplit[i]->nN >= tParams.nMinSize){ sprintf(szDir, "C%03d",i); mkdir(szDir, S_IRWXU); sprintf(szTreeFile,"%s/%s%s",szDir,szDir,TREE_SUFFIX); sprintf(szDatFile,"%s/%s%s",szDir,szDir,DAT_SUFFIX); sprintf(szListFile,"%s/%s%s",szDir,szDir,LIST_SUFFIX); printf("%d %d %f\n",i,aptSplit[i]->nN,dMaxDepth - aptSplit[i]->dDepth); tfp = fopen(szTreeFile, "w"); if(tfp){ writeTree(aptSplit[i], tfp); fprintf(tfp, ";\n"); fclose(tfp); } dfp = fopen(szDatFile, "w"); if(dfp){ writeData(dfp, &tData, aptSplit[i]->nN, aptSplit[i]->anLeaves, &tMap); fclose(dfp); } nCount=0; renumberLeaves(aptSplit[i], &nCount); lfp = fopen(szListFile, "w"); if(lfp){ writeList(lfp, aptSplit[i], dMaxDepth - aptSplit[i]->dDepth); fclose(lfp); } i++; } if(nLast > 0){ anLast = (int *) malloc(sizeof(int)*nLast); nCount = 0; printf("%d %d\n",i,nLast); iL = i; for(; i < nSplit; i++){ for(j = 0; j < aptSplit[i]->nN; j++){ anLast[nCount + j] = aptSplit[i]->anLeaves[j]; } nCount += aptSplit[i]->nN; } if(nCount > 0){ sprintf(szDir, "C%03d+",iL); mkdir(szDir, S_IRWXU); sprintf(szTreeFile,"%s/%s%s",szDir,szDir,TREE_SUFFIX); sprintf(szDatFile,"%s/%s%s",szDir,szDir,DAT_SUFFIX); sprintf(szListFile,"%s/%s%s",szDir,szDir,LIST_SUFFIX); dfp = fopen(szDatFile, "w"); if(dfp){ writeData(dfp, &tData, nLast, anLast, &tMap); fclose(dfp); } } free(anLast); } exit(EXIT_SUCCESS); }