Skip to content

Instantly share code, notes, and snippets.

@brayancruces
Created November 4, 2016 10:43
Show Gist options
  • Select an option

  • Save brayancruces/d584c6f7d9bfad8223fce76905536869 to your computer and use it in GitHub Desktop.

Select an option

Save brayancruces/d584c6f7d9bfad8223fce76905536869 to your computer and use it in GitHub Desktop.
Ejercicio 1
/*
04/11/2016
Ejercicio 1
Hoja de ejercicios
To-do:
Guardar archivo [Incompleto]
*/
#include<iostream>
#include<vector>
#include<sstream>
#include<functional>
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<fstream>
using namespace std;
class Vectores{
public:
int x, y;
public:
Vectores(int x, int y){
this->x = x;
this->y = y;
}
~Vectores(){}
string toString(){
ostringstream ss;
ss << "(" << x << ", " << y << ")";
return ss.str();
}
};
template <class T>
double escalar(T a, T b, T c){
//lambda
function <int(T, T, T)> f1 = [](T a, T b, T c){
return (a.x*b.x*c.x + a.y*b.y*c.y);
};
return f1(a, b,c);
}
template <class T>
/* Modulo de los dos primeros vectores */
double modulo(T a, T b){
//lambda
function<double(T, T)> f1 = [](T a, T b){
return(sqrt(pow((b.x - a.x), 2) + pow((b.y - a.y), 2) ));
};
return f1(a, b);
}
template<class T>
double calculo(vector<T>vec, double(*calc)(vector<T>)){
return calc(vec);
}
template<class T>
void guardar(vector<T>vec, char*filename){
ofstream archi;
archi.open(filename);
if (!archi.good()){
cout << "no se pudo abrir el archivo!" << endl;
return;
}
for (int i = 0; i < (vec.size()); i++)
{
archi << vec[i].toString() << endl;
}
}
int main(){
vector<Vectores> vec;
// Insercion manual de data
vec.push_back(Vectores(1,2));
vec.push_back(Vectores(4,6));
vec.push_back(Vectores(1,1));
srand(time(NULL));
// int n;
//cout << "Ingrese el numero de vectores: "; cin >> n;
//for (int i = 0; i < n; i++)
//{
// vec.push_back(Vectores(rand() % 5, rand() % 5));
//}
// Imprimir vectores
for (int i = 0; i < (vec.size()); i++)
{
cout << vec[i].toString() << endl;
}
cout << "El escalar: " << escalar(vec[0], vec[1], vec[2]);
cout << endl;
cout << "El modulo es: " << modulo(vec[0], vec[1]);
cout << endl;
_getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment