zoukankan      html  css  js  c++  java
  • 二维数组转化成一维指针

    二维数组转化为一维指针来使用本实例用到了随机数,链表生成,遍历,有待扩展

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<time.h>
      4 #include<math.h>
      5 //定义个结构体Emp用来存放员工信息
      6 typedef struct  Emp
      7 {
      8     int eno;
      9     char *ename;
     10     int dno;
     11     Emp *next;
     12 
     13 }emp,*pemp;
     14 //定义个结构体Emp用来存放部门信息
     15 typedef struct Dept
     16 {
     17     int dno;
     18     char *dname;
     19     Dept *next;
     20 }dept,*pdept;
     21 /**
     22     Description: 每次调用,随机产生人名所在数组的地址,用来初始化员工信息 ,
     23 
     24     Arguments:
     25 
     26     Returns:返回数组下标地址
     27 
     28 */
     29 char * rand_ename(char ename[12][15],int  rand_num)
     30 {
     31 
     32     return (ename[0]+rand_num*15);
     33 }
     34 /**
     35     Description: 用来随机产生0-11之间的随机数
     36 
     37     Arguments:
     38 
     39     Returns: 0-11之间的随机数
     40 
     41 */
     42 int rand_num()
     43 {
     44     return (rand()%12)*15;
     45 }
     46 /**
     47     Description: 用来产生0-2之间的随机数 ,用来标记部门号 使用的时候再加上11,
     48                     也就是部门号最终为11,12,13
     49 
     50     Arguments:
     51 
     52     Returns:0-2之间的随机数
     53 
     54 */
     55 int rand_dno()
     56 {
     57 
     58     return rand()%8;
     59 }
     60 /**
     61     Description: 初始化emp,随机产生一个emp表
     62 
     63     Arguments:
     64 
     65     Returns:返回pemp类型一个头指针
     66 
     67 */
     68 pemp init_emp(char ename[][15])
     69 {
     70     int num_people=10;
     71     int i;
     72     pemp emp_head=(pemp)malloc(sizeof(pemp)+20);
     73     //申请空间失败自动退出
     74     if(emp_head==NULL)
     75     {
     76         exit(1);
     77     }
     78     pemp tem=emp_head;
     79     //srand放到for循环起不到效果,for循环所用时间非常短,随机数来不及产生
     80     srand((unsigned)time(0));
     81     for(i=0;i<num_people ;i++ )
     82     {
     83         pemp new_emp=(pemp)malloc(sizeof(pemp)+5);
     84         tem->next=new_emp;
     85         new_emp->eno=i+1;
     86         new_emp->ename=ename[0]+rand_num();
     87         new_emp->dno=rand_dno();
     88         tem=new_emp;
     89     }
     90     tem->next=NULL;
     91     return emp_head;
     92 }
     93 /**
     94     Description: 初始化部门信息
     95 
     96     Arguments:
     97 
     98     Returns:返回pdept类型的一个头指针
     99 
    100 */
    101 pdept init_dept(char dpartname[][15])
    102 {
    103     pdept dept_head=(pdept)malloc(sizeof(pdept));
    104     if(dept_head==NULL)
    105     {
    106         exit(1);
    107     }
    108     int i;
    109     pdept tem=dept_head;
    110     for(i=0;i<8;i++)
    111     {
    112         pdept new_dept=(pdept)malloc(sizeof(pdept));
    113         tem->next=new_dept;
    114         new_dept->dno=i+11;
    115         new_dept->dname=(dpartname[0]+i*15);
    116         tem=new_dept;
    117     }
    118     tem->next=NULL;
    119     return dept_head;
    120 }
    121 int main()
    122 {    int i;
    123      //数组用来初始化信息所用
    124     char ename[12][15]={
    125         "范冰冰","贾玲","刘亦菲","凤姐","baby","曹敏","张馨予","林志玲"
    126         ,"蒋勤勤","张静初","奶茶妹","搞基"
    127     };
    128     char dpartname[8][15]={
    129         "蔡兴家","张家晨","崔熊华","王强","吴未","王寅","杜桂宇","彭清泉"
    130     };
    131     pemp head=(pemp)malloc(sizeof(pemp));
    132     if(head==NULL)
    133     {
    134         exit(1);
    135     }
    136     head=init_emp(ename);
    137     pemp temp ;
    138     temp=head->next;
    139 
    140     while(temp->next!=NULL)
    141     {
    142         printf("%d	",temp->eno);
    143         printf("%s	",temp->ename);
    144         printf("%s	",dpartname[temp->dno]);
    145         printf("
    ");
    146         temp=temp->next;
    147     }
    148        printf("
    ");
    149     return 0;
    150 
    151 }

     这个程序运行结果实际上是为随即产生的女明星匹配一个男神!程序可扩展空间很大!

     

  • 相关阅读:
    PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)
    PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分)
    PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)
    PAT (Advanced Level) Practice 1035 Password (20 分)
    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)
    PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)
    从零开始吧
    Python GUI编程(TKinter)(简易计算器)
    PAT 基础编程题目集 6-7 统计某类完全平方数 (20 分)
    PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)
  • 原文地址:https://www.cnblogs.com/codeblock/p/4692984.html
Copyright © 2011-2022 走看看