C + + Base instance-struct type (3)

Source: Internet
Author: User

struct
#if 0#include<iostream>using namespace Std;main () {//define struct type struct books {char title[20];    Char author[15];    int pages;    float price;        } ;     Declare struct variable struct books zbk={"VC + +", "Xc", 295,35.5};      Books WBK;    The output of the structure variable cout<< "ZBK:" <<endl;    Cout<<zbk.title <<endl;    cout<<zbk.author<<endl;    cout<<zbk.pages<<endl;    cout<<zbk.price<<endl;    cout<< "--------------------" <<endl;    zbk.pages+=10 the operation of structure members;    zbk.price+=0.5;    cout<< "zbk.pages=" <<Zbk.pages<<endl;    cout<< "Zbk.price=" <<Zbk.price<<endl;    cout<< "--------------------" <<endl;    The input and output of the structure variable cout<< "wbk.title =";    cin>>wbk.title;    cout<< "wbk.author=";    cin>>wbk.author;    cout<< "wbk.pages=";    cin>>wbk.pages;    cout<< "wbk.price=";    cin>>wbk.price;    cout<< "WBK:" <<endl;Cout<<wbk.title <<endl;    cout<<wbk.author<<endl;    cout<<wbk.pages<<endl;    cout<<wbk.price<<endl;    cout<< "--------------------" <<endl;    Mutual assignment of structural variables books temp;    TEMP=WBK;    cout<< "Temp:" <<endl;    cout<<temp.title<<endl;    cout<<temp.author<<endl;    cout<<temp.pages<<endl; Cout<<temp.price<<endl;} #endif
struct array
#if 0#include<iostream>using namespace Std;main () {int i;           Define struct type struct student {int num;           Char name[10];           float maths;           float physics;           float chemistry;    Double total;     };     Declaration Structure Array St student st[3];     Enter a value for the structure array from the keyboard cout<< "num name Maths physics chemistry" <<endl;        for (i=0;i<3;i++) {cout<<i+1<< "";        cin>>st[i].num;        cin>>st[i].name;        cin>>st[i].maths;        cin>>st[i].physics;     cin>>st[i].chemistry;    }//Calculates the total of each student for (i=0;i<3;i++) st[i].total=st[i].maths+st[i].physics+st[i].chemistry;        Outputs the values of the elements of the structure array for (i=0;i<3;i++) {cout<< "st[" <<i<< "]:";        cout<<st[i].num<< ' \ t ';        cout<<st[i].name<< ' \ t ';        cout<<st[i].maths<< ' \ t ';        cout<<st[i].physics<< ' \ t '; Cout<<st[i].chemistry<< ' \ t ';     cout<<st[i].total<<endl; }} #endif
struct pointer
#if 0/* Structure pointer */#include <iostream>using namespace Std;main () {//define struct type struct human {char name[10];       int sex;    int age;    };    Declare struct variables and struct pointer variables, and initialize the struct human x={"Xiech", 1,21},*p=null;    struct pointer variable points to object p=&x;    Displays the value of the structure variable cout<< "x.name=" <<x.name<<endl;    cout<< "x.sex=" <<x.sex<<endl;      cout<< "X.age=" <<x.age<<endl;    Use the structure pointer to display the data cout<< "(*p) in the structure object. Name=" << (*p) .name<<endl;    cout<< "(*p). sex=" << (*p) .sex<<endl;    cout<< "(*p). age=" << (*p) .age<<endl;    cout<< "P->name=" <<p->name<<endl;    cout<< "p->sex=" <<p->sex<<endl;    cout<< "P->age=" <<p->age<<endl;    Enter data cout<< "name:" For the structure object through the structure pointer;    Cin>> (*p). Name;    cout<< "Sex:";    Cin>> (*p). sex;    cout<< "Age:";    Cin>> (*p). Age; Displays the value of the structure variable cout<< "x.name=" <<x.name<<endl;    cout<< "x.sex=" <<x.sex<<endl; cout<< "x.age=" &LT;&LT;X.AGE&LT;&LT;ENDL;} #endif
Memory allocation
#if 0#include<iostream>using namespace Std;main () {//define struct type struct human {char name[10];       int sex;       int age;    };     declares struct variables and struct pointers, and initializes the struct human x={"Xiech",1,21},*p=&x;    Use the structure pointer to display data cout<< "(*p) in the structure. Name=" << (*p) .name<<endl;    cout<< "(*p). sex=" << (*p) .sex<<endl;    cout<< "(*p). age=" << (*p) .age<<endl;    cout<< "-------------------------" <<endl;    Using the new operator to allocate memory for P p=new human;    Assigns a value cout<< "p->name=" to the structure object that the P points to on the keyboard;    cin>>p->name;    cout<< "p->sex=";    cin>>p->sex;    cout<< "p->age=";    cin>>p->age;    cout<< "-------------------------" <<endl;    Displays the value of the structure object referred to by P cout<< "P->name=" <<p->name<<endl;    cout<< "p->sex=" <<p->sex<<endl;    cout<< "P->age=" <<p->age<<endl;    cout<< "-------------------------" <<endl; Display the value of a structure variable   cout<< "X.name=" <<x.name<<endl;    cout<< "x.sex=" <<x.sex<<endl;    cout<< "X.age=" <<x.age<<endl;  Releases the memory that P points to delete p; } #endif
Array of structures
#if 0#include<iostream>using namespace Std;main () {    //define struct type    struct human {       char name[10];       int sex;       int age;    };    Declares the structure array and the struct pointer variable, and initializes the    human x[]={{"weiping", 1,30},{"Lihua", 1,25},{"Liumin", 0,23}},*p=null;    Use the subscript variable for the output structure of the array element for    (int i=0;i<3;i++)    {        cout<<x[i].name<< ' \ t ';        cout<<x[i].sex<< ' \ t ';        cout<<x[i].age<<endl;    }    cout<< "----------------" <<endl;    The structure pointer is used to output the elements of the structure array for    (P=x;p<=&x[2];p + +)    {        cout<<p->name<< ' \ t ';        cout<<p->sex<< ' \ t ';        cout<<p->age<<endl;    }} #endif
struct-Body pointer
#if 0/* struct pointer */#include <iostream>using namespace Std;main () {//defines a struct type that contains pointer members struct test {char *str;    int *ip;    }x;    Use integer pointer ip x.ip=new int in struct variable x;    Allocate 1 units * (X.IP) = 100;    cout<< "X.ip:" <<x.ip<< ' \ t ' <<* (X.IP) <<endl;    cout<< "---------------" <<endl;    Delete X.ip;    X.ip=new Int[5];    Allocate 5 units for (int i=0;i<5;i++) * (x.ip+i) =100+i;    cout<< "X.ip:" <<endl;    for (i=0;i<5;i++) cout<<x.ip+i<< ' \ t ' << (* (x.ip+i)) <<endl;    Delete X.ip;    cout<< "---------------" <<endl;    Use the character-type pointer in structure variable x str x.str=new char (' A ');    Allocate 1 units cout<< "X.STR:" << (*X.STR) <<endl;    cout<< "---------------" <<endl;    Delete x.str;    X.str=new Char[5];    Assigning multiple units *x.str= ' s ';    * (x.str+1) = ' W ';    * (x.str+2) = ' x ';    * (x.str+3) = ' C ';    * (x.str+4) = ' + ';    cout<< "X.STR:" <<x.str<<endl;    Delete x.str; cout<< "---------------" <<endl;    Initialize Test y={"Very good!" when declaring a struct variable, NULL};    cout<< "Y.STR:" <<y.str<<endl; cout<< "Y.ip:" &LT;&LT;Y.IP&LT;&LT;ENDL;} #endif
Structure embedding
#if 0/* Structure Embedding */#include <iostream>using namespace Std;main () {//define date struct struct Date {int year;       int month;    int day;    };        Defines baby struct struct baby {int num;        float weight;   Date birthday;     Date is a struct type};    Declares a baby struct variable and initializes baby b1={10001,10,{2011,06,16}};    The following is a reference to the baby struct variable B1.    cout<< "b1.num=" <<b1.num<<endl;    cout<< "b1.weight=" <<b1.weight<<endl;    cout<< "B1.birthday.year=" <<b1.birthday.year<<endl;    cout<< "B1.birthday.month=" <<b1.birthday.month<<endl;    cout<< "b1.birthday.day=" <<b1.birthday.day<<endl;    cout<< "--------------------------" <<endl;    Declares the baby structure variable temp, and assigns the Operation baby Temp;    TEMP=B1;    cout<< "temp.num=" <<temp.num<<endl;    cout<< "temp.weight=" <<temp.weight<<endl;    cout<< "Temp.birthday.year=" <<temp.birthday.year<<endl; cout<< "Temp.birthday.month= "<<temp.birthday.month<<endl; cout<< "temp.birthday.day=" &LT;&LT;TEMP.BIRTHDAY.DAY&LT;&LT;ENDL;} #endif
Recursive
#if 0/* recursion */#include <iostream>using namespace Std;main () {    //define a recursive struct named list      struct list {          char         NAME[10];          int          sex;          int age          ;          List         *next;   The member next is a pointer to its own structure    };    Use recursive structure variables    list l1={"Xiech", 1,35.5,null};    cout<< "L1:" <<endl;    cout<< "Name\t" <<L1.name<<endl;    cout<< "sex\t" <<L1.sex<<endl;    cout<< "Age\t" <<L1.age<<endl;    cout<< "next\t" <<L1.NEXT<<ENDL;} #endif
Linked lists and structural bodies
#if 0/* linked list and struct */#include <iostream>using namespace Std;main () {int i;           Defines a recursive struct student {char name[10] named student;           int math;           int computer;           float sum;    Student *next;    The next member is a pointer to the structure of itself};      Declare 3 structure pointer variable struct student *head,*tail,*temp with student;    Apply for the 1th piece of data and set the initial value of each structure pointer temp=new struct student;   Request Memory Head=temp;    Head pointer tail=head;    Tail pointer//loop for linked list input data cout<< "\tname Math Computer" <<endl; for (I=1;;        i++) {cout<<i<< "\ t";        cin>>temp->name;            if (temp->name[0]!= ' * ') {cin>>temp->math>>temp->computer;            temp->sum=temp->math+temp->computer;            temp->next=null;      Tail=temp;            Set the chain footer pointer} else {//The following is the input end processing Delete temp;            tail->next=null;         Break }//apply for memory te for the next studentmp->next=new struct student;    temp=temp->next;    Causes the processing pointer temp to point to the new memory block}//To print the linked list data from beginning to end cout<< "--------------------" <<endl;    Temp=head;           while (temp!=null) {cout<<temp->name<< "," <<temp->math<< ",";           cout<<temp->computer<< "," <<temp->sum<<endl;     temp=temp->next; }} #endif
struct recursion
#if 0/* Structure recursion */#include <iostream>using namespace Std;main () {int i;           Defines a recursive struct student {char name[10] named student;           int math;           int computer;           float sum;    Student *forw;    FORW member is the former pointer student *next;    Next member is the back pointer};    Declare 3 structure pointer variable struct student *head,*tail,*temp with student;    Apply for the 1th piece of data and set the initial value of each structure pointer temp=new struct student;      Request Memory Head=temp;      Head pointer tail=head;    Tail pointer head->forw=null;    Loop record input data for linked list cout<< "\tname Math Computer" <<endl; for (I=1;;        i++) {cout<<i<< "\ t";        cin>>temp->name;            if (temp->name[0]!= ' * ') {cin>>temp->math>>temp->computer;            temp->sum=temp->math+temp->computer;            temp->next=null;      Tail=temp;            Set the chain footer pointer} else {//The following is the input end processing Delete temp; Tail->next=nULL;         Break         }//For the next student to apply for memory temp->next=new struct student;   temp->next->forw=temp;         Set the front pointer temp=temp->next;    Causes the processing pointer temp to point to the new memory block}//To print the linked list data from beginning to end cout<< "head------>tail:" <<endl;    Temp=head;           while (temp!=null) {cout<<temp->name<< "," <<temp->math<< ",";           cout<<temp->computer<< "," <<temp->sum<<endl;     temp=temp->next;    }//To print the linked list data from the end of the head cout<< "tail------>head:" <<endl;    Temp=tail;           while (temp!=null) {cout<<temp->name<< "," <<temp->math<< ",";           cout<<temp->computer<< "," <<temp->sum<<endl;     temp=temp->forw; }} #endif
Union
#if 0/* Consortium-Shared body */#include <iostream>using namespace Std;main () {    int i;    Define Union type    Union utag  {          char    C;          int     k;          float   x;    };    Declares union variable    Union utag u;    Use the character member in the union variable     u.c= ' * ';    cout<< "u.c=" <<u.c<<endl;    Use integral member u.k=1000 in a union variable     ;    cout<< "u.k=" <<u.k<<endl;    Use floating-point member u.x=3.1416 in a union variable     ;    cout<< "u.x=" <<u.x<<endl;    Initialize    utag u1={' A '} when declaring union variables;    Also refer to the members of the Union variable    cout<< "u1.c=" <<u1.c<<endl;    cout<< "u1.k=" <<u1.k<<endl;    cout<< "u1.x=" <<U1.X<<ENDL;} #endif
Structure sizeof
#if 0/* struct sizeof*/#include <iostream>using namespace Std;main () {//defines the struct type and assigns the initial value struct S_tag to the declared structure variable {           Short I;    float x;    } sx={100,3.1416};            Define a union type and assign an initial value of union U_tag {short I to the declared union variable;    float x;    } ux={1000};    Information about the output structure type and structure variables cout<< "sizeof (struct S_tag) =" <<sizeof (struct S_tag) <<endl;    cout<< "sx.i=" <<sx.i<<endl;    cout<< "sx.x=" <<sx.x<<endl;    cout<< "sizeof (SX) =" <<sizeof (SX) <<endl;    cout<< "------------------------------" <<endl;    Information about the output union type and union variable cout<< "sizeof (union U_tag) =" <<sizeof (Union U_tag) <<endl;    ux.i=200;  cout<< "ux.i=" <<ux.i<<endl;    Output Union variable i member of UX ux.x=123.456;  cout<< "ux.x=" <<ux.x<<endl; The x member of the output union variable UX cout<< "sizeof (UX) =" <<sizeof (UX) <<endl;} #endif
typedef
#if 0/*typedef*/#include <iostream>using namespace Std;main () {    //Custom type     typedef  INT  Array_int [n];    int i;    Array_int A;    Declare the array variable a     //below with the custom type A, and print the For      (i=0;i<50;i++) {       if (i%5==0)       //Every 10 digits for a row          cout<< Endl;       a[i]=i;       cout<<a[i]<< "\ t";     }    Cout<<endl;} #endif
Comprehensive
#if 0/*sizeof Synthesis */#include <iostream>using namespace std;//define struct type struct student{int num;    Char name[20]; float grade;};    void Main (void) {//declare array int i,size;    Char str[]= "This is a string.";     int int_values[] = {51, 23, 2, 44, 45,0,11};      Float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5};    Student st_arr[]={101, "Wanglin", 92,102, "Liping", 85,103, "Zhaomin", 88};    Displays a char type array element and its size size=sizeof (str)/sizeof (char);    cout<< "Number of elements in str:";    cout<<size<<endl;    for (i=0;i<size;i++) {cout<<str[i];    } cout<<endl;    Displays an array element of type int and its size size=sizeof (int_values)/sizeof (int);    cout<< "Number of elements in int_values:";    cout<<size<<endl;    for (i=0;i<size;i++) {cout<<int_values[i]<< "";    } cout<<endl;    Displays the float type array element and its size size=sizeof (float_values)/sizeof (float);    cout<< "Number of elements in float_values:"; Cout<<size<<endl;    for (i=0;i<size;i++) {cout<<float_values[i]<< "";    } cout<<endl;    Displays an array element of type student and its size size=sizeof (St_arr)/sizeof (student);    cout<< "Number of elements in St_arr:";    cout<<size<<endl;        for (i=0;i<size;i++) {cout<<st_arr[i].num<< "";        cout<<st_arr[i].name<< "";    cout<<st_arr[i].grade<<endl; }} #endif

C + + Base instance-struct type (3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.