/* algorithm from "Data structure and algorithm" - AHU p62 */ int enqueue(Queue *queue, char ch) { if (addone(queue,addone(queue,queue->rear)) == queue->front) return FULLQUE; else { queue->rear = addone(queue,queue->rear); queue->ch[queue->rear] = ch; queue->count++; return ch; /* successful */ } }
int main(int argc, char * argv[]){ int a = 10; addone(a); printf("%d\n",a); //prints 10! return 0; }
int main(int argc, char** argv, char** envp) { double g=4.5; printf("the address of g is %p\n", &g); addone(&g); printf("g is %f\n", g); return EXIT_SUCCESS; }
/* algorithm from "Data structure and algorithm" - AHU p62 */ int emptyqueue(Queue *queue) { if (addone(queue,queue->rear) == queue->front) return TRUE; else return FALSE; }
/* note that this one is different from the others, in that it calls * a function to do the add */ void computeF(workStruct_t *x) { int i; x->sum_ctr = 0; for (i = 0; i < loop_count; i++) { addone(&x->sum_ctr); } }
int main(void){ int i=1; addone(&i); printf("%d\n", i); return 0; }
int main(void){ int *i; *i = 1; addone(&i); printf("%d\n", *i); return 0; }
int main() { std::string str("ABCDEFG"); std::cout << "Before: " << str << std::endl; addone(str); std::cout << "After : " << str << std::endl; return 0; }
void MC2Switch::getText( WString& str, WVList* states, SwMode mode ) { bool state = _state[mode]; WVList found; findStates( states, found ); int icount = found.count(); for( int i=0; i<icount; i++ ) { MCState* st = (MCState*)found[i]; if( st->mode() == mode ) { state = st->state(); } } addone( str, state ); }
/* algorithm from "Data structure and algorithm" - AHU p62 */ int dequeue(Queue *queue) { char ch; if (emptyqueue(queue)) { return EMPTYQUE; } else { ch = queue->ch[queue->front]; queue->front = addone(queue,queue->front); (queue->count) --; return ch; } }
static int addone(int *counters, int last, int total) { counters[last]++; if (counters[last] >= total) { if (last == 0) return 0; if (addone(counters, last - 1, total - 1) == 0) return 0; counters[last] = counters[last - 1] + 1; } return 1; }
int main() { int x = 10; int y = addone(x); init(); printf ("x=%d, y=%d\n",x,y); printf ("addone(x)=%d\n",addone(x+10)); doubleIt(&x); printf ("x=%d, y=%d\n",x,y); printf("4! = %d\n", factorial(4)); printf("x! = %d\n", factorial(x)); printf("\n\nTesting factorial:\n"); test_factorial(10); printf("\n"); test_factorial(40); printf("\n"); return 0; }
static int addone(int *counters, int last, int total) { /* since this function recurses, it could be driven to stack overflow. */ check_stack_depth(); counters[last]++; if (counters[last] >= total) { if (last == 0) return 0; if (addone(counters, last - 1, total - 1) == 0) return 0; counters[last] = counters[last - 1] + 1; } return 1; }
/************************************************************************ * Function: main() * Description: Main entry point for the demonstration *************************************************************************/ int main() { volatile long demo_counter; volatile int pfa_demo=0; int sum = 0; volatile char cvar; /* sample char variable */ REC_TYPE1 q; volatile int localInt1; volatile long localLong1; /* Setup the global string array */ globalstring[0] = "zero"; globalstring[1] = "one"; globalstring[2] = "two"; /* Initialize the rectest structure */ rectest.long_integer = 0xFFFFEEEE; rectest.short_integer = 5555; rectest.integer_array[0] = 0; rectest.integer_array[1] = 10; rectest.integer_array[2] = 20; rectest.integer_array[3] = 30; rectest.string_pointer = "Wind River's Tools Product Family"; /* * Fill a table with factorials using recursive calls. */ factorialDemo( ); /* * Fill a table with the fibonnacci sequence using recursive calls. */ fibonacciDemo( ); /* set a local variable */ pfa_demo = 3; /* * Infinite Loop ... * Could use demo_counter as a modulo-maxcount-for-sizeof(long) * to count the iterations of the demo loop and view in a watch window. */ for ( demo_counter=0; FOREVER ;demo_counter++ ) { /* * Setup and examine 'engineer structure'. This function illustrates * arrays of structures and pointers to array of structures. */ status = engineers( 0x1234 ); /* * Setup and examine link-list structure. This function illustrates * structure pointers and pointer-to-pointers. */ status = linkList( 0x5678 ); /* * Calculate the factorial of a number. Since factorials * get very large quickly, only calculate the factorial * of the number 3 once every 25 iterations using the C modulo operator * to detect 25th iteration in the infinite loop. * * Add 'demo_counter' in a watch window and set a breakpoint * on the 'localInt1=factorial(3);' line. If there are no other * enabled breakpoints in the loop, then every time the * run button is pressed, the demo_counter is updated. But * the modulo operator filters calls to the factorial function * only once every 25 iterations through the loop. */ if ( !(demo_counter % 25) ) localInt1 = factorial( 3 ); /* * Calculate the fibonacci of a fixed number, 5, * once every 100th iteration of the main test loop. */ if ( !(demo_counter % 100) ) globalInt1 = fibonacci( 5 ); /* * Demonstrate some calendar functions. */ localLong1 = 0x12345678; globalLong1 = calendar( localLong1 ); /* * Manipulate some local and global structures. */ q.a = 55; strcpy(q.b,"December"); q.c = 12345678L; q.color = red; sum = 0; wait_index = 0; wait_count = 5; quick_index = 5; recordvar.color = red; /* * Call a function written in assembler */ while (wait_index < wait_count) { wait_index = addone(wait_index); sum = sum + 1; } cvar = date(); recordvar.a = 0; for (quick_index = 0; quick_index <= 4 ;++quick_index) { ++recordvar.a; } recordvar.color = blue; /* * Before starting the demo_count loop once again, increment * pfa_demo counter variable using a ternary condition operator. * Also, maintain a modulo-3 range on its value. */ if (pfa_demo==3) { pfa_demo=0; } else { ++pfa_demo; } } return 0; }
int addtwo(int a){ return addone(addone(a)); //addone() is now known here }
static QTNode * findeq(QTNode * node, QTNode * ex, MemoryType memtype, QTNode * subs, bool *isfind) { if ((node->sign & ex->sign) != ex->sign || node->valnode->type != ex->valnode->type || node->valnode->val != ex->valnode->val) return node; if (node->flags & QTN_NOCHANGE) return node; if (node->valnode->type == OPR) { if (node->nchild == ex->nchild) { if (QTNEq(node, ex)) { QTNFree(node); if (subs) { node = QTNCopy(subs, memtype); node->flags |= QTN_NOCHANGE; } else node = NULL; *isfind = true; } } else if (node->nchild > ex->nchild) { int *counters = (int *) palloc(sizeof(int) * node->nchild); int i; QTNode *tnode = (QTNode *) MEMALLOC(memtype, sizeof(QTNode)); memset(tnode, 0, sizeof(QTNode)); tnode->child = (QTNode **) MEMALLOC(memtype, sizeof(QTNode *) * ex->nchild); tnode->nchild = ex->nchild; tnode->valnode = (ITEM *) MEMALLOC(memtype, sizeof(ITEM)); *(tnode->valnode) = *(ex->valnode); for (i = 0; i < ex->nchild; i++) counters[i] = i; do { tnode->sign = 0; for (i = 0; i < ex->nchild; i++) { tnode->child[i] = node->child[counters[i]]; tnode->sign |= tnode->child[i]->sign; } if (QTNEq(tnode, ex)) { int j = 0; MEMFREE(memtype, tnode->valnode); MEMFREE(memtype, tnode->child); MEMFREE(memtype, tnode); if (subs) { tnode = QTNCopy(subs, memtype); tnode->flags = QTN_NOCHANGE | QTN_NEEDFREE; } else tnode = NULL; node->child[counters[0]] = tnode; for (i = 1; i < ex->nchild; i++) node->child[counters[i]] = NULL; for (i = 0; i < node->nchild; i++) { if (node->child[i]) { node->child[j] = node->child[i]; j++; } } node->nchild = j; *isfind = true; break; } } while (addone(counters, ex->nchild - 1, node->nchild)); if (tnode && (tnode->flags & QTN_NOCHANGE) == 0) { MEMFREE(memtype, tnode->valnode); MEMFREE(memtype, tnode->child); MEMFREE(memtype, tnode); } else QTNSort(node); pfree(counters); } } else if (QTNEq(node, ex)) { QTNFree(node); if (subs) { node = QTNCopy(subs, memtype); node->flags |= QTN_NOCHANGE; } else { node = NULL; } *isfind = true; } return node; }
void MC2Switch::getText( WString& str, MState* state ) { MCState* st = (MCState*)state; addone( str, st->state() ); }
/* * If node is equal to ex, replace it with subs. Replacement is actually done * by returning either node or a copy of subs. */ static QTNode * findeq(QTNode *node, QTNode *ex, QTNode *subs, bool *isfind) { if ((node->sign & ex->sign) != ex->sign || node->valnode->type != ex->valnode->type) return node; if (node->flags & QTN_NOCHANGE) return node; if (node->valnode->type == QI_OPR) { if (node->valnode->qoperator.oper != ex->valnode->qoperator.oper) return node; if (node->nchild == ex->nchild) { if (QTNEq(node, ex)) { QTNFree(node); if (subs) { node = QTNCopy(subs); node->flags |= QTN_NOCHANGE; } else node = NULL; *isfind = true; } } else if (node->nchild > ex->nchild) { /* * AND and NOT are commutative, so we check if a subset of the * children match. For example, if tnode is A | B | C, and ex is B * | C, we have a match after we convert tnode to A | (B | C). */ int *counters = (int *) palloc(sizeof(int) * node->nchild); int i; QTNode *tnode = (QTNode *) palloc(sizeof(QTNode)); memset(tnode, 0, sizeof(QTNode)); tnode->child = (QTNode **) palloc(sizeof(QTNode *) * ex->nchild); tnode->nchild = ex->nchild; tnode->valnode = (QueryItem *) palloc(sizeof(QueryItem)); *(tnode->valnode) = *(ex->valnode); for (i = 0; i < ex->nchild; i++) counters[i] = i; do { tnode->sign = 0; for (i = 0; i < ex->nchild; i++) { tnode->child[i] = node->child[counters[i]]; tnode->sign |= tnode->child[i]->sign; } if (QTNEq(tnode, ex)) { int j = 0; pfree(tnode->valnode); pfree(tnode->child); pfree(tnode); if (subs) { tnode = QTNCopy(subs); tnode->flags = QTN_NOCHANGE | QTN_NEEDFREE; } else tnode = NULL; node->child[counters[0]] = tnode; for (i = 1; i < ex->nchild; i++) node->child[counters[i]] = NULL; for (i = 0; i < node->nchild; i++) { if (node->child[i]) { node->child[j] = node->child[i]; j++; } } node->nchild = j; *isfind = true; break; } } while (addone(counters, ex->nchild - 1, node->nchild)); if (tnode && (tnode->flags & QTN_NOCHANGE) == 0) { pfree(tnode->valnode); pfree(tnode->child); pfree(tnode); } else QTNSort(node); pfree(counters); } } else { Assert(node->valnode->type == QI_VAL); if (node->valnode->qoperand.valcrc != ex->valnode->qoperand.valcrc) return node; else if (QTNEq(node, ex)) { QTNFree(node); if (subs) { node = QTNCopy(subs); node->flags |= QTN_NOCHANGE; } else { node = NULL; } *isfind = true; } } return node; }