-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolinomio.cpp
More file actions
228 lines (211 loc) · 5.99 KB
/
Copy pathPolinomio.cpp
File metadata and controls
228 lines (211 loc) · 5.99 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <iostream>
#include <math.h>
using namespace std;
struct Nodo{
int base;
int exponente;
Nodo *sig;
};
struct Lista{
Nodo *cab;
int tam;
};
Lista *crearLista();
void insertarPolinomio(int bas, int exp,Lista *a);
void imprimir(Lista *pLista);
bool vacia(Lista *pLista);
Lista *sumarPolinomios(Lista *lista1, Lista *lista2, Lista *resuma);
int evaluarVariable(Lista *evaluar, int evaluador);
int main() {
Lista *a,*b,*resuma,*evaluar;
int bas,exp;
a=crearLista();
b=crearLista();
int evaluadito;
evaluar=crearLista();
resuma=crearLista();
cout <<endl;
int opcion=0;
while(opcion!=3) {
cout << "Seleccine lo que desea hacer con sus polinomios\n1.Para sumar dos polinomios\n2.Para evaluar un polinomio\n3.Para salir\nSu opcion: ";
cin>>opcion;
switch(opcion){
case 1 : cout<<"Introduzca el numero de coeficientes para el polinomio A: ";
int cofa;
cin>>cofa;
for (int i = 0; i < cofa; ++i) {
cout<<"Introduzca la base del "<<i+1<<" coeficiente: ";
cin>>bas;
cout<<"Introduzca el exponente del "<<i+1<<" coeficiente: ";
cin>>exp;
insertarPolinomio(bas,exp,a);
}
imprimir(a);
cout<<"Introduzca el numero de coeficientes para el polinomio B: ";
int cofb;
cin>>cofb;
for (int i = 0; i < cofb; ++i) {
cout<<"Introduzca la base del "<<i+1<<" coeficiente: ";
cin>>bas;
cout<<"Introduzca el exponente del "<<i+1<<" coeficiente: ";
cin>>exp;
insertarPolinomio(bas,exp,b);
}
imprimir(b);
resuma=sumarPolinomios(a,b,resuma);
cout<<endl;
imprimir(resuma);
cout<<endl;
break;
case 2 : cout<<"Introduzca el numero de coeficientes para el polinomio a evaluar: ";
int cofeva;
cin>>cofeva;
cout<<"Intoduzca el numero con el cual desea evaluar el polinomio: ";
int evaluador;
cin>>evaluador;
for (int i = 0; i < cofeva; ++i) {
cout << "Introduzca la base del " << i + 1 << " coeficiente: ";
cin >> bas;
cout << "Introduzca el exponente del " << i + 1 << " coeficiente: ";
cin >> exp;
insertarPolinomio(bas, exp, evaluar);
}
evaluadito=evaluarVariable(evaluar,evaluador);
cout<<"El resultado del polinomio evaluado es: "<<evaluadito<<endl<<endl;
break;
case 3 : cout<<"Hasta luego\n";
break;
default : cout<<"Opcion incorrecta\n";
break;
}
}
return 0;
}
int evaluarVariable(Lista *evaluar, int evaluador) {
Nodo *eval;
eval=new Nodo;
eval=evaluar->cab;
int numero=0;
int numero2=0;
for (int i = 0; i < evaluar->tam; ++i) {
numero=0;
numero=pow(evaluador,eval->exponente);
numero=numero*(eval->base);
numero2=numero2+numero;
eval=eval->sig;
}
return numero2;
}
Lista *sumarPolinomios(Lista *lista1, Lista *lista2, Lista *resuma) {
Nodo *a,*b;
Nodo *resu;
resu=new Nodo;
a=new Nodo;
b=new Nodo;
a=lista1->cab;
b=lista2->cab;
for (int i = 0; i < lista1->tam; ++i) {
for (int j = 0; j < lista2->tam; ++j) {
if(a->exponente==b->exponente){
insertarPolinomio(((a->base)+(b->base)),(a->exponente),resuma);
}
b=b->sig;
}
a=a->sig;
b=lista2->cab;
}
int bandera=0;
a=lista1->cab;
resu=resuma->cab;
for (int k = 0; k < lista1->tam; ++k) {
for (int i = 0; i < resuma->tam; ++i) {
if(a->exponente==resu->exponente){
bandera++;
}
resu=resu->sig;
}
if(bandera==0){
insertarPolinomio(a->base,a->exponente,resuma);
}
bandera=0;
a=a->sig;
resu=resuma->cab;
}
bandera=0;
b=lista2->cab;
resu=resuma->cab;
for (int k = 0; k < lista2->tam; ++k) {
for (int i = 0; i < resuma->tam; ++i) {
if(b->exponente==resu->exponente){
bandera++;
}
resu=resu->sig;
}
if(bandera==0){
insertarPolinomio(b->base,b->exponente,resuma);
}
bandera=0;
b=b->sig;
resu=resuma->cab;
}
return resuma;
}
void imprimir(Lista *lista) {
if(vacia(lista))
{
cout<<"Polinomio: "<<endl;
}
else
{
cout<<"La informacion de la Lista es:"<<endl;
Nodo * aux;
aux = lista->cab;
int pos = 1;
while(aux != NULL)
{
cout<<" "<<aux->base<<"x^"<<aux->exponente<<" ";
aux = aux->sig;
pos++;
}
}
cout<<endl;
}
bool vacia(Lista *lista) {
if(lista->tam == 0)
{
return true;
}
else
{
return false;
}
}
void insertarPolinomio(int bas, int exp, Lista *lista) {
Nodo * nuevo;
nuevo = new Nodo;
nuevo->base = bas;
nuevo->exponente = exp;
nuevo->sig = NULL;
if(lista->tam == 0)
{
lista->cab = nuevo;
lista->tam++;
}
else
{
Nodo * aux;
aux = lista->cab;
while(aux->sig != NULL)
{
aux = aux->sig;
}
aux->sig = nuevo;
lista->tam++;
}
}
Lista *crearLista() {
Lista * lista = new Lista;
lista->tam = 0;
lista->cab = NULL;
return lista;
}