I need to know how to write this program.THIS IS ESSENTIAL TO MY MIDTERM REVIEW
ID: 3730724 • Letter: I
Question
I need to know how to write this program.THIS IS ESSENTIAL TO MY MIDTERM REVIEW SO HELP WOULD BE GREATLY APPRECIATED!!!
Midterm Review: the psuedo code
1.) If three items are provided on command line (ie argument count is 4), then we will assume we have input form #1 number operator number and process it as follows:
a. Convert and assign first "number" argument to appropiate variable
b. Convert and assign "operator" argument to appropiate variable with the following command: sscanf(argv[2], "%c", &op);
c. Convert and assign second "number" argument to appropiate variable
d. Perform a switch statement with the operator as your variable
Note: There are single quotes around the operator character in each statment. Example:
switch (op) {
case '+': do the stuff when operator is addittion break;
case'-': do the stuff when operator is subtraction break;
... CONTINUE FOR EACH CASE NEEDED ...
default: print message indicating that the operator is not valid for this input form
2. Otherwise, if two items are provided on command line ( ie argument count is 3), then we will assume we have input form 32 operator number and process it as follows:
a. Convert and assign "operator" argument to appropiate variable wiht the following command: sscanf(argv[1],"%c", &op);
b. Convert and assign "number" argument to appropiate variable
c. Perform a switch statment with the operator as your variable
Note: There are single quotes arounf the operator character in each case statment.
Example:
switch (op) {
case 'n': do the stuff when operator is negate break;
case 'a': do the stuff when the operator is absolute vaule break;
... CONTINUE FOR EACH CASE NEEDED ...
default: print message indicating that the operator is not valid for this input form
3. Otherwise, we have an input error because we did jot get valid input form, display an error message
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//commandline.c
#include<stdio.h>
int main(int argc, char *argv[]){
//checking the count of arguments
if(argc==4){
//number of arguments=4, program name + 3 extras
int num1,num2,num3;
char op;
//getting first number
sscanf(argv[1],"%d",&num1);
//getting operator
sscanf(argv[2],"%c",&op);
//getting second number
sscanf(argv[3],"%d",&num2);
//switching the operator
switch(op){
case '+':
//addition
num3=num1+num2;
printf("%d + %d = %d ",num1,num2,num3);
break;
case '-':
//subtraction
num3=num1-num2;
printf("%d - %d = %d ",num1,num2,num3);
break;
case '*':
//multiplication
num3=num1*num2;
printf("%d * %d = %d ",num1,num2,num3);
break;
case '/':
//division
num3=num1/num2;
printf("%d / %d = %d ",num1,num2,num3);
break;
case '%':
//modulus
num3=num1%num2;
printf("%d mod %d = %d ",num1,num2,num3);
break;
default: printf("operator is not valid ");
}
}else if(argc==3){
//number of arguments=3, program name + 2 extras
int num,res;
char op;
//reading the operator
sscanf(argv[1],"%c",&op);
//readin the number
sscanf(argv[2],"%d",&num);
switch(op){
case 'n':
//finding and displaying the negative of the number
res=0-num;
printf("negative of %d = %d ",num,res);
break;
case 'a':
//finding and displaying the absolute value of the number
if(num<0){
res=-num;
}else{
res=num;
}
printf("absolute value of %d = %d ",num,res);
break;
case 'i':
//incrementing by one and displaying
res=num+1;
printf("%d incremented by 1 = %d ",num,res);
break;
case 'd':
//decrementing by one and displaying
res=num-1;
printf("%d decremented by 1 = %d ",num,res);
break;
default: printf("operator is not valid ");
}
}else{
//invalid input
printf("Invalid input! ");
}
return 0;
}
/*OUTPUT*/
C:UsersuserDocuments>commandline 1 + 5
1 + 5 = 6
C:UsersuserDocuments>commandline 4 - 3
4 - 3 = 1
C:UsersuserDocuments>commandline 10 % 3
10 mod 3 = 1
C:UsersuserDocuments>commandline n 6
negative of 6 = -6
C:UsersuserDocuments>commandline a -1236
absolute value of -1236 = 1236
C:UsersuserDocuments>commandline 5 plus 3
operator is not valid
C:UsersuserDocuments>commandline i 100
100 incremented by 1 = 101
C:UsersuserDocuments>commandline d 100
100 decremented by 1 = 99
C:UsersuserDocuments>commandline dasjsk
Invalid input!