将数组元素向左或向右移动一个函数的功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数组元素向左或向右移动一个函数的功能相关的知识,希望对你有一定的参考价值。
我几天来一直在努力解决这个问题,并用谷歌搜索了许多其他选项。我发现的代码都无法帮助我解决问题。很抱歉,如果这已经在网络上的其他地方得到了解答。我觉得这种事情我应该可以自己解决,但似乎没有任何效果。我正在设计的程序是应该用于虚拟机器人的程序。机器人从溜槽中获得了随机字符,必须将它们放置在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);
}
}
如果这是我应该能弄清楚的事情,我再次感到抱歉,但我感觉好像是在撞砖墙撞头。我真的很想能够在没有帮助的情况下解决此问题,但我根本做不到。
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);}
我在正确的轨道上吗?
以上是关于将数组元素向左或向右移动一个函数的功能的主要内容,如果未能解决你的问题,请参考以下文章
在 PyQt5 中将 QsciScintilla 的文本光标向左或向右移动