diff --git a/7-teiler.c b/7-teiler.c new file mode 100644 index 0000000..f869aac --- /dev/null +++ b/7-teiler.c @@ -0,0 +1,29 @@ +/* + * lower and upper end as user input + * use a for loop to output all numbers for which 7|x is true. + */ + +#include + +int main() { + + int a,b; + printf("input lower end:\n"); + fflush(stdin); + scanf("%d",&a); + printf("input upper end:\n"); + fflush(stdin); + scanf("%d",&b); + + printf("inputs: %d %d\n", a, b); + for(int i=a; i<=b; i++){ + if(i%7==0) + printf("%d ", i); + if(i==b) + printf("\n"); + } + + // reverse array order + + return 0; +} diff --git a/7teiler.c b/7teiler.c new file mode 100644 index 0000000..bca2ea4 --- /dev/null +++ b/7teiler.c @@ -0,0 +1,27 @@ +/* + * lower and upper end as user input + * use a for loop to output all numbers for which 7|x is true. + */ + +#include + +int main() { + + int a,b; + printf("input lower end:\n"); + fflush(stdin); + scanf("%d",&a); + printf("input upper end:\n"); + fflush(stdin); + scanf("%d",&b); + + printf("inputs: %d %d\n", a, b); + for(int i=a; i<=b; i++){ + if(i%7==0) + printf("%d ", i); + if(i==b) + printf("\n"); + } + + return 0; +} diff --git a/abc.c b/abc.c new file mode 100644 index 0000000..21e5506 --- /dev/null +++ b/abc.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +int main(int argc, char **argv){ + + if(argc<3){ + printf("not enough args! Ex: ./abc 12 -3 5\n"); + exit(EXIT_FAILURE); + } + double a = atoi(argv[1]); + double b = atoi(argv[2]); + double c = atoi(argv[3]); + + printf("(%f)x²+(%f)x+(%f)=0\n", a, b, c); + double l1 = 0, l2 = 0; + double cl1 = 0, cl2 = 0; + l1=(-b + sqrt((b*b)-(4*a*c)))/(2*a); + l2=(-b - sqrt((b*b)-(4*a*c)))/(2*a); + cl1=(-b + csqrt((b*b)-(4*a*c)))/(2*a); + cl2=(-b - csqrt((b*b)-(4*a*c)))/(2*a); + printf("(-%.0f (+-) sqrt[(%.0f²)-(4*%.0f*%.0f)])/2*%.0f\n", b, b, a, c, a); + printf("x1=%f x2=%f\n", l1, l2); + printf("(complex mode)\nx1=%fi\tx2=%fi\n", cl1, cl2); + return 0; +} diff --git a/adder.c b/adder.c new file mode 100644 index 0000000..f0d22b1 --- /dev/null +++ b/adder.c @@ -0,0 +1,9 @@ +#include + +int main() { + int a, b; + printf("Please input two integers to be added.\n"); + scanf("%d %d", &a, &b); + printf("%d + %d = %d\n", a, b, a+b); + return 0; +} diff --git a/args.c b/args.c new file mode 100644 index 0000000..853554c --- /dev/null +++ b/args.c @@ -0,0 +1,15 @@ +#include + +int main(int argc, char* argv[]){ + + if(argc==1){ + printf("Recieved only one argument, idiot"); + return 1; + } + + for(int i = 0; i < argc+1; i++){ + printf("%d. [%s]\n", i+1, argv[i]); + } + + return 0; +} diff --git a/arrToBin.c b/arrToBin.c new file mode 100644 index 0000000..95480d0 --- /dev/null +++ b/arrToBin.c @@ -0,0 +1,29 @@ +#include +#include + +int main(){ + // input as single int, output as array of ints with only 0 and 1s + + char str_i[20]; + int i; + + fgets(str_i, 20, stdin); + i = strtol(str_i, NULL, 0); + printf("%d", i); + + int bin[16] = { -1 }; + int b; + + printf("[CONTROL]as hex %x\n", i); + for(int j = 16; j>0; j--){ + b = i%2; + i /= 2; + bin[i] = b; + } + for(int j = 0; j < 16; j++){ + if( + printf("%d", bin[j]); + } + printf("\n"); + return 0; +} diff --git a/ascii.c b/ascii.c new file mode 100644 index 0000000..d0605ef --- /dev/null +++ b/ascii.c @@ -0,0 +1,8 @@ +#include + +int main(){ + for(int i=0; i<256; i++){ + printf("(dec)%d\t(hex)0x%02x\t(char)%c\t\n",i,i,i); + } + return 0; +} diff --git a/calculator.c b/calculator.c new file mode 100644 index 0000000..535710b --- /dev/null +++ b/calculator.c @@ -0,0 +1,49 @@ +#include +#include +#include + +double factorial(double i) { + if(i <= 1) { + return 1; + } + return i * factorial(i - 1); +} + +double power(double a, double b) { + double result = a; + for(int i=0;i + +int* ref(int *i, int* j, int* result){ + *result = *i + *j; + return result; +} + +int val(int a, int b){ + return a+b; +} + +int main(){ + + int x = 1000; + int y = 337; + int z; + printf("given: %d\n", x); + printf("reference: %d\n", *ref(&x, &y, &z)); + printf("value: %d\n", val(x, y)); + + return 0; +} diff --git a/complex.c b/complex.c new file mode 100644 index 0000000..dcd2a52 --- /dev/null +++ b/complex.c @@ -0,0 +1,83 @@ +#include + +typedef struct +{ + float real; + float imaginary; +}complex; + +complex assignComplex(){ + float r, i; + scanf("%f", &r); + scanf("%f", &i); + complex c; + c.real = r; + c.imaginary = i; + return c; +} + +void addComplex() { + printf("First complex\n"); + complex a = assignComplex(); + printf("Second complex\n"); + complex b = assignComplex(); + + complex r; + r.real = a.real + b.real; + r.imaginary = a.imaginary + b.imaginary; + printf("Result: %f+%fi\n", r.real, r.imaginary); +} + +void subComplex() { + printf("First complex\n"); + complex a = assignComplex(); + printf("Second complex\n"); + complex b = assignComplex(); + + complex r; + r.real = a.real - b.real; + r.imaginary = a.imaginary - b.imaginary; + printf("Result: %f+%fi\n", r.real, r.imaginary); +} + +void mulComplex() { + printf("First complex\n"); + complex a = assignComplex(); + printf("Second complex\n"); + complex b = assignComplex(); + + complex r; + r.real = a.real * b.real; + r.imaginary = a.imaginary * b.imaginary; + printf("Result: %f+%fi\n", r.real, r.imaginary); +} + +void divComplex() { + printf("First complex\n"); + complex a = assignComplex(); + printf("Second complex\n"); + complex b = assignComplex(); + + complex r; + r.real = a.real / b.real; + r.imaginary = a.imaginary / b.imaginary; + printf("Result: %f+%fi\n", r.real, r.imaginary); +} + +int main() { + printf("Usage:\n\toperations: + - * /\n\tquit: q\n"); + + char in; + while(1){ + + in = getchar(); + switch(in){ + case 'q': return 0; + case '+': addComplex(); break; + case '-': subComplex(); break; + case '*': mulComplex(); break; + case '/': divComplex(); break; + default: printf("kaputt\n"); break; + } + } +} diff --git a/echo.c b/echo.c new file mode 100644 index 0000000..8278ea0 --- /dev/null +++ b/echo.c @@ -0,0 +1,8 @@ +#include + +int main() { + char s[100]; + gets(s); + printf("%s\n", s); + return 0; +} diff --git a/echochar.c b/echochar.c new file mode 100644 index 0000000..a2020a9 --- /dev/null +++ b/echochar.c @@ -0,0 +1,8 @@ +#include + +int main() { + + char a = getchar(); + printf("%c", a); + return 0; +} diff --git a/euler.c b/euler.c new file mode 100644 index 0000000..30272da --- /dev/null +++ b/euler.c @@ -0,0 +1,50 @@ +#include + +#include +#include + +// range of unsigned long long is: +// 18.446.744.073.709.551.615 +// maximum factorial with this is 65! +// Double goes up to 170! +double factorial(double i) { + + if(i <= 1) { + return 1; + } + return i * factorial(i - 1); +} + + double factorialFor(double given){ + double p = 1; + + // someone said i need 2 for loops. Totally wrong? + for(int j=1;j +#include + +// range of unsigned long long is: +// 18.446.744.073.709.551.615 +// maximum factorial with this is 65! +// Double goes up to 170! +double factorial(double i) { + + if(i <= 1) { + return 1; + } + return i * factorial(i - 1); +} + + double factorialFor(double given){ + double p = 1; + + // someone said i need 2 for loops. Totally wrong? + for(int j=1;j +#include + +int main () { + FILE *fp; + char c[] = "this is tutorialspoint"; + char buffer[100]; + + /* Open file for both reading and writing */ + fp = fopen("file.txt", "w+"); + + /* Write data to the file */ + fwrite(c, strlen(c) + 1, 1, fp); + + /* Seek to the beginning of the file */ + fseek(fp, 0, SEEK_SET); + + /* Read and display data */ + fread(buffer, strlen(c)+1, 1, fp); + printf("%s\n", buffer); + fclose(fp); + + return(0); +} diff --git a/hello-world.c b/hello-world.c new file mode 100644 index 0000000..04ce434 --- /dev/null +++ b/hello-world.c @@ -0,0 +1,6 @@ +#include + +int main() { + printf("Hello world!\n"); + return 0; +} diff --git a/huffman/huffman.c b/huffman/huffman.c new file mode 100644 index 0000000..286531e --- /dev/null +++ b/huffman/huffman.c @@ -0,0 +1,247 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +off_t fsize(const char *filename) { + struct stat st; + + if (stat(filename, &st) == 0) + return st.st_size; + + fprintf(stderr, "Cannot determine size of %s: %s\n", filename, + strerror(errno)); + + return -1; +} + +void helper() { + printf("huffman compression algorithm implementation for educational " + "purposes.\n\nSyntax:\nhuffman -f fileToCompress\t\tcompress the " + "given file\nhuffman -xf fileToDecompress\t\tdecompress the given " + "file\nhuffman -h\t\t\t\tshow this help\nhuffman -v\t\t\t\tverbose\n"); +} + +int main(int argc, char *argv[]) { + int opt; + bool extract_mode = false; + bool verbose = false; + bool debug = false; + char *filestring = NULL; + uint64_t filelen; + + FILE *fptrR = NULL; // file pointer for reading + FILE *fptrW = NULL; // file pointer for writing + + while ((opt = getopt(argc, argv, "dvxhf:")) != -1) { + if (debug) + printf("optarg is: %s\n", optarg); + switch (opt) { + case 'v': + verbose = true; + break; + case 'd': + debug = true; + break; + case 'f': + filestring = optarg; + break; + case 'h': + helper(); + exit(0); + break; + case 'x': + extract_mode = true; + break; + default: + fprintf(stderr, "Usage: %s [-dvhx -f] [file]\n", argv[0]); + exit(EXIT_FAILURE); + } + } + + // Now optind (declared extern int by ) is the index of the first + // non-option argument. If it is >= argc, there were no non-option arguments. + + if (verbose) + printf("selected file: %s\n", filestring); + + if (filestring) { + if(debug) + printf("[DEBUG]processing given file argument.\n"); + // open the given file in binary mode, I want this to work with any files, + // not just textfiles. + fptrR = fopen(filestring, "rb"); + if (fptrR == NULL) { + fprintf(stderr, "The given file does not exist or is unavailable.\n"); + exit(EXIT_FAILURE); + } + // causes bugs. + fseek(fptrR, 0L, SEEK_END); + filelen = ftell(fptrR); + fseek(fptrR, 0L, SEEK_SET); + } + else { + // empty filestring or filestring is NULL + fprintf(stderr, "Usage: %s [-dvhx -f] [file]\n", argv[0]); + exit(EXIT_FAILURE); + } + + // TODO check file size and spit a "what the heck im not a 10x dev, do a + if(verbose) + printf("filesize: %ldB\n", filelen); + + if (extract_mode) { + printf("extracting is not yet implemented.\n"); + // decompress the file + } + + else { + // compress the file + if (verbose) + printf("compressing file...\n"); + + // frequency analysis + + uint8_t buf[512]; + + // dump start of file if debugging + // FIXME add conditions if the part to print is smaller than 512B + if(debug){ + printf("[DEBUG]First 512 bytes are:\n"); + fread(buf, 1, 512, fptrR); + for(int i=0;i<512;i++){ + if(i%16==0) + printf("%08x\t", i); + printf("%02x ", buf[i]); + if(i%16==7) + printf(" "); + if(i%16==15){ + printf("\n"); + } + } + } + + uint64_t occurences[256]; + for(int i=0;i<256;i++){ + occurences[i]=0; + } + // FIXME doesnt loop through full file! only 50% for larger files + // Backup occurence counting algorithm + /* + while(!feof(fptrR)){ + fseek(fptrR, 512, SEEK_CUR); + if(fread(buf, 1, 512, fptrR)){ + for(int i=0;i<512;i++){ + occurences[buf[i]]++; + } + } + else{ + fprintf(stderr, "Error when processing file.\n"); + exit(EXIT_FAILURE); + } + + // advance filepointer 512 bytes foreward. If not possible, set endOfFile flag. + // FIXME + offset += 512; + } + */ + // backup + /* + while(1){ + fseek(fptrR, 512, SEEK_CUR); // this line seems the be making the most problems + + // On success, fread() and fwri)te() return the number of items read or written. + // This number equals the number of bytes transferred only when size is + // 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero). + // fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. + + // FIXME )This is a buggy mess + if(512 == fread(buf, 1, 512, fptrR)){ + for(int i=0;i<512;i++){ + occurences[buf[i]]++;) + } + }) + else if(0 == fread(buf, 1, 512, fptrR)){ + if(debug) + printf("[DEBUG]fread returned 0! ftell for current position is %lu\n", ftell(fptrR)); + break; + } + else{ + fprintf(stderr, "Error when processing file: %lu, At offset %lu\n",fread(buf, 1, 512, fptrR), ftell(fptrR)); + exit(EXIT_FAILURE); + } + if(ftell(fptrR) > filelen) { + // ??? unknown error + fprintf(stderr, "tried reading further than the file is long somehow?\n"); + exit(EXIT_FAILURE); + } + } + */ + // ALMOST WORKS! ~200 bytes lost for a 10M file!!! + uint8_t bufMini[1]; + while(1){ + if(fread(bufMini, 1, 1, fptrR)){ + occurences[buf[0]]++; + } + else{ + if(ferror(fptrR)){ + fprintf(stderr, "encountered error when reading file.\n"); + exit(EXIT_FAILURE); + } + } + fseek(fptrR, 1, SEEK_SET); + if(ferror(fptrR)){ + fprintf(stderr, "encountered error when reading file.\n"); + exit(EXIT_FAILURE); + } + else if(feof(fptrR)){ + continue; + } + } + if(debug){ + printf("Occurences (Hex):\n"); + for(int i=0;i<256;i++){ + if(i%4==0) + printf("\n"); + printf("0x%02x: %016lx\t", i, occurences[i]); + } + printf("\n\nfile length(by pointer):\t\t%luB\n", filelen); + long long int addedUpOccurences = 0; // FIXME might not be enough storage for larger files! + for(int i=0;i<256;i++){ + addedUpOccurences += occurences[i]; + } + printf("file length(added up occurences):\t%lldB\n", addedUpOccurences); + } + + if(verbose) + printf("\n\nDone calculating occurences of bytes.\n"); + + // TODO + // calculate the frequencies of the bytes. + double frequencies[256]; + for(int i=0;i<256;i++){ + frequencies[i]=((double)occurences[i]/(double)filelen)*100; // calculate frequencies of bytes in percent (example: 05.23 (%)) + } + if(debug){ + printf("Frequencies:\n"); + for(int i=0;i<256;i++){ + if(i%8==0) + printf("\n"); + printf("0x%02x: %05.02f%%\t", i, frequencies[i]); + } + double addedUpFrequencies = 0; + for(int i=0;i<256;i++){ + addedUpFrequencies += frequencies[i]; + } + printf("\n\nadded up frequencies: %05.02f%%\n",addedUpFrequencies); + } + } + fclose(fptrR); + printf("\n"); + exit(EXIT_SUCCESS); +} diff --git a/options.c b/options.c new file mode 100644 index 0000000..821e50a --- /dev/null +++ b/options.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + bool isCaseInsensitive = false; + int opt; + enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode = CHARACTER_MODE; + + while ((opt = getopt(argc, argv, "ilw")) != -1) { + switch (opt) { + case 'i': isCaseInsensitive = true; break; + case 'l': mode = LINE_MODE; break; + case 'w': mode = WORD_MODE; break; + default: + fprintf(stderr, "Usage: %s [-ilw] [file...]\n", argv[0]); + exit(EXIT_FAILURE); + } + } + + // Now optind (declared extern int by ) is the index of the first non-option argument. + // If it is >= argc, there were no non-option arguments. + +} diff --git a/pointer-arithmetic.c b/pointer-arithmetic.c new file mode 100644 index 0000000..1c00204 --- /dev/null +++ b/pointer-arithmetic.c @@ -0,0 +1,9 @@ +#include + +int main() { + int a[5] = {22, 33, 44, 55, 66}; + int *ptr = NULL; + ptr = a; /* point to the first array element */ + for(int i = 0; i < 50; i++) { + printf(" value:0x%-10x\tor 0d%-16d\tAdress:%-8x\n", *(ptr+i), *(ptr+i), ptr+i); /* 33 */ +}} diff --git a/pointermagic.c b/pointermagic.c new file mode 100644 index 0000000..0e9d93e --- /dev/null +++ b/pointermagic.c @@ -0,0 +1,19 @@ +#include + +void printLoc(int y) { + printf("Address of y is %x\n", &y); +} + +void printPointerLoc(int *x) { + printf("Adress of x is %x\n", &x); + printf("Adress the x pointer points to is %x\n", x); + printf("Adress of whatever *x is is %x\n", *x); +} + +int main() { + int a = 0xAAAA; + printf("Address of a is %x\n", &a); + printLoc(a); + printPointerLoc(&a); + return 0; +} diff --git a/primenumbers.c b/primenumbers.c new file mode 100644 index 0000000..2fd7994 --- /dev/null +++ b/primenumbers.c @@ -0,0 +1,43 @@ +#include +#include + +int main(int argc, char** argv) { + int maxPrime = atoi(argv[1]); + + int *arr1 = malloc(sizeof(int)*maxPrime); + if(arr1==NULL) + return -1; + for(int i=0; i0)&&(arr1[j]!=p)) + arr1[j]=-1; + } + // get next prime in arr + // FIXME + p = arr1[i]; + for(int l=0;l<512;l++){ + if(p>0) + break; + else { + p = arr1[i+l]; + } + } + for(int l=0;l0) + printf("%d ", arr1[i]); + } + free(arr1); + printf("\n"); + return 0; +} diff --git a/quersumme.c b/quersumme.c new file mode 100644 index 0000000..7484927 --- /dev/null +++ b/quersumme.c @@ -0,0 +1,30 @@ +#include +int main() { + int a, digits = 0, b = 0; + printf("input a number for calculation\n"); + fflush(stdin); + scanf("%d",&a); + + int digit[128]; + + // look out for wrong order + for(int i=0; i<128; i++) { + digit[i] = a % 10; + if(a>0) + a = a / 10; + else { + if(digits==-1) + digits = i; + digit[i] = -1; + } + //printf("%d. digit is: %d\n", i, digit[i]); + } + + // array is in wrong order, but who cares for the quersumme + for(int i=0; i<128; i++){ + if(digit[i]>0) + b += digit[i]; + } + printf("quersumme is: %d\n", b); + return 0; +} diff --git a/readfile.c b/readfile.c new file mode 100644 index 0000000..fb5f773 --- /dev/null +++ b/readfile.c @@ -0,0 +1,23 @@ +#include +#include + +int main(void) +{ + char filename[] = "file.txt"; + char buf[2]; + FILE *fp; + + if ((fp = fopen(filename, "rb")) == NULL) + { + printf("Unable to open file: %s\n", filename); + return EXIT_FAILURE; + } + + if (fread(buf, 1, 2, fp) == 2) + { + printf("%02x %02x\n", (int) buf[0], (int) buf[1]); + } + + fclose(fp); + return 0; +} diff --git a/redefinition-if.c b/redefinition-if.c new file mode 100644 index 0000000..8c03c48 --- /dev/null +++ b/redefinition-if.c @@ -0,0 +1,8 @@ +#include +int main() { + int a; + if(1){ + int a; // works, this a is only in scope of the if statement. + } + return 0; +} diff --git a/redefinition.c b/redefinition.c new file mode 100644 index 0000000..081dcea --- /dev/null +++ b/redefinition.c @@ -0,0 +1,7 @@ +#include +int a = 1; +int main() { + int a = 2; + printf("%d\n", a); // local overrides global + return 0; +} diff --git a/scanf-test.c b/scanf-test.c new file mode 100644 index 0000000..07c5bf8 --- /dev/null +++ b/scanf-test.c @@ -0,0 +1,10 @@ +#include + +int main() { +int a; +float f; +char s[20]; +scanf("%d %f %s", &a, &f, s); +printf("%d %f %s\n", a, f, s); +return 0; +\n} diff --git a/scnaf-hex-test.c b/scnaf-hex-test.c new file mode 100644 index 0000000..b9b7fa2 --- /dev/null +++ b/scnaf-hex-test.c @@ -0,0 +1,8 @@ +#include + +int main() { + int a; + scanf("%x", &a); + printf("hex: %x dec: %d\n", a, a); + return 0; + } diff --git a/signed-to-unsigned.c b/signed-to-unsigned.c new file mode 100644 index 0000000..9f461a7 --- /dev/null +++ b/signed-to-unsigned.c @@ -0,0 +1,6 @@ +#include + +int main() { + signed int a = -1; + printf("%u\n",a); // no need to type cast. %u interprets given bytes of type unsigned int regardless of the datatype. truly low level + } diff --git a/sizeofs.c b/sizeofs.c new file mode 100644 index 0000000..4a33c01 --- /dev/null +++ b/sizeofs.c @@ -0,0 +1,10 @@ +#include + +int main() { + printf("int: %ld \n", sizeof(int)); + printf("float: %ld \n", sizeof(float)); + printf("double: %ld \n", sizeof(double)); + printf("char: %ld \n", sizeof(char)); + + return 0; +} diff --git a/sum.c b/sum.c new file mode 100644 index 0000000..75126f5 --- /dev/null +++ b/sum.c @@ -0,0 +1,11 @@ +#include + +int main(){ + + int result = 0; + for (int i = 1; i < 101; i++){ + result+=i; + } + printf("sum of 1 up to 100 is: %d\n", result); + return 0; +} diff --git a/tabtest.c b/tabtest.c new file mode 100644 index 0000000..7902237 --- /dev/null +++ b/tabtest.c @@ -0,0 +1,8 @@ +#include + +int main() { + printf("horizontal\ttab\n"); + printf("vertical%ctab\n", 11); + printf("reference line\n"); + return 0; +} diff --git a/umlaut.c b/umlaut.c new file mode 100644 index 0000000..bbdb9d3 --- /dev/null +++ b/umlaut.c @@ -0,0 +1,3 @@ +#include +int main() { return 0; } +int zähler() { return 0; } // umlaute is an unknown char for gcc diff --git a/unary-double-not.c b/unary-double-not.c new file mode 100644 index 0000000..a611f37 --- /dev/null +++ b/unary-double-not.c @@ -0,0 +1,6 @@ +#include + +int main(){ + printf("%d\n",!!42); + return 0; +}