Advertisement
Guest User

q4 ponteiros

a guest
May 25th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. typedef struct _Vetor {
  7.     float x;
  8.     float y;
  9.     float z;
  10. } Vetor;
  11.  
  12. void soma ( const Vetor *v1, const Vetor *v2, Vetor **res );
  13. Vetor soma2 ( const Vetor *v1, const Vetor *v2 );
  14.  
  15. int main ()
  16. {
  17.     Vetor v1,v2;
  18.  
  19.     cout << "Digite os valores para V1:" << endl;
  20.     cout << "X:";
  21.     cin >> v1.x;
  22.     cout << "Y:";
  23.     cin >> v1.y;
  24.     cout << "Z:";
  25.     cin >> v1.z;
  26.  
  27.  
  28.     cout << "Digite os valores para V2:" << endl;
  29.     cout << "X:";
  30.     cin >> v2.x;
  31.     cout << "Y:";
  32.     cin >> v2.y;
  33.     cout << "Z:";
  34.     cin >> v2.z;
  35.  
  36.    Vetor resultado =  soma2 (&v1,&v2);
  37.  
  38.    cout << "\nResultado da soma 2:" << endl;
  39.    cout << "X =" << resultado.x << endl;
  40.    cout << "Y =" << resultado.y<< endl;
  41.    cout << "Z =" << resultado.z<< endl;
  42.  
  43.    Vetor * resposta = NULL;
  44.  
  45.    soma(&v1,&v2,&resposta);
  46.  
  47.    cout << "\nResultado da soma 1:" << endl;
  48.    cout << "X =" << resposta->x << endl;
  49.    cout << "Y =" << resposta->y << endl;
  50.    cout << "Z =" << resposta->z<< endl;
  51.  
  52.    delete resposta;
  53.  
  54.    return 0;
  55. }
  56.  
  57. void soma ( const Vetor *v1, const Vetor *v2, Vetor **res )
  58. {
  59.  
  60.     Vetor *aux = new Vetor;
  61.    
  62.     aux->x = (v1 -> x + v2 -> x);
  63.     aux->y = (v1 -> y + v2 -> y);
  64.     aux->z = (v1 -> z + v2 -> z);
  65.  
  66.     (*res) = aux;
  67.  
  68. }
  69.  
  70. Vetor soma2 ( const Vetor *v1, const Vetor *v2 )
  71. {
  72.     Vetor v = {v1 -> x + v2 -> x, v1 -> y + v2 -> y, v1 -> z + v2 -> z};
  73.     cout << "ok\n";
  74.     return  v;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement