函数“f_vowels”有效;它打印出两个字符串的共同元音数量。另一个“two_c”应该也能工作;如果两个字符串在同一位置共享至少两个公共(public)字母,则为真。我的问题是第三个,因为它应该打印那些字母但总是打印消息“字符串没有 2 个常用字母”。我想我知道原因了:我用 FOR 循环增加了“dim”,但 boolean 函数仍在使用原来的“dim”。我该如何解决这个问题?
#include <iostream>
#include <string.h>
using namespace std;
// functions' calls
void f_vowels(string, string);
bool two_c(int);
void print_2c(string, string, int, int);
// main
int main(){
string s1, s2;
int i, j, vowels;
int l1, l2;
char vow, c1, c2;
cout << "Enter your string: ";
getline(cin, s1);
cout << "\n";
cout << "Enter your string: ";
getline(cin, s2);
cout << "\n";
f_vowels(s1, s2);
int dim=0;
char common[dim];
for (int i=0; i<l1 && i<l2; i++){
if (s1[i]==s2[i])
{
common[dim++];
}
}
two_c(dim);
if (two_c(dim)==true){
print_2c(s1, s2, l1, l2);
}
else cout << "Strings don't have 2 common letters.";
}
//functions
//common vowels
void f_vowels(string s1, string s2){
int l1=s1.length();
char c1[l1];
s1.copy(c1, l1);
int l2=s2.length();
char c2[l2];
s2.copy(c2, l2);
int vowels=0;
char vow[]={'a','e','i','o','u','A','E','I','O','U'};
for (int i=0; i<10; i++){
for (int j=0; j<l1 && j<l2; j++){
if (c1[j]=vow[i] && c2[j]==vow[i]){
vowels++;
}
}
}
cout << vowels << "\n\n";
}
// true if strings have at least 2 common letters in the same position
bool two_c(int dim){
if (dim<2){
return false;
}
else return true;
}
// print boolean function
void print_2c(string s1, string s2, int l1, int l2){
int dim=0;
char common[dim];
for (int i=0; i<l1 && i<l2; i++){
for (int j=0; j<l1 && j<l2; j++){
if (s1[i]==s2[i])
common[j]=s1[i];
cout << common[j] << "is a common letter. \n";
}
}
}
最佳答案
这里有几个问题。
这个逻辑:
int dim=0;
char common[dim];
for (int i=0; i<l1 && i<l2; i++){
if (s1[i]==s2[i])
{
common[dim++];
}
}
...什么都不做。 common[dim++] 执行查找并丢弃值,因为它没有被分配给任何东西。看起来你正在尝试的是根据预期匹配的数量确定数组大小暗淡或至少增加它的大小。为此,您必须先计算 dim,然后再声明您是数组。所以:
int dim=0;
for (int i=0; i<l1 && i<l2; i++){
if (s1[i]==s2[i])
{
dim++;
}
}
char common[dim]; //won’t compile
虽然这种方法不会编译,因为编译器在程序运行之前无法知道 dim 的大小。如果您仍然认为数组是执行此操作的方法(考虑 std::vector),则需要使用“new”来动态分配数组。
int dim=0;
for (int i=0; i<l1 && i<l2; i++){
if (s1[i]==s2[i])
{
dim++;
}
}
char* common = new char[dim];
还有:
关于
void print_2c(string s1, string s2, int l1, int l2){
int dim=0;
char common[dim]; //a local array, not what you e setup
for (int i=0; i<l1 && i<l2; i++){
for (int j=0; j<l1 && j<l2; j++){
if (s1[i]==s2[i])
common[j]=s1[i];
cout << common[j] << "is a common letter. \n";
}
}
}
这里运行的任何逻辑都是在一个名为 common 的本地空数组上执行的,它与您在此调用之前创建的 common 不同。看来你想访问你的数组。您需要以某种形式(可能作为参数)传递 common ,然后您就可以使用您设置的内容。所以:
void print_2c(string s1, string s2, int l1, int l2, char* common, const int dim){
for (int i=0; i<l1 && i<l2; i++){
for (int j=0; j<l1 && j<l2; j++){
if (s1[i]==s2[i])
common[j]=s1[i];
cout << common[j] << "is a common letter. \n";
}
}
}
并称为:
print_2c(s1,s2,l1,l2,common,dim);
最后: 这个逻辑:
for (int i=0; i<l1 && i<l2; i++){
for (int j=0; j<l1 && j<l2; j++){
if (s1[i]==s2[i])
common[j]=s1[i];
cout << common[j] << "is a common letter. \n";
}
}
可能没有按照您的意愿行事。在内部循环中,当 s1 中的字符与 s2 匹配时,直到 min(l1,l2) 的所有共同值都设置为该字符,并且它只会重复打印出该字符的“是一个普通字母”。当所有值都被覆盖并设置为新的 char 值时,Common 将保持填充为相同 char 的所有条目,直到下一次匹配。不是你想要的。
如果 common 是一个 bool 数组并且每个索引代表一个符合条件的匹配字符(即元音),您可以这样做:
for (int i=0; i<l1 && i<l2; i++){
//no inner loop needed
if (s1[i]==s2[i])
common[s1[i]]=true;
cout << s1[i] << "is a common letter. \n";
}
}
关于c++ - INT 在 FOR 循环外保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47681446/