2013-04-24 62 views
-4

程序没有输入它假设要输入的if语句。例如,当句子1是oguzhan,并且句子2对于第一个字符很重要时,它应该输入第一个if语句结束替换应该是4但是它没有。输入错误的If语句

#include <iostream> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <cmath> 

using namespace std; 

int main() { 
    char sentence1[50]; 
    char sentence2[50]; 
    int m, n, k, l; 
    int i, j, substitution; 
    cout << "Enter the first word:" << endl; 
    cin >> sentence1; 
    cout << "Enter the second word:" << endl; 
    cin >> sentence2; 
    m = strlen(sentence1); 
    n = strlen(sentence2); 
    int cost[m + 1][n + 1]; 
    cost[0][0] = 0; 

    for (i = 1; i < m + 1; i++) { 
    cost[i][0] = cost[i - 1][0] + 2; 

    } 
    for (j = 1; j < n + 1; j++) { 
    cost[0][j] = cost[0][j - 1] + 2; 

    } 

    for (i = 1; i < m + 1; i++) { 
    for (j = 1; j < n + 1; j++) { 

     if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' || 
      sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' || 
      sentence1[i - 1] == 'o') && 
      (sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' || 
      sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' || 
      sentence2[j - 1] != 'o')) { 
     substitution = 4; 
     } 

     if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' || 
      sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' || 
      sentence1[i - 1] != 'o') && 
      (sentence2[j - 1] == 'a' || sentence1[i - 1] != 'u' || 
      sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' || 
      sentence1[i - 1] != 'o')) { 
     substitution = 4; 
     } 

     if (sentence1[i - 1] == sentence2[j - 1]) { 
     substitution = 0; 
     } 

     if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' || 
      sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' || 
      sentence1[i - 1] == 'o') && 
      (sentence2[j - 1] == 'a' || sentence2[j - 1] == 'u' || 
      sentence2[j - 1] == 'e' || sentence2[j - 1] == 'i' || 
      sentence2[j - 1] == 'o')) { 
     substitution = 3; 
     } 
     if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' || 
      sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' || 
      sentence1[i - 1] != 'o') && 
      (sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' || 
      sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' || 
      sentence2[j - 1] != 'o')) { 
     substitution = 3; 
     } 

     cost[i][j] = min(min(cost[i - 1][j] + 2, cost[i][j - 1] + 2), 
         cost[i - 1][j - 1] + substitution); 
    } 
    } 

    for (i = 0; i < m + 1; i++) { 
    for (j = 0; j < n + 1; j++) { 

     cout << cost[i][j] << " "; 
    } 
    cout << endl; 
    } 

    cout << sentence1[0]; 
    return 0; 
} 
+0

您是否在调试过程中看到两者的值?另外,如果有人输入超过49个字符? – chris 2013-04-24 14:30:24

+8

代码太多。时间太少了。 – mtahmed 2013-04-24 14:30:26

+0

这应该如何工作?它有效的C++? 'int cost [m + 1] [n + 1];'你使用gpp的编译器是什么? – Kupto 2013-04-24 14:32:18

回答

7

等的条件:sentence2[j-1]!='a'||sentence2[j-1]!='u'总是真 - 没有单个字符可以等于两个au,所以其中一个必须是真实的。

如果您使用的是!=,它几乎必须总是加上&&而不是||,否则结果将始终为真,无论输入如何。

+3

查看更多关于德摩根定律... http:// en .wikipedia.org /维基/ De_Morgan%27s_laws – Kupto 2013-04-24 14:35:38