【cs 50 2022】lab2 && problem set2

lab2

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// 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
    if(score1 > score2){
        printf("player 1 wins!\n");
    }
    else if(score1 == score2){
        printf("both win!\n");
    }else{
        printf("player 2 wins!\n");
    }
}

int compute_score(string word)
{
    int n = strlen(word);
    int score = 0;
    int flag = 0;
    for(int i=0;i<n;i++){
        if(word[i]>64 && word[i]<91){
            flag =  (int)(word[i]-65);
        }
        else if(word[i]>96 && word[i]<123){
            flag = (int)(word[i]-97);
        }
        else{
            printf("please check the input!\n");
        }
        score += POINTS[flag];
    }
    return score;
    // TODO: Compute and return score for string
}

 

problem set2

(1)bulbs

切记,幂不要写成2^j这样...

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

const int BITS_IN_BYTE = 8;

void print_bulb(int bit);

int main(void)
{
    // TODO
    string message = get_string("Message: ");
    int n = strlen(message);
    int number = 0;
    int flag = 0;
    for(int i=0;i<n;i++){
        number = (int)message[i];
        //printf("number:%i\n",number);
        int bit = 0;
        for(int j=7;j>=0;j--){
            flag = pow(2,j);
            // printf("flag:%i-%i",j,flag);
           if(number<flag){
                bit = 0;
           }
           else{
                bit = 1;
                number -= flag;
           }
           //printf("%i",bit);
            print_bulb(bit);
        }
        printf("\n");
    }
}

void print_bulb(int bit)
{
    if (bit == 0)
    {
        // Dark emoji
        printf("\U000026AB");
    }
    else if (bit == 1)
    {
        // Light emoji
        printf("\U0001F7E1");
    }
}

(2)caesar

 

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, string argv[])
{
    int k = atoi(argv[1]);
    if(argc != 2 || k<0 || k>126){
        printf("Usage: ./caesar key\n");
        return 1;
    }
    string check_input = argv[1];
    int m = strlen(check_input);
    int flag = 0;
    for(int i=0;i<m;i++){
        if(isdigit(check_input[i])){
            continue;
        }
        flag++;
    }
    if(flag == 0){
        string text = get_string("plaintext: ");
        int n = strlen(text);
        printf("ciphertext:");
        for(int i=0;i<n;i++){
            if(isalpha(text[i])){
                if(isupper(text[i])){
                    printf("%c",(text[i] - 65 + k) % 26 + 65);
                }
                else{
                    printf("%c",(text[i] - 97 + k) % 26 + 97);
                }
            }
            else{
            printf("%c",text[i]);
            }
        }
        printf("\n");
        return 0;
    }
    printf("Usage: ./caesar key\n");
    return 1;
}

 (3)substitution

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

const int INPUT_LENGTH = 26;

int main(int argc, string argv[])
{
    if(argc != 2){
        printf("Usage: ./substitution key\n");
        return 1;
    }
    string dictionary = argv[1];
    int len = strlen(dictionary);
    if(len!=INPUT_LENGTH){
        printf("Key must contain 26 characters.\n");
        return 1;
    }
    string text = get_string("plaintext: ");
    int text_len = strlen(text);
    int number = 0;
    bool upper_or_not = 0;
    bool check_text = 0;
    printf("ciphertext: ");
    for(int i=0;i<text_len;i++){
        check_text = isalpha(text[i]);
        if(check_text != 0){
            upper_or_not = isupper(text[i]);
            if(upper_or_not == 0){
                number = (int)text[i]-97;
                printf("%c",tolower(dictionary[number]));
            }
            else{
                number = (int)text[i]-65;
                printf("%c",toupper(dictionary[number]));
            }
        }
        else{
            printf("%c",text[i]);
        }
    }
    printf("\n");
    return 0;
}

(4)wordle50

#include <cs50.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

// each of our text files contains 1000 words
#define LISTSIZE 1000

// values for colors and score (EXACT == right letter, right place; CLOSE == right letter, wrong place; WRONG == wrong letter)
#define EXACT 2
#define CLOSE 1
#define WRONG 0

// ANSI color codes for boxed in letters
#define GREEN   "\e[38;2;255;255;255;1m\e[48;2;106;170;100;1m"
#define YELLOW  "\e[38;2;255;255;255;1m\e[48;2;201;180;88;1m"
#define RED     "\e[38;2;255;255;255;1m\e[48;2;220;20;60;1m"
#define RESET   "\e[0;39m"

// user-defined function prototypes
string get_guess(int wordsize);
int check_word(string guess, int wordsize, int status[], string choice);
void print_word(string guess, int wordsize, int status[]);

int main(int argc, string argv[])
{
    // ensure proper usage
    // TODO #1
    if(argc != 2){
        return 1;
    }

    // ensure argv[1] is either 5, 6, 7, or 8 and store that value in wordsize instead
    // TODO #2
    int k = atoi(argv[1]);
    int wordsize = 0;
    if(k>4 && k <9){
        wordsize = k;
    }else{
        printf("Error,wordsize must be either 5,6,7,or 8\n");
    }

    // open correct file, each file has exactly LISTSIZE words
    char wl_filename[6];
    sprintf(wl_filename, "%i.txt", wordsize);
    FILE *wordlist = fopen(wl_filename, "r");
    if (wordlist == NULL)
    {
        printf("Error opening file %s.\n", wl_filename);
        return 1;
    }

    // load word file into an array of size LISTSIZE
    char options[LISTSIZE][wordsize + 1];

    for (int i = 0; i < LISTSIZE; i++)
    {
        fscanf(wordlist, "%s", options[i]);
    }

    // pseudorandomly select a word for this game
    srand(time(NULL));
    string choice = options[rand() % LISTSIZE];
   // printf("choice:%s\n",choice);
    // allow one more guess than the length of the word
    int guesses = wordsize + 1;
    bool won = false;

    // print greeting, using ANSI color codes to demonstrate
    printf(GREEN"This is WORDLE50"RESET"\n");
    printf("You have %i tries to guess the %i-letter word I'm thinking of\n", guesses, wordsize);

    // main game loop, one iteration for each guess
    for (int i = 0; i < guesses; i++)
    {
        // obtain user's guess
        string guess = get_guess(wordsize);

        // array to hold guess status, initially set to zero
        int status[wordsize];

        // set all elements of status array initially to 0, aka WRONG
        // TODO #4
        for(int j=0;j<wordsize;j++){
            status[j] = 0;
        }

        // Calculate score for the guess
        int score = check_word(guess, wordsize, status, choice);
       // printf("score:%i",score);


        printf("Guess %i: ", i + 1);

        // Print the guess
        print_word(guess, wordsize, status);

        // if they guessed it exactly right, set terminate loop
        if (score == EXACT * wordsize)
        {
            won = true;
            break;
        }
    }

    // Print the game's result
    // TODO #7
    if(won!=0){
        printf("won\n");
    }

    // that's all folks!
    return 0;
}

string get_guess(int wordsize)
{
    int len = 0;
    string guess;
    do{
        guess = get_string("Input a %i-letter word: ",wordsize);
        len = strlen(guess);
    // ensure users actually provide a guess that is the correct length
    // TODO #3
    }
    while(len !=wordsize);
    return guess;
}

int check_word(string guess, int wordsize, int status[], string choice)
{
    int score = 0;
    int n = strlen(guess);
    int m = strlen(choice);
    // compare guess to choice and score points as appropriate, storing points in status
    // TODO #5
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            if((int)guess[i] == (int)choice [i]){
                score += EXACT;
                status[i] = 1;
                break;
            }
            if((int)guess[i] == (int)choice[j] && i!=j){
                score += CLOSE;
                status[i] = 2;
            }
        }
    }
    // HINTS
    // iterate over each letter of the guess
        // iterate over each letter of the choice
            // compare the current guess letter to the current choice letter
                // if they're the same position in the word, score EXACT points (green) and break so you don't compare that letter further
                // if it's in the word, but not the right spot, score CLOSE point (yellow)
        // keep track of the total score by adding each individual letter's score from above
    return score;
}

void print_word(string guess, int wordsize, int status[])
{
    // print word character-for-character with correct color coding, then reset terminal font to normal
    // TODO #6
    for(int i=0;i<wordsize;++i){
        if(status[i] == 0){
            printf(RED"%c"RESET,guess[i]);
        }
        if(status[i] == 1){
            printf(GREEN"%c"RESET,guess[i]);
        }
        if(status[i] == 2){
            printf(YELLOW"%c"RESET,guess[i]);
        }
    }

    printf("\n");
    return;
}

 

posted @ 2023-06-28 14:19  致命一姬  阅读(84)  评论(0)    收藏  举报