Thread: stack,push pop etc

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    13

    stack,push pop etc

    first and second done

    -third- error done also
    Last edited by maquina; 09-29-2007 at 02:37 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there's some places missing a ;
    And you use = where you should be using ==

    Also, your indentation SUCKS.

    Code you post to a message board should be indented with either all spaces or all tabs (preferably 4 spaces). If you mix and match, then maybe your editor manages to sort it out for you, but HTML will make a complete arse of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    .. What?

    How about you take your time to produce a question so we might know what you're trying to ask us?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    13
    code
    Last edited by maquina; 09-29-2007 at 09:16 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to:
    1. Search and replace tabs with 4 spaces BEFORE posting code.
    2. Post the error message texts (which will actually be in plain English that we can understand, and have all the details)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    13
    let me explain.. the whole problem is picking a entrada.txt
    in entrada.txt there are several operations with arrays[][] and u will make it by passing the expression to pole notation(is that the name??)

    well.. there are two erros now i didn't recognize what's the problem

    inicio = matrizes (entrada,inicio);
    case')': while (topo->prox!=NULL && topo->r='('){

    feel free to say if u saw another error

    C:\Documents and Settings\Administrador\Desktop\ep3\main.c In function `main':
    84 C:\Documents and Settings\Administrador\Desktop\ep3\main.c [Warning] passing arg 1 of `matrizes' from incompatible pointer type
    C:\Documents and Settings\Administrador\Desktop\ep3\main.c In function `converteParaPosfixa':
    201 C:\Documents and Settings\Administrador\Desktop\ep3\main.c invalid lvalue in assignment
    456 C:\Documents and Settings\Administrador\Desktop\ep3\main.c syntax error at end of input
    C:\Documents and Settings\Administrador\Desktop\ep3\Makefile.win [Build Error] [main.o] Error 1

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    1
    main.c:84: warning: passing argument 1 of ‘matrizes’ from incompatible pointer type

    This means exatcly what it says. The first argument you're trying to pass to "matrizes" function is incompatible with what that function is expecting. The function expects a pointer to a "Matriz" (* Matriz) and you're passing a pointer to a file, a filehandle (FILE *)

    main.c:201: error: invalid lvalue in assignment

    The left value of an assignement is invalid. "topo->r = '(' " should be "topo->r == '(' ". Always remember to use comparison operators when (duh) comparing values, instead of the assignement operator.

    main.c:456: error: expected declaration or statement at end of input
    main.c:456: error: expected declaration or statement at end of input

    That usually means you either forgot to return a value in a non-void function or you mis-matched a closing curly braces, ... Your really poor indentation seems to make it really hard to notice those kind of errors... it seems that converteParaPosFixa has two curly braces missing... I think there's another one somewhere, not crazy enough to search that code though...

    Piva

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    13
    well now is a lot nicer and it runs

    it seems there are some loopings iu don't know why

    tomorrow i will see in linux if i got it

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h>
    
    /*para as matrizes*/
    typedef struct Mat{
        int id, lin, col;
        double ** data;
    	struct Mat *prox;
    } Matriz;
    
    /*struc para facilitar a funcao*/
    typedef struct Cel{
        int valor;
        struct Cel * prox;
    } celula;
    
    celula * inserecel (celula *fila, int t);
    int removecel (celula *fila);
    
    /*pilha auxiliar para trasnformar infixa para posfixa*/
    typedef struct simb_{
    	char r;
    	struct simb_ *prox;
    } simb;
    
    void push_simb (simb * topo, char c);
    int pop_simb (simb * topo);
    
    #define DIG 10
    #define TAM 80
    
    
    Matriz * acharPeloId(int id2, Matriz *inicio);
    Matriz * somamatriz(Matriz *m1, Matriz *m2);
    Matriz * multiplicaescalar(Matriz *m1, Matriz *m2);
    Matriz * multiplicamatrizes(Matriz *m1, Matriz *m2);
    Matriz * diminuimatriz(Matriz *m1, Matriz *m2);
    double ** fazermatriz (int l, int c );
    Matriz * matrizesentrada (FILE * entrada, Matriz * inicio);
    void *mallocX (unsigned int nbytes);
    celula * converteParaPosfixa(char * infixa);
    Matriz * calculaExpressao(celula * cab, Matriz * inicio );
    Matriz * matrizes (Matriz * inicio, Matriz * colocar);
    void push_pilha (celula * topo, int c);
    int pop_pilha (celula * topo);
    
    
    int main (int argc, char *argv[])
    {
    	FILE * entrada;
        FILE * saida;
    	char a;
    	char infixa[TAM+1];
    	char * infixa2;
    	Matriz * inicio = NULL; /*lista pra guardar as matrizes*/
    	int i,j;
    	celula * cab; /*lista para guardar a posfixa*/
    	cab=(celula*)mallocX(sizeof(celula));
    	cab->prox=cab; /*fila circular*/
    	
        entrada = fopen (argv[1], "r");
        saida = fopen (argv[2], "w"); 
        if (entrada == NULL) 
          printf ("\nNao encontrei arquivo\n");
    	
        
        fscanf (entrada, "&#37;c", &a); 
    
        /*testa se o char &#233; f para terminar de executar o programa*/
    	while (a!='f') {
          
          /*testa se o char em e se for guarda as matrizes na lista*/
          if (a=='m') {
                
      	  inicio = matrizesentrada (entrada,inicio);
    
    	               }   
          /*testa se o char  e para come&#231;ar o calculo das expressoes com as matrizes*/    
          else {
                      /*pega a infixa*/
             fscanf(entrada, " %c", &infixa[0]);
             for (i=1;infixa[i]!='\n';i++)
                 fscanf(entrada, " %c", &infixa[i]);
             infixa[i++]='\0'; /*coloca um final*/
             /*converte para posfixa*/         
             infixa2=&infixa[0];
             cab=converteParaPosfixa(infixa2);
             inicio= calculaExpressao( cab, inicio );
             /*impressao da matriz resultado*/
             for (i=0;i<(inicio->lin);i++)
    		     for (j=0;j<(inicio->col);j++)
    			fprintf(saida, "%lf", (inicio->data)[i][j]);		
                       }
               fscanf (entrada, "%c", &a); 
                       }/*fim do while*/
        fclose(entrada);
        fclose(saida);
        free(infixa2);
        free(cab);
        free(inicio);
    	return 0;
    }
    
    /*expressao que calcula a expressao e retorna o ponteiro apontado para matriz resultado final*/
    Matriz * calculaExpressao(celula * cab, Matriz *inicio)
    {
        Matriz * mat1;
        Matriz * mat2;
        Matriz * mat3;   
        celula *pilha=NULL;
        int val1, val2, count=1;
    	while (cab!=NULL){
            if (cab->valor>=0) push_pilha(pilha, cab->valor);
    		else{
    	  		val1=pop_pilha(pilha);
    			val2=pop_pilha(pilha);
    			if (count>1) val2=1000+count;             
                             }
    			mat1 = acharPeloId(val1, inicio);
                mat2 = acharPeloId(val2, inicio);
    			
    			 /*se a operacao for + chama as funcoes para tal e coloca resultado na lista*/ 
    			if (cab->valor==-1)  {
                    mat3= somamatriz(mat1, mat2);
                    mat3->id=(1000+count);
                    inicio = matrizes (inicio, mat3);
                    count++;                    
                                        }
                /*se a operacao for - chama as funcoes para tal e coloca resultado na lista*/ 
    			else if (cab->valor==-2) {
                     
                    mat3= diminuimatriz(mat1, mat2);
                    mat3->id=(1000+count);
                    inicio = matrizes (inicio, mat3);
                    count++;
                                             }
                /*se a operacao for * chama as funcoes para tal e coloca resultado na lista*/                             
    			else if (cab->valor==-3) {
    			 
                    mat3= multiplicamatrizes(mat1, mat2);
                    mat3->id=(1000+count);
                    inicio = matrizes (inicio, mat3);
                    count++;
                                           }
                    
    			else if (cab->valor==-4) {
    			/*se a operacao for . chama as funcoes para tal e coloca resultado na lista*/
                    mat3= multiplicaescalar(mat1, mat2);
                    mat3->id=(1000+count);
                    inicio = matrizes (inicio, mat3);
                    count++;
                                          }
               cab=cab->prox;
                                }	
    	return inicio;
        
    } 
    
                   
    /*transforma uma expressao infixa para posfixa */
    celula * converteParaPosfixa(char * infixa)
    {
    	celula *cab; /*cabe&#231;a*/
    	simb *topo;
    	int i=0,j;
    	char str [DIG+1],c;
    	int count,t;
        cab=(celula*)mallocX (sizeof (celula));
    	cab->prox=cab; 
    	topo=(simb*)mallocX(sizeof (simb));
    	topo->prox=NULL;
    	while (infixa[i]!='\n')
    	{
    		switch(infixa[i]){
    			case '0':
    			case '1':
    			case '2':
    			case '3':
    			case '4':	
    			case '5':
    			case '6':
    			case '7':
    			case '8':
    			case '9': j=0;
    				while (infixa[i]>='0' && infixa[i]<='9'){
    					str[j]=infixa[i];
    					i++;
    					j++;
    					count++;
    			                  			      }
                   if (count==1){t=str[0]-'0';}
                   else t = atoi(str);
    				str[j]='\0';
    				
    				i--;				
    				cab=inserecel(cab,t);
    				break;
    				
    			case'(': push_simb (topo, '('); break;
    			
    			case')': while (topo->prox!=NULL && topo->r=='('){
    				 	str[0]=pop_simb(topo);
    				 	str[1]='\0';
    				 	cab=inserecel(cab,t);
    										    }
    				 if (topo->prox!=NULL) pop_simb(topo);
    				 break;
    			
                case '.':	 
    			case '*': while (topo->prox!=NULL && topo->r!='(' && topo->r!='+' && topo->r!='-') {
    					str[0]=pop_simb(topo);
    					str[1]='\0';
    					if (str[0]=='*'){ t=-3;}
    					if (str[0]=='.'){ t=-4;}
    					
    					cab=inserecel(cab,t);
                    }
    					push_simb(topo,infixa[i]); 
    				  break;
    	
    			case ' ': break; /*se for em branco s&#243; le o proximo*/
    			case '+': 
    			case '-': 
                         while (topo->prox!=NULL && topo->r!='(') {
    					str[0]=pop_simb(topo);
    					str[1]='\0';
    					
    					if (str[0]=='+'){ t=-1;}
    					if (str[0]=='-'){ t=-2;}
                    
    					cab=inserecel(cab,t);
                  }   
    					push_simb(topo,infixa[i]); 
    				  break;
    				  
    			/*nenhum dos otros casos*/
    			default: printf ("ERRO NA EXPRESSAO INFIXA \n"); break; } /*fim do switch*/
    
    		i++;
    		} /*fim do while*/
            
    		
    		c=pop_simb(topo);
    		if (c>='0' && c<='9'){ c=c-'0';}
    		else if ( c=='*') c=-2;
    		else if ( c=='+') c==-1;
    		else if ( c=='-') c=-3;
    		else if ( c=='.') c=-4;
    		
    		cab=inserecel(cab,c);
            return cab;
    		}
    	
    
    /*testa para nao ocasionar problema de memoria*/
    void *mallocX (unsigned int nbytes) 
    {
       void *ptr;
       ptr = malloc (nbytes);
       if (ptr == NULL) {
          printf ("Socorro! malloc devolveu NULL!\n");
          exit (EXIT_FAILURE);
       }
       return ptr;
    }
    
    	
    /*fun&#231;&#227;o q soma as matrizes e retorna o ponteiro do resultado*/
    Matriz * somamatriz(Matriz * m1, Matriz * m2)
    {
        int i, j;
    	Matriz * mat3;
    	mat3->data=fazermatriz((m1->lin),(m1->col));
                     
        /*faz a soma das das Matrizes */
              for (i=0; i<(m1->lin); i++)
                 for (j=0;j<(m1->col);j++)
                     (mat3->data)[i][j]= (m1->data)[i][j] + (m2->data)[i][j]; 
                                            
    	return mat3;   
    }    
    
    /*fun&#231;&#227;o q multiplica 1 matrize pelo escalar e retorna o ponteiro do resultado*/            
    Matriz * multiplicaescalar(Matriz * m1, Matriz * m2)
    {
    
        int i, j;
    	Matriz * mat3;
    	mat3->data=fazermatriz(m1->lin,m1->col);
        
        /*faz a multiplica&#231;&#227;o pelo escalar */
        if ((m2->lin)==1 && (m2->col)==1){
              for (i=0; i<(m1->lin); i++)
                 for (j=0;j<(m1->col);j++)
                     ((mat3->data)[i][j])= ((m1->data)[i][j]) * ((m2->data[0][0]));
                                              } 
        else {
              for (i=0; i<(m1->lin); i++)
                 for (j=0;j<(m1->col);j++)
                     ((mat3->data)[i][j])= ((m2->data)[i][j]) * ((m1->data[0][0]));
             }                                       
                     
        return mat3;
    }
    
    /*fun&#231;&#227;o q multiplica as matrizes e retorna o ponteiro resultado*/
    Matriz * multiplicamatrizes(Matriz * m1, Matriz * m2)
    {
       int i, j, v;
    
       Matriz * mat3;
       mat3->data=fazermatriz(m1->lin,m2->col);
                     
       /*faz a multiplica&#231;&#227;o das matrizes */   
       for (i = 0 ; i < (m1->lin); i++ ){
          for (j = 0; j < (m2->col); j++) {
                (mat3->data)[i][j]=0;
             for (v = 0; v < (m1->col); v++)
                (mat3->data)[i][j] = (mat3->data)[i][j] + ((m1->data)[i][v] * (m2->data)[v][j]);
                                      }
                                   }
        return mat3;
    }
    
    /*fun&#231;&#227;o q diminui as matrizes e retorna o ponteiro do resultado*/
    Matriz * diminuimatriz(Matriz * m1, Matriz * m2)
    {
        int i, j;
    	
        Matriz * mat3;
    	mat3->data=fazermatriz(m1->lin,m1->col);
                     
        /*faz a soma das duas matrizes */
              for (i=0; i<(m1->lin); i++)
                 for (j=0;j<(m1->col);j++)
                     (mat3->data)[i][j]= (m1->data)[i][j] - (m2->data)[i][j]; 
                                            
    	return mat3;   
    } 
    
    /*aloca espa&#231;o paras matrizes*/
    double ** fazermatriz (int l, int c)
    {
    	int i;
    	double **m;
    	m=(double **)mallocX(l*sizeof(double*));
    	for(i=0;i<l;i++)
    		m[i]=(double*)mallocX(c*sizeof(double));
    	return m;
    	
    }
    
    /*puxa as matrizes do entrada colocando na lista*/
    Matriz * matrizesentrada (FILE * entrada,Matriz * inicio)
    {
    	int lin,id,col,i,j;
    	Matriz * nova;
    	fscanf(entrada, "%d", &id);
    	fscanf(entrada, "%d", &lin);
    	fscanf(entrada, "%d", &col);
    	
    	nova->lin=lin;
    	nova->col=col;
    	nova->id=id;
        (nova->prox)=inicio;
    	nova->data=fazermatriz(lin,col);
    	
    	for (i=0;i<(nova->lin);i++)
    		for	(j=0;j<(nova->col);j++)
    			fscanf(entrada, " %f", (&(nova->data)[i][j]));		
    	return nova;
    
    }
    
    /*coloca uma nova struct na lista */
    Matriz * matrizes (Matriz * inicio, Matriz * colocar)
    {
    	Matriz * nova;
    	
    	nova->lin=colocar->lin;
    	nova->col=colocar->col;
    	nova->id=colocar->id;
        (nova->prox)=inicio;
    	nova->data=colocar->data;
    	
    	return nova;
    
    }
    
    /*acha a matriz pelo id associado a ela e retorna o ponteiro q aponta para ela*/
    Matriz * acharPeloId(int id2, Matriz *inicio)
    {
    	Matriz * aux;
    	aux=inicio;
    	while(aux != NULL && (inicio->id)!=id2)
    		{
    	   aux=aux->prox;		
    	    }
    	return aux;
    }
    
    /*empilha um char c*/
    void push_simb (simb * topo, char c)
    {
         simb *nova;
         nova = (simb*) mallocX(sizeof(simb)); 
         nova->r=c;
         nova->prox=topo->prox;
         topo->prox=nova;
         return;
    }
    
    /*desempilha e retorna o char desimpilhado*/
    int pop_simb (simb * topo)
    {
         simb *aux;
         aux=topo->prox;
         aux->r=topo->r;
         topo->prox=aux->prox;
         free(aux);
         return topo->r;
    }
    
    /*insere um elemento na fim da lista */
    celula * inserecel (celula *fila, int t)
    {
         celula *nova;
         nova = (celula*)mallocX(sizeof(celula));
         nova->valor=t;
         nova->prox=fila->prox;
         fila->prox=nova;
         return (nova);
    }
    
    /*remove um elemento da lista e retorna o valor que est&#225; na celula retirada*/
    int removecel (celula *fila)
    {
         int v;
         celula * aux;
         aux=fila->prox;
         v=aux->valor;
         fila->prox=aux->prox;
         free(aux);
         return v;     
    }
    
    /*empilha um int c*/
    void push_pilha (celula * topo, int c)
    {
         celula *nova;
         nova = (celula*) mallocX(sizeof(celula)); 
         nova->valor=c;
         nova->prox=topo->prox;
         topo->prox=nova;
         return;
    }
    
    /*desempilha*/
    int pop_pilha (celula * topo)
    {
         celula *aux;
         aux=topo->prox;
         aux->valor=topo->valor;
         topo->prox=aux->prox;
         free(aux);
         return topo->valor;
    }

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    first and second done

    -third- error done also
    code
    Please don't edit your posts to remove everything. It irreparably damages the flow of the thread and makes it virtually useless for future readers.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    13
    the first wouldn't help.. and the code i deleted to maybe ppl on my class doesn't find it and maybe copy
    well will remember that thanks

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >the first wouldn't help.. and the code i deleted to maybe ppl on my class doesn't find it and maybe copy

    That's an easy problem to solve, don't ask homework questions, see your teacher.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and the code i deleted to maybe ppl on my class doesn't find it and maybe copy
    That's their problem, not yours.
    You only have to worry about proving you're the originator, which is now impossible.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. push n pop
    By salmansalman in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2008, 09:41 AM
  2. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 02:08 AM
  3. Stack using push and pop
    By silicon in forum C++ Programming
    Replies: 5
    Last Post: 11-03-2003, 04:54 PM
  4. pop up menu problem
    By bigtamscot in forum Windows Programming
    Replies: 5
    Last Post: 06-10-2003, 01:46 AM
  5. pop function
    By Troll_King in forum C Programming
    Replies: 12
    Last Post: 10-15-2001, 04:45 AM