0

几天前我写了这个程序作为一种概念验证。现在我知道它有效,我正在尝试清理代码并使事情变得更好。与我编写的原始程序和它的新版本相比,最大的变化是我将不止一次使用的任何东西变成了一个函数。我遇到的问题是对于我调用任何函数的每个地方,我都会收到链接器错误。我相信程序现在已经完成,除了这个错误。

最棘手的一点是昨晚——在我遇到这个错误并处理另一个与之相关的错误之前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
4

1 回答 1

0

声明和定义不同:

宣言:

void cypher (char, int);

定义:

void cypher (char text[8192], int size)

只是一个例子。

另外,调用是错误的:

cypher(sourceText, sizeSourceText);

代替

cypher(sourceText[8192], sizeSourceText);

当你写:

void cypher (char text[8192], int size)

这意味着该函数将数组作为参数。

当你用

cypher(sourceText[8192], sizeSourceText);

这意味着您正在使用8192char数组中位置的字符调用该函数。

于 2012-05-30T16:15:00.340 回答