我实际上正在复制来自Stephen G. Kochan的C编程中的代码,但我冒昧地做了一些改动。现在输出不是我的预期。我花了最后12个小时来处理相同代码的变化。如果有人能提供帮助,我会很感激。
var badParameters:Bool = true
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if badParameters {
// your code here, like badParameters = false, e.t.c
return false
}
return true
}
当字典中存在输入时,我希望结果等于1。但是我得到-1:
示例输出:
#include <stdio.h>
struct entry{
char word[15];
char definition[50];
};
int compareStrings(char s1[], char s2[]){
//this seems to have successfully compare strings
int i = 0;
int answer;
while(s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0'){
i++;
printf("%i\n", i);
}
if(s1[i] > s2[i]){
answer = 1;
}else if(s1[i] < s2[i]){
answer = -1;
}else{
answer = 0;
}
return answer;
}
int lookup(struct entry dictionary[], char c[], int entries){
//
int mid, high, low, result;
high = entries -1;
low = 0;
int compareStrings(char s1[], char s2[]);
while(low <= high){
mid = (high + low) / 2;
result = compareStrings(dictionary[mid].word, c);
printf("mid ==> %i and result ==> %i\n", mid, result);
if(result = 1){
high = mid -1;
}else if(result = -1){
low = mid + 1;
}else{
return mid;
}
}
return -1;
}
int main(void){
struct entry dictionary[100] =
{ { "aardvark", "a burrowing African mammal" },
{ "abyss", "a bottomless pit" },
{ "acumen", "mentally sharp; keen" },
{ "addle", "to become confused" },
{ "aerie", "a high nest" },
{ "affix", "to append; attach" },
{ "agar", "a jelly made from seaweed" },
{ "ahoy", "a nautical call of greeting" },
{ "aigrette", "an ornamental cluster of feathers" },
{ "ajar", "partially opened" } };
char input[10];
int result;
int entries = 10;
int lookup(struct entry dictionary[], char c[], int entries);
printf("Please provide input for test\n");
scanf("%s", input);
result = lookup(dictionary, input, entries);
printf("result %i\n", result);
if(result != -1){
printf("%s\n", dictionary[result].word);
}else{
printf("Sorry, %s is not a word\n", input);
}
return 0;
}