【问题标题】:How can I better implement this queue using c?如何使用 c 更好地实现这个队列?
【发布时间】:2020-09-09 18:02:29
【问题描述】:

我需要为汽车服务管理系统创建一个队列。该组织提供“普通”和“紧急”两种服务类型。优先考虑选择紧急选项的客户,因此比普通客户更早得到服务。这是我的代码,请随时提出任何问题,我要求您以开放的心态对待这个问题。感谢广告 ''' '

struct node *normal= NULL;
struct node *urgent= NULL;

void queueRegistration()
{
    /*Print Menu to user */
    char urgency;




    int choice;

    char *service;
    char *serviceType;

    /*Queue cutting menu*/

    system("cls");
    gotoxy(50,1);
    printf("================================");
    gotoxy(50, 2);
    printf("QUEUE REGISTRATION");
    gotoxy(50, 3);
    printf("================================\n");
    gotoxy(50, 4);
    printf("We offer urgent and Normal prices, service prices depend on 
    the urgency\n");
    gotoxy(50, 6);
    printf("1.Normal price\n");
    gotoxy(50, 7);
    printf("2.Urgent price\n");
    gotoxy(50, 8);
    printf("Pick an option number from the list[1 or 2]: \n");
    gotoxy(50, 9);
    scanf("%d",& choice);

    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    int s[64];
    assert(strftime(s, sizeof(s), "%c", tm));
    int DATE ;
    DATE = s;
    return 0;

    if(choice==1)
    {
    struct node *temp,*ptr;
    temp=(struct node *)malloc(sizeof(struct node));
    if(temp==NULL){
    printf("\nOut of Memory Space:\n");
    return;
    }
    printf("\nEnter Name:\t" );
    scanf("%s",&temp->name );
    printf("\nEnter Car Number Plate:\t" );
    scanf("%s",&temp->plateNumber );

    temp->next =NULL;
    if(normal==NULL)
    {
                  normal=temp;
    }
    else
    {
    ptr=normal;
    while(ptr->next !=NULL){
    ptr=ptr->next ;
    }
    ptr->next =temp;
    }

    }
    else if(choice==2)
    {
    struct node *temp,*ptr;
    temp=(struct node *)malloc(sizeof(struct node));
    if(temp==NULL){
    printf("\nOut of Memory Space:\n");
    return;}

    printf("\nEnter Name:\t" );
    scanf("%s",&temp->name );

    printf("\nEnter Car Number Plate:\t" );
    scanf("%s",&temp->plateNumber );

    temp->next =NULL;
    if(urgent==NULL)
    {
    urgent=temp;}

    else{
    ptr=urgent;
    while(ptr->next !=NULL){
    ptr=ptr->next ;
    }
    ptr->next =temp;
    }
    }
    

} ' '''

【问题讨论】:

  • 那么问题出在哪里?
  • 两个队列的形成和优先级。我不知道我是否应该使用 2 个队列
  • 我建议的第一件事是创建更多具有特定工作的功能。但最终我不知道你想做什么。您是否正在尝试实施某种模型/算法来为更高优先级的任务和更低优先级的任务提供服务?
  • 是的,这正是我想要做的
  • 这是您的实际代码吗?这不可能编译。

标签: c queue


【解决方案1】:

这将是一个非常糟糕的答案,因为问题本身很糟糕。提前抱歉。我只是想我会尝试解决代码中的许多问题。它们掩盖了您提出的任何实际问题。

您发布的代码非常非法,我认为在复制粘贴时出现了问题。您不能在新行上继续字符串文字。为什么在函数中间返回 0? DATE = s 应该做什么? s 是一个数组!您重复的代码太多,这也应该分成几个较小的函数。我不明白你使用 MS-DOS 函数的目的是什么。此外,即使没有语法错误也无法编译,因为您尚未发布结构的定义。如果可以的话,您应该在问题中包含这一点。

非常重要:从不在断言中放置带有副作用的表达式。获取返回值并对其进行断言。断言将在非调试版本中被删除,表达式随之而来。此外,请勿使用scanf 进行用户输入。这不是它的用途。

我试图清理一下以进行演示。由于随机返回语句等原因,这段代码仍然无法正常工作,但它有点更清晰了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define STR_SIZE 2048

static int get_integer_input(void)
{
    char     buf[STR_SIZE];
    char *   endp = NULL;
    intmax_t ret;

    fgets(buf, STR_SIZE, stdin);
    ret = strtoll(str, &endp, 10);

    if (endp == str || ret > INT_MAX || ret < INT_MIN) {
        fputs("Invalid input\n", stderr);
        exit(1);
    }

    return (int)ret;
}

typedef struct node Node;
struct node {
    char  name[STR_SIZE];
    char  plateNumber[STR_SIZE];
    Node *next;
}

Node *normal = NULL;
Node *urgent = NULL;

void queueRegistration(void)
{
    char  urgency;
    int   choice;
    char *service;
    char *serviceType;

    printf("================================\n"
           "QUEUE REGISTRATION\n"
           "================================\n"
           "We offer urgent and Normal prices, service prices depending on the urgency\n"
           "1.Normal price\n"
           "2.Urgent price\n"
           "Pick an option number from the list[1 or 2]: \n");

    choice = get_integer_input();

    int    s[64];
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);

    { /* Never include an expression with side effects in an assertion. */
        int ret = strftime(s, sizeof(s), "%c", tm);
        assert(ret);
    }

    int DATE = s; /* WHAT??? */
    return 0;     /* ??????? */

    Node *temp = malloc(sizeof(Node));
    if (temp == NULL) {
        fprintf(stderr, "\nOut of Memory Space:\n");
        exit(1);
    }

    printf("\nEnter Name:\t");
    fgets(temp->name, STR_SIZE, stdin);
    printf("\nEnter Car Number Plate:\t");
    fgets(temp->plateNumber, STR_SIZE, stdin);

    Node *type;

    switch (choice) {
    case 1: type = normal; break;
    case 2: type = urgent; break;
    default:
        fprintf(stderr, "Invalid choice \"%d\"\n", choice);
        exit(1);
    }

    if (type == NULL) {
        type = temp;
    } else {
        while (type->next != NULL)
            type = type->next;
        type->next = temp;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2021-09-09
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多