c - 不知何故,我让它执行而不会崩溃,我该如何打印它?
问题描述
我有一个简单的程序,我为此编写了太多代码行。它是我的第二个程序。我一直在研究如何打印这两个小时。如何打印具有多种数据类型的队列?就在免费(customerArray)之前;我需要添加一个打印语句。
样本输入
2
5
10 1 STEVEN 12
12 6 AHMAD 8
13 1 JENNY 40
22 6 JERMAINE 39
100000 12 AMALIA 53
6
100 1 A 100
200 2 B 99
300 3 C 98
400 4 D 97
500 5 E 96
600 6 F 95
样本输出
STEVEN from line 1 checks out at time 100.
AHMAD from line 6 checks out at time 170.
JERMAINE from line 6 checks out at time 395.
JENNY from line 1 checks out at time 625.
AMALIA from line 12 checks out at time 100295.
A from line 1 checks out at time 630.
F from line 6 checks out at time 1135.
E from line 5 checks out at time 1645.
D from line 4 checks out at time 2160.
C from line 3 checks out at time 2680.
B from line 2 checks out at time 3205.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// customer record
typedef char customerName[9];
typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber; // line number customer gets on
int time; // arrival time at line
int numberItems; // number of items customer has
} customer;
// [singly linked] queue element
typedef customer *qitem;
typedef struct node {
qitem data; // pointer to actual data
struct node *next; // forward pointer to next item
} node;
// queue definition (singly linked list)
// NOTE:
typedef struct queue {
node *front; // pointer to first node in list ie front of line with cashier
} queue;
// qinit -- initialize/reset queue
void qinit(queue *q)
{
q->front = NULL;
}
// qempty -- returns 1 if empty or 0 if false
int qempty(queue *q)
{
return (q->front == NULL);
}
// enqueue -- append element to end of queue
void enqueue(queue *q, qitem data)
{
node *newnode;
node *prev;
newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = data;
// find the back of the queue with only a front pointer
prev = NULL;
for (node *cur = q->front; cur != NULL; cur = cur->next)
prev = cur;
// append to tail of list
if (prev != NULL)
prev->next = newnode;
// add to end of empty list
else
q->front = newnode;
}
// dequeue -- dequeue from the front of the queue
qitem dequeue(queue *q)
{
node *curnode;
qitem data;
do {
curnode = q->front;
if (curnode == NULL) {
data = NULL;
break;
}
// get node's data value (e.g. pointer to customer struct)
data = curnode->data;
// release the node's storage back to the heap
free(curnode);
} while (0);
return data;
}
// qfront -- peek at front of queue
qitem qfront(queue *q)
{
node *curnode;
qitem data;
curnode = q->front;
if (curnode != NULL)
data = curnode->data;
else
data = NULL;
return data;
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
queue* customerArray = (queue*) malloc(sizeof(queue) * 12);
for ( int i = 0; i < numCustomers; i++){
qinit(customerArray);// starting new queue
customer* newCustomer = (customer*) malloc(sizeof(customer));
scanf("%d", &(newCustomer->time));
scanf("%d", &(newCustomer->lineNumber));
scanf("%s", newCustomer->name);
scanf("%d", &(newCustomer->numberItems));
enqueue(&customerArray[newCustomer->lineNumber - 1], newCustomer);
}
int totalTime = INT_MAX;
for(int i=0;i<12;i++)
{
customer* frontCustomer = qfront(&customerArray[i]);
if(frontCustomer== NULL){
return 0;
}
if(totalTime < frontCustomer->time)
{
totalTime = frontCustomer->time;
}
}
while(numCustomers--) {
int customerToCheckOutLine = 0; int minNumberOfItems = INT_MAX;
for( int j=11 ; j>=0; j--){
customer* frontCustomer = qfront(&customerArray[j]);
if(frontCustomer->time <= totalTime)
{
if(frontCustomer->numberItems < minNumberOfItems)
{
customerToCheckOutLine = frontCustomer->lineNumber;
minNumberOfItems = frontCustomer->numberItems;
}
}
free(frontCustomer);
}
customer* customerToCheckOut = qfront(&customerArray[customerToCheckOutLine -1 ]);
totalTime += 30;
totalTime += (customerToCheckOut->numberItems) * 5;
dequeue(&customerArray[customerToCheckOutLine - 1]);
}
free(customerArray);
}
}
return 0;
}
解决方案
推荐阅读
- java - 如何读取 azure blob 存储中的文件更改?
- java - 服务器不在 Eclipse 上运行
- asp.net - 尝试在 DataView 中添加日期条件过滤器
- python - 卷积神经网络批量大小
- sql - 在oracle中插入表B后创建触发器以将新行添加到表A中时出错
- reactjs - 'dayjs' 应该列在项目的依赖项 lint 错误中
- python-3.x - 你如何获得特定的数字并从你的输入中分配它
- amazon-web-services - 从 aws 返回的图像 URL
- .net - .NET Core 应用程序发布失败
- azure - 应用服务证书 - 如何更改密钥保管库?