我正在创建一个代码...
问题我正在面对...
我有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 ;
}
感觉就像我是如此接近...那么以前我做错的任何事情。谢谢。