输入错误的If语句

时间:2013-04-24 14:28:24

标签: c++ if-statement

程序没有输入它想要输入的if语句。例如,当sentence1是oguzhan并且sentence2是第一个字符的bugrahan时,它应该输入第一个if语句结束替换应该是4,但它不是。< / p>

#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;
}

1 个答案:

答案 0 :(得分:7)

条件如:sentence2[j-1]!='a'||sentence2[j-1]!='u'始终为真 - 任何单个字符都不能等同于au,因此其中一个必须为真。

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