- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了以下函数来读取文件并将“记录”加载到 visual studio 2015 中的 BST 中。它按照我在 Windows 中的预期工作,但是当我在 mac 上运行该函数时,它会导致无限循环。我不知道为什么。有人可以阐明一下吗?已更新完整代码,问题在于以下功能:
BinarySearchTree readFile()
{
ifstream myfile;
myfile.open("character.txt");
string name;
string aString = "Empty";
BinarySearchTree loadTree;
List list1;
int index = 1;
bool finishRecord = false;
if (!myfile.peek() == myfile.eof())
{
while (!myfile.eof())
{
getline(myfile, name);
while (!finishRecord)
{
getline(myfile, aString);
if (aString == "/")
{
finishRecord = true;
}
else
{
list1.insert(index, aString);
index += 1;
}
}
KeyedItem an_item(name, list1, true);
loadTree.searchTreeInsert(an_item);
//reset variables used for each record
index = 1;
list1.removeAll();
finishRecord = false;
}
}
myfile.close();
return loadTree;
} // end readFile
完整代码:
#include <iostream> //needed for input and output and the console screen
#include <fstream> //needed to read/write a file
#include <iomanip>
#include <algorithm> //needed to use the transform function
#include "ListP.h" //linked list class
#include "BST.h" // binary tree operations
using namespace std;
string suspectsArray[256]; //array of the suspects
int suspectIndex = 0;
KeyedItem ADD();
BinarySearchTree readFile();
void QUIT(BinarySearchTree& passedTree, string addedItems[], int arrayIndex);
bool withinTree(BinarySearchTree& passedTree, string name);
string INQUIRY(BinarySearchTree& passedTree);
void suspects(TreeItemType& anItem);
void findTip(BinarySearchTree& passedTree, string tip, int &noSuspects);
int main()
{
string aString;
BinarySearchTree characterTree = readFile();
KeyedItem an_Item;
string addedItems[256] = {};
int index = 0;
string answer;
string inquiryReturn = "";
bool main = false;
bool inTree = false;
while (!main)
{
cout << "WE ARE AT MAIN LOOP. YOU CAN ADD, INQUIRY, OR QUIT";
cout << endl;
if (inquiryReturn == "")
{
cin >> answer;
cin.ignore(); //need to flush the input for the getline()
}
else
{
inquiryReturn = "";
}
//since we need to accept the input regardless of it case
//so going to make the input into lower case
transform(answer.begin(), answer.end(), answer.begin(), tolower);
//determine which operation the user called
if (answer == "add") //user wishes to ADD a character
{
cout << endl;
an_Item = ADD();
//need to determine if the record for the character
//is already in the tree or not
inTree = withinTree(characterTree, an_Item.getKey());
if (!inTree)
{
addedItems[index] = an_Item.getKey();
index += 1;
characterTree.searchTreeInsert(an_Item);
}
else
{
cout << "Character already in tree." << endl;
}
inTree = false;
}
else if (answer == "inquiry") //user wishes to do an INQUIRY
{
cout << "User wants to do an INQUIRY" << endl;
characterTree.postorderTraverse(suspects);
inquiryReturn = INQUIRY(characterTree);
answer = inquiryReturn;
suspectIndex = 0;
}
else if (answer == "quit") //user wishes to QUIT the program
{
if (index > 0) //if the user added a character, save
{
cout << "OK, saving database to file "
"\"character.txt\" ";
QUIT(characterTree, addedItems, index);
cout << "... Done. Goodbye." << endl;
main = true;
}
else
{
cout << "OK, saving database to file "
"\"character.txt\" ";
cout << "... Done. Goodbye." << endl;
main = true;
}
}
else
{
cout << "You didn't enter a valid command." << endl;
}
}
return 0;
} //end main
void suspects(TreeItemType& anItem)
{
suspectsArray[suspectIndex] = anItem.getKey();
suspectIndex++;
}
//Function to get a record to add to the binary search tree
//gets the record in the type KeyedItem
KeyedItem ADD()
{
string name;
string a_string;
List list1;
int index = 1;
bool end = false;
cout << "OK, we are now adding a character to the database."
<< endl;
cout << "Name of Character:" << setw(5) << " ";
getline(cin, name);
cout << "Attributes:" << setw(12) << " ";
//loop to get attributes if any
while (!end)
{
getline(cin, a_string);
if (a_string == "")
{
//if no attributes were added, need to prompt for one
if (list1.getLength() == 0)
{
cout << "Need to enter at least one attribute." <<
endl << setw(23) << " ";
}
else
{
cout << endl;
end = true;
}
}
else
{
list1.insert(index, a_string);
index += 1;
cout << setw(23) << " ";
}
}
KeyedItem an_item1(name, list1, true);
return an_item1;
} // end ADD
//Function to read the database file and
//load all of the records into a Binary Search Tree.
BinarySearchTree readFile()
{
ifstream myfile;
myfile.open("character.txt");
string name;
string aString = "Empty";
BinarySearchTree loadTree;
List list1;
int index = 1;
bool finishRecord = false;
if (!myfile.peek() == myfile.eof())
{
while (!myfile.eof())
{
getline(myfile, name);
while (!finishRecord)
{
getline(myfile, aString);
if (aString == "/")
{
finishRecord = true;
}
else
{
list1.insert(index, aString);
index += 1;
}
}
KeyedItem an_item(name, list1, true);
loadTree.searchTreeInsert(an_item);
//reset variables used for each record
index = 1;
list1.removeAll();
finishRecord = false;
}
}
myfile.close();
return loadTree;
} // end readFile
//Function to run if additional shady characters were manually added to the Binary Search Tree
//It should the added characters to the database text file.
void QUIT(BinarySearchTree& passedTree, string addedItems[], int arrayIndex)
{
ofstream outfile; //variable that will be assigned to the file
KeyedItem an_item; //record that needs to be added to file
string aString;
outfile.open("character.txt", ios::app);
if (outfile.is_open())
{
for (int i = 0; i < arrayIndex; i++)
{
passedTree.searchTreeRetrieve(addedItems[i], an_item);
outfile << an_item.getKey() << "\n";
int jkl = an_item.attributes.getLength();
for (int bnm = 1; bnm < jkl + 1; bnm++)
{
an_item.attributes.retrieve(bnm, aString);
outfile << aString << "\n";
}
outfile << "/\n";
}
outfile.close();
}
} // end QUIT
//function to check whether or not the character is already in the tree
bool withinTree(BinarySearchTree& passedTree, string name)
{
KeyedItem item;
try
{
passedTree.searchTreeRetrieve(name, item);
}
catch (TreeException& error)
{
//the character is not within the tree
return false;
}
//the character is within the tree
return true;
} // end withinTree
//Function to if the tip matches any of the attributes of the suspects
void findTip(BinarySearchTree& passedTree, string tip,int &noSuspects)
{
KeyedItem aItem;
bool tipFound = false;
string aString;
//look at each of the characters attributes
//to see if they are a match for the tip
for (int asd = 0; asd < suspectIndex; asd++)
{
tipFound = false;
if (suspectsArray[asd] != "")
{
passedTree.searchTreeRetrieve(suspectsArray[asd], aItem);
//goes through each of the attributes of a character
for (int iop = 1; iop < aItem.attributes.getLength() + 1; iop++)
{
aItem.attributes.retrieve(iop, aString);
transform(aString.begin(), aString.end(), aString.begin(), toupper);
if (aString == tip)
{
tipFound = true;
}
}
//if none of character's attribute match
//remove them from the suspectArray
if (tipFound == false)
{
suspectsArray[asd] = "";
noSuspects -= 1;
}
}
}
} // end findTIP
//Function to find the perpetrator, if any
string INQUIRY(BinarySearchTree& passedTree)
{
string codeName; //string variable for inquiry code name
string command; //string variable for command input
string aString;
string checkSuspect;
int noSuspects = suspectIndex;
bool tipFound = false;
cout << "OK, we are now conducting an inquiry." << endl;
cout << endl << "Enter a Code Name for this Inquiry:";
cout << setw(5) << " ";
getline(cin, codeName);
while (command != "ADD" && command != "QUIT" && command != "INQUIRY")
{
cout << "What would you like to do?" << endl;
getline(cin, command);
transform(command.begin(), command.end(), command.begin(), toupper);
if (command == "TIP")
{
cout << "Enter Tip Info:" << setw(25) << " ";
getline(cin, command);
transform(command.begin(), command.end(), command.begin(), toupper);
findTip(passedTree, command, noSuspects);
//if there's only 1 suspect left, alert the user
if (noSuspects == 1)
{
cout << "ALERT! That leaves only one \n"
<< "suspect in the " << codeName << " inquiry:"
<< setw(13) << " ";
for (int k = 0; k < suspectIndex; k++)
{
if (suspectsArray[k] != "")
{
cout << suspectsArray[k] << endl;
}
}
}
else if (noSuspects == 0) //all suspects have been eliminated
{
cout << "There no suspects that are match." << endl;
}
}
else if (command == "CHECK")
{
bool found = false;
cout << "Enter Name of character:" << setw(16) << " ";
getline(cin, checkSuspect);
transform(checkSuspect.begin(), checkSuspect.end(),
checkSuspect.begin(), toupper);
for (int p = 0; p < suspectIndex; p++)
{
aString = suspectsArray[p];
transform(aString.begin(), aString.end(), aString.begin(), toupper);
if (aString == checkSuspect)
{
cout << aString << " is a suspect." << endl;
found = true;
}
}
if (!found)
{
cout << checkSuspect << " is not a suspect." << endl;
}
}
else if (command == "PRINT")
{
cout << "Current Suspects are:" << endl;
for (int k = 0; k < suspectIndex; k++)
{
if (suspectsArray[k] != "")
{
cout << suspectsArray[k] << endl;
}
}
}
else if (command == "ADD" || command == "QUIT" || command == "INQUIRY")
{
return command;
}
}
} // end INQUIRY
最佳答案
我的猜测是您的程序没有在输入流中预期的位置找到“/”,因此永远不会设置 finishRecord = true;
。您可能应该在内部循环和外部循环中检查 eof。
关于c++ - 为什么这会导致 Mac 上的无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181266/
更新到 Xcode 12.2 后,由于与 Apple Silicon 相关的链接错误,我的项目开始无法编译。我似乎已经修复了大部分问题,但是一个构建静态链接框架的子项目给我带来了问题。然而,具有明显相
我有一台旧的 MacBook Pro,我在其中制作了两个应用程序并提交到应用程序商店。所以基本上签名身份在该机器的钥匙串(keychain)中。在 Mavericks 升级后,我不得不从那台计算机转移
我正在 MAC OSX 10.6 上编写一个示例应用程序,其 gcc 版本为 4.2。我正在使用 gcc 4.2 版编译应用程序。它在同一台机器上工作正常,但在 MAC OSX 10.5 (gcc 4
这是我的简单 mac 地址生成器: private String randomMACAddress(){ Random rand = new Random(); byte[] macA
我一直在寻找一种将十进制 MAC 地址转换为十六进制地址的方法。 例如 170.187.204.0.17.34至AA:BB:CC:00:11:22 . 致Convert HEX to Decimal
我想使用 UISceneSession 的委托(delegate)方法当用户将注意力从应用程序(窗口)移开,然后又回到应用程序(窗口)时,生命周期有助于通知我的 Mac Catalyst 应用程序。
我在签署 Mac 应用程序安装程序时遇到问题,我计划在 Mac 应用商店之外分发该应用程序。我正在使用开发人员安装程序证书来签署应用程序,但它给出了一些错误。下面是我用来签署应用程序的命令。 prod
Mac Catalyst 允许调整窗口大小,有没有办法为 Mac Catalyst 应用程序提供最小窗口大小? 最佳答案 只需将以下代码块添加到您的 application:didFinishLaun
这是一个非常理论性的问题,但对我来说很安静,即我如何进行下一步。 我正在开发一个SwiftUI MacOS应用程序,用户可以在其中上传自己的文件。元数据将存储在CoreData中,而我将文件手动存储在
滑动删除在 maccatalyst 中不起作用。相同的代码在 iPad 上运行良好。 在 maccatalyst 中未调用 UITableview trailingSwipeActionsConfig
我有两台 Mac,在进行 iPad 开发时,如果可以让另一台 Mac 启动模拟器并在构建完成后加载应用程序,我很感兴趣。 如果 iPad 应用程序在一台 Mac 屏幕和 Xcode 的模拟器中运行,所
我有一个用 objective-c 开发的 mac 应用程序。cpp 中还有另一个命令行中间应用程序,它是 native 主机应用程序,用于接收来自 chrome 扩展的消息。每当中间应用程序从扩展程
是否可以使用来自 Comodo 或 Thawte 的代码签名证书来签署应用程序并通过 Gatekeeper,或者我需要为此目的拥有 Mac 开发者订阅? 最佳答案 您必须是 Mac Developer
我正在使用 C++ 和 OpenGL/SDL 编写一个游戏,使用 Visual Studio 作为我的 IDE。我没有 Mac,甚至对这个平台都不熟悉。但我还是想发布给 Mac 用户。 我有三个问题。
我想将 MAC 地址 00163e2fbab7(存储为字符串)转换为其字符串表示形式 00:16:3e:2f:ba:b7。最简单的方法是什么? 最佳答案 使用一种完全迂回的方法来利用现有的一次将两个十
无法连接到Mac上的MySQL工作台。我收到以下错误:无法连接,服务器可能未运行。无法连接到‘127.0.0.1’上的MySQL服务器(61)如有帮助,将不胜感激。。谢谢!
我已经搜索了很长时间,似乎无法找到这个问题的答案。在 SO 上只找到两个问题/答案,但他们仍然没有回答这个问题 ( https://stackoverflow.com/search?q=netcore
我们在 Docker for Mac 中有一个 LoadBalancer 真是太酷了。 我对创建的端口有疑问: apiVersion: v1 kind: Service metadata: nam
我有一个我一直在从事的小型开源 OSX 项目,我想在 App Store 之外分发。 随着即将发布的 Mountain Lion,我想提供一个证书,以减少安装过程中的痛苦。 使用 App Store,
我的一台 Mac 没有互联网连接。我需要使用 docker pull。我的想法是,我将在我的一台具有互联网连接的 Mac 中使用 docker pull,然后将其复制到我没有互联网连接的 Mac。如何
我是一名优秀的程序员,十分优秀!