このプログラムは、一種の概念実証として数日前に作成しました。それが機能することがわかったので、コードをクリーンアップして、物事の流れを改善しようとしています。私が書いた元のプログラムとこの新しいバージョンからの最大の変更点は、何度も使用したものを関数に変えたことです。私が抱えている問題は、関数を呼び出すすべての場所で、リンカーエラーが発生することです。このエラーを除いて、プログラムはこれで終了したと思います。
最もトリッキーなのは、昨夜、このエラーに遭遇し、別のエラーを処理する前にchar*
、char
エラーを修正し、コードが正常に機能したことです。当時書かれていました。今日私がそれに取り組み始めたとき、私はどういうわけか私がその進歩を失ってしまったことに気づき、それを修正しに行きました。エラーを再度修正すると(私の知る限り、まったく同じ方法で修正しました)、関数を呼び出すたびに、また新しい関数を記述して呼び出したときに、このリンカーエラーが発生しましたmain()
。
コードは次のとおりです。
//******************************************************************************
// David Ewing
// This program is an improvement on the original Cypher program which cyphered
// upto 255 characters of inputed text according to a provided keyphrase. This
// version seeks to improve the flow of the code and allow for a larger input of
// text.
//******************************************************************************
#include <iostream>
using namespace std;
// Declair global variables
char keyphrase[8192];
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char cypherAlphabet[8192];
char sourceText[8192];
char cypherText[8192];
int sizeKeyphrase;
int sizeCypherAlphabet;
int sizeSourceText;
// ...Counters
int counter1;
int counter2;
int counter3;
// Declair functions
void cypher (char, int);
void eliminateDuplicates (char, int);
int getSize(char);
void handleSpaces (char, bool);
void reduceCase (char, int);
//******************************************************************************
// MAIN
//******************************************************************************
int main( void )
{
// Describe program
cout << "This program cyphers whatever text the user wishes according to a " << endl;
cout << "keyphrase that the user provides." << endl << endl;
// Retrieve keyphrase
cout << "Please enter the keyphrase that you would like to use for your cypher." << endl;
cout << "The keyphrase may include spaces, repeated letters, and capitals, but " << endl;
cout << "they will be removed before it is applied to the provided text." << endl;
cout << "Enter \"DONE\" when you have finished typing your keyphrase." << endl;
// Call function to remove spaces from keyphrase
handleSpaces (keyphrase[8192], false);
// Call function to get size of keyphrase
sizeKeyphrase = getSize (keyphrase[8192]);
// Call function to remove repeated letters from keyphrase
eliminateDuplicates (keyphrase[8192], sizeKeyphrase);
// Call function to normalize case of keyphrase
reduceCase (keyphrase[8192], sizeKeyphrase);
// Apply keyphrase to alphabet
strcpy (cypherAlphabet, keyphrase); // Copy the keyphrase to the beginning of cypherAlphabet
strcat (cypherAlphabet, alphabet); // Add the rest of the alphabet after the keyphrase to cypherAlphabet
// Call function to get size of cypherAlphabet
sizeCypherAlphabet = getSize (cypherAlphabet[8192]);
// Call function to remove repeated letters from cypherAlphabet
eliminateDuplicates(cypherAlphabet[8192], sizeCypherAlphabet);
// Retrieve sourceText
cout << endl << "Please enter the text which you wish to be cyphered. Capitals will be" << endl;
cout << "removed but punctuation and any other non-alphanumeric characters will" << endl;
cout << "be ignored." << endl;
cout << "Enter \"DONE\" when you have finished typing your text." << endl;
// Call function to take input for sourceText and to handle spaces
handleSpaces(sourceText[8192], true);
// Call function to get size of sourceText
sizeSourceText = getSize (sourceText[8192]);
// Call function to normalize case of sourceText
reduceCase(sourceText[8192], sizeSourceText);
// Cypher sourceText
cypher(sourceText[8192], sizeSourceText);
// Display cypherText
cout << endl << "Your cyphered text is as follows:" << endl;
cout << cypherText << endl << endl;
// Pause program
system ("pause");
// End program
return 0;
} // End main
//******************************************************************************
// CYPHER
//******************************************************************************
void cypher (char text[8192], int size)
{
// Declare counters
counter1 = 0;
counter2 = 0;
// Search alphabet for address of each letter
while (counter1 < size)
{
counter2++;
if (sourceText[counter1] == alphabet[counter2-1])
{
cypherText[counter1] = cypherAlphabet[counter2 - 1];
counter1++;
counter2 = 0;
} // End if
if (counter2 > 25) // If all the letters in the alphabet are checked
{
cypherText[counter1] = sourceText[counter1]; // give up on the letter, transcribing it over
counter1++; // and continue with the rest of the soureText
counter2 = 0; // Allows for punctuation, spaces, strange symbols, etc.
} // End if
} // End while
} // End cypher
//******************************************************************************
// ELIMINATE DUPLICATES
//******************************************************************************
void eliminateDuplicates (char text[8192], int size)
{
// Initialize counters
counter1 = 0;
counter2 = 1;
counter3 = 1;
// Initialize flag
//bool flag = false;
while (counter1 < size)
{
if (text[counter1] == text[counter1+counter2] && text[counter1] != NULL)
{
// Delete text[i+j]
for (counter3 = 1; counter3 < size; counter3++)
{
text[counter1+counter2+counter3-1] = text[counter1+counter2+counter3]; // Shift array left at repeated letter. Final value is doubled.
//if (counter3 == size - 1)
//{
// flag = true;
//} // End if
} // End for
} // End if
else if (counter2 == size)
{
counter1++;
counter2 = 1; // Reset counter
//flag = false; // Reset flag
} // End else if
else
{
counter2++;
//flag = false;
} // End else
} // End while
} // End eliminateDuplicates
//******************************************************************************
// GET SIZE
//******************************************************************************
int getSize (char text[8192])
{
int size = 0; // Declair counter/result variable
while (text[size] != NULL)
{
size++;
} // End while
return size;
} // End getSize
//******************************************************************************
// HANDLE SPACES
//******************************************************************************
void handleSpaces (char text[8192], bool includeSpaces)
{
// Declare temporary input holder
char temp[8192];
// Initialize flag
bool flag = false;
cin >> text;
while (flag == false)
{
cin >> temp;
if (strcmp(temp, "DONE"))
{
if (includeSpaces == true)
{
strcat(text, " "); // Add space after last word
} // End if
strcat(text, temp); // Add the next word to the last
} // End if
else
{
flag = true;
} // End else
} // End while
} // End handleSpaces
//******************************************************************************
// REDUCE CASE
//******************************************************************************
void reduceCase (char text[8192], int size)
{
// Declare counter
int counter1 = 0;
while (counter1 < size)
{
sourceText[counter1] = tolower(sourceText[counter1]); // Use tolower for each item in the array
counter1++;
} // End while
} // End reduceCase