2013-04-24 4 views
-4

프로그램이 if 문을 입력하지 않습니다. 예를 들어 문장 1이 oguzhan이고 문장 2가 첫 번째 문자로 bugrahan 인 경우 첫 번째 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

+8

코드가 너무 많습니다. 너무 적은 시간. – mtahmed

+0

어떻게 작동하나요? 그것은 유효한 C++인가? int cost [m + 1] [n + 1]; 어떤 컴파일러를 gpp로 사용하고 있습니까? – Kupto

답변

7

같은 조건 : sentence2[j-1]!='a'||sentence2[j-1]!='u'은 항상 사실이다 - 단 하나의 문자가 au 모두 동일 할 수 없다, 그래서 이들 중 하나에 해당되어야합니다.

!=을 사용하는 경우 ||이 아니라 거의 항상 &&으로 연결해야합니다. 그렇지 않으면 입력에 관계없이 항상 결과가 true가됩니다.

+3

자세한 내용은 De Morgan 's law ... http : // ko .wikipedia.org/wiki/De_Morgan % 27s_laws – Kupto

관련 문제