#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Poly
{
private:
int exp[50];
int deg;
public:
Poly()
{
for(int i = 0; i < 50; i++) exp[i] = 0;
}
void set(int a, int B)/>
{
exp[b] = a;
deg = degree();
}
int degree()
{
int d = 0;
for(int i = 0; i < 50; i++)
if(exp[i] != 0) d = i;
return d;
}
void print()
{
for(int i = 0; i < 50; i++)
{
if(exp[i] != 0)
{
if(exp[i] > 0) cout << "+ " << exp[i] << "x^" << i << " ";
else cout << exp[i] << "x^" << i << " ";
}
}
}
Poly add(Poly B)/>
{
Poly c;
for(int i = 0; i <= deg; i++) c.exp[i] += exp[i];
for(int i = 0; i <= b.deg; i++) c.exp[i] += b.exp[i];
c.deg = c.degree();
return c;
}
Poly sub(Poly B)/>
{
Poly c;
for(int i = 0; i <= deg; i++) c.exp[i] += exp[i];
for(int i = 0; i <= b.deg; i++) c.exp[i] -= b.exp[i];
c.deg = c.degree();
return c;
}
Poly mult(Poly B)/>
{
Poly c;
for(int i = 0; i <= deg; i++)
for(int j = 0; j <= b.deg; j++)
c.exp[i+j] += (exp[i] * b.exp[j]);
c.deg = c.degree();
return c;
}
};
//int main()
//{
// Poly a, b;
// a.set(1,0);
// a.set(2,1);
// a.set(3,8);
// a.print();
// cout << "\n";
//
// b.set(1,0);
// b.set(2,1);
// b.set(3,2);
// b.print();
// cout << "\n";
//
// cout << "Add: ";
// a.add(B)/>.print();
//
// cout << "\nSub: ";
// a.sub(B)/>.print();
//
// cout << "\nMult: ";
// a.mult(B)/>.print();
//
// system("PAUSE");
// return 0;
//}
int main()
{
Poly polynomials[2];
Poly a, b;
polynomials[0] = a;
polynomials[1] = b;
cout << "Welcome. Please enter two polynomials to manipulate.\n";
system("PAUSE");
for(int i = 0; i < 2; i++)
{
system("CLS");
string c;
cout << "Entering for polynomial " << i+1 << "\n";
do
{
int coef, exp;
cout << "Enter coefficient, then exponent of polynomial: \n";
cin >> coef >> exp;
polynomials[i].set(coef, exp);
cout << "Again? (Y/N) ";
cin >> c;
}while(c == "Y" || c == "y");
}
Poly c;
int ch;
do
{
system("CLS");
cout << "(1) Addition\n";
cout << "(2) Subtraction\n";
cout << "(3) Multiplication\n";
cout << "(4) Assignment\n";
cout << "(5) Exit\n";
cout << "Choice =====> ";
cin >> ch;
if(ch < 0 || ch > 5) continue;
switch(ch)
{
case 1:
c = a.add(B)/>;
break;
case 2:
c = a.sub(B)/>;
break;
case 3:
c = a.mult(B)/>;
break;
case 4:
//add assignment
break;
case 5:
system("CLS");
cout << "Goodbye\n";
system("EXIT");
break;
}
cout << "Result: ";
c.print();
system("PAUSE");
} while(ch != 5);
return 0;
}
So I've been working on this code for about 2 hours now. It's supposed to hold polynomials and then manipulate them. I have a commented main function that demonstrates that all my code is working as it should (the manipulations at least). My problem lies with the main method I'm trying to use right now. Everything runs fine and I don't get any errors, except that it seems that the functions within the switch statement aren't being called. The data doesn't reach there for some reason. I can't figure it out. I've narrowed it down to this place in the function being, what I think, is the problem area. If I could get any help, I would very thankful.
Also, in regard to some of my latest posts, sorry for the curtness of some of those, and my tendency to come and go only every few days, and not answer immediately. I've been busy, so I'm sorry for that.

New Topic/Question
Reply




MultiQuote






|