函数和旋转char

functions and rotating char

本文关键字:char 旋转 函数      更新时间:2023-10-16

我需要你的帮助!我试着让这个程序读取四个字符,然后以旋转的模式打印出来。旋转完四个字符后,它将读取另外四个字符并旋转它们。它重复这个读取和旋转的过程,直到文件结束。

当我运行程序时,它没有打印4次。它打印一次,不旋转任何东西。

这是我的代码

#include <iostream>
using namespace std;
const int NUM_STEPS = 4;   // number of rotation steps to perform
void DisplayRotationPattern(char c1, char c2, char c3, char c4);
void DisplayFourChars(char c1, char c2, char c3, char c4);

//     This function "rotates" four characters c1, c2, c3, and c4.
//     That is, it puts c2 in c1, c3 in c2, c4 in c3, and c1 in c4.
void RotateFourChars(char c1, char c2, char c3, char c4);

int main()
{
   char ch1, ch2, ch3, ch4;   
   cout << "Enter four characters: ";
   cin >> ch1 >> ch2 >> ch3 >> ch4;
   while( cin )
   {
      cout << "The Rotation patterns are:" << endl;
      DisplayFourChars(ch1, ch2, ch3, ch4);

      cout << "Enter four characters: ";
      cin >> ch1 >> ch2 >> ch3 >> ch4;
   }
   return 0;
}

//-----------------------------------------------------------------------
// This function displays the four characters passed to it
// in a rotated pattern.
// params: (in, in, in, in)
//-----------------------------------------------------------------------
void DisplayRotationPattern(char c1, char c2, char c3, char c4)
{
   int count = 1;
   while (count <= NUM_STEPS)
   {
      DisplayFourChars( c1, c2, c3, c4);
      cout << endl;

      RotateFourChars( c1,  c2,  c3, c4 );
      count ++;
   }
}
//-----------------------------------------------------------------------       
// This function "rotates" four characters c1, c2, c3, and c4.
// That is, it puts c2 in c1, c3 in c2, c4 in c3, and c1 in c4.
// params: (inout, inout, inout, inout)
//-----------------------------------------------------------------------
void RotateFourChars(char c1, char c2, char c3, char c4)
{  
      char temp;
       temp = c1;
       c1 = c2;
       c2 = c3;
       c3 = c4;
       c4 = temp;

}
//-----------------------------------------------------------------------
// This function has four input parameters, each of them is a character.
// The function displays the four characters at the beginning of a line.
// params: (in, in, in, in)
//-----------------------------------------------------------------------
void DisplayFourChars(char c1, char c2, char c3, char c4)
{
    cout << c1 << c2 << c3 << c4 << endl;
}

问题是您在RoateFourCharacters函数中按值传递。

更改为pass-by-reference:

 void RotateFourChars(char& c1, char& c2, char& c3, char& c4)
 {  
     char temp;
     temp = c1;
     c1 = c2;
     c2 = c3;
     c3 = c4;
     c4 = temp;
 }

或者,你可以使用指针:

 void RotateFourChars(char* c1, char* c2, char* c3, char* c4)
 {  
     char temp;
     temp = *c1;
     *c1 = *c2;
     *c2 = *c3;
     *c3 = *c4;
     *c4 = temp;
 }
 // And when calling, make sure to pass the address of the chars
 RotateFourChars(&c1, &c2, &c3, &c4);

更改RotateFourChars函数如下

声明部分

void RotateFourChars(char& c1, char& c2, char& c3, char& c4)

同样在定义部分

void RotateFourChars(char& c1, char& c2, char& c3, char& c4){
}

还有一件事,你不调用RotateFourChars函数在你的main()

while( cin )
{
    cout << "The Rotation patterns are:" << endl;
    RotateFourChars(ch1, ch2, ch3, ch4);          // <-- call RotateFourChars here
    //DisplayRotationPattern(ch1, ch2, ch3, ch4); // <-- call DisplayRotationpattern
    DisplayFourChars(ch1, ch2, ch3, ch4);         // <-- instead of calling this