Question

In: Computer Science

Write a C++ function template to add two inputs and return their result. Make exceptions for...

Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)

Solutions

Expert Solution

In the given question, it is asked to write a C++ function template to

1) Add two numbers, if it is integer

2) Sum of ASCII values to represent a character, if both are Char

3)Concatenation, if it is a string.

The C++ code including main() and function template for the following question will looklike

#include <iostream>
#include<string>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;

bool isNumber(string s) // function to check if string is integer.
    { 
    for (int i = 0; i < s.length(); i++) 
        if (isdigit(s[i]) == false) 
            return false; 
    return true; 
    }
string getString(char x) 
{ 
    string s(1, x); 
    return s;    
} 

string templa(string inp1, string inp2)
{
    if (isNumber(inp1))
    {
        int num1,num2,num3;
        num1 = std::stoi(inp1);
        num2 = std::stoi(inp2);
        num3 = num1 + num2;
        std::string strnum = to_string(num3);
        cout<<"The sum is ";
        return strnum;
    }
    else
    {
        int len1,len2;
        len1=inp1.length();
        len2=inp2.length();
        if(len1==1 && len2==1)
        {
            int cnum1,cnum2,cnum3;
            char char_array1[1], char_array2[1],c1,c2;
            unsigned char c3;
            string ascich;
            strcpy(char_array1, inp1.c_str());
            strcpy(char_array2, inp2.c_str());
            c1 = char_array1[0];
            c2 = char_array2[0];
            cnum1 = int(c1);
            cnum2 = int(c2);
            cnum3 = cnum1 + cnum2;
            c3 = static_cast<char>(cnum3);
            cout<<"The ASCII value is "<<cnum3;
            ascich = getString(c3);
            cout<<" The string is ";
            return ascich;
        }
        else
        {
            string full;
            full = inp1 + inp2;
            cout<<"The Concatenation is ";
            return full;
        }
    }
}
int main()
{
    string inp1,inp2;
    string out3;
    cin>>inp1;
    cin>>inp2;
    out3=templa(inp1,inp2);
    cout<<out3;

    return 0;
}

Here templa() is the template function.

isNumber() is used to check if a string is number or not.

getString() is used to convert char to string.

Depending on the complier, the extended ASCII character may or may not be printed.

However, all the ASCII characters, as asked in the question will be printed.

Hope, this answer helps.


Related Solutions

Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers,...
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the result is the sum...
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
Write a simple function template for predicate function isEqualTo that compares its two arguments of the...
Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false if they are not equal. Use this function template in a program that calls isEqualTo on a variety of built-in types and user define types, Complex and Date (need to overload the equality operator (operator==) and extraction operator (operator<<)).  Write a program with variety of inputs to test the...
Write a simple function template for predicate function isEqualTo that compares its two arguments of the...
Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false if they are not equal. Use this function template in a program that calls isEqualTo on a variety of built-in types and user define types, Complex and Date (need to overload the equality operator (operator==) and extraction operator (operator<<)).  Write a program with variety of inputs to test the...
Write a simple function template for predicate function isEqualTo that compares its two arguments of the...
Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false if they are not equal. Use this function template in a program that calls isEqualTo on a variety of built-in types and user define types, Complex and Date (need to overload the equality operator (operator==) and extraction operator (operator<<)).  Write a program with variety of inputs to test the...
Write a function in Python that adds two Binary Trees together. The inputs of your function...
Write a function in Python that adds two Binary Trees together. The inputs of your function should be two binary trees (or their roots, depending on how you plan on coding this), and the output will be one Binary Tree that is the sum of the two inputted. To add Binary Trees together: Add the values of nodes with the same position in both trees together, this will be the value assigned to the node of the same position in...
Suppose there are two inputs in the production function, labor and capital, and these two inputs...
Suppose there are two inputs in the production function, labor and capital, and these two inputs are perfect substitutes. The existing technology permits 5 machines to do the work of 2 workers. So the production function is f(E, K) = 2K + 5E. The firm wants to produce q units of output, where q > 0 is some number. Suppose the price of capital is $10 per machine per hour. What combination of inputs will the firm use if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT