📜  使用数字、+ 和 – 计算数组表达式

📅  最后修改于: 2022-05-13 01:57:07.539000             🧑  作者: Mango

使用数字、+ 和 – 计算数组表达式

给定一个字符串类型的数组 arr[],它由字符串“+”、“-”和数字组成。找到给定数组的总和。
例子 :

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

方法 :
1)首先,初始化sum,即sum = 0。
2)开始遍历数组。
3)由于数组的每个偶数位置都有字符串数字,因此将这个字符串转换为整数并使用C++中的stoi函数存储在一个变量中。
4)由于每个奇数位置都有运算符,检查运算符是'+'还是'-'。如果是“+”,则将该添加到总和中,否则从总和中减去。
5)最后,返回得到的总和。
以下是上述方法的实现:

C++
// C++ program to find sum of given array of
// string type in integer form
#include 
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


Javascript


输出 :

13 

时间复杂度: O(n)