From 65c7618c2a8834616b009195f10c6cd762f43b8a Mon Sep 17 00:00:00 2001 From: Mincho Paskalev Date: Wed, 7 Nov 2018 10:31:59 +0200 Subject: Добавяне на второ домашно (#30) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add 1st hw * --amend * moving readme for 1st hw to designated folder * renamed TokenType enum to TokenClass * fixed character constant lexing. added sampe output file * add new line at end-of-file. added gitignore * a bit of refactoring * adding cyclomatic complexity * refactored * adding cyclomatic complexity * refactored --- homework/81568/c2html/test.html | 163 +++++++++++++++++++++++++--------------- 1 file changed, 102 insertions(+), 61 deletions(-) (limited to 'homework/81568/c2html/test.html') diff --git a/homework/81568/c2html/test.html b/homework/81568/c2html/test.html index 83b41a5..e39aec0 100644 --- a/homework/81568/c2html/test.html +++ b/homework/81568/c2html/test.html @@ -1,4 +1,4 @@ - + LexCed @@ -35,9 +35,24 @@ .comment { color: green; } + .complexity-level-one { + background-color: PaleGreen; + } + .complexity-level-two { + background-color: DarkGreen; + } + .complexity-level-three { + background-color: SaddleBrown; + } + .complexity-level-four { + background-color: Brown; + } + .complexity-level-five { + background-color: Maroon; + } - +
 #ifndef SAMPLE_CODE_C
 #define SAMPLE_CODE_C
@@ -46,97 +61,103 @@
 
 #include <stdio.h>
 
-int bitcount(long long x) {
+int bitcount(long long x) 
{ int cnt = 0;; - while (x) { + while (x) { ++cnt; - x &= (x-1); - } + x &= (x-1); + } return cnt; -} +
} -void shift_to_pos(int *num, int p, int n) { - *num = p > n ? *num << (p - n + 1) : *num >> (n - p + 1); -} +void shift_to_pos(int *num, int p, int n)
{ + *num = p > n ? *num << (p - n + 1) : *num >> (n - p + 1); +
} -int setbits(x, p, n, y) { - int mask = (1 << n) - 1; +int setbits(x, p, n, y)
{ + int mask = (1 << n) - 1; int to_set = y & mask; - shift_to_pos(&mask, p, n); - shift_to_pos(&to_set, p, n); - return (x & ~mask) | to_set; -} + shift_to_pos(&mask, p, n); + shift_to_pos(&to_set, p, n); + return (x & ~mask) | to_set; +
} -unsigned invert(unsigned x, unsigned p, unsigned n) { +unsigned invert(unsigned x, unsigned p, unsigned n)
{ ++p; // in case p < n - while(n-- && p--) { - if (!(x & (1 << n))) { - x |= (1 << n); - } else { - x ^= (1 << n); - } - } + while(n-- && p--) { + if (!(x & (1 << n))) { + x |= (1 << n); + } else { + x ^= (1 << n); + } + } return x; -} +
} -int rightrot(long long x, int n) { - int bitcnt = bitcount(x); +int rightrot(long long x, int n)
{ + int bitcnt = bitcount(x); x <<= n; - return (x & ( (1 << (bitcnt)) - 1) ) | (x & ~( (1 << bitcnt) - 1 )) >> bitcnt; -} + return (x & ( (1 << (bitcnt)) - 1) ) | (x & ~( (1 << bitcnt) - 1 )) >> bitcnt; +
} char num[32]; -void to_binary(unsigned x) { +void to_binary(unsigned x)
{ int i = 31; num[32] = '\0'; - while (x) { - if (x & 1) num[i] = '1'; + while (x) { + if (x & 1) num[i] = '1'; else num[i] = '0'; --i; x >>= 1; - } - for (; i >= 0;; --i) num[i] = '0'; - for (; i < 32; ++i) { - printf("%c%s", num[i], !((i+1) % 4) ? " " : ""); - } -} - -void switch_why_not(void) { + } + for (; i >= 0;; --i) num[i] = '0'; + for (; i < 32; ++i) { + printf("%c%s", num[i], !((i+1) % 4) ? " " : ""); + } +
} + +void switch_why_not(void)
{ char c = 'X'; - switch(c) { + switch(c) { case 'x': - printf("hex\n"); + printf("hex\n"); break; case 'X': - printf("why break stuff\n"); + printf("why break stuff\n"); + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: default: break; - } + } return; -} +
} -main() { +main()
{ long long x = 0b110110; - printf("rightrot:\n"); - to_binary(x); - printf("\n"); - to_binary(rightrot(x, 3)); - printf("\n"); + printf("rightrot:\n"); + to_binary(x); + printf("\n"); + to_binary(rightrot(x, 3)); + printf("\n"); unsigned i = 0b101010; - printf("invert:\n"); - to_binary(i); - printf("\n"); - to_binary(invert(i, 4, 4)); - printf("\n"); + printf("invert:\n"); + to_binary(i); + printf("\n"); + to_binary(invert(i, 4, 4)); + printf("\n"); long long a = 0b111001010011; long long b = 0b11111010; - printf("bits of: "); to_binary(a); printf("\n"); - printf("bits to: "); to_binary(b); printf("\n"); - to_binary(setbits(a, 6, 4, b)); + printf("bits of: "); to_binary(a); printf("\n"); + printf("bits to: "); to_binary(b); printf("\n"); + to_binary(setbits(a, 6, 4, b)); int testInt = 12345678; @@ -144,8 +165,28 @@ double testDouble = 1234.5678; return 0;; -} +
} #endif /* SAMPLE_CODE_C */ -
+ + -- cgit v1.3.1-26-gb76a