c2-1.c
/* code: c2-1.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define MAX 128
#define PUSH_SUCCESS 1
#define PUSH_FAILURE -1
#define POP_SUCCESS 2
#define POP_FAILURE -2
/* ------------------------------------------- */
void stack_init (int *top) {
*top = 0;
}
/* ------------------------------------------- */
void display (int stack[], int top) {
int i;
printf ("STACK(%d): ", top);
for (i = 0; i < top; i++) {
printf ("%d ", stack[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int push (int stack[], int *top, int data) {
if (*top >= MAX) {
/* stack overflow */
return PUSH_FAILURE;
}
else {
stack[*top] = data;
(*top)++;
return PUSH_SUCCESS;
}
}
/* ------------------------------------------- */
int pop (int stack[], int *top, int *data) {
if ((*top) > 0) {
*data = stack[(*top) - 1];
(*top)--;
return POP_SUCCESS;
}
else {
/* stack empty */
return POP_FAILURE;
}
}
/* ------------------------------------------- */
int main () {
int stack[MAX];
int top, data;
stack_init (&top);
data = 300;
printf ("push: %d\n", data);
push (stack, &top, data);
data = 400;
printf ("push: %d\n", data);
push (stack, &top, data);
data = 500;
printf ("push: %d\n", data);
push (stack, &top, data);
display (stack, top);
pop (stack, &top, &data);
printf ("pop: %d\n", data);
pop (stack, &top, &data);
printf ("pop: %d\n", data);
pop (stack, &top, &data);
printf ("pop: %d\n", data);
return 0;
}
q2-1.c
/* code: q2-1.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define MAX 128
#define PUSH_SUCCESS 1
#define PUSH_FAILURE -1
#define POP_SUCCESS 2
#define POP_FAILURE -2
/* ------------------------------------------- */
int peek (int stack[], int *top, int *data) {
if ((*top) > 0) {
*data = stack[(*top) - 1];
return POP_SUCCESS;
}
else {
/* stack empty */
return POP_FAILURE;
}
}
/* ------------------------------------------- */
void stack_init (int *top) {
*top = 0;
}
/* ------------------------------------------- */
void display (int stack[], int top) {
int i;
printf ("STACK(%d): ", top);
for (i = 0; i < top; i++) {
printf ("%d ", stack[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int push (int stack[], int *top, int data) {
if (*top >= MAX) {
/* stack overflow */
return PUSH_FAILURE;
}
else {
stack[*top] = data;
(*top)++;
return PUSH_SUCCESS;
}
}
/* ------------------------------------------- */
int pop (int stack[], int *top, int *data) {
if ((*top) > 0) {
*data = stack[(*top) - 1];
(*top)--;
return POP_SUCCESS;
}
else {
/* stack empty */
return POP_FAILURE;
}
}
/* ------------------------------------------- */
int main () {
int stack[MAX];
int top, data;
stack_init (&top);
data = 300;
printf ("push: %d\n", data);
push (stack, &top, data);
data = 400;
printf ("push: %d\n", data);
push (stack, &top, data);
data = 500;
printf ("push: %d\n", data);
push (stack, &top, data);
peek (stack, &top, &data);
printf ("peek: %d\n", data);
peek (stack, &top, &data);
printf ("peek: %d\n", data);
display (stack, top);
pop (stack, &top, &data);
printf ("pop: %d\n", data);
peek (stack, &top, &data);
printf ("peek: %d\n", data);
pop (stack, &top, &data);
printf ("pop: %d\n", data);
pop (stack, &top, &data);
printf ("pop: %d\n", data);
return 0;
}
q2-2.c
/* code: q2-2.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 128
#define PUSH_SUCCESS 1
#define PUSH_FAILURE -1
#define POP_SUCCESS 2
#define POP_FAILURE -2
/* ------------------------------------------- */
void stack_init (int *top) {
*top = 0;
}
/* ------------------------------------------- */
void display_char (int stack[], int top) {
int i;
printf ("STACK(%d): ", top);
for (i = 0; i < top; i++) {
printf ("%c", stack[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int push (int stack[], int *top, int data) {
if (*top >= MAX) {
/* stack overflow */
return PUSH_FAILURE;
}
else {
stack[*top] = data;
(*top)++;
return PUSH_SUCCESS;
}
}
/* ------------------------------------------- */
int pop (int stack[], int *top, int *data) {
if ((*top) > 0) {
*data = stack[(*top) - 1];
(*top)--;
return POP_SUCCESS;
}
else {
/* stack empty */
return POP_FAILURE;
}
}
/* ------------------------------------------- */
int main () {
int stack[MAX];
int top, data;
int i, len;
char str[128];
sprintf (str, "%s", "The_Open_University_of_Japan");
len = strlen (str);
stack_init (&top);
for (i = 0; i < len; i++) {
data = str[i];
push (stack, &top, data);
}
display_char (stack, top);
for (i = 0; i < len; i++) {
pop (stack, &top, &data);
printf ("%c", data);
}
printf ("\n");
return 0;
}
q2-3.c
/* code: q2-3.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define DATA_SIZE 100000000
#define MAX 100000000
#define PUSH_SUCCESS 1
#define PUSH_FAILURE -1
#define POP_SUCCESS 2
#define POP_FAILURE -2
/* ------------------------------------------- */
void stack_init (int *top) {
*top = 0;
}
/* ------------------------------------------- */
void display (int stack[], int top) {
int i;
printf ("STACK(%d): ", top);
for (i = 0; i < top; i++) {
printf ("%d ", stack[i]);
}
printf ("\n");
}
/* ------------------------------------------- */
int push (int stack[], int *top, int data) {
if (*top >= MAX) {
/* stack overflow */
return PUSH_FAILURE;
}
else {
stack[*top] = data;
(*top)++;
return PUSH_SUCCESS;
}
}
/* ------------------------------------------- */
int pop (int stack[], int *top, int *data) {
if ((*top) > 0) {
*data = stack[(*top) - 1];
(*top)--;
return POP_SUCCESS;
}
else {
/* stack empty */
return POP_FAILURE;
}
}
/* ------------------------------------------- */
int main () {
int *stack;
int top, data, i;
stack = malloc (sizeof (int) * MAX);
printf ("data size: %d\n", DATA_SIZE);
stack_init (&top);
printf ("push:\n");
for (i = 0; i < DATA_SIZE; i++) {
data = rand () % 1000;
if (i >= DATA_SIZE - 5)
printf ("%3d (index:%d)\n", data, top);
push (stack, &top, data);
}
printf ("\n");
printf ("pop:\n");
for (i = 0; i < 5; i++) {
pop (stack, &top, &data);
printf ("%3d (index:%d)\n", data, top);
}
printf ("\n");
free (stack);
return 0;
}
q2-4.c
/* code: q2-4.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX 1024
#define PUSH_SUCCESS 1
#define PUSH_FAILURE -1
#define POP_SUCCESS 2
#define POP_FAILURE -2
/* ------------------------------------------- */
void stack_init (int *top) {
*top = 0;
}
/* ------------------------------------------- */
int push (int stack[], int *top, int data) {
if (*top >= MAX) {
/* stack overflodelim */
return PUSH_FAILURE;
}
else {
stack[*top] = data;
(*top)++;
return PUSH_SUCCESS;
}
}
/* ------------------------------------------- */
int pop (int stack[], int *top, int *data) {
if ((*top) > 0) {
*data = stack[(*top) - 1];
(*top)--;
return POP_SUCCESS;
}
else {
/* stack empty */
return POP_FAILURE;
}
}
/* ------------------------------------------- */
int rpn (char *str) {
int stack[MAX];
int top, i;
int a, b;
char *endptr;
char *delim = " \n\t\r\f";
a = b = 0;
stack_init (&top);
for (str = strtok (str, delim); str; str = strtok (0, delim)) {
a = (int) strtod (str, &endptr);
if (endptr > str) {
printf ("n: ");
push (stack, &top, a);
}
else if (*str == '+') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a + b));
}
else if (*str == '-') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a - b));
}
else if (*str == '*') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a * b));
}
else if (*str == '/') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a / b));
}
else {
fprintf (stderr, "[%c]: ", *str);
fprintf (stderr, "unknown oeprators\n");
}
for (i = 0; i < top; i++) {
printf (" %d", stack[i]);
}
printf ("\n");
}
if (top != 1) {
fprintf (stderr, "stack error\n");
exit (-1);
}
pop (stack, &top, &a);
return a;
}
/* ------------------------------------------- */
int main () {
int result;
char rpn_str[] = " 3 6 + 1 2 + / ";
printf ("RPN expression: %s\n\n", rpn_str);
result = rpn (rpn_str);
printf ("\n");
printf ("result: %d\n", result);
return 0;
}
q2-5.c
/* code: q2-5.c (v1.18.00) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX 1024
#define PUSH_SUCCESS 1
#define PUSH_FAILURE -1
#define POP_SUCCESS 2
#define POP_FAILURE -2
/* ------------------------------------------- */
void stack_init (int *top) {
*top = 0;
}
/* ------------------------------------------- */
int push (int stack[], int *top, int data) {
if (*top >= MAX) {
/* stack overflodelim */
return PUSH_FAILURE;
}
else {
stack[*top] = data;
(*top)++;
return PUSH_SUCCESS;
}
}
/* ------------------------------------------- */
int pop (int stack[], int *top, int *data) {
if ((*top) > 0) {
*data = stack[(*top) - 1];
(*top)--;
return POP_SUCCESS;
}
else {
/* stack empty */
return POP_FAILURE;
}
}
/* ------------------------------------------- */
int rpn (char *str) {
int stack[MAX];
int top, i;
int a, b;
char *endptr;
char *delim = " \n\t\r\f";
a = b = 0;
stack_init (&top);
for (str = strtok (str, delim); str; str = strtok (0, delim)) {
a = (int) strtod (str, &endptr);
if (endptr > str) {
printf ("n: ");
push (stack, &top, a);
}
else if (*str == '+') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a + b));
}
else if (*str == '-') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a - b));
}
else if (*str == '*') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a * b));
}
else if (*str == '/') {
printf ("%c: ", *str);
pop (stack, &top, &b);
pop (stack, &top, &a);
push (stack, &top, (a / b));
}
else {
fprintf (stderr, "[%c]: ", *str);
fprintf (stderr, "unknown oeprators\n");
}
for (i = 0; i < top; i++) {
printf (" %d", stack[i]);
}
printf ("\n");
}
if (top != 1) {
fprintf (stderr, "stack error\n");
exit (-1);
}
pop (stack, &top, &a);
return a;
}
/* ------------------------------------------- */
int main () {
int result;
char rpn_str[] = " 5 1 2 + 4 * + 3 - ";
//
// 5 + ((1 + 2) * 4) - 3
//
printf ("RPN expression: %s\n\n", rpn_str);
result = rpn (rpn_str);
printf ("\n");
printf ("result: %d\n", result);
return 0;
}
EOF