使用c语言存储100名员工的记录
使用c语言,我们如何在变量中存储100个员工记录,如员工姓名、职务、工资等,以及如何访问这些记录。简而言之,您可以首先创建一个Emp structurestruct,其中包含姓名、工资等属性。。。然后创建一个包含100个这样的Emp结构的数组 要访问它,您只需在这个数组中循环,对于每个结构,您可以访问name、salary等 希望有帮助。这里有一个例子:使用c语言存储100名员工的记录,c,C,使用c语言,我们如何在变量中存储100个员工记录,如员工姓名、职务、工资等,以及如何访问这些记录。简而言之,您可以首先创建一个Emp structurestruct,其中包含姓名、工资等属性。。。然后创建一个包含100个这样的Emp结构的数组 要访问它,您只需在这个数组中循环,对于每个结构,您可以访问name、salary等 希望有帮助。这里有一个例子: #include <stdio.h> #include <stdlib.h> #include <string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTR 50
#define MAXEMPLOYEES 100
typedef struct {
char name[MAXSTR];
char designation[MAXSTR];
float salary;
} Employee;
typedef struct {
Employee employeesArray[MAXEMPLOYEES];
int count;
} Employees;
void initEmployees(Employees*);
Employee readEmployee();
void addEmployee(Employees*, Employee);
void showAllEmployees(Employees);
int main()
{
int choice;
Employee newEmployee;
Employees employeesArchive;
initEmployees(&employeesArchive);
do{
printf("1. Add new Employee.\n");
printf("2. Show all Records.\n");
printf("0. Exit.\n");
printf("Select: ");
scanf("%d", &choice);
switch(choice){
case 1:
newEmployee = readEmployee();
addEmployee(&employeesArchive, newEmployee);
break;
case 2:
showAllEmployees(employeesArchive);
break;
}
}while(choice!=0);
return 0;
}
void initEmployees(Employees *_employees){
_employees->count = 0;
}
Employee readEmployee(){
Employee _newEmployee;
printf("NEW RECORD\n");
printf("-> Name: ");
scanf("%s", _newEmployee.name);
printf("-> Designation: ");
scanf("%s", _newEmployee.designation);
printf("-> Salary: ");
scanf("%f", &_newEmployee.salary);
return _newEmployee;
}
void addEmployee(Employees *_employees, Employee _newEmployee){
int index;
index = _employees->count;
strcpy(_employees->employeesArray[index].name, _newEmployee.name);
strcpy(_employees->employeesArray[index].designation, _newEmployee.designation);
_employees->employeesArray[index].salary = _newEmployee.salary;
_employees->count++;
printf("New Employee correctly added!\n");
system("pause");
}
void showAllEmployees(Employees _employees){
int i, allRecords;
allRecords = _employees.count;
for(i=0; i<allRecords; i++){
printf("#%d\nName: %s\tDesignation: %s\tSalary: %f\n",
i+1,
_employees.employeesArray[i].name,
_employees.employeesArray[i].designation,
_employees.employeesArray[i].salary);
printf("\n");
}
}
最好创建一个链接列表 这是一种数据结构,在这种结构中,您必须创建一个对象,其中包含employee的所有信息,以及指向列表中其他对象的指针 基于您的问题,我必须建议您在继续之前阅读数据结构 有几种类型的数据结构,如单链表、循环链表、双链表、树等。。但在这里,您可以根据自己的需求尝试最基本的功能,例如单链接列表
#include<stdio.h>
#include<stdlib.h>
#define LEN=20;
//this is our object definition
typedef struct EMPLOYER
{
char name[LEN],designation[LEN];
int salary;
struct EMPLOYER* next_object;
};
void add_employer_details();
void print_employers_details();
EMPLOYER** main_pointer;
char op;
int main()
{
printf("shall we start storing data of employee...?(y/n)\n");
scanf(" %c",&op);
while(op=='y' || op=='Y')
//to add new employee's detail
add_employer_detail();
//print whole database
print_employers_detail();
}
void add_employer_detail()
{
//creating a node
EMPLOYER *temp;
//allocating memory
temp=(EMPLOYER*)malloc(sizeof(EMPLOYER));
printf("enter details of employer...\n");
//feeling the data
gets(temp->name);
gets(temp->designation);
scanf("%d",&(temp->salary));
//making the link with other nodes
temp->next=main_pointer;
main_pointer=temp;
printf("want a new entry?(y/n)");
scanf(" %c",&op);
}
void print_employer_detail()
{
EMPLOYER* temp=*main_pointer;
while(temp)
{
printf(" name of employee : %20s\n designation of employee : %20s\n salary of employee : %8d\n",temp->name,temp->designation,temp->salary);
temp=temp->next;
}
}
当涉及到数据管理系统时,动态分配内存始终是一个好习惯。但您始终确保在不再需要内存时释放内存,以避免内存泄漏: 这是您的第一个问题,您已经成为会员2年零9个月了!在这段时间里,你为什么从来没有尝试过阅读,尤其是那些命名为和的章节。到现在为止,您还应该知道,以及如何创建一个。很高兴知道它有帮助。如果你同意,你可以接受它作为答案。