并行数组与结构数组是两种不同的数据存储方式,下面给出分别使用并行数组和结构数组来存储员工信息的例子。
#include
#define MAX_EMPLOYEES 100
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee employees[MAX_EMPLOYEES];
// 添加员工信息
strcpy(employees[0].name, "John");
employees[0].age = 30;
employees[0].salary = 5000.0;
strcpy(employees[1].name, "Mary");
employees[1].age = 25;
employees[1].salary = 4000.0;
// 输出员工信息
for (int i = 0; i < 2; i++) {
printf("Name: %s\n", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("Salary: %.2f\n\n", employees[i].salary);
}
return 0;
}
#include
#define MAX_EMPLOYEES 100
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
char names[MAX_EMPLOYEES][50];
int ages[MAX_EMPLOYEES];
float salaries[MAX_EMPLOYEES];
// 添加员工信息
strcpy(names[0], "John");
ages[0] = 30;
salaries[0] = 5000.0;
strcpy(names[1], "Mary");
ages[1] = 25;
salaries[1] = 4000.0;
// 输出员工信息
for (int i = 0; i < 2; i++) {
printf("Name: %s\n", names[i]);
printf("Age: %d\n", ages[i]);
printf("Salary: %.2f\n\n", salaries[i]);
}
return 0;
}
这两种方法都可以用来存储员工信息,选择使用哪种方式取决于具体的需求和程序设计的目标。
下一篇:并行SSH(pssh)带有输出流