Skip to content

Commit

Permalink
Task upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jekwupeter committed Jun 18, 2022
1 parent c4d5913 commit 961d63c
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"monty.h": "c"
}
}
4 changes: 4 additions & 0 deletions 00.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
push 1$
push 2$
push 3$
pall$
77 changes: 56 additions & 21 deletions dlinked_list_functions.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
#include "monty.h"
/**
* add_dnodeint - adds a new node to beggining of dlinked list
* @stack_ptr: pointer to top of dlinked list
* @n: input data for new node
*
* Return: adress to new dnode added
*
* */

stack_t *add_dnodeint(stack_t **stack_ptr, const int n)
{
stack_t *new_node;
stack_t *new_node;

/* allocate memory to node */
new_node = (stack_t *)malloc(sizeof(stack_t));
if (new_node == NULL)
return (NULL);
/* allocate memory to node */
new_node = (stack_t *)malloc(sizeof(stack_t));
if (new_node == NULL)
return (NULL);

new_node->n = n;/* put in data*/
/* make next of new node as head & prev as NULL */
new_node->next = n;
new_node->prev = NULL;
new_node->n = n;/* put in data*/
new_node->prev = NULL;
/* Check if the list is empty */
if (*stack_ptr == NULL)
{
new_node->next = NULL;
(*stack_ptr) = new_node;
return (new_node);
}
/* change the prev of stack_ptr node to new node */
(*stack_ptr)->prev = new_node;
/* make next of new node as stack_ptr & prev as NULL */
new_node->next = (*stack_ptr);
new_node->prev = NULL;
(*stack_ptr) = new_node;

/* change the prev of head node to new node */
if ((*stack_ptr) != NULL)
(*stack_ptr)->prev = new_node
return (new_node);
}

return (newnode);
int delete_dnodeint(stack_t **stack_ptr)
{
stack_t *tmp;
tmp = (*stack_ptr);
if (*stack_ptr == NULL)
return (-1);
/* Move the head pointer to the next node */
(*stack_ptr) = tmp->next;
free(tmp);
return (1);
}

size_t print_dlistint(const stack_t *stack_ptr)
{
int i = 0;
while(stack_ptr){
printf("%d\n", stack_ptr->n);
stack_ptr = stack_ptr->next;
i++;
}
return (i);
}

void free_stack(stack_t *stack_ptr)
{
stack_t *tmp;

while (stack_ptr != NULL)
{
tmp = stack_ptr;
stack_ptr = stack_ptr->next;
free(tmp);
}
}
54 changes: 54 additions & 0 deletions func1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "monty.h"

/**
* opcode - finds the functions matching the opcode
* @stack_ptr: pointer to dlinkedlist
* @str: string to compare
* @count: line counts
* @arg_data: data following the function
*/
void opcode(stack_t **stack_ptr, char *line, unsigned int count)
{
/*
instruction_t op[] = {
{"pall", pall},
{"swap", _add},
{"pop", pop},
{"pint", pint},
{"mod", _mod},
{"mul", _mul},
{"div", _div},
{"sub", _sub},
{"nop", nop},
{"add", _add},
{NULL, NULL}
};*/
instruction_t op[] = {
{"pall", pall},
{NULL, NULL}
};
int i;
char *str = NULL;
char *arg_data;
char delimit[] = " \t\r\n\f$";

str = strtok(line, delimit);
arg_data = strtok(NULL, delimit);
if (str == NULL)
return;
if (strcmp(str, "push") == 0)
{
push(stack_ptr, arg_data, count);
return;
}
for (i = 0; op[i].opcode; i++)
{
if (strcmp(op[i].opcode, str) == 0)
{
op[i].f(stack_ptr, count);
return;
}
}
fprintf(stderr, "L%d: unknown instruction %s\n", count, str);
exit(EXIT_FAILURE);
}
38 changes: 38 additions & 0 deletions func2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "monty.h"

/**
* push - add an element to the stack/queue
* @stack_ptr: pointer to dlinked list
* @arg_data: data of node to be pushed
* @count: line number
*/
void push(stack_t **stack_ptr, char *arg_data, unsigned int count)
{
stack_t *node;

if (arg_data == NULL)
{
fprintf(stderr, "L%d: usage: push integer\n", count);
free(*stack_ptr);
*stack_ptr = NULL;
exit(EXIT_FAILURE);
};

node = add_dnodeint(stack_ptr, atoi(arg_data));
if (node == NULL)
{
puts("Error: malloc failed");
free_stack(*stack_ptr);
exit(EXIT_FAILURE);
}
}

/**
* pall - prints all the values on the stack, starting from the top of the stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void pall(stack_t **stack_ptr, unsigned int count __attribute__((unused)))
{
print_dlistint(*stack_ptr);
}
46 changes: 46 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "monty.h"
/**
* main - entry point
* @argv: argument vector
* @argc: argument counter
*
* Return: Nothing
*/
int main(int argc, char *argv[])
{
FILE *fp;
char *line = NULL;
size_t len = 0;
stack_t *stack_ptr = NULL;
unsigned int count = 1;
ssize_t read;

if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}

fp = fopen(argv[1], "r");

if (fp == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[0]);
exit(EXIT_FAILURE);
}

while ((read = getline(&line, &len, fp)) != -1)
{
if (*line == '\n')
{
count++;
continue;
}
opcode(&stack_ptr, line, count);
count++;
}
free(line);
fclose(fp);
free_stack(stack_ptr);
exit(EXIT_SUCCESS);
}
Binary file added monty.exe
Binary file not shown.
15 changes: 14 additions & 1 deletion monty.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#ifndef MONTY_H
#define MONTY_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
* struct stack_s - doubly linked list representation of a stack (or queue)
* @n: integer
Expand All @@ -15,6 +19,7 @@ typedef struct stack_s
struct stack_s *prev;
struct stack_s *next;
} stack_t;

/**
* struct instruction_s - opcode and its function
* @opcode: the opcode
Expand All @@ -28,5 +33,13 @@ typedef struct instruction_s
char *opcode;
void (*f)(stack_t **stack, unsigned int line_number);
} instruction_t;
extern stack_t *stack_ptr = NULL;

size_t getline(char **lineptr, size_t *n, FILE *stream);
void pall(stack_t **stack_ptr, unsigned int count __attribute__((unused)));
size_t print_dlistint(const stack_t *stack_ptr);
int delete_dnodeint(stack_t **stack_ptr);
stack_t *add_dnodeint(stack_t **stack_ptr, const int n);
void push(stack_t **stack_ptr, char *arg_data, unsigned int count);
void opcode(stack_t **stack_ptr, char *line, unsigned int count);
void free_stack(stack_t *stack_ptr);
#endif
62 changes: 62 additions & 0 deletions other_functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "monty.h"
/**
* This code is public domain -- Will Hartung 4/9/09
* getline - reads a full line from a stream
* @lineptr: pointer to character array
* @n: pointer to variable that keeps array size
* stream: stdin is the stream from which the file is read
*
* Return: returns the number of char entered
*
* */
size_t getline(char **lineptr, size_t *n, FILE *stream) {
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;

if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;

c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
bufptr = malloc(128);
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while(c != EOF) {
if ((size_t)(p - bufptr) > (size - 1)) {
size = size + 128;
bufptr = realloc(bufptr, size);
if (bufptr == NULL) {
return -1;
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}

*p++ = '\0';
*lineptr = bufptr;
*n = size;

return p - bufptr - 1;
}

0 comments on commit 961d63c

Please sign in to comment.