首页 > 解决方案 > 入队,从输入文件中出队(结构数组)

问题描述

我想模拟一次从阵列中进入队列的客户,并在他们进入队列或离开队列以供柜员服务时收集数据。将文件中的数据读入我所做的数组中,我想知道如何出队、入队和显示从数组文本文件中离开或进入的客户(35 人)。例如:我要删除第一个客户 ID 为 1,我选择了 2(removeCustomer()),它会删除那个人,当我显示剩余的客户(displayCustomer())时,它只会显示 ID 为 2 的客户ID 35,当我选择 1 (nextCustomer()) 时,它会将 ID 为 1 的人重新添加到 ID 35 之后。

以下是我到目前为止所做的。我尝试了很多方法,但都没有奏效。我不确定我错过了什么或做错了什么。谢谢你。

#pragma once
#include<random>

#include<String>
#include<ctime>
#include<fstream> //to allow you to read data from text files
#include<iostream>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

using namespace std;
int const tellers = 3;
int const listSize = 35;
int const waitTime = 3;

int queue[listSize];
int front, rear; 
int i; 
int choice;

void nextCustomer();
void removeCustomer();
void displayCustomer();

struct Customer {
    int ID;
    string fnames;
    string lnames;
    string DW;    
    double amount;
    string bal;
    double balance;
    int arrivalTime = 0;
    int serviceTime = 0;
    int tellerNo;
};

void openFile(ifstream& inputFile, string fname)
{
    inputFile.open(fname);
    if (!inputFile)
        printf("File not found"); 
}

void readFile(string fname, Customer list1[])
{
    ifstream inFile;
    openFile(inFile, fname); //open the file 
    for (int i = 0; i < listSize; i++)
    {
        
        inFile >> list1[i].ID >> list1[i].fnames >> list1[i].lnames >> list1[i].DW >> list1[i].amount >> list1[i].bal >> list1[i].balance;
        cout << list1[i].ID << " " << list1[i].fnames << " " << list1[i].lnames << " " << list1[i].DW << " " << list1[i].amount<< " " << list1[i].bal << " "<< list1[i].balance << endl;      

    }
    inFile.close();
}
//end
//assigns random tellers
void generateRandoms(int randomTeller[], int randTime[]) {
    srand(time(NULL));
    printf("\nNext three customers, please go to the below tellers for service(s): \n");
    for (int random = 0; random < tellers; random++) {
        randomTeller[tellers] = std::rand() % 3+1;
        printf("\nTeller# %d\n", randomTeller[tellers]);
    }
    
    //waiting time of tellers
    for (int random = 0; random < waitTime; random++){    
        randTime[waitTime] = std::rand() % 5+1;
        printf("\nWaiting times for the tellers above are: %d minutes\n", randTime[waitTime]);
    }
}
//end

//customers come and leave
void nextCustomer()
{   
    int ID;
    if (rear == listSize - 1)
        printf("Line is now full.\n");
    else
    {
        if (front == -1)
            front = 0;
        printf("Enter the the customer ID: ");
        scanf_s("%d", &ID);
        rear++;
        queue[rear] = ID;
    }
}

void removeCustomer() {
    if (front == -1 || front > rear)
    {
        printf("No customers at the moment.\n");
    }
    else
    {
        printf("Customers that left the line are: \n");
        printf("ID: %d\n", queue[front]);
        front++;
    }
}

void displayCustomer()
{
    if (front == -1)
    {
        printf("No customers at the moment.\n");
    }
    else
    {
        printf("Customers in line are: \n");
        for (i = front; i <= rear; i++)
            printf("ID: %d\n", queue[i]);
    }
}

//Main 

#include <iostream>
#include "customernames.h"
#include<fstream>
#include<random>
#include<String>
#include<ctime>
#include <stdlib.h>

using namespace std;

int main()
{
    //read input file to an array 

    Customer list1[listSize]; //read from a file to an array of struct 
    ifstream inputFile;
    string fName = "customernames.txt";
    readFile(fName, list1);
    //end

    //generate random teller
    int randomTeller[tellers];
    int randTime[waitTime];
    generateRandoms(randomTeller, randTime);
    for (int random = 0; random < tellers; random++);
    for (int random = 0; random < waitTime; random++);
    //end
  

    printf("\n--------------------Bankers only--------------------\n");
    int choice;
    front = rear = -1; // initialzing front and rear to -1 indicates that it is empty
    do
    {     
        printf("1. Next customer\n2. Customer(s) that left\n3. Display the remaining customers\n4. Exit\n\n");
        printf("Enter your choice:");
        scanf_s("%d", &choice);        
        switch (choice)
        {
        case 1:
            nextCustomer();                                          
            break;
        case 2:
            removeCustomer();    
            break;
        case 3:
            displayCustomer();
            break;
        case 4:
            exit(0);
            break;
        default:
            printf("Sorry, invalid choice!\n");
            break;
        }
    } while (choice != 4);      
        return 0;       
}

标签: c++arrays

解决方案


您可以尝试使用 C++ 标准库队列堆栈库。std::queue 可能很适合用于客户队列,因为它可以很好地模拟真实情况。

虽然我不完全确定您希望做什么,但我修改了代码以显示一种使用上述库组织客户对象的方法。(其他有用的库可能是 std::map、std::list 和 std::vector)当您需要遍历元素以显示它们时,使用 std::stack 和 std::queue 有点复杂,因为它们不'不提供对中间元素的访问,但可以通过将元素复制到临时容器然后再次返回来完成,如下面的代码所示:

(如果需要使用原始数组,那么您可以尝试使用数组上的函数来实现自己的队列功能,但是 std::containers 的一个有用特性是它们具有告诉您当前有多少元素的函数,所以您不需要将大小单独存储为整数,这会删除一些代码)


#include <random>
#include <queue>
#include <stack>
#include <string>
#include <ctime>
#include <fstream> //to allow you to read data from text files
#include <iostream>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

using namespace std;

void nextCustomer();
void removeCustomer();
void displayCustomer();

struct Customer {
    int ID;
    string fnames;
    string lnames;
    string DW;    
    double amount;
    string bal;
    double balance;
    int arrivalTime = 0;
    int serviceTime = 0;
    int tellerNo;
};

queue<Customer> customer_queue;  // the waiting customers
stack<Customer> customer_history;  // record the previous customers

void openFile(ifstream& inputFile, string fname)
{
    inputFile.open(fname);
    if (!inputFile)
        printf("File not found"); 
}

void readFile(string fname)
{
    ifstream inFile;
    openFile(inFile, fname); //open the file 

    while(inFile && !inFile.eof())
    {
        Customer c;
        inFile >> c.ID >> c.fnames >> c.lnames >> c.DW >> c.amount >> c.bal >> c.balance;
        cout << c.ID << " : " << c.fnames << " " << c.lnames << " " << c.DW << " " << c.amount<< " " << c.bal << " "<< c.balance << endl; 
        customer_queue.push(c);  // add another customer into the queue from the file 
    }
}

#if (0)
// TODO
//assigns random tellers
void generateRandoms(int randomTeller[], int randTime[]) {
...
}
#endif


//customers come and leave
void nextCustomer()
{   
   if ( !customer_queue.empty())
   {
       Customer current_customer = customer_queue.front(); 
       customer_queue.pop();
       //presumably the teller part still to do...
       printf("Customer %d to next teller\n",current_customer.ID);
       customer_history.push(current_customer);
   }
   else
   {
       printf("No Customers\n");
   }
}

//display removed customers
void removeCustomer() {
    if (customer_history.empty())
    {
        printf("No customers left the line at the moment.\n");
    }
    else
    {
        // We need to temporarily store the contents of the stack
        // to iterate through it
        std::stack<Customer> temp;
        printf("Customers that left the line are: \n");
      
        while(!customer_history.empty()){
           // N.B stored in reverse order!
           Customer next = customer_history.top();
           customer_history.pop();
           printf("ID: %d\n", next.ID);
           temp.push( next );
        }
        // and restore (reverse again)
        while(!temp.empty()){
           Customer cur = temp.top();
           temp.pop();
           customer_history.push(cur);
        }
    }
}

void displayCustomer()
{
    if (customer_queue.empty())
    {
        printf("No customers at the moment.\n");
    }
    else
    { 
        // std::queue doesn't allow access to all elements so we 
        // need to move elements off the queue onto temporary queue 
        // to access elements and then put them back once done
        std::queue<Customer> temp;

        printf("Customers in line are: \n");
        // pop elements of queue to inspect
        while(!customer_queue.empty()){
           Customer next_in_line = customer_queue.front();
           customer_queue.pop();
           printf("ID: %d\n", next_in_line.ID);
           temp.push( next_in_line );
        }
        customer_queue = temp; // replaces all elements back on customer_queue
    }
}

//Main 

#include <iostream>
#include <fstream>
#include <random>
#include <ctime>
#include <stdlib.h>


using namespace std;

int main()
{
    ifstream inputFile;
    string fName = "customernames.txt";
    readFile(fName);
    //end
#if (0)
//TODO I assume
//    //generate random teller
//    int randomTeller[tellers];
//    int randTime[waitTime];
//    generateRandoms(randomTeller, randTime);
//    for (int random = 0; random < tellers; random++);
//    for (int random = 0; random < waitTime; random++);
    //end
  #endif

    printf("\n--------------------Bankers only--------------------\n");
    int choice;
    do
    {     
        printf("1. Next customer\n2. Customer(s) that left\n3. Display the remaining customers\n4. Exit\n\n");
        printf("Enter your choice:");
        scanf("%d", &choice);        
        switch (choice)
        {
        case 1:
            nextCustomer();                                          
            break;
        case 2:
            removeCustomer();    
            break;
        case 3:
            displayCustomer();
            break;
        case 4:
            exit(0);
            break;
        default:
            printf("Sorry, invalid choice!\n");
            break;
        }
    } while (choice != 4);      
        return 0;       
}

推荐阅读