Skip to content

Commit

Permalink
Add source files for calculator program
Browse files Browse the repository at this point in the history
  • Loading branch information
adrazen authored Feb 17, 2023
1 parent 43afdaa commit ac2c407
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions calculator/addition.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "operations.h"

int add(int a, int b) {
return a + b;
}
5 changes: 5 additions & 0 deletions calculator/division.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "operations.h"

int divide (int a, int b) {
return a / b;
}
51 changes: 51 additions & 0 deletions calculator/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "operations.h"


int main(int argc, char *argv[]){
int verbose = 1;
int a;
int b;
char choice;
if (strcmp(argv[1],"-q") == 0) {
verbose = 0;
}
if (verbose) {
printf("Welcome to the world's most advanced calculator!\n");
printf("After years of research and development, we are proud to welcome you to our new and improved calculator.\n");
}
if (verbose) {
a = atoi(argv[1]);
choice = argv[2][0];
b = atoi(argv[3]);
} else {
a = atoi(argv[2]);
choice = argv[3][0];
b = atoi(argv[4]);
}
// Evaluate operation based on user's choice.
int result;
switch (choice){
case '+':
result = add(a, b);
break;
case '-':
result = subtract(a, b);
break;
case '*':
result = multiply(a, b);
break;
case '/':
result = divide(a, b);
break;
}
if (verbose) {
printf("The result of your operation is %d.\n", result);
} else {
printf("%d\n", result);
}
return 0;

}
5 changes: 5 additions & 0 deletions calculator/multiplication.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "operations.h"

int multiply(int a, int b) {
return a * b;
}
4 changes: 4 additions & 0 deletions calculator/operations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int add(int, int);
int subtract(int, int);
int multiply(int, int);
int divide(int, int);
5 changes: 5 additions & 0 deletions calculator/subtraction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "operations.h"

int subtract(int a, int b) {
return a - b;
}

0 comments on commit ac2c407

Please sign in to comment.