gpt4 book ai didi

c - 稍后在程序中不会遵循用户定义的值

转载 作者:行者123 更新时间:2023-11-30 15:31:07 24 4
gpt4 key购买 nike

在下面的代码中,用户应该能够定义哪些护士本周无法工作。用户有一个名称列表,他们应该输入与该名称相对应的数字。一旦该值被存储到 slackers[4]数组,它应该使用这些用户提供的值来删除那些进行选择的护士。尽管avail_nurses[9],它似乎并没有尊重这些选定的值。在任何时间点都有正确的值(我使用 printf 语句进行测试)。

除了拼图中最重要的部分之外,一切似乎都处于良好状态。我希望得到一些建设性的批评和有用的建议。如果你能避免它,就不要为我编写代码——我必须以某种方式学习。提前致谢!

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

char *names[] = { "Denise", "Inja", "Jane", "Karen", "Maggie", "Margaret", "MJ", "Queen", "Sherri", NULL }; //ptr for names, 9 nurses
/*0 = Denise, 1 = Inja, 2 = Jane, 3 = Karen, 4 = Maggie, 5 = Margaret, 6 = MJ, 7 = Queen, 8 = Sherri*/
const char days[5][10] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
int randomNurse();
#define total_nurses 9 //number of nurses on staff
#define days_in_week 5 //number of work days in a week


int main() {

srand(time(NULL));
int day, pos, rand_num, i, j;
int slackers[4] = { 0, 0, 0, 0 }; //array that holds the selections for who isn't working
int avail_nurses[total_nurses] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //holds the status of each nurse, 0 = unavailable, 1 = available

/*this allows the user to repeat the program easily! flag determines if we run the program multiple times*/
while (char flag = 'y') {

/*prints names */
int temp_counter = 1; //counter
char **name_ptr = names;
while (*name_ptr) {
printf("%i) %s\n", temp_counter, *name_ptr);
name_ptr++;
temp_counter++;
}

/*this assumes that no more than FOUR nurses will be away on any given week*/
printf("\nEnter numbers that correspond to the nurses who won't be available for the week.\nType up to four numbers, each separated by a space.\n");
printf("When you are done, press \"Enter\".\n");
printf("If less than four nurses will be on leave, type a \"0\" in place of a selection.\n");
printf("Example: 1 2 5 0\n\n\n");

/*week selection of unavailable nurses*/
do {
printf("Who won't be here? ");
} while (scanf("%i %i %i %i", &slackers[0], &slackers[1], &slackers[2], &slackers[3]) != 4);

/*checks the selections made, and sets the available nurses to the correct value, zero if they are slacking||vacationing*/
for (int n = 0; n < 4; n++) {
int slacker = slackers[n];
if (slacker >= 1 && slacker <= 9)
avail_nurses[slacker - 1] = -1;
}


/*-----WEEKLY_ASSIGNMENT-----*/
int pos_per_day[days_in_week] = { 5, 9, 9, 8, 5 }; //number of nurses needed each day
int selection[days_in_week][total_nurses]; //the selected nurses per day

for (i = 0; i < days_in_week; i++) {
for (j = 0; j < total_nurses; j++) {
selection[i][j] = -1; //initialize to -1 which means no nurse is selected
}
}

//fill all the days of week
for (day = 0; day < days_in_week; day++) {
for (pos = 0; pos < pos_per_day[day]; pos++) { //for every position needed that day
do {
rand_num = randomNurse();
} while (!avail_nurses[rand_num]); //looks for available nurses (phrasing)
avail_nurses[rand_num] = 0; //change nurses status to not available
selection[day][pos] = rand_num; //fill the output array with appropriate nurse
}
for (i = 0; i < total_nurses; i++) {
avail_nurses[i] = 1; //initialize the nurses status for next day use
}
for (int n = 0; n < 4; n++) { //make sure we shame the slackers...
int slacker = slackers[n];
if (slacker >= 1 && slacker <= 9)
avail_nurses[slacker - 1] = -1;
}

/*DEBUGGING PRINTFs
printf("\n\nSELECTION:\n");
for (int x = 0; x < days_in_week; x++) {
for (int y = 0; y < total_nurses; y++) {
printf("%i\t", selection[x][y]);
}
printf("\n");
}*/
}
printf("\n");

/*-----PRINTS SCHEDULE FOR WEEK-----*/
for (i = 0; i < days_in_week; i++) {
printf("%-10s: ", days[i]);
for (j = 0; j < total_nurses; j++) {
if (selection[i][j] != -1)
printf("%-10s ", names[selection[i][j]]);
}
printf("\n");
}

fflush(stdin);

/*asks user if they want the program to run again*/
printf("\n\nDo you want to run the program again? (y/n) ");
scanf("%c", &flag);
if (flag == 'n' || flag == 'N') {
printf("\n");
break;
}
else {
printf("\n\n\n");
continue;
}
}
return 0;
}

/*function to generate random nurse*/
int randomNurse() {

return rand() % 9; //random number 0-8, to pick nurse
}

最佳答案

您的avail_nurses数组使用值 1要指示有空的护士,值 0指示一名护士无法空出,因为该护士已被分配,并且 -1表示某位护士因为偷懒而无法上类。然后,您可以使用 !avail_nurses[rand_num] 来测试护士是否有空。 。如果avail_nurses[rand_num],则该陈述为真。是 0如果它是任何其他值,则为 false。意味着当 avail_nurses[rand_num]-1它将退出循环,就像 1 时一样.

要修复此错误,请将测试更改为 avail_nurses[rand_num] <= 0或仅使用 0无论出于何种原因,都指示无法提供护士服务。

关于c - 稍后在程序中不会遵循用户定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25106751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com