forked from stanford-cs45/spr23-a5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source files for calculator program
- Loading branch information
Showing
6 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |