Update a1/program.c
This commit is contained in:
		
							parent
							
								
									a06a9d5f09
								
							
						
					
					
						commit
						19067c47b9
					
				
							
								
								
									
										79
									
								
								a1/program.c
									
									
									
									
									
								
							
							
						
						
									
										79
									
								
								a1/program.c
									
									
									
									
									
								
							@ -11,11 +11,6 @@ struct Stack
 | 
				
			|||||||
   int size;
 | 
					   int size;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void push_stack(struct Stack* stack, float value);
 | 
					 | 
				
			||||||
char pop_stack(struct Stack* stack);
 | 
					 | 
				
			||||||
int count_stack(struct Stack* stack);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void push_stack(struct Stack* stack, char value)
 | 
					void push_stack(struct Stack* stack, char value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    assert(stack->size < STACK_SIZE); 
 | 
					    assert(stack->size < STACK_SIZE); 
 | 
				
			||||||
@ -38,7 +33,7 @@ void print_stack(struct Stack* stack)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    for (int i=0; i<stack->size; i++)
 | 
					    for (int i=0; i<stack->size; i++)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        printf("%.2f ", stack->values[i]);
 | 
					        printf("%c ", stack->values[i]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    printf("\n");
 | 
					    printf("\n");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -86,77 +81,9 @@ int main()
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//Crossed bracket > in 12, expected )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    int err=0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    char buf[505];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    do
 | 
					 | 
				
			||||||
    {        
 | 
					 | 
				
			||||||
      char* p=fgets(buf, 299, stdin);    
 | 
					 | 
				
			||||||
      if(p)
 | 
					 | 
				
			||||||
      {
 | 
					 | 
				
			||||||
          char c=buf[0];
 | 
					 | 
				
			||||||
          float op1, op2, rez;
 | 
					 | 
				
			||||||
          if(c=='+'||c=='-'||c=='/'||c=='*')  
 | 
					 | 
				
			||||||
          {
 | 
					 | 
				
			||||||
              if(count_stack(&mystack)<2)
 | 
					 | 
				
			||||||
              {
 | 
					 | 
				
			||||||
                  printf("not enough operands\n");
 | 
					 | 
				
			||||||
                  err=3;
 | 
					 | 
				
			||||||
                  break;
 | 
					 | 
				
			||||||
              }
 | 
					 | 
				
			||||||
              op2=pop_stack(&mystack);
 | 
					 | 
				
			||||||
              op1=pop_stack(&mystack);
 | 
					 | 
				
			||||||
              switch(c)
 | 
					 | 
				
			||||||
              {
 | 
					 | 
				
			||||||
                  case '+': rez=op1+op2; break;
 | 
					 | 
				
			||||||
                  case '-': rez=op1-op2; break;
 | 
					 | 
				
			||||||
                  case '/': 
 | 
					 | 
				
			||||||
                            if(c=='/' && op2==0) 
 | 
					 | 
				
			||||||
                            {
 | 
					 | 
				
			||||||
                                err=1;
 | 
					 | 
				
			||||||
                                printf("division by zero\n");  
 | 
					 | 
				
			||||||
                            }
 | 
					 | 
				
			||||||
                            else
 | 
					 | 
				
			||||||
                                rez=op1/op2;
 | 
					 | 
				
			||||||
                            break;
 | 
					 | 
				
			||||||
                  case '*': rez=op1*op2; break;
 | 
					 | 
				
			||||||
              }              
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
          else 
 | 
					 | 
				
			||||||
          {
 | 
					 | 
				
			||||||
              if(isdigit(c))
 | 
					 | 
				
			||||||
              {
 | 
					 | 
				
			||||||
                rez=atof(buf);
 | 
					 | 
				
			||||||
              }
 | 
					 | 
				
			||||||
              else
 | 
					 | 
				
			||||||
              {
 | 
					 | 
				
			||||||
                err=1;
 | 
					 | 
				
			||||||
                printf("bad input\n");
 | 
					 | 
				
			||||||
              }
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
          
 | 
					 | 
				
			||||||
          if(!err)
 | 
					 | 
				
			||||||
          {
 | 
					 | 
				
			||||||
             if(count_stack(&mystack)==STACK_SIZE)  
 | 
					 | 
				
			||||||
             {
 | 
					 | 
				
			||||||
                 printf("full stack\n");
 | 
					 | 
				
			||||||
                 err=4;
 | 
					 | 
				
			||||||
                 break;
 | 
					 | 
				
			||||||
             }
 | 
					 | 
				
			||||||
            push_stack(&mystack, rez); 
 | 
					 | 
				
			||||||
            print_stack(&mystack);
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
      }  
 | 
					 | 
				
			||||||
      else break;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    }while(1);
 | 
					 | 
				
			||||||
    if(!err)
 | 
					 | 
				
			||||||
        printf("no input\n");
 | 
					 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					       
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user