Compare commits
23 Commits
27057ff3ab
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ccd216591a | |||
| 8ff0e89bb1 | |||
| 37c919df90 | |||
| 9dbc24efc1 | |||
| db8fc15560 | |||
| 523dac1f4d | |||
| f3b7de0489 | |||
| 3834d764c1 | |||
| 0cc5d69c0a | |||
| 02a12622c2 | |||
| e337ee3841 | |||
| 76c7281b84 | |||
| 69a943371c | |||
| 8bdf3c0fbf | |||
| b761625110 | |||
| 450579d266 | |||
| 6e1a33db8a | |||
| a214e58f2c | |||
| 840f5f5424 | |||
| bd700e78b2 | |||
| 20777e094c | |||
| f48b0288d2 | |||
| 152714b4ea |
+71
@@ -119,6 +119,8 @@ lerna-debug.log*
|
|||||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
build
|
||||||
|
|
||||||
# Runtime data
|
# Runtime data
|
||||||
pids
|
pids
|
||||||
*.pid
|
*.pid
|
||||||
@@ -246,6 +248,75 @@ dist
|
|||||||
# SvelteKit build / generate output
|
# SvelteKit build / generate output
|
||||||
.svelte-kit
|
.svelte-kit
|
||||||
|
|
||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Linker files
|
||||||
|
*.ilk
|
||||||
|
|
||||||
|
# Debugger Files
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
*.so.*
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
*.smod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
|
||||||
|
# Build directories
|
||||||
|
build/
|
||||||
|
Build/
|
||||||
|
build-*/
|
||||||
|
|
||||||
|
# CMake generated files
|
||||||
|
CMakeFiles/
|
||||||
|
CMakeCache.txt
|
||||||
|
cmake_install.cmake
|
||||||
|
Makefile
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
*.tmp
|
||||||
|
*.log
|
||||||
|
*.bak
|
||||||
|
*.swp
|
||||||
|
|
||||||
|
# vcpkg
|
||||||
|
vcpkg_installed/
|
||||||
|
|
||||||
|
# debug information files
|
||||||
|
*.dwo
|
||||||
|
|
||||||
|
# test output & cache
|
||||||
|
Testing/
|
||||||
|
.cache/
|
||||||
|
|
||||||
#!! ERROR: vscode is undefined. Use list command to see defined gitignore types !!#
|
#!! ERROR: vscode is undefined. Use list command to see defined gitignore types !!#
|
||||||
|
|
||||||
# End of https://www.toptal.com/developers/gitignore/api/node,vscode,emacs,c
|
# End of https://www.toptal.com/developers/gitignore/api/node,vscode,emacs,c
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
een programma dat newlines leest en ze dan weer print
|
||||||
|
|
||||||
|
we gaan een scanf moeten maken die newlines leest en dan malloced voor elke
|
||||||
|
string
|
||||||
|
|
||||||
|
scanf lijkt automatisch te stoppen op newlines, maar gaat wel onmiddelijk door totdat alle scanf's satisfied zijn
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define WORDCOUNT 5
|
||||||
|
|
||||||
|
void storeinput(char input[], int index, char **strarr) {
|
||||||
|
char *myword = malloc(strlen(input) +1);
|
||||||
|
//*myword = *input; //blijkbaar hier strcpy voor nodig, want anders overwrite
|
||||||
|
//ik hier alleen maar de value van de pointer zelf
|
||||||
|
strcpy(myword, input); //myword is a pointer to allocated memory, but it's empty unless strcopy
|
||||||
|
printf("myword:%s\n", myword);
|
||||||
|
strarr[index] = myword;
|
||||||
|
printf("strarr:%s\n", strarr[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// laten we eerst een array van pointers naar strings maken
|
||||||
|
char **strarr = malloc(sizeof(char *) * 10);
|
||||||
|
|
||||||
|
// moeten nu een functie maken die individuele strings malloced?
|
||||||
|
|
||||||
|
for (int i = 0; i < WORDCOUNT; i++) {
|
||||||
|
char temp[50];
|
||||||
|
scanf("%s", temp);
|
||||||
|
storeinput(temp, i, strarr);
|
||||||
|
printf("wordin:%d %s\n", i, temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < WORDCOUNT; i++) {
|
||||||
|
printf("word:%d, %s\n", i, strarr[i]);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
take input
|
||||||
|
decide if hex or binary
|
||||||
|
then convert
|
||||||
|
then print
|
||||||
|
|
||||||
|
let's convert binary to hex first
|
||||||
|
for each byte, split into nibble,
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
//might have to add some padding, but basic works
|
||||||
|
void bintohex(char* string_in){
|
||||||
|
const char hexmap[] = "0123456789ABCDEF";
|
||||||
|
char hexnum[4] = {};
|
||||||
|
int stringl = strnlen(string_in, 10)-1;
|
||||||
|
//aantal nibbles
|
||||||
|
int loopamount = stringl/4;
|
||||||
|
printf("totalstringlength=%d\n", stringl);
|
||||||
|
for (int loopcount = 0; loopcount < loopamount; loopcount++) {
|
||||||
|
int bin2dec = 0;
|
||||||
|
//rechts naar links in nibble
|
||||||
|
for (int nibbledigit = 0 ; nibbledigit <4 ; nibbledigit++) {
|
||||||
|
//nibblecount + current
|
||||||
|
int targetdigit = loopcount*4+(4-1-nibbledigit);
|
||||||
|
//check if j+i = 1
|
||||||
|
//printf("nibbledigit:%d=%c=%i\n", j, string_in[targetdigit],1<<j);
|
||||||
|
bin2dec += 1 * (string_in[targetdigit]-'0')*1<<(nibbledigit);
|
||||||
|
}
|
||||||
|
hexnum[loopcount]=hexmap[bin2dec];
|
||||||
|
printf("nibble:%i=%d\n",loopcount,bin2dec);
|
||||||
|
}
|
||||||
|
printf("hex=%s", hexnum);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
printf("input either binary or hexadecimal\n");
|
||||||
|
char buff[32+2]; //4bytes+\n+null
|
||||||
|
fgets(buff, sizeof(buff), stdin);
|
||||||
|
bintohex(buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef HASH_FNV_H
|
||||||
|
#define HASH_FNV_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
uint32_t hasher(void *data, size_t len);
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#include "stringchicken.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
char myString[100];
|
||||||
|
char ch;
|
||||||
|
FILE *fptr = fopen(argv[1], "r");
|
||||||
|
|
||||||
|
if (fptr == NULL) {
|
||||||
|
printf("file doesn't exist");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct stringchicken *mystringarray = create_string_chicken();
|
||||||
|
|
||||||
|
while (fgets(myString, 100, fptr)) {
|
||||||
|
push_SC(myString, mystringarray);
|
||||||
|
}
|
||||||
|
|
||||||
|
print_SC(mystringarray);
|
||||||
|
int mylength = strnlen(myString, 100);
|
||||||
|
|
||||||
|
fclose(fptr);
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
dit is een test
|
||||||
|
test2
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#include "stringchicken.h"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
parser that can read K=V, maybe also K:V? although would just be configurable seperator
|
||||||
|
I know python uses dictionary which uses a hash table so key -> hash -> value table
|
||||||
|
for this assignment, I could use 2 arrays? though that's... a bit dangerous? or at least tricky, in terms of reassignments.
|
||||||
|
let's just go with a struct with 2 dynamic arrays where the indices are aligned
|
||||||
|
|
||||||
|
ik neem dus eerst 1 die het omzet naar een struct van strings, dan een struct van de KV values.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct stringchicken *create_string_chicken(){
|
||||||
|
struct stringchicken *mystruct = malloc(sizeof(struct stringchicken));
|
||||||
|
mystruct->length = 0;
|
||||||
|
mystruct->capacity = 2;
|
||||||
|
|
||||||
|
mystruct->data = malloc(sizeof(char*) * mystruct->capacity);
|
||||||
|
|
||||||
|
return mystruct;
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_SC(char *string_input, struct stringchicken *struct_input){
|
||||||
|
if (struct_input->length == struct_input->capacity) {
|
||||||
|
struct_input->capacity*=2;
|
||||||
|
char **tempptr = realloc(struct_input->data, sizeof(char*) * struct_input->capacity);
|
||||||
|
|
||||||
|
if (tempptr == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct_input->data = tempptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *newstring = malloc(strnlen(string_input, 101)+1);
|
||||||
|
strcpy(newstring,string_input);
|
||||||
|
struct_input->data[struct_input->length] = newstring;
|
||||||
|
|
||||||
|
struct_input->length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_SC(struct stringchicken *struct_input){
|
||||||
|
for (int i = 0; i < struct_input->length; i++) {
|
||||||
|
free(struct_input->data[i]);
|
||||||
|
}
|
||||||
|
free(struct_input->data);
|
||||||
|
free(struct_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void print_SC(struct stringchicken *input_struct){
|
||||||
|
for (size_t i = 0; i < input_struct->length; i++) {
|
||||||
|
printf("line:%zu equals %s", i, input_struct->data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef STRINGCHICKEN_H
|
||||||
|
#define STRINGCHICKEN_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
struct stringchicken{
|
||||||
|
char **data;
|
||||||
|
size_t length;
|
||||||
|
size_t capacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct stringchicken *create_string_chicken(void);
|
||||||
|
void push_SC(char *string_input, struct stringchicken *struct_input);
|
||||||
|
void print_SC(struct stringchicken *input_struct);
|
||||||
|
|
||||||
|
#endif // STRINGCHICKEN_H
|
||||||
|
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
todo
|
||||||
|
change intarray into str array
|
||||||
|
make push array malloc for new str
|
||||||
|
make input strings instead of int (should use fgets?)
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct StringArray{
|
||||||
|
char **data;
|
||||||
|
size_t length;
|
||||||
|
size_t capacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
void print_SA(struct StringArray *in_str) {
|
||||||
|
for (size_t i = 0; i < in_str->length; i++) {
|
||||||
|
printf("word:%zu = %s\n", i, in_str->data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct StringArray *SA_create(size_t in_capacity) {
|
||||||
|
struct StringArray *mystr = malloc(sizeof(struct StringArray)); //allocate memory for struct
|
||||||
|
|
||||||
|
mystr->data = malloc(sizeof(char*) * in_capacity); //make sure the pointer in the struct points to the array
|
||||||
|
mystr->length = 0; //forgot to actually assign a value to the struct
|
||||||
|
mystr->capacity = in_capacity;
|
||||||
|
|
||||||
|
if (mystr->data == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mystr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_string_len(char *in_string){
|
||||||
|
int length = 0;
|
||||||
|
char currchar;
|
||||||
|
do {
|
||||||
|
currchar = in_string[length];
|
||||||
|
length++;
|
||||||
|
}
|
||||||
|
while (currchar != '\0');
|
||||||
|
|
||||||
|
printf("length of word: %d\n", length); //for some reason length is always 2 more than expected, so 1 more than necessary? apparently because of \n, not sure how to filter that.
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* push value to struct, also handle realloc */
|
||||||
|
void SA_push(char *in_string, struct StringArray *in_str){
|
||||||
|
if (in_str->length == in_str->capacity) { //check if already full
|
||||||
|
in_str->capacity*=2; //if so we should double
|
||||||
|
printf("array capacity is now:%zu\n", in_str->capacity);
|
||||||
|
char **temp; //create a temporary pointer
|
||||||
|
temp = realloc(in_str->data, sizeof(char*)*in_str->capacity); //realloc the data into a new bucket
|
||||||
|
if(temp == NULL){exit(0);}; //check if actually valid
|
||||||
|
in_str->data = temp; //if valid, make sure the pointer in the struct points to the realloced block
|
||||||
|
}
|
||||||
|
|
||||||
|
char *newword = malloc(sizeof(char) * get_string_len(in_string)); //allocate memory for new string, apparantly my function already includes the length of null term
|
||||||
|
strcpy(newword, in_string); //copy word into memory
|
||||||
|
in_str->data[in_str->length] = newword;
|
||||||
|
printf("word:%zu = %s\n", in_str->length, in_str->data[in_str->length]);
|
||||||
|
in_str->length++; //didn't read the flow when transferring and accidentally put this in the if block
|
||||||
|
printf("array length is now:%zu\n", in_str->length);
|
||||||
|
//also had an off-by-one error because I increased length before doing anything
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SA_free(struct StringArray *inp_str) {
|
||||||
|
for (size_t i = 0; i < inp_str->length; i++) { //for each pointer in array
|
||||||
|
free(inp_str->data[i]); //free at pointer
|
||||||
|
}
|
||||||
|
free(inp_str->data); //free pointer to pointers
|
||||||
|
free(inp_str); //free the struct
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
srand(time(NULL));
|
||||||
|
printf("input words:\n");
|
||||||
|
|
||||||
|
struct StringArray *mystringarraystr = SA_create(2);
|
||||||
|
|
||||||
|
char input[50];
|
||||||
|
do{
|
||||||
|
fgets(input, sizeof(input), stdin); //get input from stdin of size input[] and put it in input
|
||||||
|
SA_push(input, mystringarraystr); //push the value of input into mystr
|
||||||
|
} while (*input != '-');
|
||||||
|
|
||||||
|
print_SA(mystringarraystr);
|
||||||
|
|
||||||
|
SA_free(mystringarraystr);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,38 @@
|
|||||||
|
#include <ncurses.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
initscr();
|
||||||
|
noecho();
|
||||||
|
cbreak();
|
||||||
|
keypad(stdscr, TRUE);
|
||||||
|
curs_set(1);
|
||||||
|
|
||||||
|
if(has_colors() == FALSE ) {
|
||||||
|
endwin();
|
||||||
|
puts("no colors");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
start_color();
|
||||||
|
init_pair(1, COLOR_WHITE, COLOR_BLUE);
|
||||||
|
attron(COLOR_PAIR(1));
|
||||||
|
|
||||||
|
int y,x;
|
||||||
|
getmaxyx(stdscr, y,x);
|
||||||
|
y=y*0.5;
|
||||||
|
x=(x*0.5) -6;
|
||||||
|
mvwprintw(stdscr, y, x, "Hello World");
|
||||||
|
mvwprintw(stdscr, 2, 2, "Hellow World1");
|
||||||
|
refresh();
|
||||||
|
|
||||||
|
attroff(COLOR_PAIR(1));
|
||||||
|
|
||||||
|
getch();
|
||||||
|
|
||||||
|
endwin();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.24)
|
||||||
|
project(cppout)
|
||||||
|
|
||||||
|
file(GLOB SOURCE_FILES "*.cpp")
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
void printhello(const char *name) {
|
||||||
|
printf("hello %s \n", name);
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
void printhello(const char *name);
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#include <cstdlib>
|
||||||
|
#include "hello.h"
|
||||||
|
int main() {
|
||||||
|
printhello("test");
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,6 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << "Hello, world";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.24)
|
||||||
|
project(cppout)
|
||||||
|
|
||||||
|
file(GLOB SOURCE_FILES "*.cpp")
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#include <cstddef>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void bsort(std::vector<int> &input_vec){
|
||||||
|
/*
|
||||||
|
voor elk nummer i in k behalve de laatste
|
||||||
|
ga langs elk volgende nummer j behalve de laatste
|
||||||
|
en kijk of i+1 groter is dan i, als i groter is dan j, verplaats i naar
|
||||||
|
j
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (size_t i = 0; i < input_vec.size(); i++) {
|
||||||
|
printf("%zu\n", i);
|
||||||
|
for (size_t j = 0; j < input_vec.size()-1-i; j++) {
|
||||||
|
if (input_vec[j]>input_vec[j+1]) {
|
||||||
|
int temp = input_vec[j + 1];
|
||||||
|
input_vec[j + 1] = input_vec[j];
|
||||||
|
input_vec[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns an array of length filled with random positive integers of up to max
|
||||||
|
std::vector<int> createranarr(size_t input_length, size_t max){
|
||||||
|
std::vector<int> myv;
|
||||||
|
for (size_t i = 0; i < input_length; i++) {
|
||||||
|
myv.push_back(rand() % max);
|
||||||
|
}
|
||||||
|
return myv;
|
||||||
|
}
|
||||||
|
|
||||||
|
void arrprinter(const std::vector<int> &input_vec) {
|
||||||
|
for (size_t i = 0; i < input_vec.size(); i++) {
|
||||||
|
std::printf("number:%zu = %d\n",i, input_vec[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> myvector = createranarr(10, 100);
|
||||||
|
arrprinter(myvector);
|
||||||
|
bsort(myvector);
|
||||||
|
arrprinter(myvector);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.24)
|
||||||
|
project(cppout)
|
||||||
|
|
||||||
|
file(GLOB SOURCE_FILES "*.cpp")
|
||||||
|
|
||||||
|
find_package(Curses REQUIRED)
|
||||||
|
include_directories(${CURSES_INCLUDE_DIR})
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_option(${PROJECT_NAME} PRIVATE /W4)
|
||||||
|
else()
|
||||||
|
target_compile_options(${PROJECT_NAME} PRIVATE
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wpedantic
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
#include "timer.hpp"
|
||||||
|
#include "timeractions.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <clocale>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <ctime>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <thread>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
/*
|
||||||
|
todo
|
||||||
|
figure out namespaces, using, and auto
|
||||||
|
read about std function and std bind
|
||||||
|
try to do with objects / classes instead
|
||||||
|
centralize into event sender and subscriber
|
||||||
|
*/
|
||||||
|
|
||||||
|
void myupdatedisplay(const Timer &timer_input) {
|
||||||
|
int x_offset = COLS / 4;
|
||||||
|
mvprintw(timer_input.trigger_counter, timer_input.trigger_counter + x_offset,
|
||||||
|
"test: █");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> my_numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||||
|
std::vector<int> my_other_numbers = {4, 9, 8, 6, 2, 1, 10, 3, 5, 7};
|
||||||
|
|
||||||
|
void displaynumbers(int start_x, int start_y, std::vector<int> &input_numbers, cchar_t &input_char) {
|
||||||
|
for (size_t i = 0; i < input_numbers.size(); i++) {
|
||||||
|
/**
|
||||||
|
I make a horizontal line of i chars, which are supposed to represent mynumbers
|
||||||
|
above that, I want a line of bars.
|
||||||
|
_all_ bars stretch from LINES-1 to LINES-1-numbervalue
|
||||||
|
**/
|
||||||
|
mvprintw(LINES -1 - start_y, i + start_x, "%d,", input_numbers[i]);
|
||||||
|
mvwvline_set(stdscr, LINES-1-input_numbers[i]-start_y, i+start_x, &input_char, input_numbers[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void myinitialize() {
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
initscr();
|
||||||
|
nodelay(stdscr, FALSE);
|
||||||
|
refresh();
|
||||||
|
start_color();
|
||||||
|
init_pair(1, COLOR_RED, COLOR_GREEN);
|
||||||
|
attron(COLOR_PAIR(1));
|
||||||
|
printw("Hello World! 🌍 テスト 测试");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
myinitialize();
|
||||||
|
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
||||||
|
|
||||||
|
//create actions, then put them in timers, then put them into the timermanager
|
||||||
|
|
||||||
|
auto my_action = std::make_unique<SwapAction>(my_numbers);
|
||||||
|
auto clock_timer =
|
||||||
|
std::make_unique<Timer>("secondsprinter", 1, 10, std::move(my_action));
|
||||||
|
|
||||||
|
auto my_other_sort = std::make_unique<SortAction>(my_other_numbers);
|
||||||
|
auto sort_timer =
|
||||||
|
std::make_unique<Timer>("sorttimer", 0.5, std::move(my_other_sort));
|
||||||
|
|
||||||
|
TimerManager my_timer_manager;
|
||||||
|
my_timer_manager.addtimer(std::move(clock_timer));
|
||||||
|
my_timer_manager.addtimer(std::move(sort_timer));
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
erase();
|
||||||
|
|
||||||
|
my_timer_manager.updatetimers();
|
||||||
|
|
||||||
|
displaynumbers(0,0,my_numbers, my_c_char);
|
||||||
|
displaynumbers(10, 10, my_other_numbers, my_c_char);
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
}
|
||||||
|
|
||||||
|
getch();
|
||||||
|
endwin();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
// calls callbackfunc on specified interval based on accumulated deltatime
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// infinite timer
|
||||||
|
Timer::Timer(std::string name_input, double interval_input,
|
||||||
|
std::unique_ptr<TimerAction> action_input)
|
||||||
|
: time_interval(interval_input), name(name_input), is_infinite(true),
|
||||||
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
|
// limited timer
|
||||||
|
Timer::Timer(std::string name, double interval_input, int trigger_amount,
|
||||||
|
std::unique_ptr<TimerAction> action_input)
|
||||||
|
: trigger_amount(trigger_amount), time_interval(interval_input), name(name),
|
||||||
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
|
// callbackfunc once enough time has accumulated
|
||||||
|
void Timer::updatetime(double delta_time) {
|
||||||
|
accumulator += delta_time;
|
||||||
|
if (!is_expired) {
|
||||||
|
if (accumulator >= time_interval) {
|
||||||
|
action->update(*this);
|
||||||
|
accumulator -= time_interval;
|
||||||
|
trigger_counter++;
|
||||||
|
if (!is_infinite && trigger_counter >= trigger_amount) {
|
||||||
|
is_expired = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerManager::addtimer(std::unique_ptr<Timer> input_timer) {
|
||||||
|
timers.push_back(std::move(input_timer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerManager::updatetimers() {
|
||||||
|
//get dtime
|
||||||
|
auto current_time = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::duration<double> myDeltaTime = current_time - prev_time;
|
||||||
|
double dtime = myDeltaTime.count();
|
||||||
|
|
||||||
|
//update timers with dtime
|
||||||
|
for (auto &mytimer : timers) {
|
||||||
|
mytimer->updatetime(dtime);
|
||||||
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||||
|
}
|
||||||
|
//reset for next tick
|
||||||
|
prev_time = current_time;
|
||||||
|
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
||||||
|
[](auto &timer) { return timer->is_expired; }),
|
||||||
|
timers.end());
|
||||||
|
// implement delete timers
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
TimerManager::TimerManager(): prev_time(std::chrono::steady_clock::now()){}
|
||||||
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#ifndef TIMER_HPP
|
||||||
|
#define TIMER_HPP
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Timer;
|
||||||
|
|
||||||
|
class TimerAction {
|
||||||
|
public:
|
||||||
|
virtual ~TimerAction() = default;
|
||||||
|
virtual void update(const Timer &t) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Timer {
|
||||||
|
private:
|
||||||
|
double accumulator = 0;
|
||||||
|
int trigger_amount = 0;
|
||||||
|
double time_interval = 1; // only display every 1 seconds
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
int trigger_counter = 0;
|
||||||
|
bool is_infinite = false;
|
||||||
|
std::unique_ptr<TimerAction> action;
|
||||||
|
bool is_expired = false;
|
||||||
|
|
||||||
|
|
||||||
|
//infinite timer
|
||||||
|
Timer(std::string name_input, double interval_input,
|
||||||
|
std::unique_ptr<TimerAction> action_input
|
||||||
|
);
|
||||||
|
|
||||||
|
//limited timer
|
||||||
|
Timer(std::string name, double interval_input, int trigger_amount,
|
||||||
|
std::unique_ptr<TimerAction> action
|
||||||
|
);
|
||||||
|
// callbackfunc once enough time has accumulated
|
||||||
|
void updatetime(double delta_time);
|
||||||
|
};
|
||||||
|
|
||||||
|
// manage collection of timers
|
||||||
|
class TimerManager {
|
||||||
|
std::vector<std::unique_ptr<Timer>> timers;
|
||||||
|
std::chrono::steady_clock::time_point prev_time; // initial starttime
|
||||||
|
|
||||||
|
public:
|
||||||
|
TimerManager();
|
||||||
|
void addtimer(std::unique_ptr<Timer> input_timer);
|
||||||
|
void updatetimers();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
externalize state
|
||||||
|
*/
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include <vector>
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include "timeractions.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
SwapAction::SwapAction(std::vector<int> &input_vector): input_vector(input_vector){}
|
||||||
|
|
||||||
|
void SwapAction::update(const Timer &input_timer){
|
||||||
|
if (!is_complete) {
|
||||||
|
int temp = input_vector[it_i];
|
||||||
|
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
||||||
|
input_vector[input_vector.size() - 1 - it_i] = temp;
|
||||||
|
it_i++;
|
||||||
|
if(it_i>=input_vector.size() / 2){is_complete=true;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SortAction::SortAction(std::vector<int> &input_vector): input_vector(input_vector) {}
|
||||||
|
|
||||||
|
//sorts the included array using bsort and external iterators rather than for loops
|
||||||
|
void SortAction::update(const Timer &input_timer) {
|
||||||
|
if (!is_complete) {
|
||||||
|
/**
|
||||||
|
we take a number, it_i loop
|
||||||
|
compare it with its neighbour, it_j loop
|
||||||
|
if it's larger swap it
|
||||||
|
keep going until it_i == length - sorted numbers
|
||||||
|
once that is true, reset it_j and advance it_i
|
||||||
|
**/
|
||||||
|
if (input_vector[it_j]>input_vector[it_j+1]) {
|
||||||
|
// int temp = input_vector[it_j + 1];
|
||||||
|
// input_vector[it_j + 1] = input_vector[it_j];
|
||||||
|
// input_vector[it_j] = temp;
|
||||||
|
std::swap(input_vector[it_j], input_vector[it_j+1]);
|
||||||
|
}
|
||||||
|
it_j++;
|
||||||
|
|
||||||
|
if (it_j >= input_vector.size() - 1 - it_i) {
|
||||||
|
it_j = 0;
|
||||||
|
it_i++;
|
||||||
|
}
|
||||||
|
if (it_i>=input_vector.size()) {
|
||||||
|
is_complete=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef TIMERACTIONS_HPP
|
||||||
|
#define TIMERACTIONS_HPP
|
||||||
|
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class SwapAction : public TimerAction {
|
||||||
|
public:
|
||||||
|
size_t it_i = 0;
|
||||||
|
bool is_complete = false;
|
||||||
|
std::vector<int> &input_vector;
|
||||||
|
SwapAction(std::vector<int> &input_vector);
|
||||||
|
void update(const Timer &t);
|
||||||
|
};
|
||||||
|
|
||||||
|
class SortAction : public TimerAction {
|
||||||
|
public:
|
||||||
|
size_t it_i = 0;
|
||||||
|
size_t it_j = 0;
|
||||||
|
bool is_complete = false;
|
||||||
|
std::vector<int> &input_vector;
|
||||||
|
SortAction(std::vector<int> &input_vector);
|
||||||
|
void update(const Timer &t);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#include "stdio.h"
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int i = 5;
|
|
||||||
int j = 7;
|
|
||||||
int x;
|
|
||||||
|
|
||||||
x = (i>j)-(j>i);
|
|
||||||
/*
|
|
||||||
* -1 if i less than j
|
|
||||||
* 0 if i equal to j
|
|
||||||
* +1 if i more than j
|
|
||||||
* */
|
|
||||||
|
|
||||||
printf("%d: ", x);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
#include <stdbool.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void numberprinter(int inarr[], int arrlength) {
|
|
||||||
for (int i = 0; i < arrlength; i++) {
|
|
||||||
printf("number%d: %d\n", i, inarr[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Darray {
|
|
||||||
int *digits;
|
|
||||||
size_t length; // blijkbaar correcte eenheid voor length, malloc stuff ofzo (instead of hardcoding it)
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Darray *numbertodigitDarr(int input) {
|
|
||||||
|
|
||||||
// same calculation for arrsize
|
|
||||||
int op = 0;
|
|
||||||
int tempinput = input;
|
|
||||||
while (tempinput) {
|
|
||||||
tempinput /= 10;
|
|
||||||
op++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// allocate memory for struct and array, apparently more ideomatic to have a single malloc
|
|
||||||
struct Darray *DigDarr =
|
|
||||||
malloc(op * sizeof(int) + sizeof(*DigDarr) + sizeof(int));
|
|
||||||
if (DigDarr == NULL) {
|
|
||||||
printf("allocation failed");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
// forgot to assign a value to the pointer variable in the struct.
|
|
||||||
DigDarr->digits = (int *)(DigDarr + 1);
|
|
||||||
DigDarr->length = op;
|
|
||||||
|
|
||||||
// insert numbers into array in reverse order
|
|
||||||
for (int i = op - 1; i >= 0; i--) {
|
|
||||||
int last_digit = input % 10;
|
|
||||||
printf("last digit: %d\n", last_digit);
|
|
||||||
DigDarr->digits[i] = last_digit;
|
|
||||||
input /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
return DigDarr;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
void checkarrdupes(struct Darray input) {
|
|
||||||
for (int i = 0; i < input.length; i++) {
|
|
||||||
printf("digitstr: %d\n", input.digits[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int digit_seen[10] = {};
|
|
||||||
|
|
||||||
// kijk door elke arr element
|
|
||||||
for (int i = 0; i < input.length; i++) {
|
|
||||||
// kijk of input[i] al in digit_seen zit
|
|
||||||
if (digit_seen[input.digits[i]]) {
|
|
||||||
printf("duplicate number: %d\n", input.digits[i]);
|
|
||||||
} else {
|
|
||||||
digit_seen[input.digits[i]] = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
|
|
||||||
int n;
|
|
||||||
|
|
||||||
printf("enter a number: ");
|
|
||||||
// TODO: scanf is fragile. Try using fgets() and strtol() for robust input
|
|
||||||
// sanitization.
|
|
||||||
scanf("%d", &n);
|
|
||||||
int digitslength = 0;
|
|
||||||
struct Darray *mydigitsDarr = numbertodigitDarr(n);
|
|
||||||
|
|
||||||
checkarrdupes(*mydigitsDarr);
|
|
||||||
|
|
||||||
free(mydigitsDarr);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
somehow I have to find a way to get the number of digits in my number
|
|
||||||
for now, we use modulo. whatever remains is the last digit of the number
|
|
||||||
we then divide by 10 to remove the last number
|
|
||||||
|
|
||||||
we can keep a counter, and we can put it into an array. however, if we use the
|
|
||||||
iterator (which has to start from 0, because we don't know the size of the
|
|
||||||
number?) then our number will be the wrong way around.
|
|
||||||
|
|
||||||
malloc, blijkbaar very different
|
|
||||||
*/
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#include "stdlib.h"
|
|
||||||
#include "stdio.h"
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
float height, length, width, volume, weight;
|
|
||||||
printf("Enter height of box: ");
|
|
||||||
scanf("%f", &height);
|
|
||||||
printf("Enter length of box: ");
|
|
||||||
scanf("%f", &length);
|
|
||||||
printf("Enter width of box: ");
|
|
||||||
scanf("%f", &width);
|
|
||||||
|
|
||||||
volume = height * length * width;
|
|
||||||
weight = (volume + 165) / 166;
|
|
||||||
|
|
||||||
printf("Dimensions: %fx%fx%f\n", length, width, height);
|
|
||||||
printf("Volume: %f\n", volume);
|
|
||||||
printf("Weight: %f\n", weight);
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#include "stdio.h"
|
|
||||||
#include "stdlib.h"
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
int input;
|
|
||||||
printf("input:");
|
|
||||||
scanf("%d", &input);
|
|
||||||
|
|
||||||
// assume 3 digits,
|
|
||||||
|
|
||||||
int d1, d2, d3;
|
|
||||||
d1 = input / 100; //uitgaande van flooring
|
|
||||||
d2 = input % 100 / 10;
|
|
||||||
d3 = input % 10;
|
|
||||||
|
|
||||||
printf("%d%d%d",d3, d2,d1);
|
|
||||||
|
|
||||||
printf("next: ");
|
|
||||||
scanf("%1d%1d%1d", &d1,&d2,&d3);
|
|
||||||
printf("%d%d%d",d3,d2,d1);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#include "stdio.h"
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("To C, or not to C: that is the question.\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "stdio.h"
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
|
|
||||||
/*
|
|
||||||
* modify 24 hour time to 12 hour
|
|
||||||
* take first 2 numbers,
|
|
||||||
* if numbers <=12, AM
|
|
||||||
* else PM
|
|
||||||
* */
|
|
||||||
|
|
||||||
int i ; int j; char c;
|
|
||||||
printf("input time: ");
|
|
||||||
scanf("%2d%1c%2d", &i,&c, &j);
|
|
||||||
printf("int i: %d int j: %d\n", i, j);
|
|
||||||
|
|
||||||
if(i!=12&&j==00){
|
|
||||||
i%=12;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if(i <=12){
|
|
||||||
printf("%d:%.2d AM",i,j);
|
|
||||||
} else {
|
|
||||||
printf("%d:%.2d PM",i,j);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Binary file not shown.
@@ -1,74 +0,0 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
struct IntArrayStr{
|
|
||||||
int *data;
|
|
||||||
size_t length;
|
|
||||||
size_t capacity;
|
|
||||||
};
|
|
||||||
|
|
||||||
void read_int_array_str(struct IntArrayStr *in_str) {
|
|
||||||
printf("inputlength:%zu\n", in_str->length);
|
|
||||||
for (size_t i = 0; i < in_str->length; i++) {
|
|
||||||
printf("digit:%zu = %d\n", i, in_str->data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct IntArrayStr *create_int_array_str(size_t in_capacity) {
|
|
||||||
struct IntArrayStr *mystr = malloc(sizeof(struct IntArrayStr)); /* allocate memory for struct */
|
|
||||||
|
|
||||||
mystr->data = malloc(sizeof(int) * in_capacity); /* make sure the pointer in the struct points to the array */
|
|
||||||
mystr->length = 0; //forgot to actually assign a value to the struct
|
|
||||||
mystr->capacity = in_capacity;
|
|
||||||
|
|
||||||
if (mystr->data == NULL) {
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return mystr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void push_int_array_str(int in_int, struct IntArrayStr *in_str){
|
|
||||||
if (in_str->length == in_str->capacity) {
|
|
||||||
in_str->capacity*=2;
|
|
||||||
printf("intarray capacity is now:%zu\n", in_str->capacity);
|
|
||||||
int *temp;
|
|
||||||
temp = realloc(in_str->data, sizeof(int)*in_str->capacity);
|
|
||||||
if(temp == NULL){exit(0);};
|
|
||||||
in_str->data = temp;
|
|
||||||
printf("intarray length is now:%zu\n", in_str->length);
|
|
||||||
}
|
|
||||||
in_str->data[in_str->length] = in_int;
|
|
||||||
printf("number:%zu = %d\n", in_str->length, in_str->data[in_str->length]);
|
|
||||||
in_str->length++; //didn't read the flow when transfering and accidentally put this in the if block
|
|
||||||
//also had an off-by-one error because I increased length before doing anything
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
srand(time(NULL));
|
|
||||||
// change to while loop and take inputs rather than fill with random numbers
|
|
||||||
printf("input numbers:\n");
|
|
||||||
|
|
||||||
//int input;
|
|
||||||
size_t length = 10;
|
|
||||||
/* while (input != -1) { */
|
|
||||||
/* scanf("%d", &input); */
|
|
||||||
/* length++; */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
struct IntArrayStr *myintarraystr = create_int_array_str(2);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < length; i++) {
|
|
||||||
int input = rand() % 10;
|
|
||||||
push_int_array_str(input, myintarraystr);
|
|
||||||
}
|
|
||||||
|
|
||||||
read_int_array_str(myintarraystr);
|
|
||||||
|
|
||||||
free(myintarraystr->data);
|
|
||||||
free(myintarraystr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
int i, j;
|
|
||||||
/* TODO: define the 2D pointer variable here */
|
|
||||||
int ** pnumbers;
|
|
||||||
|
|
||||||
/* TODO: complete the following line to allocate memory for holding three rows */
|
|
||||||
pnumbers = (int **) malloc(sizeof(int)*6);
|
|
||||||
|
|
||||||
/* TODO: allocate memory for storing the individual elements in a row */
|
|
||||||
pnumbers[0] = (int *) malloc(1 * sizeof(int));
|
|
||||||
pnumbers[1] = (int *) malloc(2 * sizeof(int));
|
|
||||||
pnumbers[2] = (int *) malloc(3 * sizeof(int));
|
|
||||||
|
|
||||||
pnumbers[0][0] = 1;
|
|
||||||
pnumbers[1][0] = 1;
|
|
||||||
pnumbers[1][1] = 1;
|
|
||||||
pnumbers[2][0] = 1;
|
|
||||||
pnumbers[2][1] = 2;
|
|
||||||
pnumbers[2][2] = 1;
|
|
||||||
|
|
||||||
for (i = 0; i < 3; i++) {
|
|
||||||
for (j = 0; j <= i; j++) {
|
|
||||||
printf("%d", pnumbers[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < 3; i++) {
|
|
||||||
/* TODO: free memory allocated for each row */
|
|
||||||
free(pnumbers[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO: free the top-level pointer */
|
|
||||||
free(pnumbers);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
struct Darray {
|
|
||||||
int size;
|
|
||||||
int data[]; //flexible array member
|
|
||||||
};
|
|
||||||
|
|
||||||
//randomized array
|
|
||||||
void rarr(struct Darray *darr) {
|
|
||||||
for (int i=0; i<darr->size; ++i) {
|
|
||||||
darr->data[i] = rand() % 32;
|
|
||||||
}
|
|
||||||
if (darr == NULL) {
|
|
||||||
printf("failed");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Darray *createdarray(int mysize){
|
|
||||||
struct Darray *darrstruct = malloc(mysize * sizeof(int) + sizeof(*darrstruct));
|
|
||||||
|
|
||||||
if (darrstruct == NULL) {
|
|
||||||
printf("failed");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
darrstruct->size = mysize;
|
|
||||||
rarr(darrstruct);
|
|
||||||
|
|
||||||
return darrstruct;
|
|
||||||
}
|
|
||||||
|
|
||||||
void msort(int *myarr, int mysize) {
|
|
||||||
for (int i = 0 ; i < mysize; i++) {
|
|
||||||
for (int j = 0 ; j < mysize ; j ++)
|
|
||||||
if (myarr[i] > myarr[j]) {
|
|
||||||
int k = myarr[i];
|
|
||||||
myarr[i] = myarr[j];
|
|
||||||
myarr[j] = k;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void bsort(struct Darray *myarr) {
|
|
||||||
for (int i = 0; i < myarr->size; i++) {
|
|
||||||
for (int j = 0; j < myarr->size -1 ; j++ ) {
|
|
||||||
if (myarr->data[j] < myarr->data[j+1]) {
|
|
||||||
int k = myarr->data[j];
|
|
||||||
myarr->data[j] = myarr->data[j+1];
|
|
||||||
myarr->data[j+1] = k;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
srand(time(NULL));
|
|
||||||
int mysize = 7;
|
|
||||||
struct Darray *myarray;
|
|
||||||
myarray = createdarray(mysize);
|
|
||||||
|
|
||||||
printf("myarr:\n");
|
|
||||||
for (int i = 0 ; i < myarray->size; i++) {
|
|
||||||
printf("%d\n", myarray->data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
bsort(myarray);
|
|
||||||
|
|
||||||
printf("sorted myarr:\n");
|
|
||||||
for (int i = 0 ; i < myarray->size; i++) {
|
|
||||||
printf("%d\n", myarray->data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(myarray);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
struct node {
|
|
||||||
int val;
|
|
||||||
struct node * next;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct node_t * head = NULL;
|
|
||||||
head = (struct node_T*)malloc(sizeof(struct node_t));
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int *pascallarr(int iarrsize, int *ptr){
|
|
||||||
int *mypascallar = malloc(sizeof(int) * iarrsize + 1);
|
|
||||||
/* declare edges, as they're always 1 anyways so we can start the loop at 1 (so i-1 = 0) */
|
|
||||||
mypascallar[0] = 1;
|
|
||||||
mypascallar[iarrsize] = 1;
|
|
||||||
for (int i = 1; i <= iarrsize; i++) {
|
|
||||||
if (i < iarrsize) {
|
|
||||||
mypascallar[i] = ptr[i-1] + ptr[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mypascallar;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
int trianglesize = 18;
|
|
||||||
int ** pnumbers;
|
|
||||||
|
|
||||||
/* create pascals triangle, on row i, we have i numbers */
|
|
||||||
|
|
||||||
/* should create 2d array of ints, with an array of pointers to arrays*/
|
|
||||||
|
|
||||||
int **ptrtriangle = malloc(sizeof(int *)*trianglesize);
|
|
||||||
|
|
||||||
/* need to create a function that returns the pointer to an array of i numbers filled with numbers */
|
|
||||||
for (int i = 0; i < trianglesize; i++) {
|
|
||||||
ptrtriangle[i] = pascallarr(i,ptrtriangle[i-1]);
|
|
||||||
for (int j = 0; j <= i; j++) {
|
|
||||||
printf("%d|",ptrtriangle[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int recfact(int x){
|
|
||||||
int y = 1;
|
|
||||||
if (x==1){
|
|
||||||
return y;
|
|
||||||
} else {
|
|
||||||
return y *= x *recfact(x-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int recfor(int x){
|
|
||||||
int gaga = 1;
|
|
||||||
for (int i = 1; i < x;) {
|
|
||||||
gaga *= i++;
|
|
||||||
}
|
|
||||||
return gaga;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* i = 1, gaga = 1*2
|
|
||||||
* i = 2, gaga = 1*2*3 = 6
|
|
||||||
* i = 3, 1*2*3*4 = 24
|
|
||||||
*
|
|
||||||
* wrong loop time condition
|
|
||||||
* wrong calculation
|
|
||||||
*
|
|
||||||
* ---
|
|
||||||
*
|
|
||||||
* recursion
|
|
||||||
*
|
|
||||||
* forgot to assign, forgot a number (multiplied by 0)
|
|
||||||
* misunderstood calculation
|
|
||||||
|
|
||||||
wel exploren, maar wel naar een doel.
|
|
||||||
kijken naar auxililiaries.
|
|
||||||
curiosity wss, leren vragen. leren wat ik niet heb, weten wat ik niet weet.
|
|
||||||
|
|
||||||
zoektocht heeft wat sturing nodig.
|
|
||||||
|
|
||||||
hoe ik mijn probeer stel/vind
|
|
||||||
het probleem onderzoeken
|
|
||||||
|
|
||||||
limiteer de pogingen
|
|
||||||
|
|
||||||
|
|
||||||
hardware / networking
|
|
||||||
* * */
|
|
||||||
|
|
||||||
int main () {
|
|
||||||
int i = 6; // we can not handle negative numbers
|
|
||||||
|
|
||||||
printf("%d\n", recfor(i));
|
|
||||||
printf("%d\n", recfact(i));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* factorials
|
|
||||||
*
|
|
||||||
* in both for loop, as well as recursive
|
|
||||||
* factorial: voor int i, vermenigvuldig dan elke i to je bij 1 komt (uit gaande van positive ints)
|
|
||||||
*
|
|
||||||
* dus, f(3)=3*2*1 = 6 ; f(4)=4*3*2*1=24 ; f(a)=no ; f(-2)= no ; f(2.1) = no
|
|
||||||
*
|
|
||||||
* will use 2 functions:
|
|
||||||
* recfac using recursion
|
|
||||||
* itfac using iteration
|
|
||||||
*
|
|
||||||
* should probably figure out how to test this? maybe a dumb K:V?
|
|
||||||
* * */
|
|
||||||
|
|
||||||
int recfact(int input){
|
|
||||||
int fac = 1;
|
|
||||||
if (input==1) {
|
|
||||||
return fac;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
fac *= input * recfact(input-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int itfact(int input){
|
|
||||||
int sum = 1;
|
|
||||||
for (int i = 1; i <= input; i++) {
|
|
||||||
sum *= i;
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
int input = 5;
|
|
||||||
|
|
||||||
//recfact(input);
|
|
||||||
//itfact(input);
|
|
||||||
printf("%d\n", recfact(input));
|
|
||||||
printf("%d\n", itfact(input));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user