Commit de68eae7 authored by kjc015's avatar kjc015
Browse files

Lab 08, Complete

parent 1674a9b8
CC=gcc
CFLAGS=-Wall -Werror
all: strnode_test node_test
LDLIBS=-lm
all: strnode_test node_test stack_test
strnode_test: strnode.o strnode_test.o strnode.h
clean:
rm -f strnode_test *.o
node_test: node.o node_test.o node.h
clean:
rm -f node_test *.o
stack_test: stack.o stack_test.o stack.h
clean:
rm -f stack_test *.o
pcalc: pcalc.o
$(CC) $(CFLAGS) $(LDLIBS) pcalc.c -o pcalc
......@@ -3,14 +3,14 @@
#include <string.h>
#include "node.h"
struct node *node_create(void* data, int size){
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = (char*)malloc(size);
strcpy(newNode->data, data);
newNode->next = NULL;
return newNode;
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = (char*)malloc(size);
memcpy(newNode->data, data, size);
newNode->next = NULL;
return newNode;
}
void node_destroy(struct node *n){
free(n->data);
free(n->data);
free(n);
}
......@@ -11,30 +11,31 @@ struct node *n1, *n2, *n3, *p;
int main(int argc, char *argv[]) {
// create strnodes
n1 = node_create("hello", 6);
n2 = node_create("there", 6);
n3 = node_create("prof", 5);
n1 = node_create("hello", 6);
n2 = node_create("there", 6);
n3 = node_create("prof", 5);
printf("node_test running...\n");
printf("node_test running...\n");
// manually "link" the nodes together.
n1->next = n2;
n2->next = n3;
n3->next = NULL;
n1->next = n2;
n2->next = n3;
n3->next = NULL;
// p points to node n1 initially
p = n1;
while (p != NULL) {
// Complete this line to print the current node's string and
// the stored length (do not use strlen!)
printf("str: %s - length: %zu\n", (char*)p->data, strlen(p->data));
// TODO: add code to move p to point to next node
// until you add this line, this program will have an infinite loop.
node_destroy(p);
p = (*p).next;
}
p = NULL;
return 0;
p = n1;
while (p != NULL) {
printf("str: %s - length: %zu\n", (char*)p->data, strlen(p->data));
p = (*p).next;
}
p = n1;
while(p!=NULL){
struct node* p1 = p->next;
node_destroy(p);
p=p1;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "stack.c"
int main (int argc, char* argv[]){
float op1;
float op2;
float result;
int i;
char op[10];
struct stack* new_stack = stack_create();
for(i = 1; i < argc; i++){
strcpy(op, argv[i]);
if (strcmp(op, "+") == 0){
stack_pop(new_stack, &op2, sizeof(float));
stack_pop(new_stack, &op1, sizeof(float));
result = (op1+op2);
stack_push(new_stack, &result, sizeof(float));
}
else if (strcmp(op, "-") == 0){
stack_pop(new_stack, &op2, sizeof(float));
stack_pop(new_stack, &op1, sizeof(float));
result = (op1-op2);
stack_push(new_stack, &result, sizeof(float));
}
else if (strcmp(op, "x") == 0){
stack_pop(new_stack, &op2, sizeof(float));
stack_pop(new_stack, &op1, sizeof(float));
result = (op1*op2);
stack_push(new_stack, &result, sizeof(float));
}
else if (strcmp(op, "/") == 0){
stack_pop(new_stack, &op2, sizeof(float));
stack_pop(new_stack, &op1, sizeof(float));
result = (op1/op2);
stack_push(new_stack, &result, sizeof(float));
}
else if (strcmp(op, "^") == 0){
stack_pop(new_stack, &op2, sizeof(float));
stack_pop(new_stack, &op1, sizeof(float));
result = powf(op1,op2);
stack_push(new_stack, &result, sizeof(float));
}
else{
result = atof(op);
stack_push(new_stack, &result, sizeof(float));
}
}
while(stack_empty(new_stack) == 0){
stack_pop(new_stack, &result, sizeof(float));
printf("%f\n", result);
}
stack_destroy(new_stack);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
#include "node.c"
struct stack* stack_create(){
struct stack* stacks = (struct stack*) malloc(sizeof(struct stack));
return stacks;
}
int stack_empty(struct stack* s){
if(s->top == NULL){
return 1;
}
else{
return 0;
}
}
void stack_destroy(struct stack* s){
free(s);
}
void stack_push(struct stack* s, void* src, int size){
struct node* new_node = node_create(src, size);
new_node->next = s->top;
s->top = new_node;
}
void stack_pop(struct stack* s, void* dest, int size){
memcpy(dest, s->top->data, size);
struct node* temp = s->top;
s->top = temp->next;
node_destroy(temp);
}
#ifndef _stack_h_
#define _stack_h_
#include "node.h"
struct stack {
struct node* top;
};
struct stack *stack_create();
int stack_empty(struct stack *s);
void stack_destroy(struct stack *s);
void stack_push(struct stack* s, void* src, int size);
void stack_pop(struct stack* s, void* dest, int size);
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment