コード例 #1
0
ファイル: main.c プロジェクト: laurofigueroa/EDyA1
int main() {
	Stack *pila1;
	pila1 = stack_create();
	
	stack_data(pila1)[0] = 2;
	stack_data(pila1)[1] = 8;
	stack_data(pila1)[2] = 14;
	stack_back(pila1) = 2;

	pila1 = stack_push(pila1,4);



	pila1 = stack_pop(pila1);
	stack_print(pila1);	

	int i;
	for(i = 0; i<=stack_back(pila1) && stack_back(pila1) != stack_maxstack(pila1) ; i++)
				stack_data(pila)[i] = 3;



	pila1 = stack_reverse(pila1);
	stack_print(pila1);

	stack_destroy(pila1);
	


}
コード例 #2
0
ファイル: bcode.c プロジェクト: FreeBSDFoundation/freebsd
void
eval(void)
{
	int ch;

	for (;;) {
		ch = readch();
		if (ch == EOF) {
			if (bmachine.readsp == 0)
				return;
			src_free();
			bmachine.readsp--;
			continue;
		}
#ifdef DEBUGGING
		fprintf(stderr, "# %c\n", ch);
		stack_print(stderr, &bmachine.stack, "* ",
		    bmachine.obase);
		fprintf(stderr, "%zd =>\n", bmachine.readsp);
#endif

		if (0 <= ch && ch < (signed)UCHAR_MAX)
			(*jump_table[ch])();
		else
			warnx("internal error: opcode %d", ch);

#ifdef DEBUGGING
		stack_print(stderr, &bmachine.stack, "* ",
		    bmachine.obase);
		fprintf(stderr, "%zd ==\n", bmachine.readsp);
#endif
	}
}
コード例 #3
0
double evaluate_RPN(char *expr, struct stackNode **top)
{
	int i, j = 0;
	double popVal1;		// Stack'ten cikaracaginiz (pop) ilk deger
	double popVal2;		// Stack'ten cikaracaginiz (pop) ikinci deger
	double pushVal;		// Stack'e ekleyeceginiz (push) deger
	double retval = 0;
	char Val[20];

	/* TODO: Ifadenin sonuna kadar elemanlari gezecek ('\0') bir dongu kurunuz */
	for (i = 0; i < strlen(expr); i++) {
		/* TODO: Eger eleman islenen (operand) ise stack'e ekleyiniz (push)
		 * Soru: Bir karakterin sayi karsiligini nasil buluyorduk? */
		if (expr[i] == ' ') {
			Val[j + 1] = '\0';
			j = 0;
			popVal1 = atof(Val);
			stack_push(top, popVal1);
			if (DEBUG)
				stack_print(*top);
		} else if (expr[i] == '/' || expr[i] == '*' || expr[i] == '-' || expr[i] == '+') {

			popVal1 = stack_pop(top);
			popVal2 = stack_pop(top);
			pushVal = compute_operation(popVal2, popVal1, expr[i]);
			stack_push(top, pushVal);
			i++;

			if (DEBUG)
				stack_print(*top);
		}

		else {
			/* TODO: Eger eleman bir islem ise stack'ten iki deger cekiniz (pop) */
			Val[j] = expr[i];
			j++;

			if (DEBUG)
				stack_print(*top);

			/* TODO: Bu iki deger ile istenen islemi compute_operation()
			 * fonksiyonuna verip donus degeri stack'e push edin. */

			if (DEBUG)
				stack_print(*top);
		}
	}

	/* TODO: Stack'te kalan elemani cekip return edin. */
	retval = stack_pop(top);

	if (!stack_is_empty(*top)) {
		fprintf(stderr, "UYARI: Stack hala dolu, RPN ifadesi dengesiz veya algoritmayi yanlis kurdunuz.\n");
	}
	return retval;

}
コード例 #4
0
int main(void){

    stack* stacky = stack_create();

    stack_push(stacky, 82.1);
    stack_push(stacky, 51.3);
    stack_push(stacky, 89.9999);


    stack_print(stacky);
    stack_reverse(&stacky);
    
    stack_print(stacky);
    stack_destroy(stacky);
}
コード例 #5
0
ファイル: process.c プロジェクト: JonasG/Carp
void stack_push(Process *process, Obj *o) {
  assert(o);
  if(LOG_STACK) {
    printf("Pushing %s\n", obj_to_string(process, o)->s);
  }
  if(process->stack_pos >= STACK_SIZE) {
    printf("Stack overflow:\n");
    stack_print(process);
    exit(1);
  }
  process->stack[process->stack_pos++] = o;
  if(LOG_STACK) {
    stack_print(process);
  }
}
コード例 #6
0
ファイル: format_print.c プロジェクト: shawakaze/cmus
/* print '{,-}{h:,}mm:ss' */
static int print_time(char *buf, int t)
{
	int h, m, s;
	char stack[32];
	int neg = 0;
	int p = 0;

	if (t < 0) {
		neg = 1;
		t *= -1;
	}
	h = t / 3600;
	t = t % 3600;
	m = t / 60;
	s = t % 60;

	/* put all chars to stack in reverse order ;) */
	stack[p++] = s % 10 + '0';
	stack[p++] = s / 10 + '0';
	stack[p++] = ':';
	stack[p++] = m % 10 + '0';
	stack[p++] = m / 10 + '0';
	if (h) {
		stack[p++] = ':';
		do {
			stack[p++] = h % 10 + '0';
			h /= 10;
		} while (h);
	}
	if (neg)
		stack[p++] = '-';

	return stack_print(buf, stack, p);
}
コード例 #7
0
int main (int argc, const char * argv[]) {	
	
	//Stack
	
	int numbers[NUMLEN] = {
		15, 10, 5, 100
	};
	
	int i;

	Stack *stack = stack_create();
	
	for(i = 0; i < NUMLEN; i++) {
		printf("pushing: %u\n", numbers[i]);
		stack_push(stack, numbers[i]);
	}
	
	printf("\n");

	stack_print(stack);

	while(i--) {
		unsigned int result = stack_pop(stack);

		if(UINT_MAX == result) {
			break;
		}
		
		printf("popping: %u\n", result);
	}
	
	stack_destroy(stack);
	
	//Queue
	
	Queue *queue = queue_create();
	
	for(i = 0; i < NUMLEN; i++) {
		printf("\nenqueueing: %u", numbers[i]);
		queue_enqueue(queue, numbers[i]);
	}
	
	queue_print(queue);
	
	while(i--) {
		unsigned int result = queue_dequeue(queue);
		
		if(UINT_MAX == result) {
			break;
		}
		
		printf("dequeuing: %u\n", result);
	}
	
	queue_print(queue);

	queue_destroy(queue);

	return 0;
}
コード例 #8
0
ファイル: main.c プロジェクト: Mrucznik/C-Language-learning
int main()
{
	struct stack_t* stos = stack_create(10);
	stack_push(stos, 10);
	stack_push(stos, 20);
	stack_push(stos, 30);
	stack_print(stos);
	printf("%d\n", stack_pop(stos)); // 30
	stack_print(stos); // 20 10
	stack_save(stos, "stos.bin");
	stack_clear(stos);
	stack_load(stos, "stos.bin");
	stack_push(stos, 25);
	stack_print(stos);
	stack_free(stos);
	return 0;
}
コード例 #9
0
void stk_test_empty(void) {
	stack_t* s = stack_init(stk_compare, stk_print, stk_clone, stk_destroy);

	stack_print(s, stderr);
	CU_ASSERT (stack_get(s) == NULL);
	CU_ASSERT (stack_pop(s) == NULL);

	CU_ASSERT (stack_destroy(s) == 0);
}
コード例 #10
0
ファイル: expr.c プロジェクト: thewhoo/ifj15
void after_infix()
{
	printf("expr: Unget token ");
	print_token(token);
	printf("\n");
	printf("expr: Postfix  ");
	stack_print(postfix_output_stack);
	printf("\n");
}
コード例 #11
0
ファイル: stack.c プロジェクト: LindseyB/minorProjects
int main()
{
	struct stack* myStack = stack_new();

	stack_print(myStack);

	stack_push(myStack, 1);
	stack_push(myStack, 2);
	stack_push(myStack, 3);

	stack_print(myStack);

	stack_pop(myStack);

	stack_print(myStack);

	return 0;
}
コード例 #12
0
ファイル: maze.c プロジェクト: syslover33/ctank
/*
 * maze function defination
 * */
static int maze_findAllway(int **arry_maze, int nRow, int nColm)
{
    int iRet=0;
    stack stackM;
    stack_elem selemCur;
    printf_dbg("[F:%s, L:%d]\n", __FUNCTION__, __LINE__);

    if(arry_maze==NULL || nRow<1 || nColm<1) {
        return -1;
    }

    //1. construct stack
    iRet = stack_construct(&stackM, nRow*nColm);

    //2. init current stack element
    selemCur.x = 0;
    selemCur.y = 0;
    selemCur.nDir = -1;

    //3. Deepth-First-Search(DFS) search path
    do {
        iRet = maze_goNextStep(&stackM, &selemCur);

        if(iRet == 1) {
#ifdef DEBUG
            printf_dbg("[F:%s, L:%d] goNextStep success\n", __FUNCTION__, __LINE__);
	        stack_print(&stackM);
#endif
            if(selemCur.x==g_nRow-1 && selemCur.y==g_nColm-1 && g_arry_maze[selemCur.x][selemCur.y]==0) {
	           stack_print(&stackM);
	           iRet = maze_goLastStep(&stackM, &selemCur);
	        }
        } else if(iRet == 0) {
            iRet = maze_goLastStep(&stackM, &selemCur);
        }
    }while(iRet==1);


    //4. search all complete; destruct stack
    stack_destruct(&stackM);

    return iRet;
}
コード例 #13
0
int evaluate_postfix( char* expr )
{
    Stack* st = stack_new();
    char* pBuf;
    char* gen;
    int val; 
    gen = malloc(20*sizeof(char));
    pBuf = malloc(20*sizeof(char));
  //printf ("Splitting string \"%s\" into tokens:\n",expr);
    pBuf = strtok (expr," ");   
    while (pBuf != NULL)
    {
        strcpy(gen,pBuf);
        if(atoi(gen)!=0) 
        {         
            val = atoi(gen);
            stack_push(st,val);
        }
        else
        {
            if(strncmp(gen,"+",1)==0)
            {
                val = ((st->top)->data) + ((st->top->next)->data);
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
            if(strncmp(gen,"-",1)==0)
            {
                val = ((st->top->next)->data) - ((st->top)->data);
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
            if(strncmp(gen,"*",1)==0)
            {
                val = ((st->top)->data) * ((st->top->next)->data);
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
            if(strncmp(gen,"/",1)==0)
            {
                val = ((st->top->next)->data)/((st->top)->data) ;
                stack_pop(st);
                stack_pop(st);
                stack_push(st,val);
            }
       } 
       stack_print(st);   
       pBuf = strtok (NULL, " ");       
    }   
    return st->top->data;
}
コード例 #14
0
ファイル: format_print.c プロジェクト: shawakaze/cmus
static int print_double(char *buf, double num)
{
	char stack[DBL_MAX_LEN], b[DBL_MAX_LEN];
	int i, p = 0;

	i = format_double(b, DBL_MAX_LEN, num) - 1;
	while (i >= 0) {
		stack[p++] = b[i];
		i--;
	}
	return stack_print(buf, stack, p);
}
コード例 #15
0
ファイル: sample.c プロジェクト: Moumou38/ITD
int main(void) {
	A* a = malloc(sizeof(A)), *b = malloc(sizeof(A)), *c = NULL;
	List* l = list_init();
	Stack* s = stack_init();
	Queue* q = queue_init();
	DoubleLinkedList* d = dll_init();
	printf("a: %d\nB: %d\nc: %d\n", (int)a, (int)b, (int)c);
	a->a1 = 1;
	a->a2 = 'c';
	b->a1 = 2;
	b->a2 = 'a';

	printf("\n=== LIST TEST ===\n");
	list_add(l, a, 0);
	list_append(l, b);
	list_remove(l, 0);
	list_print(l);	
	c = list_get(l, 0);
	printf("c: %d\n", (int)c);
	list_delete(l);

	printf("\n=== STACK TEST ===\n");
	stack_push(s, b);
	stack_push(s, a);
	stack_pop(s);
	stack_print(s);
	c = stack_peek(s);
	printf("c: %d\n", (int)c);
	stack_delete(s);

	printf("\n=== QUEUE TEST ===\n");
	queue_push(q, a);
	queue_push(q, b);
	queue_pop(q);
	queue_print(q);
	c = queue_peek(q);
	printf("c: %d\n", (int)c);
	queue_delete(q);

	printf("\n=== DOUBLE LINKED LIST TEST ===\n");
	dll_add(d, b, 0);
	dll_prepend(d, a);
	dll_remove(d, 1);
	dll_print(d);
	c = dll_get(d, 0);
	printf("c: %d\n", (int)c);
	dll_delete(d);

	free(a);
	free(b);
	return 0;
}
コード例 #16
0
void stk_test_print(void) {
	stack_t* s = stack_init(stk_compare, stk_print, stk_clone, stk_destroy);
	CU_ASSERT (s != NULL);
	
	CU_ASSERT (stack_push(s, stk_test_s1) != NULL);;
	CU_ASSERT (stack_push(s, stk_test_s2) != NULL);;
	CU_ASSERT (stack_push(s, stk_test_s3) != NULL);;
	CU_ASSERT (stack_push(s, stk_test_s4) != NULL);;

	stack_print(s, stdout);

	CU_ASSERT (stack_destroy(s) == 0);
}
コード例 #17
0
ファイル: process.c プロジェクト: JonasG/Carp
void shadow_stack_push(Process *process, Obj *o) {
  if(LOG_SHADOW_STACK) {
    printf("Pushing to shadow stack: %p ", o);
    obj_print_cout(o);
    printf("\n");
  }
  if(process->shadow_stack_pos >= SHADOW_STACK_SIZE) {
    printf("Shadow stack overflow.\n");
    shadow_stack_print(process);
    printf("\n\nNormal stack:\n\n");
    stack_print(process);
    exit(1);
  }
  process->shadow_stack[process->shadow_stack_pos++] = o;
}
コード例 #18
0
ファイル: process.c プロジェクト: JonasG/Carp
Obj *stack_pop(Process *process) {
  if(eval_error) {
    return nil;
  }
  if(process->stack_pos <= 0) {
    printf("Stack underflow.\n");
    assert(false);
  }
  if(LOG_STACK) {
    printf("Popping %s\n", obj_to_string(process, process->stack[process->stack_pos - 1])->s);
  }
  Obj *o = process->stack[--process->stack_pos];
  if(LOG_STACK) {
    stack_print(process);
  }
  return o;
}
コード例 #19
0
ファイル: format_print.c プロジェクト: shawakaze/cmus
static int print_num(char *buf, int num)
{
	char stack[20];
	int i, p;

	if (num < 0) {
		if (width == 0)
			width = 1;
		for (i = 0; i < width; i++)
			buf[i] = '?';
		return width;
	}
	p = 0;
	do {
		stack[p++] = num % 10 + '0';
		num /= 10;
	} while (num);

	return stack_print(buf, stack, p);
}
コード例 #20
0
ファイル: p2_e3.c プロジェクト: OscarGB/Prog-2
int main (int argc, char *argv[]){
	FILE *f = NULL;
	Maze *pmaze = NULL;
	Point *ppoint = NULL;
	Stack *pila = NULL; /*Pila donde guardaremos el camino*/
	f = fopen(argv[1], "r"); /*Abrimos el archivo en escritura para no modificar su contenido*/
 	if(!f){
 		printf("Error de apertura de fichero\n");
 		return -1;
 	}
 	pmaze = maze_ini();
 	if(!pmaze){
 		printf("Error reserva pmaze\n");
 		fclose(f);
 		return -1;
 	}
 	if(!maze_read(f, pmaze)){ /*Leemos el archivo para guardar el laberinto*/
 		fclose(f);
 		printf("Error lectura maze\n");
 		maze_free(pmaze);
 		return -1;
 	}
 	fclose(f);
 	ppoint = maze_getInput(pmaze); /*Conseguimos el input de nuestro laberinto*/
 	if(!ppoint){
 		printf("Error ppoint input\n");
 		maze_free(pmaze);
 		return -1;
 	}
 	pila = Recorrer(pmaze, ppoint);
 	if (!pila){ /*Si la pila esta vacía es porque no ha encontrado camino o ha ocurrido algún error*/
 		printf("No es posible encontrar un camino\n");
 	}
 	else{
 		printf("Es posible encontrar un camino\n");
 		stack_print(stdout, pila);
 	}
 	stack_free(pila);
 	maze_free(pmaze);
	return 0;
}
コード例 #21
0
int main(){
	
	int i;
	printf("is stack empty? \n");
	printf(BOOL_PRINT(isEmpty()));
	
	push(10);
	printf("top is at %d\n",tops() );

	push(11);
	push(12);
	push(15);

	i = pop();
	printf("Popped data is %d\n",i );

	printf("top is at %d\n",tops());

	stack_print(); // Note: this is just for vizualizing and this actually results in loss of stack data

	return 0;
}
コード例 #22
0
ファイル: main.c プロジェクト: DavidDiPaola/filth
int main(){
  ex_mem_addr_t i = 0;
  char result;
  stack_t data;
  stack_t flow;

  stack_init(&data);
  stack_init(&flow);

  do{
    //get the next word, return the index
    i = get_word(get_ch, buffer, WORD_LENGTH, i);

    //if statement handling
    if( strncmp(buffer, "if{", WORD_LENGTH) == 0 ){
      //push the conditional to the flow stack
      stack_push(&flow, stack_pop(&data));
      //if the data at the top of the stack is false
      if( !stack_peek(&flow) ){
        //fast-forward to the matching else or curly brace
        i = ffwd(i);
      }
    //else statement handling
    } else if( strncmp(buffer, "}else{", WORD_LENGTH) == 0 ){
      //if the data at the top of the stack is true
      if( stack_peek(&flow) ){
        //fast forward to the matching curly brace
        i = ffwd(i);
      }
    //end curly brace handling
    } else if( strncmp(buffer, "}", WORD_LENGTH) == 0 ){
      //burn the top element of the flow stack
      stack_pop(&flow);
    } else if( strncmp(buffer, "do{", WORD_LENGTH) == 0 ){
      stack_push(&flow, i);
    } else if( strncmp(buffer, "}while", WORD_LENGTH) == 0 ){
      //if the top of the stack is true, loop and don't pop
      if( stack_pop(&data) ){
        i = stack_peek(&flow);
      //otherwise, pop and don't loop
      } else {
        stack_pop(&flow);
      }
    } else {
      //printf("Executing \"%s\"... ", buffer);
      result = exec(buffer, &data);
      //printf("\n");
    }
  } while( (i != 0) &&
           (data.error == STACK_GOOD) &&
           (flow.error == STACK_GOOD) &&
           (result == EXEC_GOOD) );

  if( data.error == STACK_OVERFLOW ){
    printf("STACK OVERFLOW AT %d\n", i);
  }
  if( data.error == STACK_UNDERFLOW ){
    printf("STACK UNDERFLOW AT %d\n", i);
  }
  if( flow.error == STACK_OVERFLOW ){
    printf("FLOW STACK OVERFLOW AT %d\n", i);
  }
  if( flow.error == STACK_UNDERFLOW ){
    printf("FLOW STACK UNDERFLOW AT %d\n", i);
  }
  if( result == EXEC_WORD_NOT_FOUND ){
    printf("UNKNOWN WORD \"%s\" AT %d\n", buffer, i);
  }

  printf("\nHALTED");
  printf(" flow: ");
  stack_print(&flow);
  printf(" data: ");
  stack_print(&data);
  printf("\n");

  return 0;
}
コード例 #23
0
ファイル: arduino.c プロジェクト: hanappe/p2pfoodlab
datapoint_t* arduino_read_data(arduino_t* arduino, int* num_points)
{
        int err = -1; 
        unsigned char sensors; 
        int datastreams[32];
        int num_streams = 0;
        datapoint_t* datapoints = NULL;
        float factors[32];

        *num_points = 0;

        err = arduino_connect(arduino);
        if (err != 0) 
                goto error_recovery;

        err = arduino_get_sensors_(arduino, &sensors);
        if (err != 0)
                goto error_recovery;

        if (sensors & SENSOR_TRH) {
                factors[num_streams] = 0.01f;
                datastreams[num_streams++] = DATASTREAM_T;
                factors[num_streams] = 0.01f;
                datastreams[num_streams++] = DATASTREAM_RH;
        }
        if (sensors & SENSOR_TRHX) {
                factors[num_streams] = 0.01f;
                datastreams[num_streams++] = DATASTREAM_TX;
                factors[num_streams] = 0.01f;
                datastreams[num_streams++] = DATASTREAM_RHX;
        }
        if (sensors & SENSOR_LUM) {
                factors[num_streams] = 1.0f;
                datastreams[num_streams++] = DATASTREAM_LUM;
        }
        if (sensors & SENSOR_USBBAT) {
                factors[num_streams] = 0.01f;
                datastreams[num_streams++] = DATASTREAM_USBBAT;
        }
        if (sensors & SENSOR_SOIL) {
                factors[num_streams] = 1.0f;
                datastreams[num_streams++] = DATASTREAM_SOIL;
        }

        stack_t stack;
        stack.framesize = num_streams + 1;


        err = arduino_set_state_(arduino, STATE_SUSPEND);
        if (err != 0) 
                goto error_recovery;
        
        err = arduino_get_frames_(arduino, &stack.frames);
        if (err != 0)
                goto error_recovery;

        log_info("Arduino: Found %d measurement frames", stack.frames); 

        if (stack.frames == 0) {
                err = arduino_set_state_(arduino, STATE_MEASURING);
                goto clean_exit;
        }

        err = arduino_get_checksum_(arduino, &stack.checksum);
        if (err != 0)
                goto error_recovery;

        log_info("Arduino: Checksum Arduino 0x%02x", stack.checksum); 

        err = arduino_get_offset_(arduino, &stack.offset);
        if (err != 0)
                goto error_recovery;

        log_info("Arduino: Time offset Arduino %lu", (unsigned int) stack.offset); 


        for (int attempt = 0; attempt < 5; attempt++) {

                log_info("Arduino: Download attempt %d", attempt + 1); 

                err = arduino_copy_stack_(arduino, &stack);
                if (err != 0)
                        continue;

                unsigned char* ptr = (unsigned char*) &stack.values[0];
                int len = stack.frames * stack.framesize * sizeof(sensor_value_t);
                unsigned char checksum = crc8(0, ptr, len);
                
                log_info("Arduino: Checksum Linux 0x%02x", checksum); 
                stack_print(&stack);

                if (checksum != stack.checksum)
                        err = -1;

                if (err == 0)
                        break;
        }
        
        if (err == 0)
                datapoints = arduino_convert_stack_(arduino, &stack, 
                                                    datastreams,
                                                    factors,
                                                    num_points);

 clean_exit:
 error_recovery:

        if (err == 0) {
                log_info("Arduino: Download successful"); 
                err = arduino_set_state_(arduino, STATE_RESETSTACK);
        } else {
                log_info("Arduino: Download failed"); 
                err = arduino_set_state_(arduino, STATE_MEASURING);
                arduino_disconnect(arduino);
        }

        return datapoints;
}
コード例 #24
0
ファイル: builtins.c プロジェクト: thomaswhitcomb/4th
void stack_dot_s(){
  stack_print(&data_stack);
}
コード例 #25
0
ファイル: main.c プロジェクト: OlgaYashan/-little-princess
void printStacks(stack_t * a) {
    system("cls");
    printf("Stack: ");
    stack_print(a);
    Sleep(500);
}
コード例 #26
0
ファイル: we.c プロジェクト: rajiv256/CodingIsFun
int main()
{

int k,n,i;
int* error=(void*)malloc(sizeof(int));

#ifdef DEBUG
printf("This program will iterate the line that is standing until the row is filled,\nor the condition for non occupancy is satisified.\n");
printf("Take an element(top element) of the line.\n");
printf("If it is equal to the pre-defined 'temp' variable it will go into the booked row.\n"); 
printf("else, it is assigned to one of the temporary rows.\n");
printf("This assigning is also done in a special manner...so that it is the only possible way.\n");
printf("Apparently in each step,the temporary rows are iterated so as to check if the 'temp' is in one of the temporary rows\n");


#endif





Stack* row=stack_new();
Stack* line=stack_new();
scanf("%d %d",&k,&n);
Stack* arr[k];
int* temp=(void*)malloc(sizeof(int));
*temp=1;
for (i=0;i<n;i++)
{
	int a;
	scanf("%d",&a);
	stack_push(line,a);
}
for (i=0;i<k;i++)
{
	arr[i]=stack_new();
}
int r;
while (stack_size(row)!=n)
{
	if (stack_top(line,error)==*temp)
	{
		row=stack_push(row,*temp);
		line=stack_pop(line);
		*temp+=1;
			
	}

	else
	{
		if (temp_in_stack(arr,temp,k)==1)
		{	
			for (i=0;i<k;i++)
			{
				if (*temp==stack_top(arr[i],error))
				{
					row=stack_push(row,*temp);
					arr[i]=stack_pop(arr[i]);
					*temp+=1;
						
				}
			}
		
		}
		else
		{	
			int j=stack_top(line,error);
			for (i=0;i<k;i++)
			{
				if (j<stack_top(arr[i],error))
				{
					arr[i]=stack_push(arr[i],j);
					line=stack_pop(line);
					break;
				}
				//else
				//{
				//	printf("NO\n");
				//	return 0;
				//}
			}
			if (i==k-1)
			{
			for (i=0;i<k;i++)
			{
				if (stack_size(arr[i])==0)
				{
					arr[i]=stack_push(arr[i],j);
					line=stack_pop(line);
					break;
				}
			}
			
			}
			
		}
				
		}
	if (stack_top(line,error)!=*temp)
	{
		int j=stack_top(line,error);
		int p=0;
		for (i=0;i<k;i++)
		{
			if (stack_top(arr[i],error)<j)
				p+=1;
		}
		if (p==k)
			if (temp_in_stack(arr,temp,k)==0)
				{
				printf("No\n");
				return 0;
				}
	}
	printf("line\n");
	stack_print(line);
	printf("row\n");
	stack_print(row);
	for (i=0;i<k;i++)
		{
		printf("arr %d\n",i);
		stack_print(arr[i]);
		}
	
	




}




printf("Yes\n");




return 0;
}
コード例 #27
0
int main(int argc, char *argv[])
{
	char command[32];
	int eventsNr, eventId, procId, priority;
	int i, iteration;
	TStack **eventsStacks;
	TQueue *procQ;

	// Daca nu exista destule argumente la rularea programului, atunci opreste executia
	if (argc < 3)
	{
		printf("Argumente insuficiente!\n");
		return -1;
	}

	// Seteaza fisierele de intrare si de iesire
	freopen(argv[1], "r", stdin);
	freopen(argv[2], "w", stdout);

	// Citeste numarul de event-uri si creeaza stivele lor
	fscanf(stdin, "%d", &eventsNr);

	eventsStacks = calloc(eventsNr,  sizeof(TStack*));
	for (i = 0; i < eventsNr; i++)
	{
		eventsStacks[i] = stack_new(sizeof(TProcess));
	}

	// Creeaza coada de prioritati
	procQ = queue_new(sizeof(TProcess), compare_process);

	// Citeste si executa comenzile din fisierul de intrare
	iteration = 0;

	while (fscanf(stdin, "%s", command) != EOF)
	{
		iteration++;

		if (strcmp(command, "start") == 0)
		{
			fscanf(stdin, "%d", &procId);
			fscanf(stdin, "%d", &priority);

			// Creeaza un proces
			TProcess p;
			p.id = procId;
			p.priority = priority;
			p.iteration = iteration;

			// Introduce procesul creat in coada de prioritati
			queue_push(procQ, &p);
		}
		else if (strcmp(command, "wait") == 0)
		{
			fscanf(stdin, "%d", &eventId);
			fscanf(stdin, "%d", &procId);

			// Creaza o stiva auxiliara
			TStack *aux = stack_new(sizeof(TProcess));

			// Muta procesele in stiva auxiliara pana cand procesul
			// cautat este gasit si mutat in stiva evenimentului
			TProcess *p;
			while (!queue_isEmpty(procQ))
			{
				p = queue_pop(procQ);

				if (p->id == procId)
				{
					stack_push(eventsStacks[eventId], p);
					free_process(p);
					break;
				}
				
				stack_push(aux, p);
				free_process(p);
			}

			// Muta procesele din stiva auxiliara inapoi in coada
			// de prioritati
			while (!stack_isEmpty(aux))
			{
				p = stack_pop(aux);
				queue_push(procQ, p);
				free_process(p);
			}

			// Distruge stiva auxiliara
			stack_destroy(&aux, free_process);
		}
		else if (strcmp(command, "event") == 0)
		{
			fscanf(stdin, "%d", &eventId);

			// Muta procesele din stiva evenimentului in coada
			// de prioritati
			TProcess *p;
			while (!stack_isEmpty(eventsStacks[eventId]))
			{
				p = stack_pop(eventsStacks[eventId]);
				queue_push(procQ, p);
				free_process(p);
			}
		}
		else if (strcmp(command, "end") == 0)
		{
			fscanf(stdin, "%d", &procId);

			// Creaza o stiva auxiliara
			TStack *aux = stack_new(sizeof(TProcess));

			// Muta procesele in stiva auxiliara pana cand procesul
			// cautat este gasit si sters
			TProcess *p;
			while (!queue_isEmpty(procQ))
			{
				p = queue_pop(procQ);

				if (p->id == procId)
				{
					free_process(p);
					break;
				}
				
				stack_push(aux, p);
				free_process(p);
			}

			// Muta procesele din stiva auxiliara inapoi in coada
			// de prioritati
			while (!stack_isEmpty(aux))
			{
				p = stack_pop(aux);
				queue_push(procQ, p);
				free_process(p);
			}

			// Distruge stiva auxiliara
			stack_destroy(&aux, free_process);
		}

		// Afiseaza iteratia
		printf("%d\n", iteration);

		// Afiseaza coada de prioritati
		if (!queue_isEmpty(procQ))
		{
			queue_print(procQ, print_process);
		}
		else
		{
			printf("\n");
		}

		// Afiseaza stivele
		for (i = 0; i < eventsNr; i++)
		{
			if (!stack_isEmpty(eventsStacks[i]))
			{
				printf("%d: ", i);
				stack_print(eventsStacks[i], print_process);
			}
		}

		printf("\n");
	}

	// Elibereaza memoria
	queue_destroy(&procQ, free_process);

	for (i = 0; i < eventsNr; i++)
	{
		stack_destroy(&eventsStacks[i], free_process);
	}
	free(eventsStacks);

	return 0;
}
コード例 #28
0
ファイル: bcode.c プロジェクト: FreeBSDFoundation/freebsd
static __inline void
print_stack(void)
{

	stack_print(stdout, &bmachine.stack, "", bmachine.obase);
}