Evaluate an array expression with numbers, + and -
Last Updated :
19 Sep, 2023
Given an array arr[] of string type which consists of strings "+", "-" and Numbers. Find the sum of the given array.
Examples :
Input : arr[] = {"3", "+", "4", "-", "7", "+", "13"}
Output : Value = 13
The value of expression 3+4-7+13 is 13.
Input : arr[] = { "2", "+", "1", "-8", "+", "13"}
Output : Value = 8
Approach :
- First of all, initialize the sum i.e, sum = 0.
- Start traversing the array.
- As there is string of numbers at every even position of the array, so convert this string into integer and store in a variable value by using stoi function in C++.
- As there is operator at every odd position, check if the operator is '+' or '-'. If it is '+', then add the value to the sum, else subtract from the sum.
- Finally, return the sum obtained.
Below is the implementation of the above approach :
C++
// C++ program to find sum of given array of
// string type in integer form
#include <bits/stdc++.h>
using namespace std;
// Function to find the sum of given array
int calculateSum(string arr[], int n)
{
// if string is empty
if (n == 0)
return 0;
string s = arr[0];
// stoi function to convert
// string into integer
int value = stoi(s);
int sum = value;
for (int i = 2; i < n; i = i + 2)
{
s = arr[i];
// stoi function to convert
// string into integer
int value = stoi(s);
// Find operator
char operation = arr[i - 1][0];
// If operator is equal to '+',
// add value in sum variable
// else subtract
if (operation == '+')
sum += value;
else
sum -= value;
}
return sum;
}
// Driver Function
int main()
{
string arr[] = { "3", "+", "4", "-",
"7", "+", "13" };
int n = sizeof(arr) / sizeof(arr[0]);
cout << calculateSum(arr, n);
return 0;
}
Java
// Java program to find sum of given array of
// string type in integer form
import java.io.*;
class GFG {
// Function to find the sum of given array
public static int calculateSum(String arr[], int n)
{
// if string is empty
if (n == 0)
return 0;
String s = arr[0];
// parseInt function to convert
// string into integer
int value = Integer.parseInt(s);
int sum = value;
for (int i = 2; i < n; i = i + 2)
{
s = arr[i];
// parseInt function to convert
// string into integer
value = Integer.parseInt(s);
// Find operator
char operation = arr[i - 1].charAt(0);
// If operator is equal to '+',
// add value in sum variable
// else subtract
if (operation == '+')
sum += value;
else
sum -= value;
}
return sum;
}
// Driver code
public static void main (String[] args)
{
String arr[] = { "3", "+", "4", "-",
"7", "+", "13" };
int n = arr.length;
System.out.println( calculateSum(arr, n));
}
}
// This code in contributed by Upendra bartwal
Python 3
# Python3 program to find sum of given
# array of string type in integer form
# Function to find the sum of given array
def calculateSum(arr, n):
# if string is empty
if (n == 0):
return 0
s = arr[0]
# stoi function to convert
# string into integer
value = int(s)
sum = value
for i in range(2 , n, 2):
s = arr[i]
# stoi function to convert
# string into integer
value = int(s)
# Find operator
operation = arr[i - 1][0]
# If operator is equal to '+',
# add value in sum variable
# else subtract
if (operation == '+'):
sum += value
else:
sum -= value
return sum
# Driver Function
arr = ["3", "+", "4", "-","7", "+", "13"]
n = len(arr)
print(calculateSum(arr, n))
# This code is contributed by Smitha
C#
// C# program to find sum of given array of
// string type in integer form
using System;
class GFG {
// Function to find the sum of given array
public static int calculateSum(string []arr,
int n)
{
// if string is empty
if (n == 0)
return 0;
string s = arr[0];
// parseInt function to convert
// string into integer
int value = int.Parse(s);
int sum = value;
for (int i = 2; i < n; i = i + 2)
{
s = arr[i];
// parseInt function to convert
// string into integer
value = int.Parse(s);
// Find operator
char operation = arr[i - 1][0];
// If operator is equal to '+',
// add value in sum variable
// else subtract
if (operation == '+')
sum += value;
else
sum -= value;
}
return sum;
}
// Driver code
public static void Main ()
{
string []arr = { "3", "+", "4", "-",
"7", "+", "13" };
int n = arr.Length;
Console.Write(calculateSum(arr, n));
}
}
// This code in contributed by nitin mittal.
PHP
<?php
// php program to find sum of given
// array of string type in integer form
// Function to find the
// sum of given array
function calculateSum($arr,$n)
{
// if string is empty
if ($n == 0)
return 0;
$s = $arr[0];
// stoi function to convert
// string into integer
$value = (int)$s;
$sum = $value;
for ($i = 2; $i < $n; $i = $i + 2)
{
$s = $arr[$i];
// cast to convert
// string into integer
$value = (int)$s;
// Find operator
$operation = $arr[$i - 1];
// If operator is equal to '+',
// add value in sum variable
// else subtract
if ($operation == '+')
$sum += $value;
else if ($operation == '-')
$sum -= $value;
}
return $sum;
}
// Driver code
$arr = array("3", "+", "4", "-",
"7", "+", "13" );
$n = sizeof($arr) / sizeof($arr[0]);
echo calculateSum($arr, $n);
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript program to find sum of given array of
// string type in integer form
// Function to find the sum of given array
function calculateSum(arr, n)
{
// if string is empty
if (n == 0)
return 0;
let s = arr[0];
// parseInt function to convert
// string into integer
let value = parseInt(s);
let sum = value;
for (let i = 2; i < n; i = i + 2)
{
s = arr[i];
// parseInt function to convert
// string into integer
value = parseInt(s);
// Find operator
let operation = arr[i - 1][0];
// If operator is equal to '+',
// add value in sum variable
// else subtract
if (operation == '+')
sum += value;
else
sum -= value;
}
return sum;
}
let arr = [ "3", "+", "4", "-", "7", "+", "13" ];
let n = arr.length;
document.write(calculateSum(arr, n));
// This code is contributed by vaibhavrabadiya117.
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Program to evaluate simple expressions You are given a string that represent an expression of digits and operands. E.g. 1+2*3, 1-2+4. You need to evaluate the string or the expression. NO BODMAS is followed. If the expression is of incorrect syntax return -1. Test cases: a) 1+2*3 will be evaluated to 9. b) 4-2+6*3 will be evaluated to 24
11 min read
Print all the possible arithmetic expressions for a given number Given an integer N, the task is to print all the possible arithmetic expressions using all numbers from 1 to N and with binary operator +, â, * and /.Examples: Input: n = 2 Output: 1+2, 1-2, 1/2, 1*2Input: n = 3 Output: 1+2+3, 1+2-3, 1+2/3, 1+2*3, 1-2+3, 1-2-3, 1-2/3, 1-2*3 1/2+3, 1/2-3, 1/2/3, 1/2*
5 min read
Evaluation of Postfix Expression Given a postfix expression, the task is to evaluate the postfix expression. A Postfix expression is of the form "a b operator" ("a b +") i.e., a pair of operands is followed by an operator.Examples:Input: arr = ["2", "3", "1", "*", "+", "9", "-"]Output: -4Explanation: If the expression is converted
6 min read
Arithmetic Expression Evaluation The stack organization is very effective in evaluating arithmetic expressions. Expressions are usually represented in what is known as Infix notation, in which each operator is written between two operands (i.e., A + B). With this notation, we must distinguish between ( A + B )*C and A + ( B * C ) b
2 min read
Maximizing array sum with given operation There is an array consisting of (2 * n - 1) integers. We can change sign of exactly n elements in the array. In other words, we can select exactly n array elements, and multiply each of them by -1. Find the maximum sum of the array. Examples : Input : arr[] = 50 50 50 Output : 150 There is no need t
6 min read
Find ratio of zeroes, positive numbers and negative numbers in the Array Given an array a of integers of size N integers, the task is to find the ratio of positive numbers, negative numbers and zeros in the array up to four decimal places. Examples: Input: a[] = {2, -1, 5, 6, 0, -3} Output: 0.5000 0.3333 0.1667 There are 3 positive, 2 negative, and 1 zero. Their ratio wo
7 min read