חזור לתיקייה: גליון 2

hw2q1

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה

    hw2q1

    02340114

    מבוא למדמח מ

    רשימות הרצאה לקריאה בשבת

    hw2q1

    הופק מתוך תקיית התואר של שקד

    uni-folder.vercel.app

    hw2q1.c

    הערה (קובץ קוד)

    קובץ זה הוא קובץ קוד מסוג C.

    #include <stdio.h>
    
    // HOW TO DO:
    // key can be 0 to 25
    // word is spaced out by spaces
    // -> if you get to a wrong letter, you can just continue until the next space
    // -> if you checked the word, and it's correct, you can print out the key
    //      and finish the exercise.
    
    #define SENTENCE_SIZE 30
    #define WORD_SIZE 10
    
    #define GET_KEY(secret, original) \
        ((secret >= original) ? secret - original : secret - original + 26)
    
    int main(void)
    {
        char originalWord[WORD_SIZE] = {0}, secretSentence[SENTENCE_SIZE];
    
        // input:
        char c;
        int sentenceSize = 0;
        while ((((c = getchar()) >= 'a' && c <= 'z') || c == ' ') &&
            sentenceSize < SENTENCE_SIZE)
        {
            secretSentence[sentenceSize] = c;
            sentenceSize++;
        }
    
        int originalSize = 0;
        while ((c = getchar()) <= 'z' && c >= 'a' &&
            originalSize < WORD_SIZE)
        {
            originalWord[originalSize] = c;
            originalSize++;
        }
    
        // finding the key:
        int key = GET_KEY(secretSentence[0], originalWord[0]), current = 1;
        // states;
        // 0 - find new word, 1 - in word checking, 2 - in word fail, 3 - done
        int state = 1;
        for (int i = 1; (state != 3) && (i < sentenceSize); i++)
        {
            switch (state)
            {
                case 0:
                {
                    if (secretSentence[i] != ' ')
                    {
                        state = 1;
                        key = GET_KEY(secretSentence[i], originalWord[0]);
                        current = 1;
                    }
                    break;
                }
                case 1:
                {
                    if (current == originalSize - 1)
                    {
                        state = 3;
                    }
                    else if (GET_KEY(secretSentence[i], originalWord[current])
                        != key)
                    {
                        state = 2;
                    }
                    else
                    {
                        current++;
                    }
                    break;
                }
                case 2:
                {
                    if (secretSentence[i] == ' ')
                    {
                        state = 0;
                    }
                    break;
                }
                default:
                    break;
            }
        }
    
        printf("%d\n", key);
    
        return 0;
    }
    
    
    
    
    
    /*
    int state = 0 ; //0 -find new work, 1 - in word ok, 2- in work fail
    
    for (int idxSecret = 0; secre)
    {
        if (state == 0)
            if (secret[idx] == ' ')
                continue;
            else
            {
                state = 1;
                key = asdasdasd;
                continue;
            }
        else
        {
            key = secret[idx], origial[0];
    
        }
    
    }*/