C ile Basit Hesap Makinesi

C ile dört işlemi yapan basit bir hesap makinesi yazmak istedim. Belki bir gün birilerinin işine yarar. İçerisinde farklılık olsun diye switch case(her ne kadar sevmiyor da olsam kullandım) ve birkaç fonksiyon kullandım. Kodlar aşağıda yer almakta. Ayrıca, ilgilenenler için ideone üzerinde de paylaştım.

#include <stdio.h>
float enteredNumber;
int numberOfLoop;

void addition(void);
void subtraction(void);
void multiplication(void);
void division(void);

int main(void){ /* Start of the main function */
    char request;

    printf("Please Enter the Sign Which Operation Do You Want:\n");
    printf("+ => Addition\n- => Subtraction\n* => Multiplication\n/ => Division\nChoice: ");
    scanf("%c", &request); /* Give the value of the char type variable 'request' from the user */

    switch (request){ /* Switch the variable named request to understand the selection. */
        case '+':
            addition();
            break;
        case '-':
            subtraction();
            break;
        case '*':
            multiplication();
            break;
        case '/':
            division();
            break;
        default: /* If the user entered an invalid char, print this message. */
            printf("The sign you entered was out of the list. Try Again.");
        } /* End of the switch-case */

        return 0; /* main function returns 0 */

} /* End of the main function */

void addition(void){
    float additionResult = 0;
    printf("How many numbers do you want to add?: ");
    scanf("%d", &numberOfLoop);
    for(int start = 1; start<=numberOfLoop; start++){
        printf("Enter number %d: ", start);
        scanf("%f", &enteredNumber);
        additionResult += enteredNumber;
    }
    printf("The result is %f\n", additionResult);
}

void subtraction(void){
    float subtractionResult;
    printf("Enter the main number which other numbers will be subtracted from: ");
    scanf("%f", &subtractionResult);
    printf("How many numbers do you want to subtract?: ");
    scanf("%d", &numberOfLoop);
    for(int start = 1; start<=numberOfLoop; start++){
        printf("Enter number %d: ", start);
        scanf("%f", &enteredNumber);
        subtractionResult -= enteredNumber;
    }
    printf("The result is %f\n", subtractionResult);
}

void multiplication(void){
    float multiplicationResult = 1;
    printf("How many numbers do you want to multiply?: ");
    scanf("%d", &numberOfLoop);
    for(int start = 1; start<=numberOfLoop; start++){
        printf("Enter number %d: ", start);
        scanf("%f", &enteredNumber);
        multiplicationResult *= enteredNumber;
    }
    printf("The result is %f\n", multiplicationResult);
}

void division(void){
    float divisionResult;
    printf("Enter the Divided Number: ");
    scanf("%f", &divisionResult);
    printf("How many numbers do you want to divide?: ");
    scanf("%d", &numberOfLoop);
    for(int start = 1; start<=numberOfLoop; start++){
        printf("Enter number %d: ", start);
        scanf("%f", &enteredNumber);
        if (enteredNumber==0){
            printf("Error: You cannot divide number by zero! Try Again!\n");
            break;
        }
        divisionResult /= enteredNumber;
    }
    printf("The result is %f\n", divisionResult);
}

Çalıştırırsanız göreceğiniz üzere kodun biraz düzenlenmesi daha kullanıcı dostu olmasını sağlayabilir. Şu anlık kullanılan yapılar işlevlerini iyi bir şekilde gerçekleştiriyor.

 
comments powered by Disqus