int shiftworker(worker *w, int n ,int id1,int id2) { int index1 = -1; // 工人1的索引 int index2 = -1; // 工人2的索引

// 遍历工人数组,找到对应编号的工人的索引
for (int i = 0; i < n; i++) {
    if (w[i].id == id1) {
        index1 = i;
    } else if (w[i].id == id2) {
        index2 = i;
    }
}

// 如果找到了工人1和工人2的索引
if (index1 != -1 && index2 != -1) {
    // 交换工人1和工人2的姓名和年龄信息
    char tempName[20];
    strcpy(tempName, w[index1].name);
    strcpy(w[index1].name, w[index2].name);
    strcpy(w[index2].name, tempName);
    
    int tempAge = w[index1].age;
    w[index1].age = w[index2].age;
    w[index2].age = tempAge;
    
    return 1; // 返回1表示交换成功
} else {
    return 0; // 返回0表示交换失败
}

}

C语言程序设计:交换结构体数组中两个工人的姓名和年龄

原文地址: https://www.cveoy.top/t/topic/pbk0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录