Question

In: Computer Science

Write C functions to do each of the following. Make sure your function has exactly the...

Write C functions to do each of the following. Make sure your function has exactly the same name, parameters, and return type as specified here. Please place all of the functions in a single .c file and write a main() to test them.

  1. int process(const char *input_filename, const char *output_filename) — reads the file named input_filename and processes it, putting results in output_filename. The input file consists of lines of the form "number operation number," for example:

    15 - 4
    3.38 / 2.288
    2 ^ -5
    35.2 + 38

    The possible operations are +, -, *, /, and ^ (power). Your function should calculate the value of each line's expression and save it (one to a line) to the output file. For example, the input file shown above would result in an output file of

    11.000000
    1.477273
    0.031250
    73.200000

    The return value of the function should be the number of calculations that it did (4 for this example file).

    (Hints: fscanf() returns the constant value EOF when the end of file is reached. The pow(x, y) function defined in <math.h> calculates xy; you will need to add -lm to the command line when compiling your code to link the math library.)

Solutions

Expert Solution

#include<stdio.h>
#include<math.h>

//function to process input file
int process(const char *input_filename, const char *output_filename) {
  
   //declaring file pointers and opeing file in appropriate modes
   FILE *inp;
   inp = fopen(input_filename, "r");
  
   FILE *outp;
   outp = fopen(output_filename, "w");
   double op1 = 0, op2 = 0;
   char opr
  
   //reading the input file
   while(fscanf(inp, "%lf %c %lf", &op1, &opr, &op2) == 3) {
       double res;
      
       //doing operation as per the operator found
       switch(opr) {
           case '+':
               res = op1 + op2;
               break;
           case '-':
               res = op1 - op2;
               break;
           case '*':
               res = op1 * op2;
               break;
           case '/':
               res = op1 / op2;
               break;
           case '^':
               res = pow(op1, op2);
               break;
       }
       //writing the result onto the output file.
       fprintf(outp, "%lf\n", res);
   }
  
   fclose(inp);
   fclose(outp);
     
}

main() {
   process("input.txt", "output.txt");
}

Input.txt

15 - 4
3.38 / 2.288
2 ^ -5
35.2 + 38

output.txt

11.000000
1.477273
0.031250
73.200000


Related Solutions

Assignment Write each of the following functions. The function header must be implemented exactly as specified....
Assignment Write each of the following functions. The function header must be implemented exactly as specified. Write a main function that tests each of your functions. Specifics In the main function ask for a filename and fill a list with the values from the file. Each file should have one numeric value per line. This has been done numerous times in class. You can create the data file using a text editor or the example given in class – do...
C++ questions, Please make sure to divide your program into functions which perform each major task....
C++ questions, Please make sure to divide your program into functions which perform each major task. The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic: ROCK defeats SCISSORS (“smashes”) PAPER defeats ROCK (“wraps”) SCISSORS defeats PAPER (“slices”) If both players choose the same selection the game ends in a tie. Write a program that asks...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Implement each of the following functions and write a basic main() function that tests each. For...
Implement each of the following functions and write a basic main() function that tests each. For convenience, you should be able to find this starter class under Mimir's assignment 4 starter code. Do not change the name, parameters, or returns of any of these functions or of the name of the class itself. There is also no need in this assignment for any global variables. You are strongly encouraged to use your solution for some of these functions in others...
C++ questions, please make sure to dividet he program into functions which perform each major task,...
C++ questions, please make sure to dividet he program into functions which perform each major task, A leap year is defined as any calendar year which meets the following criteria: If the year is not divisible by 4, it is a common year If the year is not divisible by 100, it is a leap year If the year is not divisible by 400, it is a common year Otherwise it is a leap year For your program you will...
Write the following functions. Each function needs function comments that describes function and its parameters double...
Write the following functions. Each function needs function comments that describes function and its parameters double sphereVolume( double radius) double sphereSuface( double radius) double cylinderVolume( double radius, double height) double coneSurface( double radius, double height) double coneVolume( double radius, double height) That computes the volume and surface of the sphere with radius, a cylinder with a circular base with radius radius , and height height , and a cone with a circular base with radius radius , and height height...
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT