C语言,有3个职工的信息(包括工号、 姓名 性别、工资),放在结构体数组中,要求 输出全部职工的信息
时间: 2023-12-19 12:21:36 浏览: 44
好的,以下是可以输出全部职工信息的C语言代码示例:
```c
#include <stdio.h>
// 定义职工结构体
struct Employee {
int id; // 工号
char name[20]; // 姓名
char sex; // 性别
float salary; // 工资
};
int main() {
// 定义结构体数组并初始化
struct Employee employees[3] = {
{1001, "张三", 'M', 5000},
{1002, "李四", 'F', 6000},
{1003, "王五", 'M', 7000}
};
// 输出职工信息
for (int i = 0; i < 3; i++) {
printf("工号:%d,姓名:%s,性别:%c,工资:%.2f\n",
employees[i].id, employees[i].name, employees[i].sex, employees[i].salary);
}
return 0;
}
```
输出结果为:
```
工号:1001,姓名:张三,性别:M,工资:5000.00
工号:1002,姓名:李四,性别:F,工资:6000.00
工号:1003,姓名:王五,性别:M,工资:7000.00
```
相关问题
用c语言编制某单位的职工档案管理程序,每个职工档案包括工号,姓名,性别,年龄,部门,住址,基本工资,文化程度
好的,我听到你的问题了。关于你的问题,我可以提供一些思路和建议。
首先,你可以使用 C 语言的结构体来定义职工档案的数据结构,例如:
```c
struct Employee {
int id; // 工号
char name[50]; // 姓名
char gender; // 性别,可以使用 'M' 表示男性,'F' 表示女性
int age; // 年龄
char department[50]; // 部门
char address[100]; // 住址
double salary; // 基本工资
char education[50]; // 文化程度,例如:初中、高中、大学本科、硕士研究生等
};
```
然后,你可以设计一个菜单系统,让用户可以进行添加、查询、修改和删除职工档案等操作。例如:
```c
int main() {
int choice;
struct Employee employees[1000]; // 假设最多管理 1000 个职工档案
int count = 0; // 记录当前管理的职工档案数量
while (1) {
printf("请选择操作:\n");
printf("1. 添加职工档案\n");
printf("2. 查询职工档案\n");
printf("3. 修改职工档案\n");
printf("4. 删除职工档案\n");
printf("5. 显示所有职工档案\n");
printf("6. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
// 添加职工档案的代码
break;
case 2:
// 查询职工档案的代码
break;
case 3:
// 修改职工档案的代码
break;
case 4:
// 删除职工档案的代码
break;
case 5:
// 显示所有职工档案的代码
break;
case 6:
// 退出程序
return 0;
default:
printf("无效的操作,请重新输入!\n");
}
}
return 0;
}
```
在具体实现每个操作时,你可以使用文件来保存和读取职工档案数据,也可以使用内存数组来保存数据。同时,你还需要考虑如何进行输入检查和错误处理等问题。
c语言程序设计职员信息管理,C语言程序设计,编写一个职工信息管理系统
好的,我可以为您提供一些思路和代码示例。首先,职工信息管理系统需要记录每个职工的姓名、工号、年龄、性别、职位、工资等信息。以下是一个简单的示例程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 100
typedef struct {
char name[20];
int id;
int age;
char gender[10];
char position[20];
double salary;
} Employee;
int num_employees = 0;
Employee employees[MAX_EMPLOYEES];
void add_employee() {
Employee employee;
printf("Enter name: ");
scanf("%s", employee.name);
printf("Enter ID: ");
scanf("%d", &employee.id);
printf("Enter age: ");
scanf("%d", &employee.age);
printf("Enter gender: ");
scanf("%s", employee.gender);
printf("Enter position: ");
scanf("%s", employee.position);
printf("Enter salary: ");
scanf("%lf", &employee.salary);
employees[num_employees++] = employee;
printf("Employee added successfully.\n");
}
void print_employee(Employee employee) {
printf("Name: %s\n", employee.name);
printf("ID: %d\n", employee.id);
printf("Age: %d\n", employee.age);
printf("Gender: %s\n", employee.gender);
printf("Position: %s\n", employee.position);
printf("Salary: %.2lf\n", employee.salary);
}
void view_employees() {
if (num_employees == 0) {
printf("No employees found.\n");
return;
}
printf("List of employees:\n");
for (int i = 0; i < num_employees; i++) {
printf("Employee #%d:\n", i + 1);
print_employee(employees[i]);
}
}
void delete_employee() {
int id;
printf("Enter ID of employee to delete: ");
scanf("%d", &id);
for (int i = 0; i < num_employees; i++) {
if (employees[i].id == id) {
for (int j = i; j < num_employees - 1; j++) {
employees[j] = employees[j + 1];
}
num_employees--;
printf("Employee deleted successfully.\n");
return;
}
}
printf("Employee not found.\n");
}
void save_employees() {
FILE *fp = fopen("employees.dat", "wb");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
fwrite(&num_employees, sizeof(int), 1, fp);
fwrite(employees, sizeof(Employee), num_employees, fp);
fclose(fp);
printf("Employees saved to file.\n");
}
void load_employees() {
FILE *fp = fopen("employees.dat", "rb");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
fread(&num_employees, sizeof(int), 1, fp);
fread(employees, sizeof(Employee), num_employees, fp);
fclose(fp);
printf("Employees loaded from file.\n");
}
int main() {
int choice;
while (1) {
printf("Employee Management Menu:\n");
printf("1. Add Employee\n");
printf("2. View Employees\n");
printf("3. Delete Employee\n");
printf("4. Save Employees\n");
printf("5. Load Employees\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_employee();
break;
case 2:
view_employees();
break;
case 3:
delete_employee();
break;
case 4:
save_employees();
break;
case 5:
load_employees();
break;
case 6:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
}
}
```
该程序使用了一个结构体数组来记录职工信息,并提供了以下功能:
1. 添加职工信息。
2. 查看所有职工信息。
3. 删除指定职工。
4. 将职工信息保存到文件。
5. 从文件中加载职工信息。
6. 退出程序。
您可以根据需要自行修改代码。