подскажите, пожалуйста, как обработать и вывести строку как полином типа x^2
у меня он x и степени не обрабатывает , только числа
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
| class Term
{
public:
void setTermPoly(int x)
{
poly += x;
}
void setTermPolyFirst(int x)
{
poly = x;
}
int getTermPoly()
{
return poly;
}
void setTermDegree(int x)
{
degree = x;
}
int getTermDegree()
{
return degree;
}
protected:
int poly;
int degree;
};
class Polynomial : public Term
{
public:
friend int getNums(char* str, int index);
friend void clear(char* x);
void getTerms(char* str)
{
int index = 0;
bool is_x = 0;
do
{
bool sign = true;
if (str[index] == 43)
index++;
if (str[index] == 45)
{
index++;
sign = false;
}
int x = getNums(str, index);
if (sign == false)
x *= -1;
if (str[index] != 120)
{
terms[index].setTermPoly(x);
cout << x;
}
else if (str[index] == 120)
{
index++;
is_x = 1;
if (str[index] = 94)
{
index++;
int x2 = getNums(str, index);
terms[index].setTermPoly(x);
}
}
} while ((str[index] == 43) || (str[index] == 45));
}
friend ostream &operator<<(ostream &output, Polynomial polinom) //перегрузка cout<<
{
int flag = 0;
for (int i = 9; i > 1; i--)
{
if (polinom.terms[i].getTermPoly() != 0)
{
if (polinom.terms[i].getTermPoly() > 0)
if (flag == 1)
cout << "+";
if (polinom.terms[i].getTermPoly() == -1)
cout << "-";
else if (polinom.terms[i].getTermPoly() != 1)
{
cout << polinom.terms[i].getTermPoly();
}
cout << "x^" << i << " ";
flag = 1;
}
}
if (polinom.terms[1].getTermPoly() != 0)
{
if (polinom.terms[1].getTermPoly() > 0)
if (flag == 1)
cout << "+";
if (polinom.terms[1].getTermPoly() == -1)
cout << "-";
else if (polinom.terms[1].getTermPoly() != 1)
cout << polinom.terms[1].getTermPoly();
{
cout << "x ";
flag = 1;
}
}
if (polinom.terms[0].getTermPoly() != 0)
{
if (polinom.terms[0].getTermPoly() > 0)
if (flag == 1)
cout << "+";
flag = 1;
cout << polinom.terms[0].getTermPoly();
}
return output;
} |
|