首页 > 解决方案 > 将数组元素向左或向右移动一个空格的函数

问题描述

几天来,我一直在尝试解决这个问题,并搜索了很多不同的选项。我发现的所有代码都无法帮助我解决问题;如果这已经在网络上的其他地方得到回答,我很抱歉。我觉得这是我应该能够自己解决的问题,但似乎没有任何效果。我正在设计的程序是一个应该用于虚拟机器人的程序。机器人从滑槽中获得随机字符,并且必须将它们放置在一组 20 个插槽中。它有一个硬件限制,迫使它将第一个块放入插槽 10。我遇到的问题是我正在尝试创建一个函数,将数组中的每个元素向左或向右移动一个空格。我尝试了很多不同的东西。


#include <fstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

char get_block(void)
{
    char block;
    cout << "Enter one block: ";
    cin >> block;
    return toupper(block);
}
//
// Function print_slots
// Prints the contents of the slots array in a well formatted form.
// Input: Array of slots
// Returns: Nothing (void)
//
// Example function call: print_slots(slot_array);

void print_slots(char slots[])
{
    unsigned int j = 1;
    for (j = 1; j <= 20; j++)
    {
        cout << setw(3) << j;
    }
    cout << endl;
    for (j = 0; j < 20; j++)
    {
        cout << setw(3) << slots[j];
    }
    cout << endl;
}

// Function put_block
// This function stores a character into the character array representing the slots
//
// Inputs:
// block - type char - The character to be inserted into a slot
// position - type unsigned int - index of the slot where the block will go
// array - type char - array of slots containing the blocks
//
// Returns:
// position - type unsigned int - the index of the slot where the block was placed
//
// Example function call:   put_block(block, position, slots);

unsigned int put_block(char block, unsigned int position, char array[])
{
    bool debug = true;
    array[position] = block;
    if (debug)
        cout << "Block " << block << " inserted into slot " << position << endl;
    return position;
}

// Function remove_block
// This function removes a block from the slot array
// The slot where the block is removed is then set to a space
//
// Inputs:
// position - type unsigned int - index of the slot where block is located
// array - type char - array of slots containing the blocks
//
// Returns:
// block - type char - the block removed from the slot
//
// Example function call:   remove_block(position, slots);

unsigned int remove_block(unsigned int position, char array[])
{
    bool debug = true;
    char block = ' ';
    block = array[position];
    array[position] = ' ';  // Reset slot to blank after block removed
    if (debug)
        cout << "Block " << block << " removed from slot " << position + 1 << endl;
    return block;
}


// Function shift_right
// This function increments the index simulating a movement of the robot
// to the next higher slot (index) of the array
//
// Inputs:
// position - type unsigned int - current slot position
//
// Returns:
// position - type unsigned int - The updated position which is input position + 1
//
// Example function call:  position = shift_right(position)
//

unsigned int shift_right(unsigned int position)
{
    bool debug = true;
    position++;
    if (debug)
        cout << "Position right shifted to " << position << endl;
    return position;
}

// Function shift_left
// This function decrements the index simulating a movement of the robot
// to the next lower slot (index) of the array
//
// Inputs:
// position - type unsigned int - current slot position
//
// Returns:
// position - type unsigned int - The updated position which is input position - 1
//
// Example function call: position = shift_left(position)
//

unsigned int shift_left(unsigned int position)
{
    bool debug = true;
    position--;
    if (debug)
        cout << "Position left shifted to " << position << endl;
    return position;
}

// Function robot_ltoreq_slot
// This function compares the value of the block held by the robot
// with the value of the block in a slot
//
// Inputs:
// robot - type char - value of block held by robot
// in_slot - type char - value of block in the slot
//
// Returns:
// true or false
// TRUE if block held by robot is LESS than or equal to the block in slot
// FALSE if block held by robot is GREATER than block in slot
//
// Example function call: if ( compare_blocks(robot_block, slot_block) )
//
bool robot_ltoreq_slot(char robot, char in_slot)
{
    bool debug = true;
    if (debug)
        cout << endl <<  "Comparing robot block " << robot << " with block in slot " << in_slot << endl;
    if (robot <= in_slot)
    {
        if (debug)
            cout << "Returning true. Robot block LESS than or EQUAL to block in slot. " << endl;
        return true;
    }
    else
    {
        if (debug)
            cout << "Returning false. Robot block GREATER than block in slot. " << endl;
        return false;
    }
}
// This function checks if the blocks are less than or == to the block in the robots hand
// If the block is equal to it return TRUE if the block is < it return FALSE

bool robot_equal_slot(char robot, char in_slot)
{
    bool debug = true;
    if (debug)
        cout << endl <<  "Comparing robot block " << robot << " with block in slot " << in_slot << endl;
    if (robot == in_slot)
    {
        if (debug)
            cout << "Returning true. Robot block EQUAL to block in slot. " << endl;
        return true;
    }
    else
    {
        if (debug)
            cout << "Returning false. Robot block LESS THAN than block in slot. " << endl;
        return false;
    }
}

// Function switch_blocks
// This function switches the block held by the robot with a block in a slot.
// After the switch the robot is holding the block removed from the slot.
//
// Inputs:
// robot - type char - The block to be inserted into a slot
// position - type unsigned int - index of the slot where the block will go
// array - type char - array of slots containing the blocks
//
// Returns:
// robot - type char. The value of the block removed from the slot.
//
// Example function call: block = switch_blocks(block,  position, array);
//

char switch_blocks(char robot, unsigned int position, char array[])
{
    char temp_hold;
    bool debug = true;
    if (debug)
        cout << "Switching blocks " << robot << " with " << array[position] << endl;
    temp_hold = robot;
    robot = array[position];
    array[position] = temp_hold;
    return robot;
}
// Function test_empty
// This function tests the array to determine if a slot is empty (NULL)
// or if the slot contains a blank. The slot array must be intialized to
// all NULL or all blanks (spaces) before any blocks are added.
//
// Inputs:
// position - type unsigned int - index of slot to be tested
//
// Returns:
// true or false as value o function
// TRUE if slot is empty
// FALSE if the slot contains a block
//
// Example function call: if ( test_empty(index, array) )
//


bool test_empty(unsigned int position, char array[])
{
    char blank = ' '; // Blank space
    bool debug = true;
    if  (array[position] == NULL || array[position] == blank)
    {
        if (debug)
            cout << "Slot " << position << " empty. " << endl;
        return true;
    }
    else
    {
        if (debug)
            cout << "Slot " << position << " contains a block " << endl;
        return false;
    }

}
char * move_all_to_right(char slot[], char block){
    for(int i = 0; i < 20; i++){
        if((!(test_empty(i,slot))) && (i != 20)){
            block = switch_blocks(block,i,slot);
            put_block(block,i+1,slot);
            block = switch_blocks(block, i+1, slot);

        }
        else if(i == 20){
            break;
        }
    }
    return slot;

}

int main() {
    char block = get_block();
    char slot[20];
    for (int i = 0; i < 20; i++) {
        slot[i] = NULL;
    }
    int array_size;
    int position;
    char robot;
    int counter = 0;
    int dummy;
    int filled_right;
    bool swapped = false;
    int block_counter = 0;
    int switch_counter = 0;
    //Put the first block from the chute INTO SLOT 10(hardware restriction)
    put_block(block, 10, slot);
    do {
        block = get_block();
        position = counter;
        for(int i = 0; i <sizeof(slot); i++) {
            unsigned int back_to_front = sizeof(slot);
            unsigned int front_to_back = 0;
            if(!(test_empty(back_to_front, slot))){
                do{
                    back_to_front--;
                }while(test_empty(back_to_front, slot));
                do{
                    front_to_back++;
                }while(test_empty(front_to_back,slot));
                for(int i = 0; i <sizeof(slot); i++){
                    if(!(test_empty(i,slot))){
                        block_counter++;
                    }
                }
                if((block <= slot[front_to_back]) && (test_empty(0,slot))){
                    put_block(block, front_to_back - 1,slot);
                    break;
                }
                else if((block > slot[back_to_front]) && (test_empty(19,slot))){
                    put_block(block,back_to_front + 1,slot);
                    break;
                }
                if((block == slot[back_to_front]) && (test_empty(19,slot))){
                    put_block(block,back_to_front + 1,slot);
                    break;
                }
                if(((block < slot[back_to_front]) || (block == slot[front_to_back])) && (test_empty(19,slot))){
                        while((block < slot[back_to_front]) || (block == slot[back_to_front]) &&(block!= NULL)){
                            block = switch_blocks(block,back_to_front-1,slot);
                            back_to_front--;
                            switch_counter ++;
                            if((block == slot[back_to_front]) && (switch_counter != block_counter + 1)){
                                for(int i = back_to_front; i< sizeof(slot); i++){
                                   block = switch_blocks(block,i,slot);
                                   if(test_empty(back_to_front-1,slot)){
                                       break;
                                   }
                                }
                            }
                        }
                }
            }
        }
        block_counter = 0;
        print_slots(slot);
        counter++;
        switch_counter = 0;
    } while (counter < 19);
    for(int i = 0; i < sizeof(slot); i++){
        cout << slot[i];
    }
}

在我引入冗余字符之前,这似乎有效,所以我感觉为了更简单的东西而放弃了这种方法。

int main() {
    char block = get_block();
    char temp_block = ' ';
    char array = *slot;
    for (int i = 0; i < 20; i++) {
        slot[i] = NULL;
    }
    int counter = 0;

    //Put the first block from the chute INTO SLOT 10(hardware restriction)
    put_block(block, 10, slot);
    do {
        block = get_block();
        if (block > slot[10]) {
            for(int i = 9; i < 19; i ++){
                if((block > slot[i]) &&!(test_empty(i,slot)) || i == 19){
                    shift_all_left(slot);
                    put_block(block,i,slot);
                    break;
                }
            }
        }
        if (block <= slot[10]) {
            for(int i = 10; i > 0; i --){
                if((block <= slot[i]) || i == 0){
                    shift_all_right(slot,20, block);
                    put_block(block,i,slot);
                    break;
                }
            }
        }
        counter++;
        print_slots(slot);
    }while(counter < 19);
    return 0;
}

我提出的右移函数实际上并没有移动我的数组的第一个字符,坦率地说它根本不起作用。

这就是我到目前为止所拥有的。

void shift_all_right(char array[],int n, char block){
    int i = 10;
    while(i < n - 1){
        block = switch_blocks(block,array[i],array);
        i++;
    }
    return;
}

我仍然拥有第一次尝试的所有功能,而且我还有一个 shift_all_left 功能,这是我尝试过的另一个想法,但它没有按预期工作。事实上,当我将两者结合起来时,它在我的程序中做了一些奇怪的事情。

void shift_all_left(char array[]){
    for(int i = 10; i > 0; i--){
        switch_blocks(array[i],array[i-1],array);

    }

}

如果这是我应该能够弄清楚的事情,我再次感到抱歉,但我觉得我正在用头撞砖墙。我真的很想能够在没有帮助的情况下解决这个问题,但我根本做不到。

标签: c++arraysrobotframework

解决方案


N 表示数组的大小。我的想法是我总是从插槽 10 开始并从那里排序,但我越想这个,我似乎应该从数组中块 <= 的位置开始。

void shift_right(int position, int size, char array[], char block){
   while(position < size - 1){
block = switch_block(block,array[position+1],array);}

我在正确的轨道上吗?


推荐阅读