-3

The problem:

A function which gets degrees and factors as inputs and returns a equation as output.

The issue:

I did not know how to read an array of numbers in form of a string in c++ back then in 2016 when I was a super junior. I also did not know how to search good enough!

Update:

I answered my question and you can test this in this link: http://cpp.sh/42dwz

Answer details:

  1. Main part of the code will be like this:
int main()
{
  Poly mypoly("2 -4 3", "1 5 1");
  return 0;
  
}
  1. Inputs are 2 -4 3 and 1 5 1.
  2. Output should be (2X) + (-4X5) + (3X)
  3. Class Poly has a built-in feature to print the result
  4. To make it easier we should convert degrees and factors from a single string into an array of strings.
  5. This means that a string like 2 -4 3 changes into [2, -4, 3] which makes it easy to iterate over items and create equation sentences
  6. This action is called splitting a string into an array by a delimiter which I found here for c++ https://stackoverflow.com/a/16030594/5864034
  7. Rest of the code is just looping over the array of degrees and factors to create sentences(which is pretty easy just check the answer link http://cpp.sh/42dwz)

The code:

// Example program
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>

using namespace std;

template <size_t N>
void splitString(string (&arr)[N], string str)
{
    int n = 0;
    istringstream iss(str);
    for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
        arr[n] = *it;
}

class Poly {
  public:
    string degree[10];
    string factor[10];
    
    Poly(string input_degree, string input_factor) {
        splitString(degree, input_degree);
        splitString(factor, input_factor);
        
        for (int i = 0; i < 10; i++){
            int this_degree = stoi(degree[i]);
            int this_factor = stoi(factor[i]);
            
            string this_sentence = "";
            
            if(this_degree != 1 && this_degree != 0 ){
                this_sentence = this_sentence + degree[i];
                
                if(this_factor != 0){
                    if(this_factor != 1){
                        this_sentence = this_sentence + "X" + factor[i];
                    }else{
                        this_sentence = this_sentence + "X";
                    }
                }
            }
            
            if(this_sentence != ""){
                cout << "(" << this_sentence << ")";
            }
            
            if(stoi(degree[i+1]) != 0 && stoi(degree[i+1]) != 1){
                cout << " + ";
            }
        }
    }
};

int main()
{
  Poly mypoly("2 -4 3", "1 5 1");
  return 0;
  
}

Ali Samie
  • 145
  • 2
  • 11

2 Answers2

0

The process of reading a string and extracting information from it into some sort of structure is called parsing. There are many ways to do this, and which way is appropriate depends on exactly what you want to do, how quickly it needs to run, how much memory you've got available and various other things.

You can write a simple loop which steps over each character and decides what to do based on some variables that store current state - so you might have a flag that says you're in the middle of a number, you see another digit so you add that digit to another variable which is collecting the digits of the current number. When the current number completes (perhaps you find a character which is a space), you can take what's in the accumulator variable and parse that into a number using the standard library.

Or you can make use of standard library features more fully. For your example, you'll find that std::istringstream can do what you want, out of the box, just by telling it to extract ints from it repeatedly until the end of the stream. I'd suggest searching for a good C++ input stream tutorial - anything that applies to reading from standard input using std::cin will be relevant, as like std::istringstream, cin is an input stream and so has the same interface.

Or you could use a full-blown parsing library such as boost::spirit - total overkill for your scenario, but if you ever need to do something like parsing a structured configuration file or an entire programming language, that kind of tool is very useful.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
0

So for the community rules and to make it clear i want to answer my question.

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>

using namespace std;

template <size_t N>
void splitString(string (&arr)[N], string str)
{
    int n = 0;
    istringstream iss(str);
    for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
        arr[n] = *it;
}

class Poly {
  public:
    string degree[10];
    string factor[10];
    
    Poly(string input_degree, string input_factor) {
        splitString(degree, input_degree);
        splitString(factor, input_factor);
        
        for (int i = 0; i < 10; i++){
            int this_degree = stoi(degree[i]);
            int this_factor = stoi(factor[i]);
            
            string this_sentence = "";
            
            if(this_degree != 1 && this_degree != 0 ){
                this_sentence = this_sentence + degree[i];
                
                if(this_factor != 0){
                    if(this_factor != 1){
                        this_sentence = this_sentence + "X" + factor[i];
                    }else{
                        this_sentence = this_sentence + "X";
                    }
                }
            }
            
            if(this_sentence != ""){
                cout << "(" << this_sentence << ")";
            }
            
            if(stoi(degree[i+1]) != 0 && stoi(degree[i+1]) != 1){
                cout << " + ";
            }
        }
    }
};

int main()
{
  Poly mypoly("2 1 -4", "1 3 5");
  return 0;
  
}

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ali Samie
  • 145
  • 2
  • 11