簡體   English   中英

A function 將數組的元素向左或向右移動一個空格

[英]A function to shift the elements of an array one space left or right

幾天來,我一直在嘗試解決這個問題,並搜索了很多不同的選項。 我發現的所有代碼都無法幫助我解決問題; 如果這是 web 其他地方已經回答的問題,我很抱歉。 我覺得這是我應該能夠自己解決的問題,但似乎沒有任何效果。 我正在設計的程序是一個應該用於虛擬機器人的程序。 機器人從滑槽中獲得隨機字符,並且必須將它們放置在一組 20 個插槽中。 它有一個硬件限制,迫使它將第一個塊放入插槽 10。我遇到的問題是我正在嘗試創建一個 function,它將數組中的每個元素向左或向右移動一個空格。 我嘗試了很多不同的東西。 我最成功的就是這段代碼。


#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;
}

我想出的右移 function 實際上並沒有移動我的數組的第一個字符,坦率地說它根本不起作用。

這就是我到目前為止所擁有的。

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 function ,這是我嘗試過的另一個想法,但沒有按預期工作。 事實上,當我將兩者結合起來時,它在我的程序中做了一些奇怪的事情。

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

    }

}

如果這是我應該能夠弄清楚的事情,我再次感到抱歉,但我覺得我正在用頭撞磚牆。 我真的很想能夠在沒有幫助的情況下解決這個問題,但我根本做不到。

N 表示數組的大小。 我的想法是我總是從插槽 10 開始並從那里排序,但我越想這個,我似乎應該從 position 開始,該塊在數組中 <=。

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

我在正確的軌道上嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM