How do i read in an equation in this format: 1 + 2a + 3b + 4c?

I need to extract only the value 1, 2, 3, 4.

Recommended Answers

All 4 Replies

What are you going to do with the values? Are you guaranteed that whitespace will separate the operators and operands? It could be really simple, or you might be better off writing a few classes that hold the raw equation data but still pass out extracted values. Depends on what problem you're trying to solve.

What are you going to do with the values? Are you guaranteed that whitespace will separate the operators and operands? It could be really simple, or you might be better off writing a few classes that hold the raw equation data but still pass out extracted values. Depends on what problem you're trying to solve.

I'm trying to do an >> overload.

istream& operator >> (istream& in, Quaternion& rhs)
{
in >> rhs.a;
in.ignore();
in.ignore();
in.ignore();

in >> rhs.b;
in.ignore();
in.ignore();
in.ignore();
in.ignore();

in >> rhs.c;
in.ignore();
in.ignore();
in.ignore();
in.ignore();
in >> rhs.d;

return in;
}

I don't think this will solve the problem, but lacking sufficient details, Edward will give you exactly what you asked for:

void SkipNonNumeric(istream& in)
{
  // Skip non-numeric characters
  while (in && !isdigit(in.peek()))
    in.ignore();
}

istream& operator>>(istream& in, Quaternion& rhs)
{
  in >> rhs.a;
  SkipNonNumeric(in);
  in >> rhs.b;
  SkipNonNumeric(in);
  in >> rhs.c;
  SkipNonNumeric(in);
  in >> rhs.d;

  // Clear out the remaining equation
  in.ignore(1024, '\n');

  return in;
}

I don't think this will solve the problem, but lacking sufficient details, Edward will give you exactly what you asked for:

void SkipNonNumeric(istream& in)
{
  // Skip non-numeric characters
  while (in && !isdigit(in.peek()))
    in.ignore();
}

istream& operator>>(istream& in, Quaternion& rhs)
{
  in >> rhs.a;
  SkipNonNumeric(in);
  in >> rhs.b;
  SkipNonNumeric(in);
  in >> rhs.c;
  SkipNonNumeric(in);
  in >> rhs.d;

  // Clear out the remaining equation
  in.ignore(1024, '\n');

  return in;
}

Thanks a lot. Got a feeling we are doing the same stuff.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.