How to write a C Program to Calculate 2 numbers with 1 operation in C Programming Language ?
Reverse Polish Notation - C Program to Calculate 2 numbers with 1 operation.
Solution:
- #include<stdio.h>
- //calculate 2 numbers with 1 operation
- double calc (char operator, double a, double b) {
- double result = 0;
- switch (operator) {
- case '+':
- result = a + b;
- break;
- case '-':
- result = a - b;
- break;
- case '*':
- result = a * b;
- break;
- case '/':
- result = a / b;
- break;
- }
- printf("[DEBUG] %lf %lf %c = %lf\n", a, b, operator, result);
- return result;
- }
- int main (void) {
- //declare new char array in c style
- char term[256] = {0};
- //declare variable for result
- double result = 0;
- printf("Please insert your calculation: \n");
- //read term from input stream stdin
- fgets(term, 255, stdin);
- printf("Your input: %s\n", term);
- //declare variables for numbers between operation
- double a = 0;
- double b = 0;
- int k = 0;
- char lastNumber[256] = {0};
- int numberCounter = 0;
- int lineSpaceCounter = 0;
- int lastCharType = 0; //1 - number, 2 - operator
- //parse string
- for (int i = 0; i < 256; i++) {
- //check if char is line break
- if (term[i] == '\n') {
- break;
- }
- //converts '0' to 0, '1' to 1
- double number = term[i] - '0';
- if (number >= 0 && number <= 9) {
- //its an number, add number to term
- lastNumber[k] = term[i];
- //increment counter
- k++;
- lineSpaceCounter = 0;
- lastCharType = 1;
- } else if (term[i] == ' ') {
- //its an line space, so save last number
- //convert char array to integer
- double n = 0;
- sscanf(lastNumber, "%lf", &n);
- printf("[DEBUG] %s %lf\n", lastNumber, n);
- //save last number into a or b
- if (numberCounter == 0) {
- //save lastNumber into a
- a = n;
- } else {
- //save lastNumber into b
- b = n;
- }
- //increment line space counter, break on 2 line spaces too
- lineSpaceCounter++;
- numberCounter++;
- //because an number size be more than 1 char, we need this counter variable
- k = 0;
- } else if (term[i] == '+' || term[i] == '-' || term[i] == '*' || term[i] == '/') {
- //its an operator, so calculate sub term
- result = calc(term[i], a, b);
- a = result;
- numberCounter = 1;
- k = 0;
- lineSpaceCounter = 0;
- lastCharType = 2;
- }
- }
- //print result
- printf("Result: %lf\n", result);
- return 0;
- }
- /*
- * Copyright (c) 2015 Justin Kuenzel, All Rights reserved.
- *
- * MIT License, https://opensource.org/licenses/MIT
- */
Learn More :
Calculate
- C Programm zur Berechnung von Umfang und Flächeninhalt verschiedener geomatrischer Figuren
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program to Calculate GCD using recursion
- C Program To Calculate First Numbers Using Recursion
- C Program to Calculation of One Number Raised to Another
- C Program to calculate TT and Recharge Amount
- Un-sortiertes Array and Sortiertes Array
- C Program Calculation of Simple Interest
- C Program to Calculation of Total and Percentage
- C Program for calculation of Gross Salary
- C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle.
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program for statistically calculating pi using a specific scheduling policy.
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program Number of Judge and Score From Each Judge
- C Program to Calculate the mathematical expression for the first n numbers
- C Program to Calculate the price of movie tickets
- C Program to calculate sum of two m*n matrices & store the result in 3 matrix
- C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA
- C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers
- Calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation
- Calculate sum of non-diagonal element in m*n matrix C Program
- Accept n number from user, store these number into an array and calculate the average of n number
- Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation
Operations
Number
- Find out the perfect number using c program
- Write a c program to find out H.C.F. of two numbers.
- Check the given number is armstrong number or not using c program.
- Write a c program to find largest among three numbers using conditional operator
- FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.
- FIND PRIME FACTORS OF A NUMBER USING C PROGRAM
- How To Write a C program that generates two random numbers ?
- Write a C program to find maximum or equal between two numbers ?
- How to Write a C program to find maximum between two numbers ?
- Write a C program to perform math operations on two input whole numbers. The operations are:
- Write a C program to find maximum between three numbers ?
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program to Enter an ODD number between 1 and 49.
- C program acquires keyboard 10 numbers for each of these numbers to determine if it is a first issue, by printing immediately message should at the end, if none of the numbers you entered was a first issue, print an appropriate message.
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Reverse Of Any Digit Number
- C Program To Find The Frequency Of A Number
- C Program To Print Prime Numbers Upto The Number You Want
- C Program To Print Sum Of n Digit Number
- C Program To Reverse A Number
- C Program To Search A Number Inside The Array
Reverse Polish Notation