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

hw1q3

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

    hw1q3

    02340114

    מבוא למדמח מ

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

    hw1q3

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

    uni-folder.vercel.app

    hw1q3.c

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

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

    #include <stdbool.h>
    #include <stdio.h>
    
    // 3 numbers:
    // 1. max number of digits.
    // 2. max number of different digit types.
    // 3. max number of digit repetition in the number.
    
    int main(void)
    {
        printf("Please enter condition numbers:\n");
    
        int maxDigits, maxDifferentDigits, maxRepetition;
        if (scanf(
            "%d %d %d",
            &maxDigits,
            &maxDifferentDigits,
            &maxRepetition) > 0
        )
        {
            if (maxDifferentDigits >= 1 && maxDifferentDigits <= 10)
            {
                printf("Please enter test number:\n");
    
                char c;
                int zero = 0,
                    one = 0,
                    two = 0,
                    three = 0,
                    four = 0,
                    five = 0,
                    six = 0,
                    seven = 0,
                    eight = 0,
                    nine = 0;
    
                getchar(); // removing the enter press
                while ((c = getchar()) >= '0' && c <= '9')
                {
                    switch (c)
                    {
                        case '1':
                        {
                            one++;
                            break;
                        }
                        case '2':
                        {
                            two++;
                            break;
                        }
                        case '3':
                        {
                            three++;
                            break;
                        }
                        case '4':
                        {
                            four++;
                            break;
                        }
                        case '5':
                        {
                            five++;
                            break;
                        }
                        case '6':
                        {
                            six++;
                            break;
                        }
                        case '7':
                        {
                            seven++;
                            break;
                        }
                        case '8':
                        {
                            eight++;
                            break;
                        }
                        case '9':
                        {
                            nine++;
                            break;
                        }
                        default:
                        {
                            zero++;
                        }
                    }
                }
    
                int numOfDigits = zero + one + two + three + four
                    + five + six + seven + eight + nine;
                int numOfDifferentDigits = (zero > 0) + (one > 0) + (two > 0)
                    + (three > 0) + (four > 0) + (five > 0) + (six > 0)
                    + (seven > 0) + (eight > 0) + (nine > 0);
    
                // printing results
                if ((numOfDigits <= maxDigits)
                    && (numOfDifferentDigits <= maxDifferentDigits)
                    && ((zero <= maxRepetition) && (one <= maxRepetition)
                        && (two <= maxRepetition) && (three <= maxRepetition)
                        && (four <= maxRepetition) && (five <= maxRepetition)
                        && (six <= maxRepetition) && (seven <= maxRepetition)
                        && (eight <= maxRepetition) && (nine <= maxRepetition)))
                {
                    printf("The number given meets all the conditions");
                }
                else
                {
                    printf("The number given does not meet all the conditions");
                }
            }
            else
            {
                printf("Number of unique digits must be between 1 to 10\n");
            }
        }
    
        return 0;
    }