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

This is the question Write a function add(vals1, vals2) that takes as inputs two lists of...
This is the question 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...
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...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and tell whether a number is prime or not. The user chooses how many inputs go into the calculator. For example, the code will ask the user what function they want. If the user chooses to subtract, the the code will then ask the user how many numbers they want to subtract. After, the code will ask the user to input as many numbers as...
In c++ language, write a void function GetYear that prompts for and inputs the year the...
In c++ language, write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater than...
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 member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT