输入错误的If语句
Entering into the wrong If statement
程序没有输入它应该输入的if语句。例如,当句子1是oguzhan而句子2是bugrahan的第一个字符时,它应该进入第一个if语句,结束替换应该是4,但它没有。
#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;
}
这样的条件:sentence2[j-1]!='a'||sentence2[j-1]!='u'
总是为真——没有一个字符可以同时等于a
和u
,因此其中一个必须为真。
如果您使用!=
,它必须几乎总是由&&
连接,而不是||
,否则结果将始终为真,无论输入。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++