将 C 程序转换为伪代码

Translating C programming into pseudocode

我正在为算法编写伪代码。结果看起来很像C语言,因为它是我最熟悉的语言。

代码如下:

START
Declare int m, int n
Input value of m and n
Declare int array source[n], target[m][n]

For(int i = 0; i < n; i++)
    Input value of source[i]

For(int i = 0; i < m; i++)
    For(int j =  0; j < n; j++)
        Input value of target[i][j]

Declare int maxsource, minsource, maxtarget, mintarget
For(int i = 0; i < n; i++)
    If i == 0 
        Minsource = source[i]
        Maxsource = source[i]
    Else
        If minsource > source[i]
            Minsource = source[i]
        Else if maxsource < source[i]
            Maxsource = source[i]
        Endif
    Endif

For(int i = 0; i < m; i++)
    For(int j = 0; j < n; j++)
        If j == 0
            Mintarget = target[i][j]
            Maxtarget = target[i][j]
        Else
            If mintarget > target[i][j]
                Mintarget = target[i][j]
            Else if maxtarget < target[i][j]
                Maxtarget = target[i][j]
            Endif
        Endif
    If minsource == mintarget && maxsource == maxtarget
        Print true
        STOP
    Else if maxtarget > maxsource
        Print false
        STOP
    Endif

Print false
STOP

如何将它变成更像伪代码的代码?有什么建议吗?

如果我理解你的话,你不希望伪代码看起来很像程序代码,以便使用不同编程语言的人可以使用它来解决相同的任务。我有一些技巧可以帮助您实现这一目标。主要思想是您应该避免使用编程语言的句法元素。考虑要解决的问题,而不是编程语言。用任何人都能阅读和理解的简单语言编写伪代码。没有编码技能的人可以遵循它并解决相同的任务应该很简单。下面是一个函数的简单伪代码示例,该函数用于查找数组中的第二大元素。

The function takes two inputs:
  arr - array of integers
  length - lenght of the array
It returns:
  secMax - Second maximum element

然后是步骤(要用简单的语言)

Define and set the maximum and second maximum as the minimum possible value

Repeat for each element of the array
{
  If the current element is greater than the current maximu
  {
    then make current maximum as second maximum
    Make the maximum as current array element
    (Now you have new maximum and second maximum)
  }
  else if the current array element is less than maximum but is greater than second 
  maximum
  { 
  then make it second maximum
  }
 
}
return second maximum

如您所见,我只是在没有使用任何编程语言的句法元素的情况下解释这些步骤。所以现在任何人都可以使用 his/her 舒适的语言来执行我的步骤来解决相同的任务。

假设我想用 C 来实现它,我的代码将是这样的:

//Defining the function according to the information above
//It takes to inputs and returns an integer

int findSecondMaximum(int arr[], int length)
{
  //Define and Set the maximum and second maximum as the minimum possible value
    int max, secondMax;
    max = secondMax = INT_MIN;

  for (int i = 0; i < length; i++) //Repeat for each element of the array
  {
     //If the current element is greater than the current maximum
     if (vect[i] > max)
     {
       //then make the current maximum as second maximum
        secondMax = max;
           
      //Make the maximum as the current array element
          max = vect[i];
     }

     /*else if the current array element is less than maximum but is greater than 
     second maximum*/
     else if (vect[i] > secondMax && vect[i] < max)
     { 
       //make it second maximum
        secondMax = vect[i];
     }
 
  }
  //return second maximum
   return secondMax;
  
}

希望对您有所帮助。

主要检查wikipedia on pseudocode。 或者:

  • 使用 Pascal 风格(例如),或者
  • 使用数学风格

首先是一个错误:

    If minsource > source[i]
        minsource = source[i]
    Else if maxsource < source[i]
        maxsource = source[i]
    Endif

应该是

    If minsource > source[i]
        minsource = source[i]
    Endif
    If maxsource < source[i]
        maxsource = source[i]
    Endif

大小写要一致(minsource/Minsource)。

你可能会介绍

Function {int min, int max} array_min_max(int[] array)
    min = Integer.MAX_VALUE;
    max = Integer.MIN_VALUE;
    For value in array
        min = Math.min(min, value);
        max = Math.max(min, value);
    Endfor
Endfunction

首次使用时声明可读性更好。

Declare int minsource, maxsource
For (int i = 0; i < source.length; i++)
    If i == 0 
        minsource = source[i]
        maxsource = source[i]
    Else
        If minsource > source[i]
            minsource = source[i]
        EndIf
        If maxsource < source[i]
            maxsource = source[i]
        Endif
    Endif
Endfor

For (int i = 0; i < m; i++)
    Declare int mintarget, maxtarget
    {mintarget, maxtarget} = array_min_max(target[i]);
    If minsource == mintarget && maxsource == maxtarget
        Print true
        STOP
    Else if maxtarget > maxsource
        Print false
        STOP
    Endif
Endfor

Print false
STOP

关于伪语言本身:上面的“Basic”及其可识别的 C 我觉得有点烦人。另外,我会选择 min_target 或 minTarget 而不是 mintarget。

有这么多语言。在计算机科学中有一个长期的非传统。高级表达式语言 Algol68 被经常使用,因为它甚至有一个 bold 类型和关键字的字体。要是能用richt text就好了

Pascal 之类的伪代码,可能是从 Modula 借来的IF THEN ELSE END

就我个人而言,我不喜欢驼峰式大小写,更喜欢全部小写。