Browse Source

week02

master
RinRi 2 years ago
parent
commit
c5663dc2ba
20 changed files with 224 additions and 0 deletions
  1. BIN
      week01/ex4
  2. +2
    -0
      week02/.clang-format
  3. BIN
      week02/ex1
  4. +19
    -0
      week02/ex1.c
  5. +4
    -0
      week02/ex1.sh
  6. BIN
      week02/ex2
  7. +18
    -0
      week02/ex2.c
  8. +4
    -0
      week02/ex2.sh
  9. BIN
      week02/ex3
  10. +54
    -0
      week02/ex3.c
  11. +4
    -0
      week02/ex3.sh
  12. BIN
      week02/ex4
  13. +28
    -0
      week02/ex4.c
  14. +4
    -0
      week02/ex4.sh
  15. BIN
      week02/ex5
  16. +21
    -0
      week02/ex5.c
  17. +4
    -0
      week02/ex5.sh
  18. BIN
      week02/ex6
  19. +58
    -0
      week02/ex6.c
  20. +4
    -0
      week02/ex6.sh

BIN
week01/ex4 View File


+ 2
- 0
week02/.clang-format View File

@@ -0,0 +1,2 @@
BasedOnStyle: LLVM
IndentWidth: 4

BIN
week02/ex1 View File


+ 19
- 0
week02/ex1.c View File

@@ -0,0 +1,19 @@
#include <float.h>
#include <limits.h>
#include <stdio.h>

int main() {
int i = INT_MAX;
unsigned short us = USHRT_MAX;
long l = LONG_MAX;
float f = FLT_MAX;
double d = DBL_MAX;

printf("int: %lu, %d\n"
"unsigned short: %lu, %hu\n"
"long: %lu, %ld\n"
"float: %lu, %f\n"
"double: %lu, %lf\n",
sizeof(i), i, sizeof(us), us, sizeof(l), l, sizeof(f), f, sizeof(d),
d);
}

+ 4
- 0
week02/ex1.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex1.c -o ex1
./ex1

BIN
week02/ex2 View File


+ 18
- 0
week02/ex2.c View File

@@ -0,0 +1,18 @@
#include <stdio.h>
#include <string.h>

int main() {
char s[257], c;
int i = 0;
while ((c = getc(stdin)) != '.') {
if (c == '\n')
break;
s[i++] = c;
}
s[i] = '\0';
// i is already the size of s. I've used strlen to follow the 'hints'
// section.
for (i = strlen(s) - 1; i >= 0; --i) {
putc(s[i], stdout);
}
}

+ 4
- 0
week02/ex2.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex2.c -o ex2
./ex2

BIN
week02/ex3 View File


+ 54
- 0
week02/ex3.c View File

@@ -0,0 +1,54 @@
#include <stdio.h>
#include <string.h>

/* DISCLAIMER: As far as I understand, I need to create a function,
which takes a long long number (not a string), convert it to a
string, use that string as it was a number in the source
number system, and then print out in the target number system.

I think it is way better to use string for the number itself and
read it as a string from the beginning. But I will follow the
assignment instructions since I don't want to get a lower grade.
Please, make it more clear next time.
*/

void convert(long long n, int s, int t) {
if (!(s >= 2 && s <= 10 && t >= 2 && t <= 10)) {
printf("cannot convert!\n");
return;
}

// convert n from source number system to normal long long integer
long long n_actual = 0, cur = 1;
while (n) {
if (n % 10 >= s) {
printf("cannot convert!\n");
return;
}
n_actual += cur * (n % 10);
cur *= s;
n /= 10;
}

// convert the actual n to target number system
char buf[65];
int i = 0;
while (n_actual) {
buf[i++] = (n_actual % t) + '0';
n_actual /= t;
}
buf[i] = '\0';

for (--i; i >= 0; --i) {
putc(buf[i], stdout);
}

putc('\n', stdout);
}

int main() {
long long number;
int source, target;
scanf("%lld%d%d", &number, &source, &target);
convert(number, source, target);
}

+ 4
- 0
week02/ex3.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex3.c -o ex3
./ex3

BIN
week02/ex4 View File


+ 28
- 0
week02/ex4.c View File

@@ -0,0 +1,28 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int count(char s[], char c) {
int cnt = 0, sz = strlen(s);
for (int i = 0; i < sz; ++i) {
if (tolower(s[i]) == tolower(c))
cnt++;
}

return cnt;
}

void countAll(char s[]) {
int sz = strlen(s);
for (int i = 0; i < sz; ++i) {
int cnt = count(s, s[i]);
if (cnt > 0)
printf("%c:%d%s", tolower(s[i]), cnt, (i == sz - 1 ? "\n" : ", "));
}
}

int main() {
char s[257];
scanf("%s", s);
countAll(s);
}

+ 4
- 0
week02/ex4.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex4.c -o ex4
./ex4

BIN
week02/ex5 View File


+ 21
- 0
week02/ex5.c View File

@@ -0,0 +1,21 @@
#include <stdio.h>

long long tribonacci(int n) {
long long a = 0, b = 1, c = 1;
if (n == 0)
return a;
if (n == 1)
return b;
if (n == 2)
return c;

for (int i = 3; i <= n; ++i) {
long long d = a + b + c;
a = b, b = c, c = d;
}
return c;
}

int main() {
printf("%lld\n%lld\n", tribonacci(4), tribonacci(36));
}

+ 4
- 0
week02/ex5.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex5.c -o ex5
./ex5

BIN
week02/ex6 View File


+ 58
- 0
week02/ex6.c View File

@@ -0,0 +1,58 @@
#include <stdio.h>

void triangle(int n) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j)
printf(" ");

for (int j = 1; j <= i * 2 - 1; ++j) {
printf("*");
}
printf("\n");
}
}

void triangle2(int n) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
printf("*");
}
printf("\n");
}
}

void arrow(int n) {
for (int i = 1; i <= (n + 1) / 2; ++i) {
for (int j = 1; j <= i; ++j) {
printf("*");
}
printf("\n");
}
for (int i = n / 2; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
printf("*");
}
printf("\n");
}
}

void rectangle(int n, int m) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
printf("*");
}
printf("\n");
}
}

int main() {
int n;
printf("Specify number of rows: ");
scanf("%d", &n);
// triangle(n);
triangle2(n);
printf("\n");
arrow(n);
printf("\n");
rectangle(n, n);
}

+ 4
- 0
week02/ex6.sh View File

@@ -0,0 +1,4 @@
#!/bin/sh

gcc ex6.c -o ex6
./ex6

Loading…
Cancel
Save