• 3.Compound data types


    The codes:

    // 3.Compound Data Types
    
    /*****************************************
    1
    Arrays
    */
    
    // arrays example
    #include <iostream>
    using namespace std;
    
    int billy [] = {16, 2, 77, 40, 12071};
    int n, result=0;
    
    int main ()
    {
      for ( n=0 ; n<5 ; n++ )
      {
        result += billy[n];
      }
      cout << result;
      return 0;
    }
    
    
    
    // arrays example
    #include <iostream>
    using namespace std;
    
    #define WIDTH 5
    #define HEIGHT 3
    
    int jimmy [HEIGHT][WIDTH];
    int n,m;
    
    int main ()
    {
      for (n=0;n<HEIGHT;n++)
        for (m=0;m<WIDTH;m++)
        {
          jimmy[n][m]=(n+1)*(m+1);
        }
      return 0;
    }
    
    
    
    // arrays example
    #include <iostream>
    using namespace std;
    
    #define WIDTH 5
    #define HEIGHT 3
    
    int jimmy [HEIGHT * WIDTH];
    int n,m;
    
    int main ()
    {
      for (n=0;n<HEIGHT;n++)
        for (m=0;m<WIDTH;m++)
        {
          jimmy[n*WIDTH+m]=(n+1)*(m+1);
        }
      return 0;
    }
    
    
    // arrays as parameters
    #include <iostream>
    using namespace std;
    
    void printarray (int arg[], int length) {
      for (int n=0; n<length; n++)
        cout << arg[n] << " ";
      cout << "
    ";
    }
    
    int main ()
    {
      int firstarray[] = {5, 10, 15};
      int secondarray[] = {2, 4, 6, 8, 10};
      printarray (firstarray,3);
      printarray (secondarray,5);
      return 0;
    }
    
    
    /**************************************
    2
    Character Sequences
    */
    
    // null-terminated sequences of characters
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      char question[] = "Please, enter your first name: ";
      char greeting[] = "Hello, ";
      char yourname [80];
      cout << question;
      cin >> yourname;
      cout << greeting << yourname << "!";
      return 0;
    }
    
    
    /*****************************************
    3
    Pointers
    */
    
    // my first pointer
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int firstvalue, secondvalue;
      int * mypointer;
    
      mypointer = &firstvalue;
      *mypointer = 10;
      mypointer = &secondvalue;
      *mypointer = 20;
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      return 0;
    }
    
    
    // more pointers
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int firstvalue = 5, secondvalue = 15;
      int * p1, * p2;
    
      p1 = &firstvalue;  // p1 = address of firstvalue
      p2 = &secondvalue; // p2 = address of secondvalue
      *p1 = 10;          // value pointed by p1 = 10
      *p2 = *p1;         // value pointed by p2 = value pointed by p1
      p1 = p2;           // p1 = p2 (value of pointer is copied)
      *p1 = 20;          // value pointed by p1 = 20
      
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      return 0;
    }
    
    
    // more pointers
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int numbers[5];
      int * p;
      p = numbers;  *p = 10;
      p++;  *p = 20;
      p = &numbers[2];  *p = 30;
      p = numbers + 3;  *p = 40;
      p = numbers;  *(p+4) = 50;
      for (int n=0; n<5; n++)
        cout << numbers[n] << ", ";
      return 0;
    }
    
    
    // more...
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      char * terry = "hello";
      cout << *(terry+4) << endl;
      cout << terry[4] << endl;
      return 0;
    }
    
    
    
    // more...
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      char *mychar;
      short *myshort;
      long *mylong;
      cout<<hex<<(unsigned)mychar<<" "<<myshort<<" "<<mylong<<endl;
      cout<<hex<<(unsigned)(++mychar)<<" "<<++myshort<<" "<<++mylong<<endl;
      return 0;
    }
    
    
    
    // more...
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      char a;
      char *b;
      char **c;
      char ***d=&c; // 
      char ****e=&d; // can..
      a='z';
      b=&a;
      c=&b;
      cout<<a<<endl;
      cout<<hex<<(unsigned)b<<" "<<c<<endl;
      cout<<hex<<unsigned(*c)<<" "<<**c<<endl;
      
      return 0;
    }
    
    
    
    // void pointer, increaser
    #include <iostream>
    using namespace std;
    
    void increase (void* data, int psize) //void
    {
      if ( psize == sizeof(char) )
      { 
        char* pchar; 
        pchar=(char*)data; // (char*)
        ++(*pchar); 
      }
      else if (psize == sizeof(int) )
      { 
        int* pint; 
        pint=(int*)data;  // (int*)
        ++(*pint); 
      }
    }
    
    int main ()
    {
      char a = 'x';
      int b = 1602;
      increase (&a,sizeof(a));
      increase (&b,sizeof(b));
      cout << a << ", " << b << endl;
      return 0;
    }
    
    
    // NULL pointer, void pointers
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int b = 1602;
      int *p=0;
      int *t=NULL; // pointint to "nowhere"
      void *s=0; // point to a value that has no type
      p=&b;
      t=&b;
      s=&b; // 
      cout << s << endl; // just a dress
      cout << *(int*)s << endl; // can't be *s
      return 0;
    }
    
    
    
    // pointer to functions
    #include <iostream>
    using namespace std;
    
    int addition (int a, int b)
    { return (a+b); }
    
    int subtraction (int a, int b)
    { return (a-b); }
    
    int operation (int x, int y, int (*functocall)(int,int))
    {
      int g;
      g = (*functocall)(x,y);
      return (g);
    }
    
    int main ()
    {
      int m,n;
      int (*minus)(int,int) = subtraction; // minus is a pointer to a function subtraction
    
      m = operation (7, 5, addition); // choice addition
      n = operation (20, m, minus);  // choice minus
      cout <<n;
      return 0;
    }
    
    
    /***********************************************
    4
    Dynamic Memory
    */
    
    // rememb-o-matic
    #include <iostream>
    #include <new>
    using namespace std;
    
    int main ()
    {
      int i,n;
      int * p;
      cout << "How many numbers would you like to type? ";
      cin >> i;
      p= new (nothrow) int[i];
      if (p == 0)
        cout << "Error: memory could not be allocated";
      else
      {
        for (n=0; n<i; n++)
        {
          cout << "Enter number: ";
          cin >> p[n];
        }
        cout << "You have entered: ";
        for (n=0; n<i; n++)
          cout << p[n] << ", ";
        delete[] p; // no argument
      }
      return 0;
    }
    
    /************************************************
    5
    Data Structures
    */
    
    // 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 A Space Odyssey";
      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:
     ";
      printmovie (mine);
      cout << "And yours is:
     ";
      printmovie (yours);
      return 0;
    }
    
    void printmovie (movies_t movie)
    {
      cout << movie.title;
      cout << " (" << movie.year << ")
    ";
    }
    
    
    
    // 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 << "
    You have entered these movies:
    ";
      for (n=0; n<N_MOVIES; n++)
        printmovie (films[n]);
      return 0;
    }
    
    void printmovie (movies_t movie)
    {
      cout << movie.title;
      cout << " (" << movie.year << ")
    ";
    }
    
    
    
    // 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 << "
    You have entered:
    ";
      cout << pmovie->title;
      cout << " (" << pmovie->year << ")
    ";
    
      return 0;
    }
    
    
    // nesting structures
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    struct movies_t {
      string title;
      int year;
    };
    
    struct friends_t {
      string name;
      string email;
      movies_t favorite_movie;
    } charlie,maria;
    
    friends_t * pfriends = &charlie;
    
    int main ()
    {
      charlie.name="charlie";
      cout<< charlie.name << endl;
      maria.favorite_movie.title="ss";
      cout<< maria.favorite_movie.title << endl;
      charlie.favorite_movie.year = 1984;
      cout<< charlie.favorite_movie.year << endl;
      cout<< pfriends->favorite_movie.year << endl;
    
      return 0;
    }
    
    
    /***************************************
    6
    Other Data Types
    */
    
    
    // typedef
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    typedef struct movies_t {
      string title;
      int year;
    } movies_t;
    int main ()
    {
      typedef char C;
      typedef unsigned int WORD;
      typedef char * pChar;
      typedef char filed [50]; // 
      movies_t movi;
      movi.title="test";
      movi.year=1984;
      C c;
      WORD i;
      pChar p;
      filed f; // char f[50]
      f[49]='t';
      cout << f[49] << endl;
      p = f;
      p[49]='p';
      cout << f[49] << endl;
    
      return 0;
    }
    
    
    
    /*
    union的作用主要是节省内存空间,但现在内存都比较大,
    所以在一般的编程中很少用到,而且处理不好容易出错,
    建议非特殊情况不要使用。
    */
    
    // union
    #include <iostream>
    using namespace std;
    
    union mytypes_t {
      char c;
      int i;
      double f;
    } myunion;
    
    int main ()
    {
      cout<< sizeof(myunion) << endl;
      myunion.c='c';
      cout << myunion.c << endl;
      myunion.i=1; // same physical space in memory
      cout << myunion.c << endl;
    
      return 0;
    }
    
    
    
    // enumerations
    #include <iostream>
    using namespace std;
    
    enum colors {
      black,
      blue,
      green,
      cyan,
      red,
      purple,
      yellow,
      white
    };
    
    int main ()
    {
      colors mycolor;
      mycolor = blue;
      cout << mycolor << endl;
      mycolor = red;
      cout << mycolor << endl;
    
      return 0;
    }

    TOP

  • 相关阅读:
    Balance的数学思想构造辅助函数
    1663. Smallest String With A Given Numeric Value (M)
    1680. Concatenation of Consecutive Binary Numbers (M)
    1631. Path With Minimum Effort (M)
    1437. Check If All 1's Are at Least Length K Places Away (E)
    1329. Sort the Matrix Diagonally (M)
    1657. Determine if Two Strings Are Close (M)
    1673. Find the Most Competitive Subsequence (M)
    1641. Count Sorted Vowel Strings (M)
    1679. Max Number of K-Sum Pairs (M)
  • 原文地址:https://www.cnblogs.com/xin-le/p/4083881.html
Copyright © 2020-2023  润新知