Sample codes for Lab03 A. The following example shows the usage of strtod() function. This function converts string to double. Example code: #include #include int main () { char str[30] = "...

i have a C++ homework isnt not long but i need it todayi attached all the files you need also my own codes


Sample codes for Lab03 A. The following example shows the usage of strtod() function. This function converts string to double. Example code: #include #include int main () { char str[30] = "20.30300 This is test"; char *ptr; double ret; ret = strtod(str, &ptr); printf("The number(double) is %lf\n", ret); printf("String part is |%s|", ptr); return(0); } Use these codes in your solution: char str[30] = "”; char *ptr; double ret; ret = strtod(str, &ptr); B. Use the following to read a string from the keyboard/file that ends with a new line: const int STRINGSIZE = 20; char task[STRINGSIZE]; scanf("%[^\n]%*c", task); C. Use strcmp(char[] str1, char[] str2) library method to compare the input string and required operators like “+”, “-“, “*”, “/” to find out which operation to execute. Also be advised that if str1 and str2 are equal only then strcmp will return 0. const int STRINGSIZE = 20; char task[STRINGSIZE]; scanf("%[^\n]%*c", task); if(strcmp(task,"+") == 0){ // tasks to complete // Make sure that you have at least 2 values stored on // the stack (array). If you have less than 2 then print //the error message: // printf(“Not enough operands available.\n”); } D. Create a double type of array of size 10 to use as a stack. All your input values will be pushed on this stack (simply adding to the array). The lowest index will point towards the bottom of the stack and the highest index will point towards the top of the stack. The new entries will always be on the top of the stack. Every time you add a new value to the stack, make sure to increase the stack index by 1. The stack index will have maximum value = 9 for this case (STACKSIZE -1). When you read from the stack (essentially the array) read from the top of the stack (end of the array). The stack index will give you the location of the end of the array (top of the stack). Also, after reading the value from the stack, that array cell needs to cleared up and the stack index needs to be decremented by 1. Your Name: CSCI2025L – Systems Programming Lab3 1 A calculator is a commonly provided system utility. Write a calculator program in C that will process inputs that are either numbers or operators (1 per line). Where number is a double precision floating point number and operator is one of these characters: + - * / p or e. Numbers should be stored in a 10 element array. When an operator is entered, the last 2 operands in the array are removed and replaced by the result. The p operator tells the program to print the currently stored numbers. The e operator tells the program to print the final accumulator value and quit. The current value of the last element in the array should be printed after each operation. Any other operator should print a suitable error message. See the back of this lab sheet for an example output of this program. Run at least the commands on the back of this lab sheet on your program and screenshot the results. Lab3.c (source file) 2 Copy your program from step 4 into a new file called Lab3b.c. If you implemented the operator decision logic in your program with if statements, convert it to use a switch statement. If you implemented the operator decision logic in your program with a switch statement, convert it to use if statements. Run the tests from step 3 to verify that the new implementation works (and screenshot the results). Lab3b.c (source file) Example of the inputs and outputs expected for your calculator program Begin Calculations Enter a number or an operator (or e to end): 2.5 = 2.500000 Enter a number or an operator (or e to end): 5.6 = 5.600000 Enter a number or an operator (or e to end): + = 8.100000 Enter a number or an operator (or e to end): 2.9 = 2.900000 Enter a number or an operator (or e to end): - = 5.200000 Enter a number or an operator (or e to end): 2 = 2.000000 Enter a number or an operator (or e to end): * = 10.400000 Enter a number or an operator (or e to end): 3 = 3.000000 Enter a number or an operator (or e to end): / = 3.466667 Enter a number or an operator (or e to end): + Not enough operands for this operation = 3.466667 Enter a number or an operator (or e to end): 1 = 1.000000 Enter a number or an operator (or e to end): 2 = 2.000000 Enter a number or an operator (or e to end): 3 = 3.000000 Enter a number or an operator (or e to end): 4 = 4.000000 Enter a number or an operator (or e to end): 5 = 5.000000 Enter a number or an operator (or e to end): 6 = 6.000000 Enter a number or an operator (or e to end): 7 = 7.000000 Enter a number or an operator (or e to end): 8 = 8.000000 Enter a number or an operator (or e to end): 9 = 9.000000 Enter a number or an operator (or e to end): 10 Stack Full Error - input ignored = 9.000000 Enter a number or an operator (or e to end): p Current stack contents (from top to bottom) [9] = [9.000000] [8] = [8.000000] [7] = [7.000000] [6] = [6.000000] [5] = [5.000000] [4] = [4.000000] [3] = [3.000000] [2] = [2.000000] [1] = [1.000000] [0] = [3.466667] = 9.000000 Enter a number or an operator (or e to end): + = 17.000000 Enter a number or an operator (or e to end): + = 24.000000 Enter a number or an operator (or e to end): + = 30.000000 Enter a number or an operator (or e to end): + = 35.000000 Enter a number or an operator (or e to end): + = 39.000000 Enter a number or an operator (or e to end): + = 42.000000 Enter a number or an operator (or e to end): + = 44.000000 Enter a number or an operator (or e to end): + = 45.000000 Enter a number or an operator (or e to end): + = 48.466667 Enter a number or an operator (or e to end): + Not enough operands for this operation = 48.466667 Enter a number or an operator (or e to end): e = 48.466667 Calculations concluded. Note1: the characters printed in bold above are typed in by the calculator user (you). The non-bolded parts are typed by your calculator program. Note2: In this program, you may find a use for the library function atof. This function takes one argument (a string) and returns a double (assuming the string contains a valid floating-point number). It is defined in the include file . #include #include #include int main () { char *ptr; double ret; int count= 0 ; const int STRINGSIZE = 10; double task[STRINGSIZE]; while(true){ printf("Enter a number or an operator (or e to end): "); scanf("%[^\n]%*c", task); ret = strtod(task, &ptr); printf("The number(double) is %lf\n", ret); printf("String part is |%s|\n", ptr); if(strcmp(task,"+") == 0){ if(task[count-1] == 0 ){ task[count+1]= task[count] + task[count]; printf("= ",task[count+1]); count++; } else task[count]+task[count-1]; printf("= ",task[count+1]); count++; } /*else if(strcmp(task,"*") == 0){ } else if(strcmp(task,"-") == 0){ } else if(strcmp(task,"/") == 0){ } else if(strcmp(task,"p") == 0){ } else if(strcmp(task,"e") == 0){ return 0; } */ } }
Sep 05, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here