gpt4 book ai didi

c - 结构和字符串

转载 作者:行者123 更新时间:2023-11-30 17:08:42 26 4
gpt4 key购买 nike

基本上,我无法理解这个问题。

所以我正在尝试刺激一些电梯。

  • 最初,所有电梯都在 1 楼,里面有 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;
}

最佳答案

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;
}
}

关于c - 结构和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33557995/

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