Question

In: Computer Science

Complete these steps to write a simple calculator program: 1. Create a calc project directory 2....

Complete these steps to write a simple calculator program:

1. Create a calc project directory

2. Create a mcalc.c source file containing five functions; add, sub, mul, div, and mod; that implement integer addition, subtraction, multiplication, division, and modulo (remainder of division) respectively. Each function should take two integer arguments and return an integer result.

3. Create a mcalc.h header file that contains function prototypes for each of the five functions implemented in mcalc.c.

4. Create a calc.c source file with a single main function that implements a simple calculator program with the following behavior:

-> Accepts a line of input on stdin of the form ”number operator number” where number is a signed decimal integer, and operator is one of the characters ”+”, ”-”, ”*”, ”/”, or ”%” that corresponds to the integer addition, subtraction, multiplication, division, and modulo operation respectively.

-> Performs the corresponding operation on the two input numbers using the five mcalc.c functions.

-> Displays the signed decimal integer result on a separate line and loops to accept another line of input.

-> Or, for any invalid input, immediately terminates the program with exit status 0.

5. Create a Makefile using the following Makefile below as a starting point. Modify the Makefile so that it builds the executable calc as the default target. The Makefile should also properly represent the dependencies of calc.o and mcalc.o.

CC = gcc
CFLAGS = -g -O2 -Wall -Werror
ASFLAGS = -g

objects = calc.o mcalc.o

default: calc

.PHONY: default clean clobber

calc: $(objects)
   $(CC) -g -o $@ $^

calc.o: calc.c mcalc.h


%.o: %.c
   $(CC) -c $(CFLAGS) -o $@ $<

%.o: %.s
   $(CC) -c $(ASFLAGS) -o $@ $<

clean:
   rm -f $(objects)

clobber: clean
   rm -f calc

6. Compile the calc program by executing ”make” and verify that the output is proper. For example:

# ./calc

3 + 5

8

6 * 7

42

Solutions

Expert Solution

If you have any doubts, please give me comment...

mcalc.h

int add(int n1, int n2);

int sub(int n1, int n2);

int mul(int n1, int n2);

int div(int n1, int n2);

int mod(int n1, int n2);

mcalc.c

#include "mcalc.h"

int add(int n1, int n2){

    return n1+n2;

}

int sub(int n1, int n2){

    return n1-n2;

}

int mul(int n1, int n2){

    return n1*n2;

}

int div(int n1, int n2){

    return n1/n2;

}

int mod(int n1, int n2){

    return n1%n2;

}

calc.c

#include<stdio.h>

#include "mcalc.h"

int main(){

    int n1, n2;

    char op;

    while(scanf("%d %c %d", &n1, &op, &n2)==3){

            if(op=='+')

                printf("%d\n", add(n1, n2));

            else if(op=='-')

                printf("%d\n", sub(n1, n2));

            else if(op=='*')

                printf("%d\n", mul(n1, n2));

            else if(op=='/')

                printf("%d\n", div(n1, n2));

            else if(op=='%')

                printf("%d\n", mod(n1, n2));

            else{

                printf("Invalid operator");

                break;

            }

    }

    return 0;

}


Related Solutions

For Project 2, write a program for MIPS that performs the following steps. It 1. Receives...
For Project 2, write a program for MIPS that performs the following steps. It 1. Receives a string of upper- and/or lowercase letters from keyboard (user types the letters on the SPIM console window). 2. Stores ASCII codes of the letters in stack. 3. Stop receiving letters when ? is typed by the user. 4. Converts uppercase letters to their lowercase letters and vice versa, and prints the converted string on the SPIM console window. Assume only valid inputs are...
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
In this project you will create a basic console based calculator program. The calculator can operate...
In this project you will create a basic console based calculator program. The calculator can operate in two modes: Standard and Scientific modes. The Standard mode will allow the user to perform the following operations: (+, -, *, /) add, subtract, multiply, and divide The Scientific mode will allow the user to perform the same functionality as the Standard add, subtract, multiply, and divide (+, -, *, / ) plus the following: sin x, cos x, tan x. (sin x,...
Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from...
Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from the command line: $ java CmdLineCalc 1 + 2 3.0 $ java CmdLineCalc 1 - 2.5 -1.5 $ java CmdLineCalc 3 + 4 - 5 2.0 $ java CmdLineCalc 6.5 - 7 + 8 7.5 To keep it simple, your program only needs to support addition (+) and subtraction (-). You may assume that, starting with the first argument, every other argument will be...
Create a program using python that provides a simple calculator: Requires a login with a prompt...
Create a program using python that provides a simple calculator: Requires a login with a prompt for a username and a password prior to using the calculator If username and password are incorrect, the program should re-prompt to re-enter correct username and password Once logged in, the user should have access to the calculator and should have the ability for the following mathematical operators: Addition Subtraction Multiplication Division Square Root PI Exponents
For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that...
Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that a player must traverse to win. 3. The maze must be text-based and adjustable from 5x5 to 20x20. • Player gets to choose size either as: 1) any size in the range from 5-20 by 5-20. 2) selects from 4 set size options [5x5,10x10,15x15, and 20x20] • the player can choose among different mazes. • You will need to figure out how to denote...
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
PYTHON PROGRAM Requirements: 1. Create an empty directory a variable named song_search 2. Repeatedly prompt the...
PYTHON PROGRAM Requirements: 1. Create an empty directory a variable named song_search 2. Repeatedly prompt the user to enter either the title of a song or enter nothing if they have no more songs to enter 3. For each song title entered by the user, split the song title into words by using the split function and splitting on a space. For each word from each song title, ensure the song_search dictionary has a entry with that word as the...
Java For this project, we want to build a calculator program that can perform simple calculations...
Java For this project, we want to build a calculator program that can perform simple calculations and provide an output. If it encounters any errors, it should print a helpful error message giving the nature of the error. You may use any combination of If-Then statements and Try-Catch statements to detect and handle errors. Program Specification Input Our program should accept input in one of two ways: If a command-line argument is provided, it should read input from the file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT