Dev - C++ - me podrian ayudar a cambiar este programa a la libreria <stdio.h> o bueno con printf y scanf

 
Vista:

me podrian ayudar a cambiar este programa a la libreria <stdio.h> o bueno con printf y scanf

Publicado por Hugo (1 intervención) el 09/07/2019 20:00:34
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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <sstream>
using namespace std;
 
void coeficientes(double *x,double *y,double **matriz,int grado,int nDatos);//Llena la matriz del sistema de ecuaciones de los coeficientes.
void recorrido(double **matriz, int n,bool &op);//Recorre la matriz para resolverla por gauss jordan.
void reduccion (double **matriz, int i, int j,int n);//Reducción gauss jordan a ceros.
bool mdiagonal(double **matriz, int n);//Valída la matriz digonal antes de reducirla a unos.
void rauno(double **matriz,int n);//Reduduccion de la matriz diagonal a unos.
void pedirdatos(double *x,double *y,int nDatos);//Llena los vectores X y Y con los pares de coordenadas.
void imp(double **matriz, int n,bool op);//Impresion de los resultados.
 
void coeficientes(double *x,double *y,double **matriz,int grado,int nDatos)
{
	double *s=new double[(2*grado)+1];
	double suma;
	for(int k=0; k < 2*grado+1; k++){
		suma=0.0;
		for(int i=0; i < nDatos; i++)
			suma+=pow(x[i], k);
		s[k]=suma;
	}
	for(int k=0; k<grado+1; k++){
		suma=0.0;
		for(int i=0; i < nDatos; i++)
			suma+=pow(x[i], k)*y[i];
		matriz[k][grado+1]=suma;
	}
	for(int i=0; i < grado+1; i++){
		for(int j=0; j < grado+1; j++)
			matriz[i][j]=s[i+j];
	}
}
 
void recorrido(double **matriz, int n,bool &op)
{
	int i,j,k=0;
	for (j = 0; j < n-1; ++j){
		for (i = n-1; i >k ; --i){
			if(mdiagonal(matriz,n))
				rauno(matriz,n);
			reduccion(matriz,i,j,n);
		}
		k++;
	}
	for (j = n-1;  j> 0; j--){
		for (i = 0; i < k; ++i){
			if(mdiagonal(matriz,n))
				rauno(matriz,n);
			reduccion(matriz,i,j,n);
		}
		k--;
	}
	for (i = 0; i < n; ++i){
		if(matriz[i][i]==0)
			op=true;
	}
	if (!op&&mdiagonal(matriz,n))
		rauno(matriz,n);
}
 
void reduccion(double **matriz,int i,int j,int n)
{
	double *ra=new double[n+1];
	int k;
	for (k = 0; k < n+1; ++k)
		ra[k]=((matriz[i][k]*matriz[j][j])-(matriz[j][k]*matriz[i][j]));
	for (int k = 0; k < n+1; ++k)
		matriz[i][k]=ra[k];
}
 
bool mdiagonal(double **matriz, int n)
{
	bool op=false;
	int i;
	for (i = 0; i < n; ++i){
		if(matriz[i][i]!=1)
			op=true;
	}
	for (i = 0; i < n; ++i){
		if (matriz[i][i]==0)
			op=false;
	}
	return op;
}
 
void rauno(double **matriz,int n)
{
	int i,j;
	double aux;
	for (i = 0; i < n; ++i){
		aux=matriz[i][i];
		for (j = 0; j < n+1; ++j)
			matriz[i][j]=(matriz[i][j]/aux);
	}
}
 
void pedirdatos(double *x,double *y,int nDatos)
{
	string entrada;
	system("cls");
	cout<<"\n\tIntroduce los datos xi."<<endl;
	for (int i = 0; i < nDatos; ++i){
		while(true){
			cout<<"\n\tx"<<i+1<<": ";
			getline(cin,entrada);
			stringstream mystream(entrada);
			if (mystream>>x[i])
				break;
			cout<<"\n\tEntrada invalida."<<endl;
		}
	}
	system("pause");
	cout<<"\n\tIntroduce los datos yi."<<endl;
	for (int i = 0; i < nDatos; ++i){
		while(true){
			cout<<"\n\ty"<<i+1<<": ";
			getline(cin,entrada);
			stringstream mystream(entrada);
			if (mystream>>y[i])
				break;
			cout<<"\n\tEntrada invalida."<<endl;
		}
	}
	system("pause");
	system("cls");
}
 
void imp(double **matriz, int n,bool op)
{
	system("cls");
	if (op)
		cout<<"\n\n\tLos pares de datos introducidos no generan un sistema de ecuaciones con solucion."<<endl;
	else{
		cout<< "\n\n\tLos coeficientes de tu polinomio son:\n"<<endl;
		for (int i = 0; i < n; ++i)
			if (matriz[i][n]>=0)
				cout<<setw(10)<<"a"<<i<<"  =  "<<matriz[i][n]<<endl;
			else
				cout<<setw(10)<<"a"<<i<<"  = "<<matriz[i][n]<<endl;
	}
	system ("pause");
}
 
int main(int argc, char const *argv[])
{
	cout<<"\n\n   =Metodo de minimos cuadrados polinomial="<<endl<<"\n   ";
	system("pause");
	do{
		system ("cls");
		int nDatos,grado;
		bool op=false;
		string entrada;
		while(true){
			cout<<"\n\tIntroduce la cantidad de puntos que vas a ingresar: ";
			getline(cin,entrada);
			stringstream mystream(entrada);
			if (mystream>>nDatos)
				break;
			cout<<"\n\tEntrada invalida."<<endl;
		}
		double *X=new double[nDatos];
		double *Y=new double[nDatos];
		pedirdatos(X,Y,nDatos);
		while(true){
			cout<<"\n\tIntroduce el grado del polinomio que deseas encontrar: ";
			getline(cin,entrada);
			stringstream mystream(entrada);
			if (mystream>>grado)
				break;
			cout<<"\n\tEntrada invalida."<<endl;
		}
		double **matriz=new double*[grado+1];
		for (int i = 0; i < grado+1; ++i)
			matriz[i]=new double[grado+2];
		coeficientes(X,Y,matriz,grado,nDatos);
		recorrido(matriz,grado+1,op);
		imp(matriz,grado+1,op);
	}while(1);
	return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder