uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 6485c71e32ac0d2ea552c830c0f110f0d68b9f48
parent 9c7421782de7405dddbc4b2dd9d381dd320ec79c
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sun, 29 Dec 2019 22:59:08 +0200

added makefiles, removed scripts and edited README

Diffstat:
Massignment-1.4-functions/README.md | 49+++++++++++++++----------------------------------
Dassignment-1.4-functions/autocompile.bat | 27---------------------------
Dassignment-1.4-functions/autocompile.sh | 18------------------
Aassignment-1.4-functions/hanoi-tower/Makefile | 29+++++++++++++++++++++++++++++
Rassignment-1.4-functions/hanoi-tower/hanoi-tower.c -> assignment-1.4-functions/hanoi-tower/src/hanoi-tower.c | 0
Aassignment-1.4-functions/menu/Makefile | 29+++++++++++++++++++++++++++++
Dassignment-1.4-functions/menu/exec/a.out | 0
Aassignment-1.4-functions/menu/include/menu.h | 12++++++++++++
Dassignment-1.4-functions/menu/obj/main.o | 0
Dassignment-1.4-functions/menu/obj/menu-libs.o | 0
Dassignment-1.4-functions/menu/src/menu-full.c | 111-------------------------------------------------------------------------------
Rassignment-1.4-functions/menu/src/menu-libs.c -> assignment-1.4-functions/menu/src/menu.c | 0
Dassignment-1.4-functions/menu/src/menu.h | 12------------
Aassignment-1.4-functions/sine-cos-taylor/Makefile | 29+++++++++++++++++++++++++++++
Dassignment-1.4-functions/sine-cos-taylor/exec/a.out | 0
Aassignment-1.4-functions/sine-cos-taylor/include/sine-cos-taylor.h | 13+++++++++++++
Dassignment-1.4-functions/sine-cos-taylor/obj/main.o | 0
Dassignment-1.4-functions/sine-cos-taylor/obj/sine-cos-taylor-libs.o | 0
Dassignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-full.c | 125-------------------------------------------------------------------------------
Rassignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-libs.c -> assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.c | 0
Dassignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.h | 13-------------
21 files changed, 127 insertions(+), 340 deletions(-)

diff --git a/assignment-1.4-functions/README.md b/assignment-1.4-functions/README.md @@ -1,4 +1,4 @@ -# Compilation, Linking & Execution steps +# Execution steps * ```program_name``` = program's name * ```full_path/``` = full path @@ -6,41 +6,22 @@ ## Linux -### Method 1 - -1. Open a terminal emulator, or the integrated terminal of your editor. -2. Move to the ```src``` directory of the program: ```$ cd full_path/assignment-1.4-functions/program_name/src``` -3. Compile ```main.c``` and ```program_name-libs.c```: ```$ gcc -c main.c program_name-libs.c && mv main.o program_name-libs.o ../obj && cd ../obj``` -4. Link ```main.o``` and ```program_name-libs.o```: ```$ gcc main.o program_name-libs.o -lm && mv a.out ../exec && cd ../exec``` -5. Execute program: ```$ ./a.out``` - -### Method 2 - -1. Open a terminal emulator, or the integrated terminal of your editor. -2. Move to the assignment's directory: ```$ cd full_path/assignment-1.4-functions``` -3. _In case step 4 fails, do this first_: ```$ chmod +x autocompile.sh``` -4. Run the ```autocompile.sh``` script: ```$ ./autocompile.sh program_name``` - - -In case you want to compile and run ```hanoi-tower```, just do: ```$ gcc -c hanoi-tower.c && gcc hanoi-tower.o && ./a.out``` +```shell +$ cd full_path/assignment-1.4-functions/program_name +$ make +$ make run +$ make clean #optional +``` ## Windows __Requirements:__ MinGW gcc compiler -### Method 1 - -1. Open Command Prompt, or the integrated terminal of your editor. -2. Move to the ```src``` directory of the program: ```$ cd full_path/assignment-1.4-functions/program_name/src``` -3. Compile ```main.c``` and ```program_name-libs.c```: ```$ gcc -c main.c program_name-libs.c && mv main.o program_name-libs.o ../obj && cd ../obj``` -4. Link ```main.o``` and ```program_name-libs.o```: ```$ gcc main.o program_name-libs.o -lm && mv a.exe ../exec && cd ../exec``` -5. Execute program: ```$ a.exe``` - -### Method 2 - -1. Run the ```autocompile.bat``` script. -2. Enter the name of the program when the script prints ```"Program name: "``` __exactly__ as it's written in the folder. - - +In CMD, do the following: -In case you want to compile and run ```hanoi-tower```, just do: ```$ gcc -c hanoi-tower.c && gcc hanoi-tower.o && a.exe```- \ No newline at end of file +```bat +cd full_path/assignment-1.4-functions/program_name +make +make run +make clean +```+ \ No newline at end of file diff --git a/assignment-1.4-functions/autocompile.bat b/assignment-1.4-functions/autocompile.bat @@ -1,26 +0,0 @@ -@ECHO OFF - -set /p "program=Program name: " - -:: Cd to program directory -cd %program% - -:: Create (if missing) obj and exec directories -if not exist obj md obj -if not exist exec md exec - -:: Cd to source code directory -cd src - -:: Compile and move object files to the right directory -gcc -c main.c %program%-libs.c -move main.o ../obj && move %program%-libs.o ../obj -cd ../obj - -:: Link and move executable to the right directory -gcc main.o %program%-libs.o -lm -move a.exe ../exec -cd ../exec - -:: Execute program -a.exe- \ No newline at end of file diff --git a/assignment-1.4-functions/autocompile.sh b/assignment-1.4-functions/autocompile.sh @@ -1,17 +0,0 @@ -#!/bin/bash - -# Cd to program directory -cd $1 - -# Create (if missing) obj and include directories -mkdir -p obj exec - -# Cd to source code directory -cd src - -# Compile and move new files to the right directory -gcc -c main.c $1-libs.c && mv main.o $1-libs.o ../obj && cd ../obj; -gcc main.o $1-libs.o -lm && mv a.out ../exec && cd ../exec; - -# Execute program -./a.out- \ No newline at end of file diff --git a/assignment-1.4-functions/hanoi-tower/Makefile b/assignment-1.4-functions/hanoi-tower/Makefile @@ -0,0 +1,28 @@ +EXEC = ht + +SRC_DIR = src +OBJ_DIR = obj + +SRC = $(wildcard $(SRC_DIR)/*.c) +OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +CPPFLAGS += -Iinclude +CFLAGS += -Wall +LDFLAGS += -Llib +LDLIBS += -lm + +.PHONY: all clean + +all: $(EXEC) + +$(EXEC): $(OBJ) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +run: + ./$(EXEC) + +clean: + $(RM) $(OBJ) $(EXEC)+ \ No newline at end of file diff --git a/assignment-1.4-functions/hanoi-tower/hanoi-tower.c b/assignment-1.4-functions/hanoi-tower/src/hanoi-tower.c diff --git a/assignment-1.4-functions/menu/Makefile b/assignment-1.4-functions/menu/Makefile @@ -0,0 +1,28 @@ +EXEC = menu + +SRC_DIR = src +OBJ_DIR = obj + +SRC = $(wildcard $(SRC_DIR)/*.c) +OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +CPPFLAGS += -Iinclude +CFLAGS += -Wall +LDFLAGS += -Llib +LDLIBS += -lm + +.PHONY: all clean + +all: $(EXEC) + +$(EXEC): $(OBJ) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +run: + ./$(EXEC) + +clean: + $(RM) $(OBJ) $(EXEC)+ \ No newline at end of file diff --git a/assignment-1.4-functions/menu/exec/a.out b/assignment-1.4-functions/menu/exec/a.out Binary files differ. diff --git a/assignment-1.4-functions/menu/include/menu.h b/assignment-1.4-functions/menu/include/menu.h @@ -0,0 +1,11 @@ +#ifndef MENU_H +#define MENU_H + +void menu(int, int); +void pause(); +void results(int, int, int); +int power(int, int); +int factorial(int); +int combinations(int, int); + +#endif+ \ No newline at end of file diff --git a/assignment-1.4-functions/menu/obj/main.o b/assignment-1.4-functions/menu/obj/main.o Binary files differ. diff --git a/assignment-1.4-functions/menu/obj/menu-libs.o b/assignment-1.4-functions/menu/obj/menu-libs.o Binary files differ. diff --git a/assignment-1.4-functions/menu/src/menu-full.c b/assignment-1.4-functions/menu/src/menu-full.c @@ -1,110 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <math.h> - -void menu(int, int); -void pause(); -void results(int, int, int); -int power(int, int); -int factorial(int); -int combinations(int, int); - - -int main(int argc, char **argv) -{ - int a, b; - - printf("A: "); - scanf("%d", &a); - printf("B: "); - scanf("%d", &b); - - menu(a, b); - - return 0; -} - - -void menu(int a, int b) -{ - int menuChoice; - - do - { - system("clear||cls"); - - printf("[1] %d to the power of %d\n[2] %d! and %d!\n[3] Number of combinations between %d and %d\n[4] Exit\n", a, b, a, b, a, b); - printf("Choice: "); - scanf(" %d", &menuChoice); - - switch(menuChoice) - { - case 1: - results(a, b, 1); - break; - case 2: - results(a, b, 2); - break; - case 3: - results(a, b, 3); - break; - } - - pause(); - } while (menuChoice != 4); - - results(a, b, 4); -} - -void pause() -{ - do - { - printf("\nPress [Enter] to continue. . ."); - getchar(); - } while (getchar() != '\n'); - -} - -void results(int a, int b, int menuChoice) -{ - static int numChoices = 0; - - switch(menuChoice) - { - case 1: - printf("%d^%d = %d\n", a, b, power(a, b)); - break; - case 2: - printf("%d! = %d\n", a, factorial(a)); - printf("%d! = %d\n", b, factorial(b)); - break; - case 3: - printf("Combinations between %d and %d: %d\n", a, b, combinations(a, b)); - break; - case 4: - printf("Total number of choices: %d\n", numChoices); - } - - numChoices++; -} - -int power(int a, int b) -{ - return pow(a, b); -} - -int factorial(int num) -{ - int i, fac; - - for (i = 1, fac = 1; i <= num; i++) - fac *= i; - - return fac; -} - -int combinations(int a, int b) -{ - return factorial(a) / (factorial (b) * factorial(a - b)); -}- \ No newline at end of file diff --git a/assignment-1.4-functions/menu/src/menu-libs.c b/assignment-1.4-functions/menu/src/menu.c diff --git a/assignment-1.4-functions/menu/src/menu.h b/assignment-1.4-functions/menu/src/menu.h @@ -1,11 +0,0 @@ -#ifndef HEADER_FILE -#define HEADER_FILE - -void menu(int, int); -void pause(); -void results(int, int, int); -int power(int, int); -int factorial(int); -int combinations(int, int); - -#endif- \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/Makefile b/assignment-1.4-functions/sine-cos-taylor/Makefile @@ -0,0 +1,28 @@ +EXEC = sct + +SRC_DIR = src +OBJ_DIR = obj + +SRC = $(wildcard $(SRC_DIR)/*.c) +OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +CPPFLAGS += -Iinclude +CFLAGS += -Wall +LDFLAGS += -Llib +LDLIBS += -lm + +.PHONY: all clean + +all: $(EXEC) + +$(EXEC): $(OBJ) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +run: + ./$(EXEC) + +clean: + $(RM) $(OBJ) $(EXEC)+ \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/exec/a.out b/assignment-1.4-functions/sine-cos-taylor/exec/a.out Binary files differ. diff --git a/assignment-1.4-functions/sine-cos-taylor/include/sine-cos-taylor.h b/assignment-1.4-functions/sine-cos-taylor/include/sine-cos-taylor.h @@ -0,0 +1,12 @@ +#ifndef SINE_COS_TAYLOR_H +#define SINE_COS_TAYLOR_H + +double input(); +double rads_conv(double); +double sine_calc(double); +double cosine_calc(double); +double power(double, int); +double factorial(int); +int compare(double, double); + +#endif+ \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/obj/main.o b/assignment-1.4-functions/sine-cos-taylor/obj/main.o Binary files differ. diff --git a/assignment-1.4-functions/sine-cos-taylor/obj/sine-cos-taylor-libs.o b/assignment-1.4-functions/sine-cos-taylor/obj/sine-cos-taylor-libs.o Binary files differ. diff --git a/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-full.c b/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-full.c @@ -1,124 +0,0 @@ -#include <stdio.h> -#include <math.h> - -#define PI 3.141592654 -#define ACCURACY 0.000001 -#define COMPARISON 0.000009 - -double input(); -double rads_conv(double); -double sine_calc(double); -double cosine_calc(double); -double power(double, int); -double factorial(int); -int compare(double, double); - - -int main(int argc, char **argv) -{ - double xDegrees, xRads; - - xDegrees = input(); - - xRads = rads_conv(xDegrees); - - if (compare(sine_calc(xRads), sin(xRads))) - printf("sin(%f) = sin(%f) = %f rads\n", xDegrees, xRads, sine_calc(xRads)); - else - printf("Error.\n"); - - if (compare(cosine_calc(xRads), cos(xRads))) - printf("cos(%f) = cos(%f) = %f rads\n", xDegrees, xRads, cosine_calc(xRads)); - else - printf("Error.\n"); - - return 0; -} - - -double input() -{ - double xDegrees; - - printf("x (in degrees): "); - scanf("%lf", &xDegrees); - - return xDegrees; -} - -double rads_conv(double xDegrees) -{ - return xDegrees * (PI/180.0); -} - -double sine_calc(double xRads) -{ - double currentFrac, previousFrac, sineValue = 0; - int exponent = 1, sign = 1; - - currentFrac = power(xRads, exponent) / factorial(exponent); - - do - { - sineValue += sign * currentFrac; - - exponent += 2; - - previousFrac = currentFrac; - currentFrac = power(xRads, exponent) / factorial(exponent); - - sign *= -1; - } while (fabs(previousFrac - currentFrac) > ACCURACY); - - return sineValue; -} - -double cosine_calc(double xRads) -{ - double currentFrac = 1, previousFrac, cosineValue = 0; - int exponent = 0, sign = 1; - - do - { - cosineValue += sign * currentFrac; - - exponent += 2; - - previousFrac = currentFrac; - currentFrac = power(xRads, exponent) / factorial(exponent); - - sign *= -1; - } while (fabs(previousFrac - currentFrac) > ACCURACY); - - return cosineValue; -} - -double power(double xRads, int exponent) -{ - int i; - double value; - - for (i = 0, value = 1; i < exponent; i++) - value *= xRads; - - return value; -} - -double factorial(int exponent) -{ - int i; - double fac; - - for (i = 1, fac = 1; i <= exponent; i++) - fac *= i; - - return fac; -} - -int compare(double calcValue, double funcValue) -{ - if (fabs(calcValue - funcValue) <= COMPARISON) - return 1; - else - return 0; -}- \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-libs.c b/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.c diff --git a/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.h b/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.h @@ -1,12 +0,0 @@ -#ifndef HEADER_FILE -#define HEADER_FILE - -double input(); -double rads_conv(double); -double sine_calc(double); -double cosine_calc(double); -double power(double, int); -double factorial(int); -int compare(double, double); - -#endif- \ No newline at end of file