好吧,基本上你可以通过代码看出,我正在编写一个终端来读取用户给出的命令,其中一个命令是创建停车场的命令“p”,参数为name,容量、cost15(在公园停留15分钟的费用)、cost15_after 1小时(第一个小时后每15分钟的费用)和max_daily_cost。问题是,在命令中输入停车场名称时,当名称之间用空格分隔时,例如 "park number one" ,输入应该在 "" 之间,而当仅给出名称时用一个词来说,例如park1,它不带有“”。这不是我的选择,这是一个大学项目,所以删除“”不是一个选择。然而,奇怪的是,它工作得很好,但前提是我首先输入一个带有单词名称的停车场,然后然后分配一个名称以空格分隔的停车场,因为如果我尝试先分配停车场名字用空格隔开,不行!顺便说一下,单独使用命令 p 不带任何参数,会显示现有的停车场。这是输出:
p "park name" 20 1 2 3
Error: invalid capacity.
p park 20 1 2 3
Parking lot park successfully created!
p "park name" 20 1 2 3
Parking lot park name successfully created!
p
Existing parking lots:
park 20 20
park name 20 20
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
// Definition of the structure for a parking lot
typedef struct {
char name[50];
int capacity;
float cost15;
float cost15_after1hour;
float max_daily_cost;
int available_spaces;
} ParkingLot;
// Global variable to store the created parking lots
ParkingLot parkingLots[10];
int num_parkingLots = 0;
// Function to create a new parking lot
void createParkingLot(char name[], int capacity, float cost15, float cost15_after1hour, float max_daily_cost) {
if (num_parkingLots >= 10) {
printf("Error: too many parking lots.\n");
return;
}
// Check if the parking lot name already exists
for (int i = 0; i < num_parkingLots; i++) {
if (strcmp(parkingLots[i].name, name) == 0) {
printf("Error: %s already exists.\n", name);
return;
}
}
// Check if the capacity is valid
if (capacity <= 0) {
printf("Error: invalid capacity.\n");
return;
}
// Check if the tariff values are increasing
if (cost15 <= 0 || cost15_after1hour <= cost15 || max_daily_cost <= cost15_after1hour) {
printf("Error: invalid cost.\n");
return;
}
// Create the new parking lot
strcpy(parkingLots[num_parkingLots].name, name);
parkingLots[num_parkingLots].capacity = capacity;
parkingLots[num_parkingLots].cost15 = cost15;
parkingLots[num_parkingLots].cost15_after1hour = cost15_after1hour;
parkingLots[num_parkingLots].max_daily_cost = max_daily_cost;
parkingLots[num_parkingLots].available_spaces = capacity;
num_parkingLots++;
printf("Parking lot %s successfully created!\n", name);
}
// Function to list existing parking lots
void listParkingLots() {
printf("Existing parking lots:\n");
for (int i = 0; i < num_parkingLots; i++) {
printf("%s %d %d\n", parkingLots[i].name, parkingLots[i].capacity, parkingLots[i].available_spaces);
}
}
int main() {
char command[20];
char name[50];
int capacity;
float cost15, cost15_after1hour, max_daily_cost;
printf("p [ <park-name> <capacity> <cost-15> <cost-15-after-1hour> <max-daily-cost> ] - Create parking lot\n");
while (1) {
scanf("%s", command);
if (strcmp(command, "p") == 0) {
char nextChar = getchar(); // Reads the next character after the "p"
if (nextChar == '\n') {
listParkingLots();
} else {
ungetc(nextChar, stdin); // Returns the unread character to the buffer
char str[MAXSIZE];
fgets(str, MAXSIZE, stdin);
char *p = strchr(str, '\"');
if (p){
char str_between_quotes[MAXSIZE];
int i = 0;
p++; // Moves the pointer to the next character after the first quote
while (*p != '\"' && *p != '\n' && *p != '\0') {
str_between_quotes[i] = *p;
i++;
p++;
}
str_between_quotes[i] = '\0'; // Ends the string
strcpy(name,str_between_quotes);
} else {
sscanf(str, "%s %d %f %f %f", name, &capacity, &cost15, &cost15_after1hour, &max_daily_cost);
}
createParkingLot(name,capacity,cost15,cost15_after1hour,max_daily_cost);
}
} else if (strcmp(command, "q") == 0) {
break;
} else {
printf("Invalid command.\n");
}
}
return 0;
}
我尝试使用linux中的gdb工具对其进行调试,但没有得出结论。我认为这可能与指针或其他东西有关,我还不是真正的 C 大师。我也很欣赏这段代码的一些提示,有没有更好的方法来完成我想做的事情?希望有人可以帮助我,谢谢
在这个 if 语句中
if (p){
char str_between_quotes[MAXSIZE];
int i = 0;
p++; // Moves the pointer to the next character after the first quote
while (*p != '\"' && *p != '\n' && *p != '\0') {
str_between_quotes[i] = *p;
i++;
p++;
}
str_between_quotes[i] = '\0'; // Ends the string
strcpy(name,str_between_quotes);
}
您忘记提取这些项目:capacity、cost15、cost15_after1hour、max_daily_cost。因此它们具有不确定的值,或者这个 if 语句是第一个在 while 循环中获得输入控制权的语句。