Typedef的问题 - 需要忽略某些代码

时间:2014-02-03 00:17:38

标签: c++ sorting typedef

这是我最大的问题。

我正在开发一个执行一系列快速排序算法的程序。 (Quick,Radix和Merge)。

但是,我想对多种类型的数据(特别是整数和字符串列表)进行排序。因此我决定使用Typedef以使我的关键变量能够改变类型。

在Radix代码中,我有一个需要进行整数数学的区域。我为整数编写了一个版本,并为字符串编写了一个版本。 (在字符串版本中,我使用stoi())。

当我想使用Integers运行程序时,我需要它来忽略需要stoi()转换的代码区域。

如何根据typedef语句中的内容使编译器有条件地忽略代码?

编辑:我建议使用#ifndef来隐藏代码段,现在,因为我现在主要想调试我的代码。 (我的Radix Sort不使用字符串。)

一旦我的所有代码都正常工作,我会尝试解决模板的问题!


根据要求,这是代码。没有包含实际功能的代码。

#include<iostream>
#include<ctime>
#include<cmath>
#include<fstream>
#include<stdlib.h>
#include<string>
#include"Queue.h"
using namespace std;

// Global variables
ItemType Data[100000];      // Data array
Queue QArray[26];           // Array of Queues. 0-9 used for numbers, 0-25 used for characters/strings

// Functions
void QuickSort(ItemType Data[], int Lower, int Upper);
int Partition(ItemType Data[], int Lower,int Upper);
void RadixSort(ItemType Data[], int counter, int digits);
void MergeSort(ItemType Data[], int Lower, int Upper);
void Merge(int Lower, int Mid, int Upper);

// Main
int main()
{
    // Initialize and open the text files
    ifstream NumberInput, StringInput;
    ofstream QuickNumOut, QuickStrOut, RadixNumOut, RadixStrOut, MergeNumOut, MergeStrOut;
    NumberInput.open("Numbers.txt");
    StringInput.open("Strings.txt");
    QuickNumOut.open("QuickSortedNumbers.txt");
    QuickStrOut.open("QuickSortedStrings.txt");
    RadixNumOut.open("RadixSortedNumbers.txt");
    RadixStrOut.open("RadixSortedStrings.txt");
    MergeNumOut.open("MergeSortedNumbers.txt");
    MergeStrOut.open("MergeSortedStrings.txt");


    // Declare the variables
    time_t totaltime;   // Used to calculate run time of algorithms
    int counter = 0;    // Counter for items
    int choice, type;   // User chooses string or integer sort, and type of sort
    ItemType max;       // Largest number, used for Radix sort
    int digits = 0;     // Finds number of digits in max, for Radix sort

    cout << "Would you like to sort the list of (1) Numbers, or (2) Strings?  ";
    cin >> choice;
    cout << "Would you like to use (1) Quick Sort, (2) Radix Sort, or (3) Merge Sort?  ";
    cin >> type;

    switch(type)
    {
    case 1:
        {
            // Switch statement to read information into array from the text files
            switch (choice)
            {
            case 1: 
                {
                    // While loop reads in data until end of file is found
                    while(!NumberInput.eof())
                    {
                        NumberInput >> Data[counter];
                        counter++;
                    }
                    break;
                }
            case 2:
                {
                    // While loop reads in data until end of file is found
                    while(!StringInput.eof())
                    {
                        StringInput >> Data[counter];
                        counter++;
                    }
                    break;
                }
            }

            // Close input files
            NumberInput.close();
            StringInput.close();

            // Start timer
            totaltime = time(0);
            // Perform QuickSort
            QuickSort(Data, 0, counter-1);
            // Stop timer
            totaltime = time(0) - totaltime;

            // Switch statement to print sorted list to text files
            switch (choice)
            {
            case 1:
                {
                    // For loop goes from 0 to the length of actual data in the array
                    for (int i = 0; i < counter; i++)
                        QuickNumOut << Data[i] << endl;
                    break;
                }
            case 2:
                {
                    // For loop goes from 0 to the length of actual data in the array
                    for (int i = 0; i < counter; i++)
                        QuickStrOut << Data[i] << endl;
                    break;
                }
            }

            // Print out information
            cout << "QuickSort data written to file." << endl;
            cout << "Total time for QuickSort was " << totaltime << " seconds." << endl << endl;
            break;
        }
    case 2:
        {
            // Reset counter
            counter = 0;

            // Reopen files
            NumberInput.open("Numbers.txt");
            StringInput.open("Strings.txt");

            // Switch statement to read information into array from the text files
            switch (choice)
            {
            case 1: 
                {
                    max = Data[0];
                    // While loop reads in data until end of file is found
                    while(!NumberInput.eof())
                    {
                        NumberInput >> Data[counter];
                        if (Data[counter] > max)
                            max = Data[counter];
                        counter++;
                    }
                    break;
                }
            case 2:
                {
                    /*
                    max = Data[0];
                    // While loop reads in data until end of file is found
                    while(!StringInput.eof())
                    {
                        StringInput >> Data[counter];
                        if (Data[counter] > max)
                            max = Data[counter];
                        counter++;
                    }*/
                    break;
                }
            }

            // Find out how many digits are in largest number
            while (max)
            {
                max /= 10;
                digits++;
            }

            // Close input files
            NumberInput.close();
            StringInput.close();

            // Start timer
            totaltime = time(0);
            // Perform QuickSort
            RadixSort(Data, counter, digits);
            // Stop timer
            totaltime = time(0) - totaltime;

            // Switch statement to print sorted list to text files
            switch (choice)
            {
            case 1:
                {
                    // For loop goes from 0 to the length of actual data in the array
                    for (int i = 0; i < counter; i++)
                        RadixNumOut << Data[i] << endl;
                    break;
                }
            case 2:
                {
                    // For loop goes from 0 to the length of actual data in the array
                    for (int i = 0; i < counter; i++)
                        RadixStrOut << Data[i] << endl;
                    break;
                }
            }

            // Print out information
            cout << "RadixSort data written to file." << endl;
            cout << "Total time for RadixSort was " << totaltime << " seconds." << endl << endl;
            break;
        }
    case 3:
        {
            // Reset counter
            counter = 0;

            // Reopen files
            NumberInput.open("Numbers.txt");
            StringInput.open("Strings.txt");

            // Switch statement to read information into array from the text files
            switch (choice)
            {
            case 1: 
                {
                    // While loop reads in data until end of file is found
                    while(!NumberInput.eof())
                    {
                        NumberInput >> Data[counter];
                        counter++;
                    }
                    break;
                }
            case 2:
                {
                    // While loop reads in data until end of file is found
                    while(!StringInput.eof())
                    {
                        StringInput >> Data[counter];
                        counter++;
                    }
                    break;
                }
            }

            // Close input files
            NumberInput.close();
            StringInput.close();

            // Start timer
            totaltime = time(0);
            // Perform QuickSort
            MergeSort(Data, 0, counter-1);
            // Stop timer
            totaltime = time(0) - totaltime;

            // Switch statement to print sorted list to text files
            switch (choice)
            {
            case 1:
                {
                    // For loop goes from 0 to the length of actual data in the array
                    for (int i = 0; i < counter; i++)
                        MergeNumOut << Data[i] << endl;
                    break;
                }
            case 2:
                {
                    // For loop goes from 0 to the length of actual data in the array
                    for (int i = 0; i < counter; i++)
                        MergeStrOut << Data[i] << endl;
                    break;
                }
            }

            // Print out information
            cout << "MergeSort data written to file." << endl;
            cout << "Total time for MergeSort was " << totaltime << " seconds." << endl << endl;
            break;
        }
    default:
        {
            cout << "Invalid choice.";
            break;
        }
    }

    // Close output files
    QuickNumOut.close();
    QuickStrOut.close();
    RadixNumOut.close();
    RadixStrOut.close();

    system("PAUSE");
    return 0;
}

2 个答案:

答案 0 :(得分:1)

我刚刚注意到您在switch内处理了类型识别,而不是typedef(就像您在主题中暗示的那样)。在这种情况下 - 使用模板功能。这很容易实现 - 例如QuickSort将成为:

template <typename T>
void QuickSort(T Data[], int Lower, int Upper)
{
   // Your current code operating on 'T' instead of 'int'/'string'
}

等等。此解决方案将使您的代码缩短一吨。

然后你就这样使用它:

switch(type){
   case 1:
      QuickSort<int>(table,lower,upper);
      break;
   case 2:
      QuickSort<string>(table,lower,upper);
}

此外 - 使用 containers 代替数组,使事情变得更加简单。我建议std::vector

答案 1 :(得分:0)

您可以使用以下内容对代码中的某个部分进行预处理:

#define IGNORE_STOI

...

#ifndef IGNORE_STOI
// string to int section of code
#endif

如果定义了#ifndef,这将删除#endifIGNORE_STOI语句之间的部分。