飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2239|回复: 0

编程马拉松(13)联合

[复制链接]

该用户从未签到

发表于 2007-8-26 07:53:14 | 显示全部楼层 |阅读模式
联合:
定义一个联合类型的一般形式为:
union 联合名
{
成员表
};
成员表中含有若干成员,成员的一般形式为: 类型说明符 成员名 成员名的命名应符合标识符的规定。
例如:
union perdata
{
int class;
char office[10];
};
  定义了一个名为perdata的联合类型,它含有两个成员,一个为整型,成员名为class;另一个为字符数组,数组名为office。联合定义之后,即可进行联合变量说明,被说明为perdata类型的变量,可以存放整型量class或存放字符数组office。

如果还不理解就这样看吧:把perdata当个变量用,他可以放int和char的数据。你还可以给他更多的数据类型。
设有一个教师与学生通用的表格,教师数据有姓名,年龄,职业,教研室四项。学生有姓名,年龄,职业,班级四项。
编程输入人员数据, 再以表格输出。
其中(教研室,班级)分别为字符型和整型可以这样输入:教研室:数学教研室,班级:61
//有了前面几个关于结构体的题这个很好写了。
//freesoft:C++

#include <iOStream>
#include <string>
using namespace std;
struct test
{
char name[20];
int nianling;
int zhiyie;
union
{
char jy[20];
int bj;
}jybj;

test *next;
};
test *index;//全局指针
//test tmpbegin[5];
test *setdata()//输入数据
{
       
        test *tmpbegin=new test;
        //for(int i=0;i<5;i++)
        //{
        cout<<"输入教师和学生数据,输入Q结束。"<<endl;
        cout<<"姓名";
        cin>>tmpbegin->name;
        int tmpend0;
        tmpend0=strcmp(tmpbegin->name,"Q");
        if(tmpend0==0)return index;
        cout<<"年龄";
        cin>>tmpbegin->nianling;
        int tmp=1;
        while(tmp==1)
        {
                cout<<"职业(1--教师,2--学生):";
                cin>>tmpbegin->zhiyie;
                switch(tmpbegin->zhiyie)
                {
                case 1:
                        cout<<"教研室或班级:";
                        cin>>tmpbegin->jybj.jy;
                        tmp=0;
                        break;
                case 2:
                        cout<<"教研室或班级";
                        cin>>tmpbegin->jybj.bj;
                        tmp=0;
                        break;
                default :
                        cout<<"您输入错误,请重新输入"<<endl;
                        tmp=1;
                }
        }
        //}
        index=NULL;
        test *tmpend=tmpbegin;
        while (tmpbegin->name!=NULL)
        {
                if(index==NULL)
                        index=tmpbegin;
                else
                        tmpend->next=tmpbegin;
                tmpend=tmpbegin;
                tmpbegin=new test;
                        cout<<"输入教师和学生数据,输入0结束。"<<endl;
                        cout<<"姓名";
                        cin>>tmpbegin->name;
                        tmpend0=strcmp(tmpbegin->name,"Q");
                        if (tmpend0==0)return index;
                        cout<<"年龄";
                        cin>>tmpbegin->nianling;
                        int tmp=1;
        while(tmp==1)
        {
                cout<<"职业(1--教师,2--学生):";
                cin>>tmpbegin->zhiyie;
                switch(tmpbegin->zhiyie)
                {
                case 1:
                        cout<<"教研室或班级:";
                        cin>>tmpbegin->jybj.jy;
                        tmp=0;
                        break;
                case 2:
                        cout<<"教研室或班级:";
                        cin>>tmpbegin->jybj.bj;
                        tmp=0;
                        break;
                default :
                        cout<<"您输入错误,请重新输入"<<endl;
                        tmp=1;
                }
        }
        }
        tmpend->next=NULL;
        delete tmpbegin;
        return index;
}
void showdata()
{
        test *tmpbegin=index;
        //for(int i=0;i<5;i++)
        //{
                cout<<tmpbegin->name;
                cout<<tmpbegin->nianling;

                switch(tmpbegin->zhiyie)
                {
                case 1:
                        cout<<"教师  "<<tmpbegin->jybj.jy<<endl;
                        break;
                case 2:
                        cout<<"学生  "<<tmpbegin->jybj.bj<<endl;
                        break;
                }

                       
                tmpbegin=tmpbegin->next;
        //}

}
void main()
{
        setdata();
        showdata();
}
PYG19周年生日快乐!
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表