diff options
author | EliteTK <tomasz.kramkowski@gmail.com> | 2014-05-24 13:05:18 +0100 |
---|---|---|
committer | EliteTK <tomasz.kramkowski@gmail.com> | 2014-05-24 13:05:18 +0100 |
commit | 39c3721ca16d2dfdbd8e5488e7d1f57a6d54310e (patch) | |
tree | fcec9d101136a5b004ffc3fc1cc6a66dc848a6e5 /foursquare.c | |
parent | 5bbcb1f542c239ce1e9b5a7d3c7214a7bf001d6a (diff) | |
parent | a530bad7a6ea2778129db0dc26c446d9f6df6ddd (diff) | |
download | c-stuff-39c3721ca16d2dfdbd8e5488e7d1f57a6d54310e.tar.gz c-stuff-39c3721ca16d2dfdbd8e5488e7d1f57a6d54310e.tar.xz c-stuff-39c3721ca16d2dfdbd8e5488e7d1f57a6d54310e.zip |
Merge branch 'master' of https://github.com/EliteTK/c-stuff
Merge
Diffstat (limited to 'foursquare.c')
-rw-r--r-- | foursquare.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/foursquare.c b/foursquare.c new file mode 100644 index 0000000..9552ed4 --- /dev/null +++ b/foursquare.c @@ -0,0 +1,121 @@ +// foursquare.c +#include <stdio.h> +#include <ctype.h> +#include <stdlib.h> +#include <string.h> + +#define Q ('Q'-'A') + +typedef struct vec2 { + int x; + int y; +} Vec2; + +int increinc(int *); +void allupper(char *); +void genkey(char *, char *); +Vec2 *newVec2(const int, const int); + +int main(int argc, char **argv) +{ + int x, y, c=0; + + if(argc!=4){ + printf("Usage: %s <key1> <key2> <cipher>", *argv); + exit(1); + } + + printf("%s, %s ", *(argv+1), *(argv+2)); + + char *trkey = *(argv+1); + allupper(trkey); + char *trfullkey = malloc(26); + genkey(trkey, trfullkey); + + char *blkey = *(argv+2); + allupper(blkey); + char *blfullkey = malloc(26); + genkey(blkey, blfullkey); + + char *cipher = *(argv+3); + allupper(cipher); + + int i=0; + Vec2 *trtable[25]; + for(x=0; x<5; x++) + for(y=0; y<5; y++) + trtable[trfullkey[i++]-'A'] = newVec2(x, y); + free(trfullkey); + + i = 0; + Vec2 *bltable[25]; + for(x=0; x<5; x++) + for(y=0; y<5; y++) + bltable[blfullkey[i++]-'A'] = newVec2(x, y); + free(blfullkey); + + char basetable[5][5]; + for(x=0; x<5; x++) + for(y=0; y<5; y++){ + if(c==Q) + c++; + basetable[x][y] = 'A' + c++; + } + + for(i=0; i<strlen(cipher)/2; i++){ + char c1 = cipher[2*i]; + char c2 = cipher[2*i+1]; + + putchar(basetable[trtable[c1-'A']->x][bltable[c2-'A']->y]); + putchar(basetable[bltable[c2-'A']->x][trtable[c1-'A']->y]); + } + putchar('\n'); + + for(i=0; i++; i<25){ + free(trtable[i]); + free(bltable[i]); + } + + return 0; +} + +int increinc(int *num) +{ + (*num)++; + return (*num)++; +} + +void allupper(char *input) +{ + int i; + for(i=0; i<strlen(input); i++) + if(isalpha(input[i])) + input[i]=toupper(input[i]); +} + +void genkey(char *input, char *output) +{ + int dict[26]; + memset(dict, 0, 26*sizeof(int)); + + int i, outpt = 0; + for(i=0; i<strlen(input); i++){ + if(!dict[input[i]-'A']){ + output[outpt++]=input[i]; + } + dict[input[i]-'A'] += 1; + } + + for(i=0; i<26; i++) + if(!dict[i] && i!=Q) + output[outpt++]=i+'A'; + output[outpt]='\0'; +} + +Vec2 *newVec2(const int x, const int y) +{ + Vec2 *v = malloc(sizeof(Vec2)); + v->x = x; + v->y = y; + return v; +} |