Question

In: Computer Science

please write the code in C not c++, and not to use Atoi or parseint to...

please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you.


#include <stdio.h>

#include <stdbool.h>

/*

* The isinteger() function examines the string given as its first

* argument, and returns true if and only if the string represents a

* well-formed integer. A well-formed integer consists only of an

* optional leading - followed by one or more decimal digits.

* Returns true if the given string represents an integer, false

* otherwise.

*/

bool isinteger(char *str);

/*

* The parseint() function parses a well-formed string representation of

* an integer (one which would return true from isinteger()) and returns

* the integer value represented by the string. For example, the string

* "1234" would return the value 1234. This function does not need to

* handle badly-formed strings in any particular fashion, its operation

* on badly-formed strings is undefined.

* Returns the integer value stored in the given string.

*/

int parseint(char *str);

/*

* The main function is where C programs begin.

* This function parses its arguments and returns the value they

* represent. Its arguments are either:

* - A single argument representing an integer

* - Three arguments, where the first and third are integers and the

* second is an operator to be performed on those integers

* Remember that the argument in argv[0] is the name of the program, so

* a program passed exactly one argument on the command line will

* receive _two_ arguments: its name in argv[0] and the provided

* argument in argv[1].

* Arguments:

* argc - The number of arguments received

* argv - The arguments received as an array of C strings

*

* Returns 0 if the arguments are well-formed and the calculation could

* be performed, non-zero otherwise.

*/

int main(int argc, char *argv[]) {

/* Your main program logic should go here, with helper logic in the

* functions isinteger() and parseint(), which you can place below

* the closing brace of main() */

return 0;

}

Solutions

Expert Solution

// do comment if any problem arises

//code

#include <stdio.h>

#include <stdbool.h>

//this function returns length of string

int length(char* str)

{

int i=0;

while(str[i]!='\0')

{

i++;

}

return i;

}

/*

* The isinteger() function examines the string given as its first

* argument, and returns true if and only if the string represents a

* well-formed integer. A well-formed integer consists only of an

* optional leading - followed by one or more decimal digits.

* Returns true if the given string represents an integer, false

* otherwise.

*/

bool isinteger(char *str)

{

int i=0;

while(str[i]!='\0')

{

//if any character is not between 0 to 9 then return false

if(!(str[i]>='0'&&str[i]<='9'))

return false;

i++;

}

return true;

}

/*

* The parseint() function parses a well-formed string representation of

* an integer (one which would return true from isinteger()) and returns

* the integer value represented by the string. For example, the string

* "1234" would return the value 1234. This function does not need to

* handle badly-formed strings in any particular fashion, its operation

* on badly-formed strings is undefined.

* Returns the integer value stored in the given string.

*/

int parseint(char *str)

{

int l=length(str);

int number=0;

int i=0;

while(str[i]!='\0')

{

int current=str[i]-'0';

number*=10;

number+=current;

i++;

}

return number;

}

/*

* The main function is where C programs begin.

* This function parses its arguments and returns the value they

* represent. Its arguments are either:

* - A single argument representing an integer

* - Three arguments, where the first and third are integers and the

* second is an operator to be performed on those integers

* Remember that the argument in argv[0] is the name of the program, so

* a program passed exactly one argument on the command line will

* receive _two_ arguments: its name in argv[0] and the provided

* argument in argv[1].

* Arguments:

* argc - The number of arguments received

* argv - The arguments received as an array of C strings

*

* Returns 0 if the arguments are well-formed and the calculation could

* be performed, non-zero otherwise.

*/

int main(int argc, char *argv[])

{

/* Your main program logic should go here, with helper logic in the

* functions isinteger() and parseint(), which you can place below

* the closing brace of main() */

if (argc == 2 && isinteger(argv[1]))

{

printf("%d", parseint(argv[1]));

return 0;

}

else if (argc == 4 && isinteger(argv[1]) && isinteger(argv[3]))

{

int first=parseint(argv[1]);

int second=parseint(argv[3]);

switch (argv[2][0])

{

case '*':

printf("%d",first*second);

break;

case '+':

printf("%d",first+second);

break;

case '-':

printf("%d",first-second);

break;

case '/':

printf("%d",first/second);

break;

default:

break;

}

return 0;

}

return -1;

}

Output:


Related Solutions

Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h and .cpp files and add tester program and screenshot of output! -please complete all parts I will upvote thank you!!! Define the following classes to manage the booking of patients in a medical clinic. a) Define a class Date that has the following integer data members: month, day and year. b) Define a class AppointmentTime that has the following data members: day (string), hour...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT