Ejemplo n.º 1
0
int main()
{
	char s[MAXOP];
	getop(s);
	printf("s=%s\n", s);
	ungets(s);
	getop(s);
	printf("s=%s\n", s);
	getop(s);
	printf("s=%s\n", s);
	return 0;
}
Ejemplo n.º 2
0
/*
 *  Write the record to the storage in case there are write and closes the
 *  record and frees up the stuff. With the pickled buf for each udf_record
 *  it create single pickled buf for the entire LDT to be sent to the remote
 *  for replica.
 *
 *  Parameter:
 *  	lrecord : LDT record to operate on
 *  	pickled_* (out) to be populated is null if there was delete
 *		lrecord_op (out) is set properly for the entire ldt
 *	set_id : Set id for record. Passed for delete operation.
 *
 *  Returns: 0 on success 
 *           otherwise on failure
 */
static int
rw_finish(ldt_record *lrecord, write_request *wr, udf_optype * lrecord_op, uint16_t set_id)
{
	int subrec_count = 0;
	udf_optype h_urecord_op = UDF_OPTYPE_READ;
	*lrecord_op           = UDF_OPTYPE_READ;
	udf_record *h_urecord = as_rec_source(lrecord->h_urec);
	bool is_ldt           = false;
	int  ret              = 0;

	getop(h_urecord, &h_urecord_op);

	if (h_urecord_op == UDF_OPTYPE_DELETE) {
		post_processing(h_urecord, &h_urecord_op, set_id);
		wr->pickled_buf      = NULL;
		wr->pickled_sz       = 0;
		as_rec_props_clear(&wr->pickled_rec_props);
		*lrecord_op  = UDF_OPTYPE_DELETE;
	} else {

		if (h_urecord_op == UDF_OPTYPE_WRITE) {
			*lrecord_op = UDF_OPTYPE_WRITE;
		}

		FOR_EACH_SUBRECORD(i, j, lrecord) {
			udf_optype c_urecord_op = UDF_OPTYPE_READ;
			udf_record *c_urecord = &lrecord->chunk[i].slots[j].c_urecord;
			getop(c_urecord, &c_urecord_op);

			if (UDF_OP_IS_WRITE(c_urecord_op)) {
				is_ldt = true;
				subrec_count++;
			}
			post_processing(c_urecord, &c_urecord_op, set_id);
		}

		// Process the parent record in the end .. this is to make sure
		// the lock is held till the end. 
		post_processing(h_urecord, &h_urecord_op, set_id);

		if (is_ldt) {
			// Create the multiop pickled buf for thr_rw.c
			ret = as_ldt_record_pickle(lrecord, &wr->pickled_buf, &wr->pickled_sz);
			FOR_EACH_SUBRECORD(i, j, lrecord) {
				udf_record *c_urecord = &lrecord->chunk[i].slots[j].c_urecord;
				// Cleanup in case pickle code bailed out
				// 1. either because this single node run no replica
				// 2. failed to pack stuff up.
				udf_record_cleanup(c_urecord, true);
			}
		} else {
Ejemplo n.º 3
0
int main()
{
    int type;
    double op2;
    char s[MAXOP];

    while ((type = getop(s)) != EOF) {
        switch (type) {
            case NUMBER:
                push(atof(s));
                break;
            case LIBFUNC:
                push(dofunc(s));
                break;
            case GETVAR:
                // 1 a= a +
                push(get_var(s[0]));
                break;
            case SETVAR:
                // 1 a=
                op2 = pop();
                save_var(op2, s[0]);
                push(op2);
                break;
            case '+':
                push(pop() + pop());
                break;
            case '*':
                push(pop() * pop());
                break;
            case '-':
                op2 = pop();
                push(pop() - op2);
                break;
            case '/':
                op2 = pop();
                if (op2 != 0.0)
                    push(pop() / op2);
                else
                    printf("error: zero divisor\n");
                break;
            case '%':
                op2 = pop();
                if (op2 != 0.0)
                    push(fmod(pop(), op2));
                else
                    printf("error: zero divisor\n");
                break;
            case '\n':
                recent_result = pop();
                printf("\t = %.8g\n", recent_result);
                break;
            default:
                printf("error: unknown command %s\n", s);
                break;
        }
    }

    return 0;
}
Ejemplo n.º 4
0
int main(){
	int //type, 
	len;
	int type;
	double op2;
	char s[MAXOP];
	char line[MAXOP];
	while ((len = get_line(line, MAXOP)) > 0){
		lineindex = 0;
		while (lineindex < len){
			type = getop(s, line,len);
			lineindex++;
			// printf("%f\n", (atof(s)));
			
			
			switch(type){
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if (op2 != 0.0)
					push(pop() / op2);
				else
					printf("error: zero divisor\n");
				break;
			case '%':
				op2 = pop();
				push((long) pop() % (long) op2);
				break;
			case ('e'):
				push(exp(pop()));
				break;
			case ('^'):
				push(pow(pop(), pop()));
				break;
			case ('s'):
				push(sin(pop()));
				break;
			case '\n':
				printf("\t%.8g\n", pop());
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;			
			}
		}
	}
	return 0;
}
Ejemplo n.º 5
0
Archivo: sac.c Proyecto: iogf/sample
double calc() {
	double x, y;
	char op;

	pop(&x);
	pop(&y);

	op = *getop();

	printf("%lf %c %lf", x, op, y);
	switch(op) {
		case POW:
			return pow(x, y);
		case MUL: 
			return x * y;
		case DIV:
			return x / y;
		case SUM:
			return x + y;
		case SUB:
			return y - x;
	}

	printf("teste\n");
	return 0;
}
Ejemplo n.º 6
0
int main(int argc, char *argv[]) {
	double temp;
	
	while (--argc > 0)
		switch (getop(*++argv)) {
		case NUMBER:
			push(atof(argv[0]));
			break;
		case '+':
			push(pop() + pop());
			break;
		case '-':
			temp = pop();
			push(pop() - temp);
			break;
		case '*':
			push(pop() * pop());
			break;
		case '/':
			temp = pop();
			push(pop() / temp);
			break;
		default:
			printf("invalid operand '%s'\n", argv[0]);
			break;
		}
	printf("%f\n", pop());
	return 0;
}
Ejemplo n.º 7
0
void estiva_precondjacobi(MX *A, double *x, double *D, double *b)
{
  int i,j,J,k,n, step = 2;
  static double *xo;

  if ( defop("-jacobistep") ) step = atoi(getop("-jacobistep"));

  n = dim1(D);

  ary1(xo,n+1);
    
  for (i=0; i<n; i++) xo[i] = 0.0;

  for(k=0;k<step;k++) {
    for(i=0;i<n;i++) {
      x[i]=b[i];
      for(j=0; j< A->w; j++) {
	J = A->IA[i][j];
	if (J != 0) if ( J-1 != i) if(A->A[i][j] !=0.0) {
	  x[i] -= A->A[i][j]*xo[J-1];
	}
      }
      x[i]=x[i]*D[i];
    }
    for(i=0;i<n;i++) xo[i]=x[i];
  }
}
Ejemplo n.º 8
0
double cacul(char s[])
{
    int type;
    double op2;
    double quyu1,quyu2;
    double result;
    
    while((type =  getop(s))!='q')
    {
        switch(type)
        {
            case NUMBER:
                push(sign * (atof(s)));
                sign=1;
                break;
            case '+':
                quyu1=pop();
                quyu2=pop();
               push(quyu1+quyu2);
                // push(pop()+pop());
                break;
            case '*':
                push(pop()*pop());

                break;
            case '-':
                op2=pop();
                push(pop()-op2);
                break;
            case '/':
                op2=pop();
                if(op2!=0.0)
                    push(pop()/op2);
                else
                    printf("error,zero divisor\n");
                break;
            case '%':
                quyu1=pop();
                quyu2=pop();
                if(quyu2>=0)
                {
                    op2=((int)quyu2%((int)quyu1));
                    push(op2);
                }
                else
                {
                    push(-((-(int)quyu2) % (int)quyu1));
                }
                break;
            case '\n':
                result=pop();
                printf("\t%.8g\n",result);
                return result;
            default:
                printf("error:unknown command %s\n",s);
                return -1;
        }                
    }
    return result;
}
Ejemplo n.º 9
0
/* reverse Polish calculator */
main()
{
	int type;
	double op2;
	char s[MAXOP];
	
	while ((type = getop(s)) != EOF) {
		switch (type) {
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if (op2 != 0.0) {
					push(pop() / op2);
				}
				else {
					printf("error: zero divisor for / operator\n");
				}
				break;
			case '%': /* The modulus operator works on integers, 
					   * although the following implementation also works for floating-point numbers.  
					   * If b > 0, a % b >= 0, i.e., rounds towards negative infinity;  
					   * if b < 0, a % b <= 0, i.e., rounds towards positive infinity.  
					   * The implementation finds r = a % b, so that:
					   * a = (k + r) * b, where k is an integer and r is in the range [0,1).  
					   */
				op2 = pop();
				if (op2 != 0.0) {
					double q = pop() / op2;
					double r = q - (int)q;
					while (r < 0 || r >= 1) {
						r += (r < 0) ? 1 : -1;
					}
					push(r * op2);
				}
				else {
					printf("error: zero divisor for \% operator\n");
				}
				break;
			case '\n':
				printf("\t%.8g\n", pop());
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;
		}
	}
	return 0;
}
Ejemplo n.º 10
0
int main(){
    int type;
    double op2;
    char s[MAXOP];
    while((type = getop(s)) != EOF){
        switch(type) {
            case NUMBER:
	        push(atof(s));
	        break;
	    case '+':
	        push(pop()+pop());
	        break;
	    case '-':
	        op2 = pop();
	        push(pop()-op2);
	        break;
	    case '*':
	        push(pop()*pop());
	        break;
	    case '/':
	        op2 = pop();
	        push(pop()/op2);
	        break;
  	    case '\n':
	        printf("%f\n",pop());
	        break;
	    default:
	        printf("error in push():unknow command %s\n",s);
	        break;
	}
    }
    return 0;
}
Ejemplo n.º 11
0
static int pexpi (int k) {
    int ei0, ei1, ei2, ptok;

    if (lextab[k][0] == 0)
        return pexp5 ();

    if ((ei0 = pexpi (k + 1)) == -1) {
        SUwarning (0, "pexpi", "cannot create first expression");
        return -1;
    }
    while ((ptok = getop (EEltok, k)) != -1) {
        if ((ei1 = EEcnew (ptok)) == -1) {
            SUwarning (0, "pexpi", "cannot create code");
            return -1;
        }
        EEcsetfp (ei1, ei0);
        EElgtok ();
        if ((ei2 = pexpi (k + 1)) == -1) {
            SUwarning (0, "pexpi", "cannot create next expression");
            return -1;
        }
        EEcsetnext (ei0, ei2);
        ei0 = ei1;
    }
    return ei0;
}
Ejemplo n.º 12
0
Archivo: 7-5.c Proyecto: icesyc/K-R
int main(int argc, char *argv[]){
	char s[100];
	while(getop(s) != EOF){
		printf("op=%s\n", s);
	}
	return 0;
}
main()
{
    char s[MAXLINE], s2[MAXLINE], s3[] = "ahoy hoy", s4[] = "oy", s5[MAXLINE];
    int len, n = 12345, op;
    printf("Enter some text, testing getline:\n");
    len = getline(s, MAXLINE);
    printf("echoing:\n%s\n", s);

    printf("converting the number %d to a string:\t", n);
    itoa(n, s2);
    printf("%s\n", s2);

    printf("index of '%s' in '%s': %d\n", s4, s3, strindex(s3, s4));
    printf("index of '%s' in '%s': %d\n", s4, s, strindex(s, s4));

    printf("Enter reverse Polish notation calculator input:\n");
    while ((op = getop(s5)) != EOF)
    {
        printf("op = ");
        if (op)
        {
            printf("%c", op);
        }
        else
        {
            printf("%s", s5);
        }
        printf("\n");
    }
}
Ejemplo n.º 14
0
int main ( int argc, char *argv[] )
{
    int type;
	double op2;

	char s[MAXOP];

    // 进入循环
	while((type = getop(s) != EOF))
	{
		switch (type)
		{
			case NUMBER:
				push(atof(s));
            case '+':
				push(pop()+pop());
				break;
			case '-':
				op2 = pop();
				push(pop()-op2);
				break;
			case '*':
				break;
			case '/':
				break;
			default:
				break;
		}
	}
	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
Ejemplo n.º 15
0
main()
{
	int type;
	double op2;
	char s[MAXOP];
	
	while ((type = getop(s)) != EOF) {
		switch (type) {
			case NUMBER: push(atof(s)); break;
			case FUNCTION: push(calcfunctions(s)); break;
			case '+': push(pop() + pop()); break;
			case '*': push(pop() * pop()); break;
			case '-': op2 = pop(); push(pop() - op2); break;
			case '/': 
				op2 = pop(); 
				if (op2 != 0.0) push(pop() / op2);
				else printf("error: zero divisor\n");
				break;
			case '%':
				op2 = pop();
				if (op2 != 0.0) push(fmod(pop(), op2));
				else printf("error: zero divisor\n");
				break;
			case '\n': printf("\t%.8g\n", pop()); break;
			default: printf("error: unknown command %s\n", s); break;
		}
	}
	
	return 0;
}
Ejemplo n.º 16
0
int main()
{
    int type;
    double op2;
    double op1;
    int coc;
    char s[MAXOP];

    while ((type = getop(s)) != EOF){
        switch(type){
        case NUMBER:
            push(atof(s));
            break;
        case '+':
            push(pop() + pop());
            break;
        case '*':
            push(pop() * pop());
            break;
        case '-':
            op2 = pop();
            push(pop() - op2);
            break;
        case '/':
            op2 = pop();
            if (op2 != 0.0)
                push(pop() / op2);
            else
                printf("error: divisor cero\n");
            break;
        case 's':         /* comando seno */
            push(sin(pop()));
            break;
        case 'e':                  /* comando exponente de e */
            push(exp(pop()));
            break;
        case 'p':                   /* comando potencia */
            op2 = pop();
            push(pow(pop(), op2));
            break;
        case '%':
            op2 = pop();
            op1 = pop();
            if (op2 != 0.0){
                coc = op1 / op2;
                push(op1 - (coc * op2));
            }
            else
                printf("error: divisor cero\n");
            break;
        case '\n':
            printf("\t%.8g\n", pop());
            break;
        default:
            printf("error: comando desconocido %s\n", s);
            break;
        }
    }
    return 0;
}
Ejemplo n.º 17
0
main()
{
	int type;
	double op2;
	char s[MAXOP];
	while ((type = getop(s)) != EOF) {
			switch(type) {
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());	
				break;
			case '-':
				op2 = pop();
				push(op2 + pop());
				break;
			case '/':
				op2 = pop();
				push(op2 + pop());
				break;
			case '\n':
				printf("\t%.8g\n",pop());
				break;
			default:
				printf("unknown character");
				break;
			}
	}	
	return 0;
}
/* sort input lines */
int main(int argc, char *argv[])
{
    int nlines; /* number of input lines read */
    int numeric = 0; /* 1 if numeric sort */
    int reverse = 0; /* 1 if reverset sort */

    getop(argc, argv, &numeric, &reverse);

    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
	if (numeric){
	    if (reverse) {
		_qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) rnumcmp);
	    } else {
		_qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) numcmp);
	    }
	} else {
	    if (reverse) {
		_qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) rstrcmp);
	    } else {
		_qsort((void**) lineptr, 0, nlines - 1, (int (*) (void *, void *)) strcmp);
	    }
	}

	writelines (lineptr, nlines);
	return 0;
    } else {
	printf ("input too big to sort\n");
	return 1;
    }
}
Ejemplo n.º 19
0
int main(int argc, char** argz)
{
		  static int count = 0;
		  static int start = 0;
		  static int max_size = 0;

		  unsigned char * buffer;
		  unsigned char * mp;
		  mp = malloc(max_size);

		  FILE * fp = fopen(argz[1],"rb");
		  //FILE * fp = fopen("/u/jiyinjie/CS429-hw3/prog9.yb","rb");
		  fread(buffer, 1, 2, fp);
		  int i;
		  //  printf("%X %X\n",buffer[0],buffer[1]);
		  //  printf("\n");
		  if (buffer[0] != HEADER_0 || buffer[1] != HEADER_1)
		  {
					 printf("This is not a y86 file.\n");
					 return 1;
					 // Error code 1 means incorrect file format.
		  }

		  while (fread(buffer, 1, 4, fp) == 4)
		  {
					 //     printf("\nNext block:\n");
					 count = buffer[3] + buffer[2]*256;
					 //     printf("%d\n",count);
					 start = buffer[1] + buffer[0]*256;
					 int temp = count+start;
					 max_size = (temp>max_size) ? temp : max_size;
					 mp = realloc(mp,sizeof(char*)*max_size);

					 static int check_count = 0;
					 check_count = fread(buffer, 1, count, fp);
					 if (! (count == check_count))
					 {
								printf("Corrupt object file!\n");
								return 2;
					 }

					 for (i = 0; i < count; i++)
								mp[i+start] = buffer[i];
		  }

		  int *tst = malloc(0);
		  for (i = 0; i < max_size; i++)
		  {
					 tst = (int*) &mp[i];
					 printf("%04X\t",i);
					 printf("%02X\t",mp[i]);
					 printf("%08X\t",*tst);
					 printf("%10d\t",*tst);
					 printf("%s\n",getop(mp[i]));
				//	printf("%04X\t%02X\t%08X\t%10d\t%s\n", i, mp[i], *tst, *tst, getop(mp[i]));
		  }
		  fclose(fp);
		  return 0;
}
Ejemplo n.º 20
0
/* reverse Polish calculator */
int main(){

	int type;
	double op2;
	char s[MAXOP];

	while((type = getop(s)) != EOF){

		switch(type){
			case NUMBER:
				push( atof(s) );
				break;
			case '+':
				push(pop() + pop() );
				break;
			case '*':
				push( pop() * pop() );
				break;
			case '-':
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if( op2 != 0.0 ){
					push( pop() / op2);
				}else{
					printf("Error: Division by zero.\n");
				}
				break;
			case '\n':
				printf("\t%.8g\n", pop());
				break;
				
			/* added modulus */
			case '%':
				op2 = pop();/* get divisor */
				push( ((int) pop()) % ((int) op2));
				break;
			case 'p':
				printTop();
				break;
			case 'd':
				duplicate();
				break;
			case 's':
				swap();
				break;
			case 'c':
				clear();
				break;
			default:
				printf("Error: Unknown command %s.\n",s);
				break;
		}
	}
	return 0;

}
Ejemplo n.º 21
0
// How many words an instruction is.
static int instruction_len(uint32_t instr) {
  switch (getop(instr)) {
    case OP_SETDISPATCH: return 1 + ptr_words;
    case OP_TAGN: return 3;
    case OP_SETBIGGROUPNUM: return 2;
    default: return 1;
  }
}
Ejemplo n.º 22
0
Archivo: test.c Proyecto: alimon/dash
int
testcmd(int argc, char **argv)
{
    const struct t_op *op;
    enum token n;
    int res = 1;

    if (*argv[0] == '[') {
        if (*argv[--argc] != ']')
            error("missing ]");
        argv[argc] = NULL;
    }

    t_wp_op = NULL;

recheck:
    argv++;
    argc--;

    if (argc < 1)
        return res;

    /*
     * POSIX prescriptions: he who wrote this deserves the Nobel
     * peace prize.
     */
    switch (argc) {
    case 3:
        op = getop(argv[1]);
        if (op && op->op_type == BINOP) {
            n = OPERAND;
            goto eval;
        }
    /* fall through */

    case 4:
        if (!strcmp(argv[0], "(") && !strcmp(argv[argc - 1], ")")) {
            argv[--argc] = NULL;
            argv++;
            argc--;
        } else if (!strcmp(argv[0], "!")) {
            res = 0;
            goto recheck;
        }
    }

    n = t_lex(argv);

eval:
    t_wp = argv;
    res ^= oexpr(n);
    argv = t_wp;

    if (argv[0] != NULL && argv[1] != NULL)
        syntax(argv[0], "unexpected operator");

    return res;
}
Ejemplo n.º 23
0
Archivo: E4_5.c Proyecto: bcotm/learnc
int main(void)
{
    int type;
    double op2;
    char s[MAXOP];

    while ((type = getop(s)) != EOF) {
        switch (type) {
        case NUMBER:
            push(atof(s));
            break;
        case MATHLIB:
            if (strcmp(s, "sin") == 0)
                push(sin(pop()));
            else if (strcmp(s, "cos") == 0)
                push(cos(pop()));
            else if (strcmp(s, "pow") == 0) {
                op2 = pop();
                push(pow(pop(), op2));
            } else
                printf("error: unknown command %s\n", s);
            break;
        case '+':
            push(pop() + pop());
            break;
        case '*':
            push(pop() * pop());
            break;
        case '-':
            op2 = pop();
            push(pop() - op2);
            break;
        case '/':
            op2 = pop();
            if (op2 != 0.0)
                push(pop() / op2);
            else
                printf("error: zero divisor\n");
            break;
        case '%':
            op2 = pop();
            if (op2 != 0.0)
                push(fmod(pop(), op2));
            else
                printf("error: zero divisor\n");
            break;
        case '\n':
            printf("= %g\n", pop());
            break;
        default:
            printf("error: unknown command %s\n", s);
        break;
        }
    }

    return 0;
}
Ejemplo n.º 24
0
int main(void) {
	int type;
	double op2, op1;
	char s[MAXOP];
	void clearsp(void);

	while ((type = getop(s)) != EOF) {
		switch (type) {
		case NUMBER:
			push(atof(s));
			break;
		case '+':
			push(pop()+pop());
			break;
		case '*':
			push(pop()*pop());
			break;
		case '-':
			op2 = pop();
			push(pop()-op2);
			break;
		case '/':
			op2 = pop();
			if (op2 != 0.0)
				push(pop() / op2);
			else
				printf("error:zero divisor\n");
			break;
		case'?':
			op2 = pop();
			printf("\t%.8g\n",op2);
			push(op2);
			break;
		case 'c':
			clearsp();
			break;
		case 'd':
			op2 = pop();
			push(op2);
			push(op2);
			break;
		case 's':
			op1 = pop();
			op2 = pop();
			push(op1);
			push(op2);
			break;
		case '\n':
			printf("\t%.8g\n", pop());
			break;
		default:
			printf("error: unknown command %s\n",s);
			break;
		}
	}
	return 0;
}
Ejemplo n.º 25
0
Archivo: xp-actions.c Proyecto: mpw/p2
static void
do_action1 (char *op_name, int *indx, int *op)
{
    if (not_concrete(op_name)) {
        /* Delete trailing '#' from op_name. */
        int len = strlen(op_name);
        {
            char s[len];
            strncpy(s, op_name, len-1);
            s[len-1] = '\0';
            (*op) = getop(s);
        }
    }
    else
        (*op) = getop(op_name);

    (*indx) = getindxop(*op);
}
Ejemplo n.º 26
0
int main()	/* Reverse Polish notation */
{
	int type;
	double op2;
	char s[MAXOP];
	double ans = 0.0;
	while (Getline(expression, MAXOP))
		while ((type = getop(s)) != EOF) {
			switch (type) {
				case NUMBER:
					push(atof(s));
					break;
				case 'a':
					push(ans);
					break;
				case '+':
					push(pop() + pop());
					break;
				case '*':
					push(pop() * pop());
					break;
				case '-':
					op2 = pop();
					push(pop() - op2);
					break;
				case '/':
					op2 = pop();
					if (op2 != 0.0) push(pop() / op2);
					else printf("error: zero divisor\n");
					break;
				case '%':
					op2 = pop();
					if (op2 != 0.0) push(fmod(pop(), op2));	/* use g++ to compile */
					else printf("error: zero divisor\n");
					break;
				case SIN:
					push(sin(pop()));
					break;
				case EXP:
					push(exp(pop()));
					break;
				case POW:
					op2 = pop();
					push(pow(pop(), op2));
					break;
				case '\n':
					ans = pop();
					printf("\t%.8g\n", ans);
					break;
				default:
					printf("error: unknown command %s\n", s);
					break;
			}
			if (type == '\n') break;
		}
	return 0;
}
Ejemplo n.º 27
0
/* reverse Polish calculator */
int main()
{
	int type;
	double op2;
	char s[MAXOP];

	while ((type = getop(s)) != EOF) {
		switch (type) {
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if (op2 != 0.0)
					push(pop() / op2);
				else
					printf("error: zero divisor\n");
				break;
#ifndef ORIGINAL
			case '%':
				op2 = pop();
				if ((int)op2 != 0)
					push((int)pop() % (int)op2);
				else
					printf("error: zero modulus\n");
				break;
			case SIN:
				push(sin(pop()));
				break;
			case EXP:
				push(exp(pop()));
				break;
			case POW:
				op2 = pop();
				push(pow(pop(), op2));
				break;
#endif
			case '\n':
				printf("\t%.8g\n", pop());
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;
		}
	}
	return 0;
}
Ejemplo n.º 28
0
/**
 * reverse polish calculator\
 * 1 2 - 4 5 + * == (1 - 2) * (4 + 5)
 */
int main (void) {
	int type;
	double op2;
	char s[MAXOP];

	while ((type = getop(s)) != EOF) {
		switch(type) {
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				/** first pop second operand*/
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if (op2 != 0.0)
					push(pop() / op2);
				else
					printf("error: division by zero\n");
				break;
			case '%':
				op2 = pop();
				if (op2 != 0.0)
					push(fmod(pop(), op2));
				else
					printf("error: division by zero\n");
				break;
			case '?':
				stack_show();
				break;
			case '#':
				stack_duplicate();
				break;				
			case '~':
				stack_swap();
				break;
			case '!':
				stack_clear();
				break;
			case '\n':
				printf("\t%.8f\n", pop());
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;
		}
	}
	return 0;
}
int main(void)
{
  int type;
  char s[MAXOP];

  while ((type = getop(s)) != EOF)
    printf("%c", type);
  return 0;
}
Ejemplo n.º 30
0
main(void)
{
	int type;
	double op1,op2,fracPart;
	char s[MAXOP];
	
	while((type = getop(s))!= EOF){
		switch(type){
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if(op2 != 0.0){
					push(pop() / op2);
				}
				else{
					printf("error: zero divisor\n");
				}
				break;
			case '%':
				op2 = pop();
				op1 = pop();
				fracPart = (op1 - ((int)op1)) + (op2 - ((int)op2));
				if(fracPart != 0.0){
					printf("Integer-only operation on floating point number.\n");
					break;
				}
				if(op2 != 0.0){
					push((int)(op1) % ((int)op2));
				}
				else{
					printf("error: zero divisor\n");
				}
				break;
			case '\n':
				printf("\t%.8g\n",pop());
				break;
			default:
				printf("error: unkown command %s\n",s);
				break;
		}
	
	}
	return 0;
}