LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2010, 09:56 AM   #1
skyluke
LQ Newbie
 
Registered: Nov 2010
Posts: 9

Rep: Reputation: 0
how to use operator overloading and catch invalid input


#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <complex>

using namespace std;

const char openParen = '(';
const char closeParen = ')';
const char iChar = 'i';
const string ops("+-*/");
const string signs("-+");
const int negPos[] = { -1, 1 };

int main(int argc, char *argv[]) {
complex<float> *c1, *c2, result;
float a, b, c, d;
char opChar, lp, rp, signChar, i;
size_t op, s1, s2;
string in;
stringstream ss;

cout << "Enter complex equation ..." << endl;
cout << " (a +|- bi) op (c +|- di) " << endl;
cout << "where op is one of : " << ops << endl;
while (true) {
cout << endl << "> ";
getline(cin,in);
ss.clear(); ss.str(in);a;
if ( (ss >> lp >> a >> signChar >> b >> i >> rp >> opChar) &&
(lp == openParen) && (i == iChar) && (rp == closeParen) &&
( (s1 = signs.find(signChar) ) != string::npos) &&
( (op = ops.find(opChar) ) != string::npos) &&
(ss >> lp >> c >> signChar >> d >> i >> rp) &&
( (s2 = signs.find(signChar) ) != string::npos) &&
(lp == openParen) && (i == iChar) && (rp == closeParen) ) {

b *= negPos[s1]; d *= negPos[s2];
c1 = new complex<float>(a,b);
c2 = new complex<float>(c,d);
switch (opChar) {
case '+' : result = *c1 + *c2; break;
case '-' : result = *c1 - *c2; break;
case '*' : result = *c1 * *c2; break;
case '/' : result = *c1 / *c2; break;
}
cout << endl << in << " = (" << noshowpos << result.real()
<< showpos << result.imag() << "i)" << endl;
delete c1;
delete c2;
}
}
return 0;
}
 
Old 11-10-2010, 04:28 AM   #2
lesca
Member
 
Registered: Sep 2010
Posts: 58

Rep: Reputation: 0
Sorry, I just have misunderstand your question ...

But I would like to give you some docs about operator overloading:

http://www.parashift.com/c++-faq-lit...html#faq-13.10

Hope that would help you.

Last edited by lesca; 11-10-2010 at 04:34 AM.
 
Old 11-10-2010, 05:12 AM   #3
x-nc
Member
 
Registered: Feb 2007
Location: VA, USA
Distribution: CentOS, Fedora
Posts: 53

Rep: Reputation: 5
Arrow

I couldn't quite follow the code in the original post so I did a little formatting and used the code block tags to make it a bit easier to read. FWLIW, when I try this out it just goes into an infinite loop on the input. But my C++ skills are nascent at best.

Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <complex>

using namespace std;

const char openParen = '(';
const char closeParen = ')';
const char iChar = 'i';
const string ops("+-*/");
const string signs("-+");
const int negPos[] = { -1, 1 };

int main(int argc, char *argv[]) {

    complex<float> *c1, *c2, result;
    float a, b, c, d;
    char opChar, lp, rp, signChar, i;
    size_t op, s1, s2;
    string in;
    stringstream ss;

    cout << "Enter complex equation ..." << endl;
    cout << " (a +|- bi) op (c +|- di) " << endl;
    cout << "where op is one of : " << ops << endl;
	
    while (true) {
        cout << endl << "> ";
        getline(cin,in);
        ss.clear(); ss.str(in);a;
	
        if ( (ss >> lp >> a >> signChar >> b >> i >> rp >> opChar) &&
           (lp == openParen) && (i == iChar) && (rp == closeParen) &&
           ( (s1 = signs.find(signChar) ) != string::npos) &&
           ( (op = ops.find(opChar) ) != string::npos) &&
           (ss >> lp >> c >> signChar >> d >> i >> rp) &&
           ( (s2 = signs.find(signChar) ) != string::npos) &&
           (lp == openParen) && (i == iChar) && (rp == closeParen) ) {

           b *= negPos[s1]; d *= negPos[s2];
           c1 = new complex<float>(a,b);
           c2 = new complex<float>(c,d);

           switch (opChar) {
               case '+' : result = *c1 + *c2; break;
               case '-' : result = *c1 - *c2; break;
               case '*' : result = *c1 * *c2; break;
               case '/' : result = *c1 / *c2; break;
           }

           cout << endl << in << " = (" << noshowpos << result.real()
                << showpos << result.imag() << "i)" << endl;

           delete c1;
           delete c2;
        }
    }
    return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
(C++) operator = overloading with gcc 4.0.1 Omni Programming 7 10-16-2007 06:30 AM
help overloading 'new' operator c++ qwijibow Programming 1 03-07-2007 04:58 PM
c++ [] operator overloading exodist Programming 12 04-17-2004 03:06 PM
C# with dotGNU, operator overloading exodist Programming 2 03-15-2004 05:52 PM
c++ overloading input operator true_atlantis Programming 4 02-25-2004 07:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration