เฉลย การเขียนโปรเเกรมตามเงื่อนไข เเละเเสดงผลตามโจทย์

#include<stdio.h>

int main(){

    printf(“Program Calculator\n”);

    int num, max, min,choice;

    float total;

    printf(“Please input number : “);

    scanf(“%d”,&num);

    total = num;

    max = min = num;

    do{

        printf(“\n\t1. Add 2. Sub 3. Multiply 4. Divide 5. Total\n\n”);

        printf(“please select : “);

        scanf(“%d”,&choice);

        switch(choice){

            case 1: printf(“+\n”);

                    printf(“Please input number : “);

                    scanf(“%d”,&num);

                    total = total + num;

                    printf(“\n%f\n”,total);

                    break;

            case 2: printf(“-\n”);

                    printf(“Please input number : “);

                    scanf(“%d”,&num);

                    total = total – num;

                    printf(“\n%f\n”,total);

                    break;

            case 3: printf(“x\n”);

                    printf(“Please input number : “);

                    scanf(“%d”,&num);

                    total = total * num;

                    printf(“\n%f\n”,total);

                    break;

            case 4: printf(“/\n”);

                    printf(“Please input number : “);

                    scanf(“%d”,&num);

                    total = total * 1.0 /num;

                    printf(“\n%f\n”,total);

                    break;

            case 5: break;

            default : printf(“select 1 – 5 only\n”);

        }

        if(num > max)

            max = num;

        if(num < min)

            min = num;

    }while(choice != 5);

    printf(“===================\n”);

    printf(“total = %f\n”,total);

    printf(“Max = %d\n”, max);

    printf(“Min = %d\n”, min);

    int def, i, j;

    def = max – min;

    printf(“\n\n\n==========\n”);

    for(i = 1; i<=def; i++){

        for(j = 1; j<=def; j++){

            printf(“*”);

        }

        printf(“\n”);

    }

    printf(“===========\n”);

    for(i = 1; i<=def; i++){

        for(j = 1; j<=i; j++){

            printf(“*”);

        }

        printf(“\n”);

    }

    printf(“===========\n”);

    for(i = 1; i<=def; i++){

        for(j = 1; j<=def; j++){

            if(i == 1||i == def|| j==1 || j==def){

                printf(“*”);

            }else{

                printf(” “);

            }

        }

        printf(“\n”);

    }

    printf(“===========\n”);

}

//ขอให้สนุกกับการเขียนโปรเเกรมครับ>-<

One Reply to “เฉลย การเขียนโปรเเกรมตามเงื่อนไข เเละเเสดงผลตามโจทย์”

ใส่ความเห็น