搜索算法无法提取选择的数字

时间:2019-11-24 15:27:48

标签: c++

我正在创建一个代码...

  1. 文本文件中的数字被读入数组
  2. 用户可以搜索一个号码,程序会告诉您该号码是否在文本文件中。
  3. 数字被排序并输出到另一个文件
  4. 我从3中的排序数据文件中进行二进制搜索

问题我正在面对...

  1. 当我搜索“ 0”时有效。但是,如果它是任何其他数字,则找不到。
  2. 在另一个文件中输出数字时,一个随机的巨大数字将添加到列表中。 (即10 9 8 7 6 5 4 3 2 1 -8723641187(假设-8723641187为0))

我有4个文本文件,每个文本文件有10个小数字。其中3个在不同部分中具有0。其中一个没有任何0。

代码(在合适的地方#delen len 10):

功能1

 void simpleSearch(std::string inputFile, int value)
    {
        bool found = false ;
        int num,
        count = 0 ;

        std::ifstream file(inputFile.c_str()) ;

        while(!file.eof())
        {
            file >> num ;
            count++ ;

            if (num == value)
            {
                std::cout << "Target Is Found\n" ;
                found = true ;
                break ;
            }
        }

        if(found == false)
        {
            std::cout << "Target Not Found\n" ;
        }    
}

功能2

    void sorting(std::string inputFile, std::string outputFile)
{
    int num,
        first,
        temp,
        i = 0,
        size = len + 1,
        contents[len + 1];

    std::ifstream file(inputFile.c_str()) ;

    while(!file.eof())
    {
        file >> num ;
        contents[i++] = num ; 
    }

    for (i = size - 1; i > 0; i--)
    {
        first = 0 ;
        for(int j = 1; j <= i; j++)
        {
            if(contents[j] < contents[first])
            {
                first = j ;
            }
        }

        temp = contents[first] ;
        contents[first] = contents[i] ;
        contents[i] = temp ;
    }

    std::ofstream outFile(outputFile.c_str()) ;

    for(i = 0; i < len + 1 ; i++)
    {
        outFile << contents[i] << " " ;
    }

    std::cout << "Sorted Values Stored In '" << outputFile << "'\n" ;

    return ; 
}

功能#3

int binarySearch(std::string inputFile, int value)
{
    int middle,
        num,
        array[len],
        first = 0,
        size = len + 1,
        last = size - 1,
        position = - 1 ;

    bool found = false ;

    std::ifstream file(inputFile.c_str()) ;

    while(!file.eof())
    {
        file >> num ;
        size++ ;

         middle = (first + last) / 2 ;
         if(array[middle] == value)
         {
            found = true ;
            position = middle ;
         }

         else if (array[middle] > value)
         {
            last = middle - 1 ;
         }

         else
         {
            first = middle + 1 ;
         }


    }

    if(position == value)
    {
      std::cout << inputFile + ": Target Is Found\n" ;
      found = true ;
    }

    else if (position == -1)
    {
        std::cout << inputFile + ": Target Is Not Found\n" ; 
    }

    return position ; 
}

主要

int main()
{
    int target,
        num = 0,
        count = 0;
    std::string outputFile ;

    std::ofstream file1("random.txt") ;
    for(int i = 0; i < len + 1; i++)
    {
        file1 << (i + 1) << " " ;
    }

    file1.close() ;

    std::ofstream file2("early.txt") ;
    std::ofstream file3("middle.txt") ;
    std::ofstream file4("end.txt") ;

    std::ifstream file("random.txt") ;

    file2 << num << " " ;

    while(!file.eof())
    {
        file >> num ;

        if (count == len / 2)
        {
            file3 << "0" << " " ;
        }

        file2 << num << " " ;
        file3 << num << " " ;
        file4 << num << " " ;

        count++ ;
    }

    file4 << "0" << " ";

    file.close() ;

    std::cout << "1. Reading Value From File\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << " Testing the files have been read by testing if\n" ;
    std::cout << " the listed files have '0'\n\n" ;

    simpleSearch("random.txt", 0 ) ;
    simpleSearch("early.txt", 0 ) ;
    simpleSearch("middle.txt", 0) ;
    simpleSearch("end.txt", 0) ;

    std::cout << "\n2. Simple Search\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << "Enter a Target Number to Find: \n" ;
    std::cin >> target ;

    simpleSearch("random.txt", target ) ;
    simpleSearch("early.txt", target) ;
    simpleSearch("middle.txt", target) ;
    simpleSearch("end.txt", target) ;

    std::cout << "\n3. Sorting\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << "Enter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("random.txt", outputFile) ;

    std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("early.txt", outputFile) ;

    std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("middle.txt", outputFile) ;

    std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("end.txt", outputFile) ;

    std::cout << "\n4. Binary Search\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << " Testing the files with binary search\n" ;

    std::cout << "Enter a Target Number to Find: \n" ;
    std::cin >> target ;

    binarySearch("random.txt", target) ;
    binarySearch("early.txt", target) ;
    binarySearch("middle.txt", target) ;
    binarySearch("end.txt", target) ;

    return 0 ;
}

感觉就像我是如此接近...那么以前我做错的任何事情。谢谢。

0 个答案:

没有答案