Giter Site home page Giter Site logo

danieldotwav / math-expression-interpretter Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 42 KB

A simple calculator that performs basic arithmetic operations such as addition, subtraction, multiplication, division, and modulo. It reads expressions from the standard input (stdin) or the command line (in the case of text files), processes each expression, and outputs the result to the standard output (stdout).

C 100.00%
c calculator file-processing standard-input-output

math-expression-interpretter's Introduction

Simple Calculator Program in C

This C program acts as a simple calculator that performs basic arithmetic operations such as addition, subtraction, multiplication, division, and modulo. It reads expressions from the standard input (stdin) or the command line (in the case of text files), processes each expression, and outputs the result to the standard output (stdout).

Features

  • Arithmetic Operations: Supports addition (+), subtraction (-), multiplication (*), division (/), and modulo (%) operations.
  • Error Handling: Provides feedback for common input errors, including missing operands, invalid operators, and attempts to use negative numbers.
  • Interactive: Continuously reads and evaluates expressions from the standard input until an EOF (End-of-File) signal is received.

Components

  • Main Function: Orchestrates the reading of input, identification of operands and operators, and the execution of the specified operation.
  • Helper Functions:
    • isDigit(int ch): Checks if a character is a digit.
    • isSpace(int ch): Checks if a character is a whitespace character (space or tab).
    • getOperatorType(int ch): Determines the type of operator based on the input character.
  • Operation Functions: Perform the actual arithmetic operations. These include:
    • add(int left_operand, int right_operand): Returns the sum of two integers.
    • subtract(int left_operand, int right_operand): Returns the difference between two integers.
    • multiply(int left_operand, int right_operand): Returns the product of two integers.
    • divide(int left_operand, int right_operand): Returns the quotient of two integers as a float.
    • modulo(int left_operand, int right_operand): Returns the remainder of the division of two integers.
  • Wrapper Function:
    • performSpecifiedOperation(int left_operand, int right_operand, char operator_symbol): Executes the appropriate arithmetic operation based on the provided operator symbol and operands.

Error Handling

The program now includes advanced error handling mechanisms for:

  • Invalid expressions (missing operands or operators).
  • Unsupported operators.
  • Negative numbers as operands.
  • Division by zero.
  • Extraneous characters after the right operand.

Limitations

  • Negative Numbers: The current implementation does not support negative numbers as operands. Negative numbers may be printed as results, however (e.g., 5 - 10 = -5).
  • Floating-Point Numbers: It does not support floating-point numbers; all operands are treated as integers except for the division operation, which returns a floating-point result.
  • Input Format: Each expression must be in the form of operand1 operator operand2 (e.g., 10 + 20), with each component separated by whitespace.

math-expression-interpretter's People

Contributors

danieldotwav avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.