From a2682d25c6cd0e069ff73a43df74c17a63caabac Mon Sep 17 00:00:00 2001 From: Fudgerboy <91767657+Fudgerboy@users.noreply.github.com> Date: Sat, 17 Feb 2024 22:59:23 +0000 Subject: Sat, Feb 17, 2024, 2:59 PM -08:00 --- wk2/lab/scrabble/scrabble.c | 28 +++++++++++++ wk2/lab2/scrabble/scrabble.c | 28 ------------- wk2/lect/greet.c | 10 +++++ wk2/lect/lowercase.c | 15 +++++++ wk2/lect/status.c | 16 ++++++++ wk2/lect/uppercase.c | 21 ++++++++++ wk2/lect2/greet.c | 10 ----- wk2/lect2/lowercase.c | 15 ------- wk2/lect2/status.c | 16 -------- wk2/lect2/uppercase.c | 21 ---------- wk2/pset/caesar/caesar.c | 78 +++++++++++++++++++++++++++++++++++++ wk2/pset/readability/readability.c | 52 +++++++++++++++++++++++++ wk2/pset2/caesar/caesar.c | 78 ------------------------------------- wk2/pset2/readability/readability.c | 52 ------------------------- wk2/sect/alpha.c | 20 ++++++++++ wk2/sect/array.c | 22 +++++++++++ wk2/sect/cla.c | 12 ++++++ wk2/sect/initials.c | 13 +++++++ wk2/sect/str.c | 15 +++++++ wk2/sect2/alpha.c | 20 ---------- wk2/sect2/array.c | 22 ----------- wk2/sect2/cla.c | 12 ------ wk2/sect2/initials.c | 13 ------- wk2/sect2/str.c | 15 ------- 24 files changed, 302 insertions(+), 302 deletions(-) create mode 100644 wk2/lab/scrabble/scrabble.c delete mode 100644 wk2/lab2/scrabble/scrabble.c create mode 100644 wk2/lect/greet.c create mode 100644 wk2/lect/lowercase.c create mode 100644 wk2/lect/status.c create mode 100644 wk2/lect/uppercase.c delete mode 100644 wk2/lect2/greet.c delete mode 100644 wk2/lect2/lowercase.c delete mode 100644 wk2/lect2/status.c delete mode 100644 wk2/lect2/uppercase.c create mode 100644 wk2/pset/caesar/caesar.c create mode 100644 wk2/pset/readability/readability.c delete mode 100644 wk2/pset2/caesar/caesar.c delete mode 100644 wk2/pset2/readability/readability.c create mode 100644 wk2/sect/alpha.c create mode 100644 wk2/sect/array.c create mode 100644 wk2/sect/cla.c create mode 100644 wk2/sect/initials.c create mode 100644 wk2/sect/str.c delete mode 100644 wk2/sect2/alpha.c delete mode 100644 wk2/sect2/array.c delete mode 100644 wk2/sect2/cla.c delete mode 100644 wk2/sect2/initials.c delete mode 100644 wk2/sect2/str.c (limited to 'wk2') diff --git a/wk2/lab/scrabble/scrabble.c b/wk2/lab/scrabble/scrabble.c new file mode 100644 index 0000000..5674efe --- /dev/null +++ b/wk2/lab/scrabble/scrabble.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +// Points assigned to each letter of the alphabet +int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10}; + +int compute_score(string word); + +int main(void) +{ + // Get input words from both players + string word1 = get_string("Player 1: "); + string word2 = get_string("Player 2: "); + + // Score both words + int score1 = compute_score(word1); + int score2 = compute_score(word2); + + // TODO: Print the winner +} + +int compute_score(string word) +{ + // TODO: Compute and return score for string + +} diff --git a/wk2/lab2/scrabble/scrabble.c b/wk2/lab2/scrabble/scrabble.c deleted file mode 100644 index 5674efe..0000000 --- a/wk2/lab2/scrabble/scrabble.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include - -// Points assigned to each letter of the alphabet -int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10}; - -int compute_score(string word); - -int main(void) -{ - // Get input words from both players - string word1 = get_string("Player 1: "); - string word2 = get_string("Player 2: "); - - // Score both words - int score1 = compute_score(word1); - int score2 = compute_score(word2); - - // TODO: Print the winner -} - -int compute_score(string word) -{ - // TODO: Compute and return score for string - -} diff --git a/wk2/lect/greet.c b/wk2/lect/greet.c new file mode 100644 index 0000000..ccecd7d --- /dev/null +++ b/wk2/lect/greet.c @@ -0,0 +1,10 @@ +#include +#include + +//argc is argument count, ie how many arguments were given +//argv is an array of the arguments given +//argv[0] is the command that calls the script, ie ./greet +int main(int argc, string argv[]) +{ + printf("hello, %s\n", argv[1]); +} diff --git a/wk2/lect/lowercase.c b/wk2/lect/lowercase.c new file mode 100644 index 0000000..ee473ae --- /dev/null +++ b/wk2/lect/lowercase.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +int main(void) +{ + string s = get_string("Before: "); + printf("After: "); + for (int i = 0, n = strlen(s); i < n; i++) + { + printf("%c", tolower(s[i])); + } + printf("\n"); +} diff --git a/wk2/lect/status.c b/wk2/lect/status.c new file mode 100644 index 0000000..ebce04c --- /dev/null +++ b/wk2/lect/status.c @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc, string argv[]) +{ + if (argc != 2) + { + printf("Missing command-line argument\n"); + return 1; + } + else + { + printf("hello, %s\n", argv[1]); + return 0; + } +} diff --git a/wk2/lect/uppercase.c b/wk2/lect/uppercase.c new file mode 100644 index 0000000..3a34b97 --- /dev/null +++ b/wk2/lect/uppercase.c @@ -0,0 +1,21 @@ +#include +#include +#include + +int main(void) +{ + string s = get_string("Before: "); + printf("After: "); + for (int i = 0; i < strlen(s); i++) + { + if (s[i] >= 'a' && s[i] <= 'z') + { + printf("%c", s[i] - 32); + } + else + { + printf("%c", s[i]); + } + } + printf("\n"); +} diff --git a/wk2/lect2/greet.c b/wk2/lect2/greet.c deleted file mode 100644 index ccecd7d..0000000 --- a/wk2/lect2/greet.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -//argc is argument count, ie how many arguments were given -//argv is an array of the arguments given -//argv[0] is the command that calls the script, ie ./greet -int main(int argc, string argv[]) -{ - printf("hello, %s\n", argv[1]); -} diff --git a/wk2/lect2/lowercase.c b/wk2/lect2/lowercase.c deleted file mode 100644 index ee473ae..0000000 --- a/wk2/lect2/lowercase.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include -#include - -int main(void) -{ - string s = get_string("Before: "); - printf("After: "); - for (int i = 0, n = strlen(s); i < n; i++) - { - printf("%c", tolower(s[i])); - } - printf("\n"); -} diff --git a/wk2/lect2/status.c b/wk2/lect2/status.c deleted file mode 100644 index ebce04c..0000000 --- a/wk2/lect2/status.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -int main(int argc, string argv[]) -{ - if (argc != 2) - { - printf("Missing command-line argument\n"); - return 1; - } - else - { - printf("hello, %s\n", argv[1]); - return 0; - } -} diff --git a/wk2/lect2/uppercase.c b/wk2/lect2/uppercase.c deleted file mode 100644 index 3a34b97..0000000 --- a/wk2/lect2/uppercase.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include - -int main(void) -{ - string s = get_string("Before: "); - printf("After: "); - for (int i = 0; i < strlen(s); i++) - { - if (s[i] >= 'a' && s[i] <= 'z') - { - printf("%c", s[i] - 32); - } - else - { - printf("%c", s[i]); - } - } - printf("\n"); -} diff --git a/wk2/pset/caesar/caesar.c b/wk2/pset/caesar/caesar.c new file mode 100644 index 0000000..70b4544 --- /dev/null +++ b/wk2/pset/caesar/caesar.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include + + +bool only_digits(string phrase); +int rotate(char c, int i); + + +int main(int argc, string argv[]) +{ + if (argc != 2 || only_digits(argv[1]) == false) { + printf("Usage: ./caesar key\n"); + return 1; + } else { + int key = (atoi(argv[1])) % 26; + string plain = get_string("plaintext: "); + int plainlen = strlen(plain); + int cypher[plainlen]; + printf("ciphertext: "); + for (int i = 0; i < plainlen; i++) { + cypher[i] = rotate(plain[i], key); + printf("%c", cypher[i]); + } + printf("\n"); + } +} + + +bool only_digits(string phrase) { + for (int i = 0; phrase[i] != '\0'; i++) { + if (47 > phrase[i] || phrase[i] > 58) { + return false; + } + } + return true; +} + + +int rotate(char c, int i) { + if ((64 < c && c < 91) || (96 < c && c < 123)) { + c += i; + if (64 >= c || (c >= 91 && 96 >= c) || c >= 123) { + c -= 26; + } + } + return c; +} + + + +/* +Your program must accept a single command-line argument, a non-negative integer. Let’s call it *k* + for the sake of discussion. +If your program is executed without any command-line arguments or with more than one command-line + argument, your program should print an error message of your choice (with printf) and return from + main a value of 1 (which tends to signify an error) immediately. +If any of the characters of the command-line argument is not a decimal digit, your program should print + the message Usage: ./caesar key and return from main a value of 1. +Do not assume that k will be less than or equal to 26. Your program should work for all non-negative + integral values of k less than 2^31-26. In other words, you don’t need to worry if your program eventually + breaks if the user chooses a value for k that’s too big or almost too big to fit in an int. (Recall + that an int can overflow.) But, even if k is greater than 26, alphabetical characters in your program’s + input should remain alphabetical characters in your program’s output. For instance, if k is 27, A should + not become \ even though \ is positions away from A in ASCII, per asciitable.com; A should become B, + since B is 27 positions away from A, provided you wrap around from Z to A. +Your program must output plaintext: (with two spaces but without a newline) and then prompt the user for + a string of plaintext (using get_string). +Your program must output ciphertext: (with one space but without a newline) followed by the plaintext’s + corresponding ciphertext, with each alphabetical character in the plaintext “rotated” by k positions; + non-alphabetical characters should be outputted unchanged. +Your program must preserve case: capitalized letters, though rotated, must remain capitalized letters; + lowercase letters, though rotated, must remain lowercase letters. +After outputting ciphertext, you should print a newline. Your program should then exit by returning 0 + from main. +*/ diff --git a/wk2/pset/readability/readability.c b/wk2/pset/readability/readability.c new file mode 100644 index 0000000..89f675e --- /dev/null +++ b/wk2/pset/readability/readability.c @@ -0,0 +1,52 @@ +#include +#include +#include + +int main(void) +{ + string text = get_string("Text: "); + float s = 0; + float w = 1; + float l = 0; + int i = 0; + char c; + do + { + c = text[i]; + int j = c; + i++; + if (c == '.' || c == '!' || c == '?') + { + s++; + } + else if (c == ' ') + { + w++; + } + else if ((64 < j && j < 91) || (96 < j && j < 123)) + { + l++; + } + } + while (c != '\0'); + // printf("s: %f w: %f l: %f\n", s, w, l); + float L = (float) (l / w) * 100; + float S = (double) (s / w) * 100; + float index = 0.0588 * L - 0.296 * S - 15.8; + if (index < 1) + { + printf("Before Grade 1\n"); + } + else + { + int ans = round(index); + if (ans >= 16) + { + printf("Grade 16+\n"); + } + else + { + printf("Grade %i\n", ans); + } + } +} diff --git a/wk2/pset2/caesar/caesar.c b/wk2/pset2/caesar/caesar.c deleted file mode 100644 index 70b4544..0000000 --- a/wk2/pset2/caesar/caesar.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include - - -bool only_digits(string phrase); -int rotate(char c, int i); - - -int main(int argc, string argv[]) -{ - if (argc != 2 || only_digits(argv[1]) == false) { - printf("Usage: ./caesar key\n"); - return 1; - } else { - int key = (atoi(argv[1])) % 26; - string plain = get_string("plaintext: "); - int plainlen = strlen(plain); - int cypher[plainlen]; - printf("ciphertext: "); - for (int i = 0; i < plainlen; i++) { - cypher[i] = rotate(plain[i], key); - printf("%c", cypher[i]); - } - printf("\n"); - } -} - - -bool only_digits(string phrase) { - for (int i = 0; phrase[i] != '\0'; i++) { - if (47 > phrase[i] || phrase[i] > 58) { - return false; - } - } - return true; -} - - -int rotate(char c, int i) { - if ((64 < c && c < 91) || (96 < c && c < 123)) { - c += i; - if (64 >= c || (c >= 91 && 96 >= c) || c >= 123) { - c -= 26; - } - } - return c; -} - - - -/* -Your program must accept a single command-line argument, a non-negative integer. Let’s call it *k* - for the sake of discussion. -If your program is executed without any command-line arguments or with more than one command-line - argument, your program should print an error message of your choice (with printf) and return from - main a value of 1 (which tends to signify an error) immediately. -If any of the characters of the command-line argument is not a decimal digit, your program should print - the message Usage: ./caesar key and return from main a value of 1. -Do not assume that k will be less than or equal to 26. Your program should work for all non-negative - integral values of k less than 2^31-26. In other words, you don’t need to worry if your program eventually - breaks if the user chooses a value for k that’s too big or almost too big to fit in an int. (Recall - that an int can overflow.) But, even if k is greater than 26, alphabetical characters in your program’s - input should remain alphabetical characters in your program’s output. For instance, if k is 27, A should - not become \ even though \ is positions away from A in ASCII, per asciitable.com; A should become B, - since B is 27 positions away from A, provided you wrap around from Z to A. -Your program must output plaintext: (with two spaces but without a newline) and then prompt the user for - a string of plaintext (using get_string). -Your program must output ciphertext: (with one space but without a newline) followed by the plaintext’s - corresponding ciphertext, with each alphabetical character in the plaintext “rotated” by k positions; - non-alphabetical characters should be outputted unchanged. -Your program must preserve case: capitalized letters, though rotated, must remain capitalized letters; - lowercase letters, though rotated, must remain lowercase letters. -After outputting ciphertext, you should print a newline. Your program should then exit by returning 0 - from main. -*/ diff --git a/wk2/pset2/readability/readability.c b/wk2/pset2/readability/readability.c deleted file mode 100644 index 89f675e..0000000 --- a/wk2/pset2/readability/readability.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include - -int main(void) -{ - string text = get_string("Text: "); - float s = 0; - float w = 1; - float l = 0; - int i = 0; - char c; - do - { - c = text[i]; - int j = c; - i++; - if (c == '.' || c == '!' || c == '?') - { - s++; - } - else if (c == ' ') - { - w++; - } - else if ((64 < j && j < 91) || (96 < j && j < 123)) - { - l++; - } - } - while (c != '\0'); - // printf("s: %f w: %f l: %f\n", s, w, l); - float L = (float) (l / w) * 100; - float S = (double) (s / w) * 100; - float index = 0.0588 * L - 0.296 * S - 15.8; - if (index < 1) - { - printf("Before Grade 1\n"); - } - else - { - int ans = round(index); - if (ans >= 16) - { - printf("Grade 16+\n"); - } - else - { - printf("Grade %i\n", ans); - } - } -} diff --git a/wk2/sect/alpha.c b/wk2/sect/alpha.c new file mode 100644 index 0000000..054950f --- /dev/null +++ b/wk2/sect/alpha.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +int main(void) +{ + string word = get_string("Word: "); + int l = strlen(word); + for (int i = 0; i < l - 1; i++) + { + // If NOT alphabetical + if (word[i] > word[i + 1]) + { + printf("No\n"); + return 0; + } + } + printf("Yes\n"); +} diff --git a/wk2/sect/array.c b/wk2/sect/array.c new file mode 100644 index 0000000..a8807a7 --- /dev/null +++ b/wk2/sect/array.c @@ -0,0 +1,22 @@ +#include +#include + +int main(void) +{ + int n; + do + { + n = get_int("Length: "); + } + while (n < 1); + int array[n]; + array[0] = 1; + for (int i = 1; i < n; i++) + { + array[i] = array[i - 1] * 2; + } + for (int i = 0; i < n; i++) + { + printf("%i\n", array[i]); + } +} diff --git a/wk2/sect/cla.c b/wk2/sect/cla.c new file mode 100644 index 0000000..431b04c --- /dev/null +++ b/wk2/sect/cla.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include + +int main(int argc, string argv[]) +{ + for (int i = 0; i < argc; i++) + { + printf("argv[%i] is %s\n", i, argv[i]); + } +} diff --git a/wk2/sect/initials.c b/wk2/sect/initials.c new file mode 100644 index 0000000..45a36c6 --- /dev/null +++ b/wk2/sect/initials.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include + +int main(int argc, string argv[]) +{ + for (int i = 1; i < argc; i++) + { + printf("%c", argv[i][0]); + } + printf("\n"); +} diff --git a/wk2/sect/str.c b/wk2/sect/str.c new file mode 100644 index 0000000..5075431 --- /dev/null +++ b/wk2/sect/str.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +int main(void) +{ + string name = "Emma"; + int l = strlen(name); + for (int i = 0; i < l; i++) + { + printf("%i ", name[i]); + } + printf("\n"); +} diff --git a/wk2/sect2/alpha.c b/wk2/sect2/alpha.c deleted file mode 100644 index 054950f..0000000 --- a/wk2/sect2/alpha.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include - -int main(void) -{ - string word = get_string("Word: "); - int l = strlen(word); - for (int i = 0; i < l - 1; i++) - { - // If NOT alphabetical - if (word[i] > word[i + 1]) - { - printf("No\n"); - return 0; - } - } - printf("Yes\n"); -} diff --git a/wk2/sect2/array.c b/wk2/sect2/array.c deleted file mode 100644 index a8807a7..0000000 --- a/wk2/sect2/array.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -int main(void) -{ - int n; - do - { - n = get_int("Length: "); - } - while (n < 1); - int array[n]; - array[0] = 1; - for (int i = 1; i < n; i++) - { - array[i] = array[i - 1] * 2; - } - for (int i = 0; i < n; i++) - { - printf("%i\n", array[i]); - } -} diff --git a/wk2/sect2/cla.c b/wk2/sect2/cla.c deleted file mode 100644 index 431b04c..0000000 --- a/wk2/sect2/cla.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include -#include - -int main(int argc, string argv[]) -{ - for (int i = 0; i < argc; i++) - { - printf("argv[%i] is %s\n", i, argv[i]); - } -} diff --git a/wk2/sect2/initials.c b/wk2/sect2/initials.c deleted file mode 100644 index 45a36c6..0000000 --- a/wk2/sect2/initials.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include -#include - -int main(int argc, string argv[]) -{ - for (int i = 1; i < argc; i++) - { - printf("%c", argv[i][0]); - } - printf("\n"); -} diff --git a/wk2/sect2/str.c b/wk2/sect2/str.c deleted file mode 100644 index 5075431..0000000 --- a/wk2/sect2/str.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include -#include - -int main(void) -{ - string name = "Emma"; - int l = strlen(name); - for (int i = 0; i < l; i++) - { - printf("%i ", name[i]); - } - printf("\n"); -} -- cgit v1.2.3