#define MAXVAL 100 /*maximum depth of val stack */
int sp = 0; /*next free stack position */
double val[MAXVAL]; /*value stack */
/*push: push f onto value stack */
void push(double f){
if (sp < MAXVAL)
val[sp++] = f;
else
printf("error:stack full, cant push %g\n", f);
}
/*pop: pop and return top value from stack */
double pop(void){
if(sp > 0)
return val[--sp];
else{
printf("error: stack empty\n");
return 0.0;
}
}
Author: curious_cat
Reverse polish calculator
#include <stdio.h>
#include <stdlib.h> /* for atof */
#define MAXOP 100 /*max size of operand or operator */
#define NUMBER '0' /*signal that a number was found */
int getop(char []);
void push(double);
double pop(void);
/*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\n");
break;
case '\n':
printf("\t%.8g\n",pop());
break;
default:
printf("error: unknown command %s\n", s);
break;
}
}
return 0;
}
atoi:using atof
/*atoi: convert string s to integer using atof */
int atoi(char s[]){
double atof(char s[]);
return (int) atof(s);
}
atof:convert string to double
#include<ctype.h>
/*atof: convert string s to double */
double atof(char s[]){
double val,power;
int i, sign;
for(i=0;isspace(s[i]);i++) /*skip white space */
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]);i++)
val = 10.0 *val + (s[i] - '0');
if (s[i] == '.')
i++;
for(power = 1.0; isdigit(s[i]);i++){
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
return sign *val/power;
}
Find lines matching a pattern
#include <stdio.h>
#define MAXLINE 1000 /*maximum input line length */
int getline(char line[], int max);
int strindex(char source[], char searchfor[]);
char pattern[] = "ould"; /*pattern to search for */
/*find all lines matching pattern */
main()
{
char line[MAXLINE];
int found = 0;
while (getline(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0) {
printf("%s", line);
found++;
}
return found;
}
/*getline: get line into s, return length */
int getline(char s[], int lim){
int c, i;
i = 0;
while(--lim > 0 && (c =getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
/*strindex: return index of t in s, -1 if none */
int strindex(char s[], char t[])
{
int i, j, k;
for(i = 0; s[i] != '\0';i++){
for (j =i, k= 0; t[k] != '\0' && s[j] == t[k];j++, k++)
;
if(k > 0 && t[k] == '\0')
return i;
}
return -1;
}
Remove trailing blanks,tabs,newlines.
/*remove trailing blanks, tabs, newlines */
int trim(char s[]){
int n;
for (n =strlen(s) -1; n >= 0; n--)
if(s[n] != ' ' && s[n] != '\t' && s[n] != '\n')
break;
s[n + 1] ='\0';
return n;
}
itoa:convert integer to characters.
/*itoa: convert n to characters in s */
void itoa(int n , char s[])
{
int i, sign;
if((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do {
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /*delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
reverse string in place
#include <string.h>
/*reverse: reverse string s in place */
void reverse(char s[])
{
int c , i, j;
for (i = 0, j =strlen(s) - 1; i < j; i++, j--){
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
shellsort.
/* shellsort: sort v[0].......v[n -1] into increasing order */
void shellsort(int v[], int n)
{
int gap, i, j, temp;
for(gap =n/2; gap > 0;gap /= 2)
for(i =gap ; i < n ;i++)
for(j =i - gap; j>= 0 && v[j] > v[j + gap]; j -=gap) {
temp = v[j];
v[j] =v[j + gap];
v[j + gap] = temp;
}
}
atoi:convert string to integer;2.0
#include <ctype.h>
/*atoi : convert s to integer; version 2 */
int atoi(char s[])
{
int i, n, sign;
for (i =0; isspace(s[i]);i++) /*skip white space */
;
sign = (s[i] == '-') ? -1 ; 1;
if (s[i] == '+' || s[i] == '-') /*skip sign */
i++;
for(n = 0;isdigit(s[i]); i++)
n =10 * n + (s[i] - '0');
return sign * n;
}