格式化代码如何显示为 PDF

格式化代码如何显示为 PDF

我正在编写一个包含大量代码的 PDF,但我无法将长代码块拆分为一页内的两列。长代码块占据整个页面,不会继续进入下一页。基本上我想要的是当算法 1 结束时,算法 2 在其下方继续,并转到算法 2 开始的下一列。在此处输入图片描述这是我的代码的一部分:

\begin{algorithm}
\caption{ReadFile.h}
\label{alg1}
 \algsetup{linenosize=\scriptsize}
  \scriptsize
\begin{algorithmic}[1]
\STATE \#include <stdio.h>
\STATE 
\STATE class ReadFile
\STATE \{
\STATE public:
 \STATE    
\STATE     void OpenFile(std::string);
\STATE     void CountArray(std::string);
\STATE     void SetNumberOfWords(int);
\STATE     int GetNumberOfWords();
\STATE     
\STATE     std::vector <std::string> myArrayOfWords;
\STATE     
\STATE private:
\STATE     int numberOfWords;
\STATE \};
\end{algorithmic}
\end{algorithm}

\begin{algorithm}
\caption{ReadFile.cpp}
\label{alg2}
 \algsetup{linenosize=\scriptsize}
  \scriptsize
\begin{algorithmic}[1]
\STATE \#include <iostream>
\STATE \#include <string>
\STATE \#include <math.h>
\STATE \#include <iomanip>
\STATE \#include <fstream>
\STATE \#include <vector>
\STATE \#include "ReadFile.h"
\STATE 
\STATE using namespace std;
\STATE 
\STATE //Este método abre el archivo de texto y guarda cada línea en un         array de tipo string
\STATE 
\STATE void ReadFile::OpenFile(string fileName)
\STATE \{
\STATE     string word;
\STATE     CountArray(fileName);
\STATE     int i = GetNumberOfWords();
\STATE     string *myArray = new string[i];
\STATE     cout << i << endl;
\STATE     ifstream myfile (fileName.c\_str());>
\STATE     if (myfile.i\_open())
\STATE     \{
\STATE         int j = 0;
\STATE         while(getline(myfile, word))
\STATE         \{
\STATE             myArray[j] = word;
\STATE             myArrayOfWords.push\_back(word);
\STATE             j++;
\STATE         \}
\STATE         myfile.close();
\STATE     \}
\STATE     else cout << "No se puede abrir el archivo";
\STATE \}

\STATE //Este método, cuenta cuantas líneas (palabras) fueron guardadas\\ en el array
\STATE 
\STATE void ReadFile::CountArray(string fileName)
\STATE \{
\STATE     int counter = 0;
\STATE     string word;
\STATE     ifstream myfile (fileName.c\_str());
\STATE     
\STATE     if (myfile.is\_open())
\STATE     \{
\STATE         while(getline(myfile, word))
\STATE             counter = counter+1;
\STATE         
\STATE         SetNumberOfWords(counter);
\STATE         cout << counter << endl;
\STATE         myfile.close();
\STATE     \}
\STATE     else cout << "File cannot be opened :";
\STATE \}

\STATE // Este método guarda el número de palabras del array

\STATE void ReadFile::SetNumberOfWords(int number)
\STATE \{
\STATE     numberOfWords=number;
\STATE \}
\STATE 
\STATE //Este método lee el número de palabras del array
\STATE 
\STATE int ReadFile::GetNumberOfWords()
\STATE \{
\STATE     return numberOfWords;
\STATE \}
\end{algorithmic}
\end{algorithm}

相关内容