C++基础实例-结构类型(3)

struct

#if 0
#include<iostream>
using namespace std;
main()
{
    //定义结构类型
    struct    books
    {
    char   title[20];
    char   author[15];
    int    pages;
    float  price;
    } ;
    
    //声明结构变量
    struct books Zbk={"VC++ ","Xc",295,35.5}; 
    books Wbk;  

    //对结构变量的输出
    cout<<"Zbk:"<<endl;
    cout<<Zbk.title <<endl;
    cout<<Zbk.author<<endl;
    cout<<Zbk.pages<<endl;
    cout<<Zbk.price<<endl;
    cout<<"--------------------"<<endl;

    //对结构成员的运算
    Zbk.pages+=10;
    Zbk.price+=0.5;
    cout<<"Zbk.pages="<<Zbk.pages<<endl;
    cout<<"Zbk.price="<<Zbk.price<<endl;
    cout<<"--------------------"<<endl;

    //对结构变量的输入输出
    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;

    //结构变量之间的相互赋值
    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数组

#if 0
#include<iostream>
using namespace std;
main()
{
    int i;
    //定义结构类型 
    struct student {
           int  num;
           char  name[10];
           float maths;
           float physics;
           float chemistry;
           double  total;
    };

     //声明结构数组st
     student st[3];

     //从键盘上为结构数组输入值 
     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;
     }

    //计算每个学生的总成绩
    for (i=0;i<3;i++)
         st[i].total=st[i].maths+st[i].physics+st[i].chemistry;

    //输出结构数组各元素的值 
    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指针

#if 0
/*结构体指针*/
#include<iostream>
using namespace std;
main()
{
    //定义结构类型
    struct human {
       char name[10];
       int sex;
       int age;
    };

    //声明结构变量和结构指针变量,并初始化
    struct human x={"XieCh",1,21},*p=NULL;

    //结构指针变量指向对象
    p=&x;

    //显示结构变量的值
    cout<<"x.name="<<x.name<<endl;
    cout<<"x.sex="<<x.sex<<endl;
    cout<<"x.age="<<x.age<<endl;
  
    //利用结构指针显示结构对象中的数据
    cout<<"(*p).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;

    //通过结构指针为结构对象输入数据
    cout<<"name:";
    cin>>(*p).name;
    cout<<"sex:";
    cin>>(*p).sex;
    cout<<"age:";
    cin>>(*p).age;

    //显示结构变量的值
    cout<<"x.name="<<x.name<<endl;
    cout<<"x.sex="<<x.sex<<endl;
    cout<<"x.age="<<x.age<<endl;
}
#endif

内存分配

#if 0
#include<iostream>
using namespace std;
main()
{
    //定义结构类型
    struct human {
       char name[10];
       int sex;
       int age;
       };

    //声明结构变量和结构指针,并初始化
    struct human x={"XieCh",1,21},*p=&x;
 
    //利用结构指针显示结构中的数据
    cout<<"(*p).name="<<(*p).name<<endl;
    cout<<"(*p).sex="<<(*p).sex<<endl;
    cout<<"(*p).age="<<(*p).age<<endl;
    cout<<"-------------------------"<<endl;

    //利用new运算符为p分配内存
    p=new human;

    //从键盘上为p指向的结构对象赋值
    cout<<"p->name=";
    cin>>p->name;
    cout<<"p->sex=";
    cin>>p->sex;
    cout<<"p->age=";
    cin>>p->age;
    cout<<"-------------------------"<<endl;

    //显示p所指结构对象的值
    cout<<"p->name="<<p->name<<endl;
    cout<<"p->sex="<<p->sex<<endl;
    cout<<"p->age="<<p->age<<endl;
    cout<<"-------------------------"<<endl;

    //显示结构变量的值
    cout<<"x.name="<<x.name<<endl;
    cout<<"x.sex="<<x.sex<<endl;
    cout<<"x.age="<<x.age<<endl;

    //释放p指向的内存
    delete p;  
}
#endif

结构数组

#if 0
#include<iostream>
using namespace std;
main()
{
    //定义结构类型
    struct human {
       char name[10];
       int sex;
       int age;
    };

    //声明结构数组和结构指针变量,并初始化
    human x[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL;

    //用下标变量的输出结构数组的元素
    for (int i=0;i<3;i++)
    {
        cout<<x[i].name<<'\t';
        cout<<x[i].sex<<'\t';
        cout<<x[i].age<<endl;
    }
    cout<<"----------------"<<endl;

    //用结构指针输出结构数组的元素
    for (p=x;p<=&x[2];p++)
    {
        cout<<p->name<<'\t';
        cout<<p->sex<<'\t';
        cout<<p->age<<endl;
    }
}
#endif

结构体指针

#if 0
/*结构体指针*/
#include<iostream>
using namespace std;
main()
{
    //定义一个包含指针成员的结构类型
    struct test {
       char *str;
       int  *ip;
    }x;

    //使用结构变量x中的整型指针ip
    x.ip=new int;    //分配1个单元
    *(x.ip)=100;
    cout<<"x.ip:"<<x.ip<<'\t'<<*(x.ip)<<endl;
    cout<<"---------------"<<endl;
    delete x.ip;
    x.ip=new int[5];    //分配5个单元
    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;

    //使用结构变量x中的字符型指针str
    x.str=new char('A');    //分配1个单元
    cout<<"x.str:"<<(*x.str)<<endl;
    cout<<"---------------"<<endl;
    delete x.str;
    x.str=new char[5];    //分配多个单元
    *x.str='s';
    *(x.str+1)='w';
    *(x.str+2)='x';
    *(x.str+3)='c';
    *(x.str+4)='\0';
    cout<<"x.str:"<<x.str<<endl;
    delete x.str;
    cout<<"---------------"<<endl;

    //在声明结构变量时初始化
    test y={"Very Good!",NULL};
    cout<<"y.str:"<<y.str<<endl;
    cout<<"y.ip:"<<y.ip<<endl;
}
#endif

结构体嵌入

#if 0
/*结构体嵌入*/
#include<iostream>
using namespace std;
main()
{
    //定义date结构
    struct date
    {
       int year;
       int month;
       int day;
    };

    //定义baby结构
    struct baby {
        int    num;
        float   weight;
        date   birthday;   // date为结构类型 
    }; 
	
    //声明baby结构变量并初始化
    baby b1={10001,10,{2011,06,16}};

    //下列是baby结构变量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;

    //声明baby结构变量temp,并进行赋值运算
    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="<<temp.birthday.day<<endl;
}
#endif

递归

#if 0
/*递归*/
#include<iostream>
using namespace std;
main()
{
    //定义名为list的递归结构  
    struct list {
          char         name[10];
          int          sex;
          int          age;
          list         *next;   //成员next为指向其自身结构的指针
    };

    //使用递归结构变量
    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

链表与结构体

#if 0
/*链表与结构体*/
#include<iostream>
using namespace std;
main()
{
    int i;
    //定义名为student的递归结构 
    struct  student {
           char name[10];
           int  math;
           int  computer;
           float sum;
           student *next;    //next成员是指向自身的结构指针 
    };

    //用student声明3个结构指针变量
    struct student *head,*tail,*temp;  	

    //申请第1块数据,并设置各结构指针的初值
    temp=new struct student;    //申请内存 
    head=temp;   // 头指针 
    tail=head;    // 尾指针 

    //循环为链表输入数据
    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;      //设置链表尾指针 
         }
         else
         {
          // 以下是输入结束处理 
            delete temp;
            tail->next=NULL;
            break;
         }
        //为下一个学生申请内存
        temp->next=new struct student; 
        temp=temp->next;    // 使处理指针temp指向新内存块
    }

    //将链表数据从头到尾打印出来
    cout<<"--------------------"<<endl;
    temp=head;
    while (temp!=NULL) {
           cout<<temp->name<<","<<temp->math<<",";
           cout<<temp->computer<<","<<temp->sum<<endl;
           temp=temp->next;
     }
}
#endif

结构体递归

#if 0
/*结构体递归*/
#include<iostream>
using namespace std;
main()
{
    int i;
    //定义名为student的递归结构 
    struct  student {
           char name[10];
           int  math;
           int  computer;
           float sum;
           student *forw;    //forw成员是前指针 
           student *next;    //next成员是后指针
    };

    //用student声明3个结构指针变量
    struct student *head,*tail,*temp;

    //申请第1块数据,并设置各结构指针的初值
    temp=new struct student;    //申请内存 
    head=temp;      // 头指针 
    tail=head;      // 尾指针 
    head->forw=NULL;

    //循环为链表记录输入数据
    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;      //设置链表尾指针 
         }
         else
         {
          // 以下是输入结束处理 
            delete temp;
            tail->next=NULL;
            break;
         }
        //为下一个学生申请内存
        temp->next=new struct student; 
        temp->next->forw=temp;   //设置前指针
        temp=temp->next;         //使处理指针temp指向新内存块
    }

    // 将链表数据从头到尾打印出来
    cout<<"head------>tail:"<<endl;
    temp=head;
    while (temp!=NULL) {
           cout<<temp->name<<","<<temp->math<<",";
           cout<<temp->computer<<","<<temp->sum<<endl;
           temp=temp->next;
     }

    // 将链表数据从尾到头打印出来
    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
/*联合体-共用体*/
#include<iostream>
using namespace std;
main()
{
    int i;
    //定义联合类型
    union utag  {
          char    c;
          int     k;
          float   x;
    };

    //声明联合变量
    union utag u;

    // 使用联合变量中的字符型成员 
    u.c='*';
    cout<<"u.c="<<u.c<<endl;

    // 使用联合变量中的整型成员 
    u.k=1000;
    cout<<"u.k="<<u.k<<endl;

    // 使用联合变量中的浮点型成员 
    u.x=3.1416;
    cout<<"u.x="<<u.x<<endl;

    //声明联合变量时初始化
    utag u1={'A'};

    //同时引用联合变量的各成员
    cout<<"u1.c="<<u1.c<<endl;
    cout<<"u1.k="<<u1.k<<endl;
    cout<<"u1.x="<<u1.x<<endl;
}
#endif

结构体sizeof

#if 0
/*结构体sizeof*/
#include<iostream>
using namespace std;
main()  
{
    //定义结构类型,并为声明的结构变量赋初值
    struct s_tag {
           short    i;
           float x;
    } sx={100,3.1416};

    //定义联合类型,并为声明的联合变量赋初值
    union   u_tag  {
            short    i;
            float x;
    } ux={1000};

    //输出结构类型和结构变量的有关信息
    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;

    //输出联合类型和联合变量的有关信息
    cout<<"sizeof(union u_tag)="<<sizeof(union u_tag)<<endl;
    ux.i=200;
    cout<<"ux.i="<<ux.i<<endl;  //输出联合变量ux 的i成员
    ux.x=123.456;
    cout<<"ux.x="<<ux.x<<endl;  //输出联合变量ux 的x成员
    cout<<"sizeof(ux)="<<sizeof(ux)<<endl;
}
#endif

typedef

#if 0
/*typedef*/
#include<iostream>
using namespace std;
main()
{
    //自定义类型 
    typedef  int  ARRAY_INT[50];
    int i;
    ARRAY_INT a;    //用自定义类型声明数组变量a 

    //以下为数组a赋值,并打印  
    for (i=0;i<50;i++) {
       if (i%5==0)       //每10个数换一次行 
         cout<<endl;
       a[i]=i;
       cout<<a[i]<<"\t";
     }
    cout<<endl;
}
#endif

综合

#if 0
/*sizeof综合*/
#include<iostream>
using namespace std;
//定义结构类型
struct student
{
    int   num;
    char  name[20];
    float grade;
};
void main(void)
{
    //声明数组
    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};
		
    //显示char类型数组元素及其大小
    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;

    //显示int类型数组元素及其大小
    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;

    //显示float类型数组元素及其大小
    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;

    //显示student类型数组元素及其大小
    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Swxctx

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值