2012-02-17 100 views
2
int main (int argc, const char * argv[]) { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSDictionary * ProbablyInaccurateFinnishtoEnglshtranslation= [NSDictionary dictionaryWithObjectsAndKeys: @"postilaatikko", @"mailbox", @"oppikirja", @"textbook", @"näppäimistö", @"keyboard", @"piano", @"piano", @"laskukone", @"calculator", @"manteli", @"almond", @"lumi", @"snow", @"vuori", @"mountain", @"aika", @"time", @"kynttilä", @"candle", nil]; 
    NSString * inputSentence; 
    char cstring[451]; 
    NSArray * sentenceIntoWords; 
    NSMutableString * translatedSentence; 
    int i = 0; 

    NSLog(@"Enter a sentence: \n"); 
    //scanf("%s", &cstring); 
    gets (cstring); 

    inputSentence = [NSString stringWithCString: cstring encoding: NSASCIIStringEncoding]; 

    sentenceIntoWords = [inputSentence componentsSeparatedByString: @" "]; 

    for(i=0; i<[sentenceIntoWords count]; i++) 
    { 
     if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i] == nil]) 
     { 
      translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]]; 

     } 
     else { 
      translatedSentence= [ProbablyInaccurateFinnishtoEnglshtranslation objectForKey: [sentenceIntoWords objectAtIndex: i]]; 
     } 
    } 


    NSLog(@"%@", translatedSentence); 
    [pool drain]; 
    return 0; 
} 

我想要做的是将用户输入的句子的每个单词与NSArray进行比较,匹配时用翻译的单词替换关键字。好的,我已将它更改为: if([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]]) { translatedSentence = [ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]];传递参数1使得整型指针没有转换

} 
    else { 
     translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]]; 
      } 

现在只有输入句子的最后一个字出现了,我的逻辑中是否有错误?

回答

2

支架位于错误的地方。你想这样的:

if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]] == nil) 

你现在有什么是大致相同的:

BOOL isNil = [sentenceIntoWords objectAtIndex:i]] == nil; 
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:isNil) 

显然这是错误的,因为objectForKey是(最有可能)预期的对象的指针,而不是一个布尔值。

0

您不能使用objectForKey:的原始类型。

if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i] == nil]) 

[sentenceIntoWords objectAtIndex:i] == nil行会给你布尔输出

所以这是错误的if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:boolean_value(yes/NO))

你应该做的就像是在我的计划显示这个

if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]] == nil) 
+0

如果objectForKey预期的指针,如何想,如果([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:boolean_value(是/否)),有意义吗? – user1216983 2012-02-17 19:55:43

+0

您不应该在objectForKey函数中使用原始值。 – 2012-02-17 20:08:46

+0

啊现在有道理,我只是放错了支架。我做了你所说的,现在输入句子的最后一个字是输出。 – user1216983 2012-02-17 20:15:13

-1

类似的错误,iepassing 'strlen'的参数1使得整型指针没有转换

//编程读取fasta序列并随机化它!

#include<stdio.h> 
#include<stdlib.h> 
#include<ctype.h> 
#include<string.h> 

int main(int ac,char *av[]) 
{ 
    int i; 
    int l1; 
    FILE *file1; 

    if (ac < 2) return 0; 
    file1= fopen(av[1] , "r"); //"r" is a string and 'r' is a character 
    char x; 
    while(((x=fgetc(file1))!= EOF) && (x!='\n')); //skip the header part 
    while(((x=fgetc(file1))!= EOF) && (x!='>')) 
    { 
     if(isalpha(x))        //reading all the alphabets after this(>) sign 

     { 
     printf("%c",x); 

     } 
    } 

//To calculate the length of sequence 

    l1=strlen(x); 
    printf ("The sequence is %d characters long.\n",l1); 

    //to Randomize all the characters present in a string(FASTA sequence) 
    srand(time(NULL)); 

    for (i=l1-1;i>0;i--) 
    { 
    char j = rand() % (i+1); 
    char temp = j; 
    j = x; 
    x = temp; 

    } 
    fclose(file1); 
    return 0; 
} 
+0

错误在strlen函数中 – vShivang 2014-05-31 11:43:44

相关问题