C++/Data Type/Struct

Материал из C\C++ эксперт
Перейти к: навигация, поиск

array of structures

  
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define N_MOVIES 3
struct movies_t {
  string title;
  int year;
} films [N_MOVIES];
void printmovie (movies_t movie);
int main ()
{
  string mystr;
  int n;
  for (n=0; n<N_MOVIES; n++)
  {
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,mystr);
    stringstream(mystr) >> films[n].year;
  }
  cout << "\nYou have entered these movies:\n";
  for (n=0; n<N_MOVIES; n++)
    printmovie (films[n]);
  return 0;
}
void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}


Defines and uses a struct.

 
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Product 
{
  string name;        
  double sales;       
};
inline void print( const Product& v)
{
   cout << fixed << setprecision(2)
        << left  << setw(20) << v.name
        << right << setw(10) << v.sales << endl;
}
int main()
{
   Product productObject, productObject2;
   productObject.name    = "one";
   productObject.sales   = 37.37;
   productObject2.name   = "two";
   productObject2.sales  = 231.23;
   productObject.sales   += 17.11;
   cout << "  Product            Sales\n"
        << endl;
   print( productObject);
   print( productObject2);
   cout << "\nTotal of sales: "
        << productObject.sales + productObject2.sales << endl;
   Product *ptr = &productObject2;         
   if( productObject2.sales < productObject.sales)         
     ptr = &productObject;
   cout << "\nSalesman of the month: " << ptr->name << endl;   
                                
   return 0;
}


demonstrates nested structures

   
#include <iostream>   
  using namespace std;   
  struct Distance{   
     int feet;   
     float inches;   
  };   
  struct Room{   
     Distance length;
     Distance width;
  };   
  int main()   
  {   
     Room dining;   
    
     dining.length.feet = 13;    
     dining.length.inches = 6.5;   
     dining.width.feet = 10;   
     dining.width.inches = 0.0;   
     float l = dining.length.feet + dining.length.inches/12;   
     float w = dining.width.feet  + dining.width.inches/12;   
     cout << l * w;   
     return 0;   
    }


demonstrates passing structure as argument

   
#include <iostream>   
  using namespace std;   
  
  struct Distance{   
     int feet;   
     float inches;   
  };   
  void engldisp( Distance );
    
  int main(){   
     Distance d1, d2; 
     cout << "Enter feet: ";  
     cin >> d1.feet;   
     cout << "Enter inches: ";  
     cin >> d1.inches;   
    
     cout << "\nEnter feet: ";  
     cin >> d2.feet;   
     cout << "Enter inches: ";  
     cin >> d2.inches;   
    
     cout << "\nd1 = ";   
     engldisp(d1); 
     cout << "\nd2 = ";   
     engldisp(d2); 
     cout << endl;   
     return 0;   
  }   
  void engldisp( Distance dd ){   
     cout << dd.feet << "\"-" << dd.inches << "\"";   
  }


demonstrates passing structure by reference

   
#include <iostream>   
  using namespace std;   
  
  struct Distance{   
     int feet;   
     float inches;   
    };   
  void scale( Distance&, float );
  void engldisp( Distance );     
    
  int main() {   
     Distance d1 = { 12, 6.5 };  
     Distance d2 = { 10, 5.5 };   
    
     cout << "d1 = "; 
     engldisp(d1);
     cout << "\nd2 = "; 
     engldisp(d2);   
    
     scale(d1, 0.5);    
     scale(d2, 0.25);   
    
     cout << "\nd1 = "; 
     engldisp(d1);   
     cout << "\nd2 = "; 
     engldisp(d2);   
     cout << endl;   
     return 0;   
  }   
  void scale( Distance& dd, float factor){   
     float inches = (dd.feet*12 + dd.inches) * factor;   
     dd.feet = static_cast<int>(inches / 12);   
     dd.inches = inches - dd.feet * 12;   
  }   
  void engldisp( Distance dd ){   
     cout << dd.feet << "\"-" << dd.inches << "\"";   
  }


example about structures

  
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
  string title;
  int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
  string mystr;
  mine.title = "2001 This is a test";
  mine.year = 1968;
  cout << "Enter title: ";
  getline (cin,yours.title);
  cout << "Enter year: ";
  getline (cin,mystr);
  stringstream(mystr) >> yours.year;
  cout << "My favorite movie is:\n ";
  printmovie (mine);
  cout << "And yours is:\n ";
  printmovie (yours);
  return 0;
}
void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}


pointers to structures

  
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
  string title;
  int year;
};
int main ()
{
  string mystr;
  movies_t amovie;
  movies_t * pmovie;
  pmovie = &amovie;
  cout << "Enter title: ";
  getline (cin, pmovie->title);
  cout << "Enter year: ";
  getline (cin, mystr);
  (stringstream) mystr >> pmovie->year;
  cout << "\nYou have entered:\n";
  cout << pmovie->title;
  cout << " (" << pmovie->year << ")\n";
  return 0;
}


uses parts inventory to demonstrate structures

   
#include <iostream>   
  using namespace std;   
  struct part{   
     int modelnumber; 
     int partnumber;  
     float cost;      
  };   
  int main(){   
     part part1;      
    
     part1.modelnumber = 6244;
     part1.partnumber = 373;   
     part1.cost = 217.55F;   
     cout << "Model "    << part1.modelnumber;   
     cout << ", part "   << part1.partnumber;   
     cout << ", costs $" << part1.cost << endl;   
     return 0;   
  }