Question

In: Computer Science

in C programing language Two numbers entered from the command line (example: calculate.exe 2 + 2)...

in C programing language

Two numbers entered from the command line (example: calculate.exe 2 + 2)
Add, subtract, divide and multiply by (+, -, /, *) operators
Write the C program that makes the calculation using switch-case.
hint: operator [2][0] comes from index .

Solutions

Expert Solution

Note: here expression stores in argv[1] not argv[2] for my compiler

Source Code:

#include <stdio.h>

int main(int argc, char const *argv[])
{
   int operand1,operand2,operator;
   operand1=argv[1][0]-'0';
   operand2=argv[1][2]-'0';
   operator=argv[1][1];

   int result=0;
   switch(operator)
   {
       case '+':
           result=operand1+operand2;
           break;
       case '-':
           result=operand1-operand2;
           break;
       case '*':
           result=operand1*operand2;
           break;
       case '/':
           result=operand1/operand2;
           break;
   }
   printf("Result of %s is %d\n",argv[1],result);
}

Sample input and output:


Related Solutions

IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
Write a script that represents a simple command line calculator. It will accept two numbers from...
Write a script that represents a simple command line calculator. It will accept two numbers from command line, and then display sum, difference and product of these two numbers. Provide script code and screenshot of output. Using Linux Ubuntu bash
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
c programing language When a new program is executed, a new process is created with the...
c programing language When a new program is executed, a new process is created with the next available process ID. However, there are a few special processes that always have the same process ID, which are usually given the ID value less than 5 these are called system processes. Can you identify which of the two system processes have the process ID of 0 and 1 respectively?
I need programing in C language (not C#,C++) Sheldon Game RPSLS: - Stone: Win against Scissors...
I need programing in C language (not C#,C++) Sheldon Game RPSLS: - Stone: Win against Scissors who destroys and against Lizard who bursts,ties with himself, and loses to Rock Covering Paper and Spock that vaporizes the stone. - Role: Win against Stone who he covers and against Spock who refutes, tie with himself, and loses against Scissors who cut it and against Lizard who eat. - Scissors: Win against Paper who cuts and against Lizard who decapitates, tie with himself,...
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command...
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the...
programing language JAVA: Design and implement an application that reads a sentence from the user, then...
programing language JAVA: Design and implement an application that reads a sentence from the user, then counts all the vowels(a, e, i, o, u) in the entire sentence, and prints the number of vowels in the sentence. vowels may be upercase
This code is an expression of cp command in c language. But I don't understand this...
This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one.(in detail) #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<errno.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc, char *argv[]) { char ch; int src, dst; struct stat st; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } if (stat(argv[1], &st) == -1) { perror("stat : "); exit(-1); } src = open(argv[1], O_RDONLY); if(src...
In C programming using C11 library, how can we validate command line arguments? meaning for example...
In C programming using C11 library, how can we validate command line arguments? meaning for example if we input "./math 65 45 2.3 1 0 68" into the terminal, how can we validate that the numbers "65 45 2.3 1 0 68" are floats and not chars? In other terms the program should display and error message if the command line arguments are not floats and if they are floats, the program carries on as usual.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT