Question

In: Computer Science

I have written code in C programming that checks where the command line arguments are floats...

I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0 0 2.5 3" I want to add two flags. the first flag being "-f" which tells the code what kind of calculation you want it to do with those 6 numbers before it and a second flag which can either be "-r" for radians or "-d" for degrees. The question is how do I scan if those two flags using code? So while my code checks if the 6 numbers are floats, I do not know how to check for those two flags... an example of the command line argument is "./math 1 1 0 0 2.5 3 -f -d"

Solutions

Expert Solution

Hello, you need to take all arguments as string i.e char array, and first check for the flag which starts with '-' symbol.

Then for operands check if it not start with '-', then convert it according to math calculation type i.e float/ integer.

Here is the example how you do this:




Here is the text code:

#include <stdio.h>
#include <stdlib.h>

/* declared sum constant */
const int UNKNOWN_TYPE = -1;
const int DEGREE_TYPE = 0;
const int RADIAN_TYPE = 1;
const int CALC_FLOAT = 0;
const int CALC_INT = 1;

int main(int argc, char *argv[])
{
/* read and store from command line argument */
int degreeOrRadian = UNKNOWN_TYPE;
int calcType = UNKNOWN_TYPE;

/* Loop through all arguments */
for(int i=1; i<argc; i++)
{
/* Get the argument */
char *arg = argv[i];
/* Check if it is flag */
if(arg[0] == '-')
{
/* store the flag in variable */
switch(arg[1])
{
case 'f':
calcType = CALC_FLOAT;
break;

case 'i':
calcType = CALC_INT;
break;

case 'd':
degreeOrRadian = DEGREE_TYPE;
break;

case 'r':
degreeOrRadian = RADIAN_TYPE;
break;

default:
printf("Unknown flag: %s\n", arg);
}
}
}
/* If -d flag is passed, it is degree */
/* else if -r passed, it is radian */
if(degreeOrRadian == DEGREE_TYPE)
{
printf("Degree Calculation\n");
}
else if(degreeOrRadian == RADIAN_TYPE)
{
printf("Radian Calculation\n");
}

/* check for -f flag or any flag you need */
if(calcType == CALC_FLOAT)
{
printf("Calculation type: Float\n");
float sum = 0;
/*Loop through argument and check if it is not flag */
/* then it must be operands */
for(int i=0; i<argc; i++)
{
if(argv[i][0] != '-')
{
/* convert argument to float/double */
sum += atof(argv[i]);
}
}

printf("Calculate result: %.4f",sum);

}
else if(calcType == CALC_INT)
{
printf("Calculation type: Integer\n");

int sum = 0;
for(int i=0; i<argc; i++)
{
if(argv[i][0] != '-')
{
/* Convert argument to integer */
sum += atoi(argv[i]);
}
}

printf("Calculate result: %d",sum);

}
else
{
printf("Unknown calculation type\n");
}


return 0;
}

Output


Related Solutions

How do I add additional command line arguments in C++? I am working on a programming...
How do I add additional command line arguments in C++? I am working on a programming assignment that has the user input a file into the command line and then they have the option to also add a series of other arguments to the command line. I know how to accept the text file from the command line by using: int main(int argc, char *argv[]) { /.../ } Then filename(argv[1]) would be the text file that they put into the...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
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...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal...
write the following C program that ccepts command-line arguments following argv[0] — up to 20 decimal integer literals. Each of these decimal integer literals will be expressed using decimal digits, only, and none of the decimal integer literals will be prefixed by a plus or minus sign. None of the decimal integer literals will be greater than ULONG_MAX. The program prints the integers in order, least-to-greates and also prints the sum of the integers.
I want this code to be written in c++. Take a string of length n as...
I want this code to be written in c++. Take a string of length n as an input from the user such that n>=8 and only lower-case letters (a-z) are allowed as valid string characters. Deleting a letter from the string removes all the occurrences of that letter. The objective is to find the longest possible string such that it is left with only two unique letters and no two consecutive characters in a string are the same. If there...
THIS IS FOR ARDUINO PROGRAMMING Write code that checks the sensor data and meets the following...
THIS IS FOR ARDUINO PROGRAMMING Write code that checks the sensor data and meets the following conditions: For night conditions, turn on white LED 1 For day conditions, turn off white LED 1 Code should check intervals every 5 seconds. NEED THE BELOW CODE TO FIT THE ABOVE REQUIREMENTS. const int lightSensor = 5; //light sensor variable float sensValue = 0; //variable to hold sensor readings const int button = 3; //pin for button reads const int LED1 = 5;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT