Write a .c file named calc that:
- Takes arguments from the command line
- Solves a simple math problem
- Returns the result as an exit status
calc.c:
//this is the function that gets called when the executable is run.
int main(int argc, char * argv[]){
//I've given you the next part for free.
//it gets the first three arguments from the command line.
//we'll discuss exactly how this happens in a later class.
//to be explicit imagine we wrote
// ./calc 1 0 2
int op1 = argv[1][0]-48; // this would be 1
int operation = argv[2][0]-48; // this would be 0
int op2 = argv[3][0]-48; // this would be 2
// START OF YOUR CODE
// fill out the main method to support
// 0 - addition
// 1 - subtraction
// 2 - multiplication
// 3 - division
//
// don't forget to return your result.
// you can view the result returned from your code by typing
// echo $?
// from the terminal (command line)
// END OF YOUR CODE
}
makefile:
first:
gcc -o calc calc.c
To solve a simple math problem in the `calc.c` file and return the result as an exit status, follow these steps:
1. Open a text editor and create a new file called `calc.c`.
2. Copy and paste the given code into the `calc.c` file.
```c
#include <stdlib.h>
#include <stdio.h>
// This is the function that gets called when the executable is run.
int main(int argc, char *argv[]) {
// I've given you the next part for free.
// It gets the first three arguments from the command line.
// To be explicit, imagine we wrote
// ./calc 1 0 2
int op1 = argv[1][0] - '0'; // this would be 1
int operation = argv[2][0] - '0'; // this would be 0
int op2 = argv[3][0] - '0'; // this would be 2
// Start of your code
int result = 0;
switch (operation) {
case 0: // Addition
result = op1 + op2;
break;
case 1: // Subtraction
result = op1 - op2;
break;
case 2: // Multiplication
result = op1 * op2;
break;
case 3: // Division
if (op2 != 0) {
result = op1 / op2;
} else {
printf("Error: Division by zero\n");
return 1; // Return an error status
}
break;
default:
printf("Invalid operation\n");
return 1; // Return an error status
}
// Print the result for debugging purposes
printf("Result: %d\n", result);
// End of your code
return result; // Return the result as an exit status
}
```
3. Save the `calc.c` file.
4. Open a text editor and create a new file called `makefile`.
5. Copy and paste the following code into the `makefile`:
```makefile
first:
gcc -o calc calc.c
```
6. Save the `makefile` file.
To compile and run the `calc.c` file, follow these steps:
1. Open your terminal or command prompt.
2. Use the `cd` command to navigate to the folder where the `calc.c` and `makefile` files are saved.
3. Run the following command to compile the `calc.c` file:
```
make
```
This command will create an executable file named `calc`.
4. To solve a simple math problem using the executable, run the following command:
```
./calc <op1> <operation> <op2>
```
Replace `<op1>`, `<operation>`, and `<op2>` with the desired values for the math problem. For example:
```
./calc 5 0 3
```
This will add `5` and `3` together and return the result as an exit status.
5. To view the result returned by the code, run the following command:
```
echo $?
```
This command will print the exit status, which represents the result of the math problem.
Note: Make sure to replace `<op1>`, `<operation>`, and `<op2>` with the appropriate values for your math problem when running the `./calc` command.