Example #1
0
/****************************************************
 *               Hlavní program.
 ****************************************************/
int main(void)
{
    double rnumber;             // číslo se kterým se má počítat
    double inumber;             // číslo se kterým se má počítat
    char enter[CMDLEN];         // vstup od uživatele

    setrad();
    /* Čte řádky až do konce souboru */
    while (readEnter(enter) != EOF) {
        if (sscanf(enter, "%lg,%lg", &rnumber, &inumber) == 2) {
            pushalg(rnumber, inumber);
        } else if (sscanf(enter, "%lgL%lg", &rnumber, &inumber) == 2) {
            pushexp(rnumber, inumber);
        } else if (sscanf(enter, ",%lg", &inumber) == 1) {
            pushalg(0, inumber);
        } else if (sscanf(enter, "%lg", &rnumber) == 1) {
            pushalg(rnumber, 0);
        } else if (strcmp(enter, "pi") == 0) {
            pushalg(M_PI, 0);
        } else if (strcmp(enter, "e") == 0) {
            pushalg(M_E, 0);
        } else if (strcmp(enter, "+") == 0) {
            make2wrap(makePlus, enter);
        } else if (strcmp(enter, "-") == 0) {
            make2wrap(makeMinus, enter);
        } else if (strcmp(enter, "*") == 0) {
            make2wrap(makeTimes, enter);
        } else if (strcmp(enter, "/") == 0) {
            make2wrap(makeDivid, enter);
        } else if (strcmp(enter, "**") == 0) {
            /* n-tá mocnina */
            make2wrap(cpow, enter);
        } else if (strcmp(enter, "^") == 0) {
            /* převrácená hodnota */
            make1wrap(makeFlipp, enter);
        } else if (strcmp(enter, "sqrt") == 0 || strcmp(enter, "t") == 0) {
            make1wrap(csqrt, enter);
        } else if (strcmp(enter, "exp") == 0 || strcmp(enter, "x") == 0) {
            /* exponenciální funkce */
            make1wrap(cexp, enter);
        } else if (strcmp(enter, "ln") == 0) {
            make1wrap(clog, enter);
        } else if (strcmp(enter, "log") == 0) {
            make1wrap(clog10, enter);
        } else if (strcmp(enter, "r") == 0) {
            /* přepne kalkulačku do radiánů */
            setrad();
        } else if (strcmp(enter, "d") == 0) {
            /* přepne kalkulačku do stupňů */
            setdeg();
        } else if (strcmp(enter, "la") == 0) {
            /* poslední číslo převede do algebraickém tvaru */
            last2alg();
            printStack();
        } else if (strcmp(enter, "le") == 0) {
            /* poslední číslo převede exponenciálním tvaru */
            last2exp();
            printStack();
        } else if (strcmp(enter, "aa") == 0) {
            /* všechny čísla převede do algebraickém tvaru */
            all2alg();
            printStack();
        } else if (strcmp(enter, "ae") == 0) {
            /* všechny čísla převede do exponenciálním tvaru */
            all2exp();
            printStack();
        } else if (strcmp(enter, "da") == 0) {
            setalg();
        } else if (strcmp(enter, "de") == 0) {
            setexp();
        } else if (strcmp(enter, "switch") == 0 || strcmp(enter, "s") == 0) {
            if (!lastSwitch())
                fprintf(stderr, " error > Nelze prohodit\n");
        } else if (strcmp(enter, "deg") == 0) {
            make1wrap(rad2deg, enter);
        } else if (strcmp(enter, "rad") == 0) {
            make1wrap(deg2rad, enter);
            /* Goniometrické funkce */
        } else if (strcmp(enter, "sin") == 0) {
            make1wrap(csin, enter);
        } else if (strcmp(enter, "cos") == 0) {
            make1wrap(ccos, enter);
        } else if (strcmp(enter, "tan") == 0 || strcmp(enter, "tg") == 0) {
            make1wrap(ctan, enter);
        } else if (strcmp(enter, "asin") == 0) {
            make1wrap(casin, enter);
        } else if (strcmp(enter, "acos") == 0) {
            make1wrap(cacos, enter);
        } else if (strcmp(enter, "atan") == 0 || strcmp(enter, "atg") == 0) {
            make1wrap(catan, enter);
        } else if (enter[0] == '>') {
            /* ukládá do proměnné */
            complex double number = pop();
            memput(number, enter, algform ? ALGFORM : EXPFORM);
            push(number, algform);
        } else if (enter[0] == '<') {
            /* vybírá obsah proměnné */
            enter[0] = '>';
            char form;
            complex double number = memget(enter, &form);
            push(number, form == ALGFORM ? true : false);
        } else if (strcmp(enter, "rem") == 0 || strcmp(enter, "m") == 0) {
            pop();
            printStack();
        } else if (strcmp(enter, "clr") == 0 || strcmp(enter, "c") == 0) {
            stackclear();       // vymaže celý zásobník
        } else if (strcmp(enter, "p") == 0 || strcmp(enter, "print") == 0) {    // jen tiskne zásobník
            printStack();
        } else if (strcmp(enter, "help") == 0 || strcmp(enter, "h") == 0) {
            printHelp();
        } else if (strcmp(enter, "quit") == 0 || strcmp(enter, "q") == 0) {
            break;
        } else {
            fprintf(stderr, " error > %s\n", enter);
            fprintf(stderr, " error > Neplatný vstup\n");
        }
    }
    stackclear();               // maže zásobník
    memclear();               // maže zásobník
    return 0;
}
int main(void) {
	int type;
	double op1, op2;
	char s[MAXOP];

	reset_cavars();
	reset_stack_mark_cavars();

	while ((type = getop(s)) != EOF) {
		switch (type) {
			case NUMBER:
				push(atof(s));
				break;
			case CAVAR:
				push((double) s[0]);
				stack_mark_cavar();
				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 modulo\n");
				break;
			case SET:
				op2 = popreal();
				set_cavar((int) op2, pop());
				push(get_cavar((int) op2));
				break;
			case SIN:
				push(sin(pop()));
				break;
			case COS:
				push(cos(pop()));
				break;
			case TAN:
				push(tan(pop()));
				break;
			case EXP:
				push(exp(pop()));
				break;
			case POW:
				op2 = pop();
				op1 = pop();
				if (op1 == 0 && op2 <= 0)
					printf("error: pow domain error\n");
				else
					push(pow(op1, op2));
				break;
			case 'P': /* print element at top of stack */
				printf("%f\n", stacklast());
				break;
			case 'D': /* duplicate top element */
				stackdup();
				break;
			case 'S': /* swap top two elements */
				stackswap();
				break;
			case 'C': /* clear stack */
				stackclear();
				printf("stack cleared\n");
				break;
			case '\n':
				if (stackcount() > 0) {
					printf("\t%.8g\n", stacklast());
				}
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;
		}
	}

	return 0;
}
void
base_quicksort7(unsigned int a[], int N)
{
    int l, r;
    int i;
    int m;
    int il, ir; /* names follow pl, pm, and pn from bently/mcilroy. used ir instead of in */

    stackinit(N);

    describe_predictor(&global_predictor[0], "i");
    describe_predictor(&global_predictor[1], "j");
    describe_predictor(&global_predictor[2], "partition end");
    describe_predictor(&global_predictor[3], "insertion");
    describe_predictor(&global_predictor[4], "median");
    /*	describe_predictor(&global_predictor[4], "median of 7 ab"); */
    describe_predictor(&global_predictor[5], "median of 7 bc");
    describe_predictor(&global_predictor[6], "median of 7 ac");
    describe_predictor(&global_predictor[7], "median of 7 cb");
    describe_predictor(&global_predictor[8], "median of 7 ca");
    describe_predictor(&global_predictor[9], "median of 7 ab2");
    describe_predictor(&global_predictor[10], "median of 7 bc2");
    describe_predictor(&global_predictor[11], "median of 7 ac2");
    describe_predictor(&global_predictor[12], "median of 7 cb2");
    describe_predictor(&global_predictor[13], "median of 7 ca2");
    describe_predictor(&global_predictor[14], "median of 3 cmp1");
    describe_predictor(&global_predictor[15], "median of 3 cmp2");
    describe_predictor(&global_predictor[16], "median of 3 cmp3");


    r = N-1;
    l = 0;

    while(1)
    {
        int n = r - l;
        int n6 = n/6;
        int n3 = n/3;
        if (r - l <= THRESHHOLD)
        {
            if (stackempty())
                break;

            l = pop();
            r = pop();
            continue;
        }

        /* pseudo - Median of 7 partitioning*/
        m = (l+r)/2;
        if (n > 40)
        {

            il = med3(a, l, l + n6, l + n3);

            /* the 2 is for seperate branch predictors, as it's inlined */
            ir = med3_2(a, r - n3, r - n6, r);

            exch(a[l], a[il]);
            exch(a[r], a[ir]);
            exch(a[m], a[r-1]);
        }


        pred_compexch(a[l], a[r-1], 14);
        pred_compexch(a[l], a[r], 15);
        pred_compexch(a[r-1], a[r], 16);

        i = partition(a,l+1,r-1);

        /* here is the bug */
        /* then key is being copied more times than necessary. the reason for this is that it is not being removed when it is taken as the key */
        /* instead, it is being put in place more than once */
        /* example: i == 1, j == 10; key = a[1]; key < pivot, so key is swapped with a[2], the key is now in a[1] and a[2]. uh oh  */
        if (i-l > r-i)
        {
            push(i-1,l);
            l = i+1;
        }
        else
        {
            push(r,i+1);
            r = i-1;
        }
    }

    stackclear();
    /* the +1 isnt immediately obvious. its because THRESHHOLD is the difference between l and r up above */
    if (2*THRESHHOLD > N) insertion_sentinel(a,N);
    else insertion_sentinel(a,2*THRESHHOLD);

    insertion(a, N);

    /* add the predictors up */
    add_predictor(&global_predictor[4], &global_predictor[5]);
    add_predictor(&global_predictor[4], &global_predictor[6]);
    add_predictor(&global_predictor[4], &global_predictor[7]);
    add_predictor(&global_predictor[4], &global_predictor[8]);
    add_predictor(&global_predictor[4], &global_predictor[9]);
    add_predictor(&global_predictor[4], &global_predictor[10]);
    add_predictor(&global_predictor[4], &global_predictor[11]);
    add_predictor(&global_predictor[4], &global_predictor[12]);
    add_predictor(&global_predictor[4], &global_predictor[13]);
    add_predictor(&global_predictor[4], &global_predictor[14]);
    add_predictor(&global_predictor[4], &global_predictor[15]);
    add_predictor(&global_predictor[4], &global_predictor[16]);
    init_predictor(&global_predictor[5]);
    init_predictor(&global_predictor[6]);
    init_predictor(&global_predictor[7]);
    init_predictor(&global_predictor[8]);
    init_predictor(&global_predictor[9]);
    init_predictor(&global_predictor[10]);
    init_predictor(&global_predictor[11]);
    init_predictor(&global_predictor[12]);
    init_predictor(&global_predictor[13]);
    init_predictor(&global_predictor[14]);
    init_predictor(&global_predictor[15]);
    init_predictor(&global_predictor[16]);
}