diff options
| -rw-r--r-- | 4.11.c | 113 | ||||
| -rw-r--r-- | qsort.c | 77 | 
2 files changed, 190 insertions, 0 deletions
@@ -0,0 +1,113 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <math.h> + +#define MAXOP 100 +#define NUMBER_SIG '0' + +double pop(void); +void push(double f); +char getop(char str[]); + +int  +main(void) +{ +  char str[MAXOP]; +  char type; +  double op2; + +  while ((type = getop(str)) != EOF) { +    switch (type) { +      case NUMBER_SIG : +        push(atof(str)); +        break; +      case '+' : +        push(pop() + pop()); +        break; +      case '*' : +        push(pop() * pop()); +        break; +      case '-' : +        op2 = pop(); +        push(pop() - op2); +        break; +      case '/' : +        if ((op2 = pop()) == 0.0) +          printf("Err: deviser cant be zero\n"); +        else  +          push(pop() / op2); + +        break; +      case '%' : +        op2 = pop(); +        if (op2 != 0) +          push(fmod(pop(), op2)); +        else  +          printf("Err: deviser cant be zero\n"); + +        break; +      case '\n' : +        printf("%.8g\n", pop()); +        break; +      default : +        printf("Err: unknown command\n"); +    } +  } + +  return 0; +} + +#define MAXVAL 100 +double val[MAXVAL]; +int sp = 0; + +double  +pop(void) +{ +  if (sp > 0) +    return val[--sp]; +  else { +    printf("Err: stack empty\n"); +    return 0.0; +  } +} + +void +push(double f) +{ +  if (sp < MAXVAL) +    val[sp++] = f; +  else +    printf("Err: stack is full\n"); +} + + +char  +getop(char str[]) +{ +  static char buff = ' '; +  char input; +  int i; + +  /* ignore blanks */ +  for (input = buff, str[0] = buff; isblank(input);) +    input = str[0] = getchar(); +   +  buff = ' '; + +  /* return operator */ +  if (!isdigit(input) && input != '.') +    return input; + +  /* collect digits*/ +  for (i = 1; i < MAXOP && (isdigit(input=getchar()) || input == '.'); i++) +    str[i] = input; + +  str[i] = '\0'; + +  if (input != EOF) +    buff = input; + +  return NUMBER_SIG; +} @@ -0,0 +1,77 @@ +#include <stdio.h> + +void printia(int array[], int len); +void qsort(int array[], int left, int right); +void swap(int array[], int i, int j); + +int  +main(void) +{ +  /*               0  1  2  3  4  5  6  7  8  9*/ +  int array[10] = {9, 6, 1, 5, 3, 7, 0, 4, 2, 8}; + +  printf("c> 0 1 2 3 4 5 6 7 8 9\n"); +  printia(array, sizeof(array)/sizeof(int)); +  qsort(array, 0, sizeof(array)/sizeof(int)-1); +  printia(array, sizeof(array)/sizeof(int)); + +  return 0; +} + +void  +printia(int array[], int len) +{ +  int i; + +  printf("a> "); +   +  for (i = 0; i < len; i++) +    printf("%d ", array[i]); + +  printf("\n"); +} + +void  +qsort(int array[], int left, int right) +{ +  int i, last; + +  if (left >= right) +    return; + +  printf("1> array[%d] <=> array[%d] | %d <=> %d\n", left, (left+right)/2, array[left], array[(left+right)/2]); +  swap(array, left, (left+right)/2); +  printia(array, 10); +  last  = left; + +  for (i = left+1; i <= right; i++) +    if (array[i] < array[left]) { +      printf("2> array[%d] <=> array[%d] | %d <=> %d\n", last+1, i, array[last+1], array[i]); +      swap (array, ++last, i); +      printia(array, 10); +    } +  printf("s> |---------<for loope>-+-------|\n"); + +  printf("3> array[%d] <=> array[%d] | %d <=> %d\n", left, last, array[left], array[last]); +  swap(array, left, last); +  printia(array, 10); + +  qsort (array, left, last-1); +  qsort(array, last+1, right); +  printf("s> |------------<END>----+-------|\n"); + +} + +void +swap(int array[], int i, int j) +{ +  int temp; + +  if (i == j) +    printf("s> |---------<same swap>-+-------|\n"); + +  temp = array[i]; + +  array[i] = array[j]; +  array[j] = temp; +}  | 
