- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我为一个项目编写了这段代码,我认为它可以工作,但它只是对我的一个文件(IkeaWords.txt 文件)中的第一个术语进行比较。我哪里做错了? 所以这就是我写的,希望这已经足够了。
/*Display each IKEA product name that can be found in the English dictionary.
The full list of the 1764 unique IKEA product words is in IKEAwords.txt
To see if words exist in English, use the 40,444 word English dictionary dictionary.txt,
where the longest word has 21 characters. To lookup a word in the dictionary consider
using binary search,
Print out each word that is found.
*/
#define _CRT_SECURE_NO_WARNINGS
#define NumberOfWordsInDictionary 40437
#define MaxWordSize 21+1
#define NumberOfWordsInIkea 1764
#include <stdio.h>
#include <string.h> // for string length
#include <stdlib.h> // for exit()
// Maximum size of any word in the dictionary, + 1 for null
const char DictionaryFileName[] = "dictionary.txt"; // File name for where dictionary words are found
const char IkeaFileName[] = "IKEAwords.txt";
//--------------------------------------------------------------------------------------
// Use binary search to look up the word from the .txt file in the dictionary array,
//returning index if found, -1 otherwise
int binarySearch(const char ikeaWord[][MaxWordSize], // word to be looked up
const char dictionary[][MaxWordSize], // the dictionary of words
int numberOfDictionaryWords //number of words in the dictionary
)
{
int low, mid, high; // array indices for binary search
int searchResult = -1; // Stores index of word if search succeeded, else -1
// Binary search for word
low = 0;
high = numberOfDictionaryWords - 1;
int i = 0;
while (i < MaxWordSize)
{
while (low <= high)
{
mid = (low + high) / 2;
// searchResult negative value means word is to the left, positive value means
// word is to the right, value of 0 means word was found
searchResult = strcmp(ikeaWord[i], dictionary[mid]);
if (searchResult == 0) {
// Word IS in dictionary, so return the index where the word was found
return mid;
}
else if (searchResult < 0)
{
high = mid - 1; // word should be located prior to mid location
}
else
{
low = mid + 1; // word should be located after mid location
}
}
i++;
}
// Word was not found
return -1;
}//end binarySearch()
//--------------------------------------------------------------------------------------
// Read in the words from the dictionary file
void readWordsInFromDictionaryFile(FILE *pInputFile, char dictionary[][MaxWordSize])
{
int index = 0; // index of dictionary word being read
int maxWordLength = 0;
// Associate the actual file name with file pointer and try to open it
pInputFile = fopen(DictionaryFileName, "r");
// verify that file open worked
if (pInputFile == NULL) {
printf("Can't open %s. Verify it is in correct location\n", DictionaryFileName);
exit(-1);
}
// Keep reading words while there are any
while (fscanf(pInputFile, "%s", dictionary[index]) != EOF) {
int tempLength = (int)strlen(dictionary[index]);
if (tempLength > maxWordLength) {
maxWordLength = tempLength;
}
index++;
}
// uncomment out code test array dictionary[][]
//printf("There were %d words in the dictionary, with max length %d. \n", index, maxWordLength);
fclose(pInputFile); // close the dictionary file
printf("There were %d words read from the dictionary with max length %d.\n", index, maxWordLength);
}//end readInputFile()
void readWordsInFromIkeaFile(FILE *pInputFile2, char ikeaWord[][MaxWordSize])
{
int index2 = 0; // index of dictionary word being read
int maxIkeaWordLength = 0;
// Associate the actual file name with file pointer and try to open it
pInputFile2 = fopen(IkeaFileName, "r");
// verify that file open worked
if (pInputFile2 == NULL)
{
printf("Can't open %s. Verify it is in correct location\n", IkeaFileName);
exit(-1);
}
// Keep reading words while there are any
while (fscanf(pInputFile2, "%s", ikeaWord[index2]) != EOF)
{
int tempLength2 = (int)strlen(ikeaWord[index2]);
if (tempLength2 > maxIkeaWordLength)
{
maxIkeaWordLength = tempLength2;
}
index2++;
}
printf("There were %d words read from the Ikea file with max length %d.\n", index2,maxIkeaWordLength);
}
//--------------------------------------------------------------------------------------
int main()
{
char dictionary[NumberOfWordsInDictionary][MaxWordSize];
char ikeaWord[NumberOfWordsInIkea][MaxWordSize];
FILE *pInputFile = fopen(DictionaryFileName, "r"); // file pointer
FILE *pInputFile2 = fopen(IkeaFileName, "r");
readWordsInFromDictionaryFile(pInputFile, dictionary);
readWordsInFromIkeaFile(pInputFile2, ikeaWord); // used as input
// Find index of word in dictionary
int index = -1;
int j = 0; // counter
while(j<NumberOfWordsInIkea)
{
index = binarySearch(ikeaWord[j], dictionary, NumberOfWordsInDictionary);
// Display results
if (index != -1)
{
// word was found, so display it
printf("The word \"%s\" was found.\n", dictionary[index]);
}
j++;
}
system("pause");
return 0;
}
如果您也需要了解的话,我是在 Visual Studio 2015 中编写的。
感谢您的帮助!
最佳答案
您的代码中有几个错误和不必要的东西。我冒昧地更改了一些内容以使其正常工作(如果您按照注释中的提示进行操作,您可能已经找到它们),并更改了一些内容以使其更清晰(来自 GCC 的非编译器警告)。由于缺少 MSVS,因此未与 MSVS 进行检查。
#define _CRT_SECURE_NO_WARNINGS
// changed values to accomodate different data-files sizes
#define NumberOfWordsInDictionary 99172
#define MaxWordSize 64
#define NumberOfWordsInIkea 1393
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// from /usr/dict/words (put to lower case)
const char DictionaryFileName[] = "words.txt";
// scraped from http://lar5.com/ikea/ (put to lower case)
const char IkeaFileName[] = "ikea_names.txt";
// ripped 'const' and changed ikeaWord[][] to take a the single entry
int binarySearch(char *ikeaWord, char dictionary[][MaxWordSize],
int numberOfDictionaryWords)
{
int low, mid, high;
int searchResult = -1;
low = 0;
high = numberOfDictionaryWords - 1;
// ripped outer loop because we search for Ikea names one by one
while (low <= high) {
mid = (low + high) / 2;
searchResult = strcmp(ikeaWord, dictionary[mid]);
if (searchResult == 0) {
return mid;
} else if (searchResult < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
int readWordsInFromDictionaryFile(FILE * pInputFile,
char dictionary[][MaxWordSize])
{
int index = 0;
int maxWordLength = 0;
// ripped fopen() because that happened already in main()
// Changed from fscanf to fgets because the *scanf() family is a
// never ending source of problems, see stackoverflow et al. for endless examples
while (fgets(dictionary[index], MaxWordSize - 1, pInputFile)) {
int tempLength = (int) strlen(dictionary[index]);
// Because of the change from fscanf to fgets we need to snip the newline off
// (for "\r\n" endings snipp two)
dictionary[index][tempLength - 1] = '\0';
if (tempLength > maxWordLength) {
maxWordLength = tempLength;
}
index++;
}
// If fgets returns NULL it is either EOF or an error
if (ferror(pInputFile)) {
fprintf(stderr, "something bad happend while reading dictionary\n");
return 0;
}
fclose(pInputFile);
printf("There were %d words read from the dictionary with max length %d.\n",
index, maxWordLength);
return 1;
}
// snipped off the addition of "2" to the variable names, no need for that
int readWordsInFromIkeaFile(FILE * pInputFile, char ikeaWord[][MaxWordSize])
{
int index = 0;
int maxIkeaWordLength = 0;
while (fgets(ikeaWord[index], MaxWordSize - 1, pInputFile)) {
int tempLength = (int) strlen(ikeaWord[index]);
ikeaWord[index][tempLength - 1] = '\0';
if (tempLength > maxIkeaWordLength) {
maxIkeaWordLength = tempLength;
}
index++;
}
if (ferror(pInputFile)) {
fprintf(stderr, "something bad happend while reading ikeawords\n");
return 0;
}
printf("There were %d words read from the Ikea file with max length %d.\n",
index, maxIkeaWordLength);
return 1;
}
//--------------------------------------------------------------------------------------
int main()
{
char dictionary[NumberOfWordsInDictionary][MaxWordSize];
char ikeaWord[NumberOfWordsInIkea][MaxWordSize];
int res;
// added error-checks
FILE *pInputFile = fopen(DictionaryFileName, "r");
if (pInputFile == NULL) {
fprintf(stderr, "Can't open %s. Verify it is in correct location\n",
DictionaryFileName);
exit(EXIT_FAILURE);
}
FILE *pInputFile2 = fopen(IkeaFileName, "r");
if (pInputFile2 == NULL) {
fprintf(stderr, "Can't open %s. Verify it is in correct location\n",
IkeaFileName);
exit(EXIT_FAILURE);
}
if ((res = readWordsInFromDictionaryFile(pInputFile, dictionary)) == 0) {
fprintf(stderr, "Error in reading dictionary\n");
exit(EXIT_FAILURE);
}
if ((res = readWordsInFromIkeaFile(pInputFile2, ikeaWord)) == 0) {
fprintf(stderr, "Error in reading ikea-file\n");
exit(EXIT_FAILURE);
}
int index = -1;
int j = 0;
while (j < NumberOfWordsInIkea) {
index = binarySearch(ikeaWord[j], dictionary, NumberOfWordsInDictionary);
if (index != -1) {
printf("The word \"%s\" was found.\n", dictionary[index]);
}
j++;
}
// Seems to be useful when run in MS-Windows
#if defined _WIN32 || defined WIN32 || defined WIN64 || defined _WIN64
sytem("pause");
#endif
exit(EXIT_SUCCESS);
}
我没有打磨每个角落,还需要一些工作。例如:读取两个文件的两个函数实际上在做相同的事情,只是针对不同的文件和不同的字典。这可以通过单个函数来完成。文件的名称、文件的长度以及这些文件的条目的长度是固定的,它们可以是动态的,以便能够使用不同的输入而无需重新编译。
但一切都结束了:开始还不错!
关于c - 使用二分搜索比较两个文件的一些快速帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780898/
我有这个问题: 我们声称对 float 使用相等测试是不安全的,因为算术运算会引入舍入错误,这意味着两个应该相等的数字实际上并不相等。 对于这个程序,您应该选择一个数字 N,并编写一个程序来显示 1
为什么这个脚本的输出是 5 而不是 8 ? 我认为 -- 意味着 -1 两次。 var x = 0; var y = 10; while ( x
我现在可以从 cmd 窗口中执行的 FFmpeg 过程中读取最后一行。 使用脚本主机模型对象引用此源。 Private Sub Command1_Click() Dim oExec
使用 vlookup,当匹配发生时,我想从匹配发生的同一行显示工作表 2 中 C 列的值。我想出的公式从 C 列表 2 中获取值,但它从公式粘贴在表 3 上的行中获取,而不是从匹配发生的位置获取。 这
我在破译 WCF 跟踪文件时遇到了问题,我希望有人能帮助我确定管道中的哪个位置发生了延迟。 “Processing Message XX”的跟踪如下所示,在事件边界和传输到“Process Actio
我有四个表,USER、CONTACT、CONACT_TYPE 和 USER_CONTACT USER_CONTACT 存储用户具有填充虚拟数据的表的所有联系人如下 用户表 USER_ID(int)|
以下有什么作用? public static function find_by_sql($sql="") { global $database; $result_set = $data
我正在解决 JavaBat 问题并且对我的逻辑感到困惑。 这是任务: Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat,
我正在研究一些 Scala 代码,发现这种方法让我感到困惑。在匹配语句中,sublist@ 是什么?构造?它包含什么样的值(value)?当我打印它时,它与 tail 没有区别,但如果我用尾部替换它,
我正在使用以下代码自行缩放图像。代码很好,图像缩放也没有问题。 UIImage *originImg = img; size = newSize; if (originImg.size.width >
Instruments 无法在我的 iPad 和 iPhone 上启动。两者都已正确配置,我可以毫无问题地从 xcode 调试它们上的代码,但 Instruments 无法启动。 我听到的只是一声嘟嘟
我想用 iPhone 的 NSRegularExpression 类解析此文本: Uploaded652.81 GB 用于摘录上传和652.81文本。 最佳答案 虽然我确实认为 xml 解析器更适合解
我找到了 solution在 Stackoverflow 上,根据过滤器显示 HTML“li”元素(请参阅附件)。本质上基于 HTML 元素中定义的 css 类,它填充您可以从中选择的下拉列表。 我想
这是一个简单的问题,但我是在 SQL 2005 中形成 XML 的新手,但是用于形成如下所示表中的 XML 的最佳 FOR XML SQL 语句是什么? Column1 Column2 -
我在 www.enigmafest.com 有一个网站!您可以尝试打开它!我面临的问题是,在预加载器完成后,主页会出现,但其他菜单仍然需要很长时间才能加载,而且声音也至少需要 5 分钟! :( 我怎样
好吧,我正在尝试用 Haskell 来理解 IO,我想我应该编写一个处理网页的简短小应用程序来完成它。我被绊倒的代码片段是(向 bobince 表示歉意,但公平地说,我并不想在这里解析 HTML,只是
如何使用背景页面来突出显示网站上的某个关键字,无论网站是什么(谷歌浏览器扩展)?没有弹出窗口或任何东西,它只是在某人正在查看的网站上编辑关键字。我以前见过这样的,就是不明白怎么做!谢谢你的帮助。 最佳
我是 Javascript 新手,需要一些帮助。 先看图片: . 积分预测器应用程序。 基本上当用户通过单选按钮选择获胜团队时它应该在积分栏中为获胜队添加 10 分,并且并根据得分高的球队自动对表格进
这是我的情况 - 我要发送一份时事通讯,我试图做的是,当用户单击电子邮件中的链接时,它会重定向到我的网页,然后会弹出一个灯箱,显示视频。我无法在页面加载时触发灯箱,因为您可以在查看灯箱之前转到同一页面
我有这个代码。 ¿Cuanto es ? Ir 我想获取用户输入的“验证码”值。我尝试这个但行不通。有什么帮助吗? var campo = d
我是一名优秀的程序员,十分优秀!