【链表4】<最新>结构体数组实现表(Table)
文/Edward
typedef struct
{
int Number;
int WorkId;
char Name[20];
char Sex[10];
int age;
char PhoneNumber[20];
char HouseholdRegister[100];
} OfficeWorker_t;
#include<stdio.h>
#include<string.h>
static void WorkerInfoRecord(int Number, int WorkId, char *Name, char *Sex,
int age, char *PhoneNumber, char *HouseholdRegister);
typedef struct
{
int Number;
int WorkId;
char Name[20];
char Sex[10];
int age;
char PhoneNumber[20];
char HouseholdRegister[100];
} OfficeWorker_t;
OfficeWorker_t WorkInfo[5]; //定义OfficeWorker_t类型的结构体数组
int main(void)
{
int i;
WorkerInfoRecord(1, 60001, "Xiaoming", "Male", 34, "16677789076", "Shanghai");//填入小明的信息
WorkerInfoRecord(2, 60012, "XiaoLi", "Female", 29, "17767812312", "Jiangsu"); //填入小丽的信息
WorkerInfoRecord(3, 60106, "Xiaoqiang", "Male", 30, "18910023897", "Hubei"); //填入小强的信息
WorkerInfoRecord(4, 60113, "Xiaowang", "Male", 38, "17776388990", "Hubei"); //填入小王的信息
WorkerInfoRecord(5, 60128, "Xiaoyu", "Male", 38, "16090077882", "Xinjiang");//填入小于的信息
for(i = 0; i < 5; i ++)
{
printf("\t%d\t%d\t%-20s\t%s\t%d\t%s\t%s\n", WorkInfo[i].Number, WorkInfo[i].WorkId, WorkInfo[i].Name,\
WorkInfo[i].Sex, WorkInfo[i].age, WorkInfo[i].PhoneNumber, \
WorkInfo[i].HouseholdRegister);
}
}
static void WorkerInfoRecord(int Number, int WorkId, char *Name, char *Sex,
int age, char *PhoneNumber, char *HouseholdRegister)
{
WorkInfo[Number - 1].Number = Number;
WorkInfo[Number - 1].WorkId = WorkId;
strcpy(WorkInfo[Number - 1].Name, Name);
strcpy(WorkInfo[Number - 1].Sex, Sex);
WorkInfo[Number - 1].age = age;
strcpy(WorkInfo[Number - 1].PhoneNumber, PhoneNumber);
strcpy(WorkInfo[Number - 1].HouseholdRegister, HouseholdRegister);
}
#include<stdio.h>
#include<string.h>
typedef struct
{
int Number;
int WorkId;
char Name[20];
char Sex[10];
int age;
char PhoneNumber[20];
char HouseholdRegister[100];
} OfficeWorker_t;
const OfficeWorker_t WorkInfo[5] =
{
{1, 60001, "Xiaoming", "Male", 34, "16677789076", "Shanghai" },
{2, 60012, "XiaoLi", "Female", 29, "17767812312", "Jiangsu" },
{3, 60106, "Xiaoqiang", "Male", 30, "18910023897", "Hubei" },
{4, 60113, "Xiaowang", "Male", 38, "17776388990", "Hubei" },
{5, 60128, "Xiaoyu", "Male", 38, "16090077882", "Xinjiang" },
}; //定义OfficeWorker_t类型的结构体数组
int main(void)
{
int i;
for(i = 0; i < 5; i ++)
{
printf("\t%d\t%d\t%-20s\t%s\t%d\t%s\t%s\n", WorkInfo[i].Number, WorkInfo[i].WorkId, WorkInfo[i].Name,\
WorkInfo[i].Sex, WorkInfo[i].age, WorkInfo[i].PhoneNumber, \
WorkInfo[i].HouseholdRegister);
}
}
#include<stdio.h>
#include<string.h>
#define FAIL 0x00
#define SUCCESS 0xFF
typedef struct
{
int Number;
int WorkId;
char Name[20];
char Sex[10];
int age;
char PhoneNumber[20];
char HouseholdRegister[100];
} OfficeWorker_t;
const OfficeWorker_t WorkInfo[5] =
{
{1, 60001, "Xiaoming", "Male", 34, "16677789076", "Shanghai" },
{2, 60012, "XiaoLi", "Female", 29, "17767812312", "Jiangsu" },
{3, 60106, "Xiaoqiang", "Male", 30, "18910023897", "Hubei" },
{4, 60113, "Xiaowang", "Male", 38, "17776388990", "Hubei" },
{5, 60128, "Xiaoyu", "Male", 38, "16090077882", "Xinjiang" },
}; //定义OfficeWorker_t类型的结构体数组
int main(void)
{
int i;
int IdToSearch; //搜索的ID
int SearchResult = FAIL; //搜索结果
int SearchResultIndex = 0; //搜索成功的Index
printf("Input the worker's ID you want to search:");
scanf("%d", &IdToSearch);
/*遍历表*/
for(i = 0; i < 5; i ++)
{
if(WorkInfo[i].WorkId == IdToSearch)
{
SearchResult = SUCCESS;
SearchResultIndex = i;
break;
}
}
if(SearchResult == SUCCESS)
{
printf("\t%d\t%d\t%-20s\t%s\t%d\t%s\t%s\n", WorkInfo[SearchResultIndex].Number, WorkInfo[SearchResultIndex].WorkId, \
WorkInfo[SearchResultIndex].Name, WorkInfo[SearchResultIndex].Sex, \
WorkInfo[SearchResultIndex].age, WorkInfo[SearchResultIndex].PhoneNumber, \
WorkInfo[SearchResultIndex].HouseholdRegister);
}
else
{
printf("The worker is not found\n");
}
}
赞 (0)