基本上,我有一个问题需要理解。
所以我试图刺激一些电梯。
最初,所有电梯都在一楼,里面有0名乘客 和0使用(即,还没有人完成使用它们)。
给定一系列楼层号码,电梯到达 相应的楼层一个接一个。
如果电梯从较低楼层移至较高楼层m,则为m 乘客将尝试进入电梯。但是,曾经 电梯里的乘客人数达到15人,这就是 电梯的容量,没有额外的乘客将能够 进入电梯。
如果有p乘客的电梯从较高楼层到较低楼层
n,n(或p,以较小者为准)的乘客将退出
电梯。退出电梯的实际乘客数量
应计入已完成的乘客数量
使用电梯。
此后,我试图打印出相应的楼层,乘客数量和使用情况。
Enter number of elevators: 1
Enter sequence for elevator 1: 24653
Elevator 1:
Floor: 3
Number of passengers: 4
Usage: 8
Most used elevator: 1
Enter number of elevators: 2
Enter sequence for elevator 1: 24653
Enter sequence for elevator 2: 798635
Elevator 1:
Floor: 3
Number of passengers: 4
Usage: 8
Elevator 2:
Floor: 5
Number of passengers: 5
Usage: 15
Most used elevator: 2
我需要协助我的功能" runElevators"和" goToFloor"因为我不确定他们需要什么。我认为我在" runElevators"中做错了什么,但我不确定到底是什么。
我明白这个角色" 2"并不意味着整数" 2"但我似乎无法解决它。
非常感谢任何帮助。谢谢!
#include <stdio.h>
#include <string.h>
#define CAPACITY 15
#define MAX_LEN 21
#define MAX_ELEVATORS 5
typedef struct {
int floor, passenger, usage;
} elevator_t;
void setupElevators(elevator_t [], int);
void readSequences(char [][MAX_LEN], int);
void runElevators(elevator_t [], char [][MAX_LEN], int);
void goToFloor(elevator_t *, int);
void printElevators(elevator_t [], int size);
int mostUsedElevator(elevator_t [], int);
// This function is given. You are NOT to modify it.
int main(void){
int size;
elevator_t elevators[MAX_ELEVATORS];
char sequences[MAX_ELEVATORS][MAX_LEN];
printf("Enter number of elevators: ");
scanf("%d", &size);
setupElevators(elevators, size);
readSequences(sequences, size);
runElevators(elevators, sequences, size);
printElevators(elevators, size);
printf("Most used elevator: %d\n", mostUsedElevator(elevators, size));
return 0;
}
void setupElevators(elevator_t elevators[], int size){
int i;
for(i = 0; i < size; i++)
{
elevators[i].floor = 1;
elevators[i].passenger = 0;
elevators[i].usage = 0;
}
}
// Read in the sequences of floors the elevators go to.
// This function is given. You are NOT to modify it.
void readSequences(char sequences[][MAX_LEN], int size){
int i;
for (i = 0; i<size; i++){
printf("Enter sequence for elevator %d: ", i+1);
scanf("%s", sequences[i]);
}
}
void runElevators(elevator_t elevators[], char sequences[][MAX_LEN], int size){
int i, c;
for(i = 0; i < size; i++)
for(c = 0; c < strlen(sequences[i]); c++)
{
elevators[i].floor = sequences[i][strlen(sequences[i]) - 1];
if((elevators[i].passenger < 15) && (sequences[i][c+1] > sequences[i][c]))
elevators[i].passenger += sequences[i][c];
if(elevators[i].passenger > 15)
elevators[i].passenger = 15;
if((elevators[i].passenger > 0) && (sequences[i][c] > sequences[i][c+1]))
elevators[i].passenger -= sequences[i][c+1];
if(sequences[i][c] > sequences[i][c+1])
elevators[i].usage += sequences[i][c+1];
}
}
void goToFloor(elevator_t *elevator, int floor){
}
void printElevators(elevator_t elevators[], int size){
int i;
for(i = 0; i < size; i++)
{
printf("Elevator : %d\n", i+1);
printf("Floor: %d\n", elevators[i].floor);
printf("Number of passengers: %d\n", elevators[i].passenger) ;
printf("Usage: %d\n", elevators[i].usage);
}
}
int mostUsedElevator(elevator_t elevators[], int size){
// Incomplete
return 1;
}
答案 0 :(得分:0)
void runElevators(elevator_t elevators[], char sequences[][MAX_LEN], int size)
{
int i, c;
int prevFloor, currFloor;
for(i = 0; i < size; i++)
{
for(c = 0; c < strlen(sequences[i]); c++)
{
prevFloor = elevators[i].floor;
currFloor = sequences[i][c] - '0';
if (currFloor > prevFloor)
{
// Elevator going UP, passengers will enter the elevator
int numPassengerToEnter;
// Max capacity of elevator is CAPACITY (=15)
numPassengerToEnter = CAPACITY - elevators[i].passenger;
// At floor m, only m passengers (= currFloor) are available (to enter elevator)
if (numPassengerToEnter < currFloor)
{
elevators[i].passenger += numPassengerToEnter;
}
else
{
elevators[i].passenger += currFloor;
}
}
else if (currFloor < prevFloor)
{
// Elevator going DOWN, passengers will exit the elevator
int numPassengerToExit;
// Smaller of (Current floor number n) and (current number of passenger in elevator) will exit the elevator
if (elevators[i].passenger < currFloor)
{
numPassengerToExit = elevators[i].passenger;
}
else
{
numPassengerToExit = currFloor;
}
elevators[i].passenger -= numPassengerToExit;
}
// Update current floor of elevator
elevator[i].floor = currFloor;
}
}