Facebook
From ja, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 242
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. using namespace std;
  5. typedef struct Pipe {
  6.      double diameter ; // [mm]
  7.      double length ;  // [m]
  8.      double thickness ; // [mm]
  9.      char *material ;   // "Cast iron" "Steel"  "Cu" "PVC" "Al"
  10. } PIPE;      // ---------------------------------
  11. void display_pipe(PIPE pipe0 );
  12. void create_pipe(PIPE *pipe0);
  13. int main() // How to use  STRUCTURES !!!! -------------------
  14. {   const int SIZE=1000;
  15.     PIPE  pipes[SIZE];
  16.     for (int i=0; i<SIZE; i++){
  17.         create_pipe(&pipes[i]);
  18.         display_pipe(pipes[i]);
  19.     }
  20.  
  21.  
  22.  
  23.     return 0;
  24. } // --------------------------------------
  25. double pipe_volume(PIPE pipe0){
  26.     double vol, s1, s2;
  27.     s1 = M_PI*pow(pipe0.diameter,2)/4;
  28.     s2 = M_PI*pow(pipe0.diameter-2*pipe0.thickness,2)/4;
  29.     return vol=(s1-s2)*pipe0.length/1000000;
  30. }
  31. double pipe_mass(PIPE pipe0){
  32.     double mass, density=1000;
  33.     if(pipe0.material="Steel") density=7800;
  34.     if(pipe0.material="Cast iron") density=7800;
  35.     if(pipe0.material="Al") density=2700;
  36.     if(pipe0.material="PVC") density=3200;
  37.     if(pipe0.material="Cu") density=8900;
  38. return mass= density*pipe_volume(pipe0);
  39. }
  40. void create_pipe(PIPE *pipe0){
  41.    pipe0->length = (rand()%61+10)/10.+1.;   // 1.-7. [m]
  42.    pipe0->diameter = rand()%(5+1)*10;
  43.    pipe0->thickness = pipe0->diameter*0.1;
  44.    int a = rand()%5 + 1;     // 1 - 5
  45.    if (a== 1) pipe0->material = "Steel";
  46.    if (a== 2) pipe0->material = "Cu";
  47.    if (a== 3) pipe0->material = "Cast Iron";
  48.    if (a== 4) pipe0->material = "PVC";
  49.    if (a== 5) pipe0->material = "Al";
  50. } // -------------------------------------------------
  51. void display_pipe(PIPE pipe0 ){
  52.     cout <<"Pipe parameters"<<endl;
  53.     cout << " diameter  " <<pipe0.diameter<< endl;
  54.     cout << " length  " <<pipe0.length<< endl;
  55.     cout << " thickness   " <<pipe0.thickness<< endl;
  56.     cout << " material  " <<pipe0.material << endl;
  57.     cout << " mass  " <<pipe_mass(pipe0) <<" kg "<< endl;
  58.     cout << endl;
  59. } // ----------------------------------------------
  60.