This commit is contained in:
2023-04-10 00:19:18 +00:00
parent 3dcfa29624
commit 32c1bfa545
6 changed files with 25 additions and 60 deletions
+18 -4
View File
@@ -10,39 +10,53 @@
#include <stdlib.h>
#include <string.h>
// Globals
// ptr_size is number of chars in stack
// ptr is pointer to stack memory
int ptr_size = 0;
char* ptr;
// Pushes parameter addition to stack
void push(char addition) {
// Deal with allocation every time push is called and pop. If not initialized, the source is NULL
// Deal with allocation every time push is called and pop. If not initialized, the source is NULL to initialize
// Add one to the current size, and realloc using the extended memory
ptr = realloc((ptr_size == 0)? NULL: ptr, (ptr_size+1)*sizeof(char));
// Set the item at the new maximum position and increment the size after it is stored
*(ptr+ptr_size) = addition;
ptr_size++;
}
// Takes the last element off the stack and returns it
char pop() {
// Before data is adjusted, take the item to be removed out and store for returning later
char removal = *(ptr+ptr_size-1);
ptr_size--;
// Deal with allocation every time push is called and pop. If not initialized, the source is NULL to initialize
// Realloc the memory, effectively "deleting" the newest, now "popped"
ptr = realloc((ptr_size == 0)? NULL:ptr, (ptr_size)*sizeof(char));
return removal;
}
// Prints the entire stack
void print() {
// Starts at the end, where the start of the stack is
for (int i = ptr_size - 1; i >= 0; i--)
printf("%c\n",*(ptr+i));
}
int main() {
char input[20] = "";
// Start input menu
while(strncmp("quit",input,4)!= 0) {
printf("Enter command (push, print, or quit): ");
printf("Enter command (push, pop, print, or quit): ");
fgets(input,20,stdin);
// Strip newline character from input (fgets)
for (int i=0;i<strlen(input);i++) if (input[i] == '\n') input[i] = '\0';
// Check if the first 4 letter of string are push, then process string for remaining integer
// Check if the first 4 letter of string are push
if (strncmp("push",input,4) == 0) {
// Test for maximum
if (ptr_size != 10) {
// Extract char from push. Should be slot 5
char temp = input[5];
@@ -51,7 +65,7 @@ int main() {
printf("Error: Maximum elements in the stack\n");
}
}
// Check if the first 3 letters of the string are pop
if (strncmp("pop",input,3) == 0)
printf("Popped: %c \n",pop());
+6
View File
@@ -11,14 +11,20 @@
#include "PEASE.h"
int main() {
// Input string
printf("Reverse a phrase: ");
char input[20];
fgets(input,20,stdin);
// Strip newline
for (int i=0;i<strlen(input);i++) if (input[i] == '\n') input[i] = '\0';
// Start String Reversal, reversal string only has to be as long as the input string
char reverse[strlen(input)];
// Two Loops, one to throw in the stack....
for (int i = 0; i < strlen(input); i++) {
push(input[i]);
}
// ... one to yank it back out again
for (int i = 0; i < strlen(input); i++) {
reverse[i] = pop();
}
Binary file not shown.
-5
View File
@@ -1,24 +1,19 @@
// Library File for Part C adapting my code from part B
#ifndef PEASE_H_
#define PEASE_H_
#include <stdlib.h>
int ptr_size = 0;
char* ptr;
void push(char addition) {
// Deal with allocation every time push is called and pop. If not initialized, the source is NULL
ptr = realloc((ptr_size == 0)? NULL: ptr, (ptr_size+1)*sizeof(char));
*(ptr+ptr_size) = addition;
ptr_size++;
}
char pop() {
char removal = *(ptr+ptr_size-1);
ptr_size--;
ptr = realloc((ptr_size == 0)? NULL: ptr, (ptr_size)*sizeof(char));
return removal;
}
#endif
BIN
View File
Binary file not shown.
-50
View File
@@ -1,50 +0,0 @@
#include <stdio.h>
/*
* Users can pass command line arguments when they execute a C program
*
* e.g., following command will execute the 'test_program' and supply 5 as the first argument
* ./test_program 5
*
* e.g., following command will execute the 'test_program' and supply "COS135" as the first argument and "Hello" as the second
* ./test_program COS135 Hello
*
* Notice the change to the main() function, int argc, char *argv[]
* int argc - refers to the number of arguments passed (including the program name; so if you are passing one argument, the value of argc will be 2)
* char *argv[] - is a pointer array which points to each argument passed to the program
*
* Noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied,
* and *argv[n] is the last argument.
* If no arguments are supplied, argc will be one, and if you pass one argument then argc is set at 2.
*
*/
int main(int argc, char *argv[]) // note the change to the parameters in the main() function
{
// argv[0] is always the program name
printf("Program name %s\n", argv[0]);
// argc contains the number of arguments supplied.
// if no arguments, it is 1 as the first argument, by default, is the program name
printf("There are %d arguments.\n", argc);
// The first argument is at argv[1] array location. Note that these values are in char arrays (i.e., strings)
// you can use functions in stdlib.h to convert to numbers, if necessary
// e.g., atoi(), atof(), strtod(), or strtol() functions
// check sample codes at week8-2-codes.zip
// note: you cannot typecast string (char array) to int
if( argc == 2 ) {
printf("The first argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("More than one arguments supplied. The list of arguments supplied are: \n");
// using a loop to output all the arguments
for(int i = 1; i < argc; i++)
printf("%s\n", argv[i]);
putchar('\n');
}
else { // just making sure at least one argument should be given
printf("One argument expected.\n");
}
}