C++中有一个for循环无错误,但无法实现,求解答??急!!!红字那段
#include<iostream>#include<string>
using namespace std;
int main()
{
void JiaMi(char ZhiHuan[]); //加密函数
void setZhiHuan(char ZhiHuan[]); //设置置换表函数
char ZhiHuanBiao['z'+1]; //正向置换表,加密时用
setZhiHuan(ZhiHuanBiao);
JiaMi(ZhiHuanBiao); //设置置换表
return 0;
}
void JiaMi(char ZhiHuan[]) //加密
{
string MingWen; //保存明文
cout<<endl<<"请输入明文:";
getline(cin,MingWen); //读入明文
cout<<MingWen<<endl;
cout<<"加密后的密文为:"<<endl;
for(int i=0; i<MingWen.length(); i++) //直接利用置换表输出密文
{
if(MingWen[i]>='a' && MingWen[i]<='z')
cout<<ZhiHuan[ MingWen[i] ];
else cout<<MingWen[i];
}
}
void setZhiHuan(char ZhiHuanBiao[])
{
int i; //i和j是循环变量,除此之外没有任何意义
char j;
string s; //密钥
cout<<"请输入密钥:";
cin>>s;
cout<<s<<endl; //输出密钥
cout<<"置换表为:"<<endl;
string s1;
for(i = 0 ; i < s.length();i++) //对密钥进行处理(去掉空格和重复的字符)
{
bool sign =0; //标志变量
if(s[i] >= 'a' && s[i] <='z')
{
for(int j = 0; j < s1.length();j++)
{
if(s[i] == s1[j])
{
sign = 1;
break;
}
}
if(sign == 0) s1 = s1 + s[i];
else sign = 0;
}
}
for( j ='a';j<='z';j++)cout<<j<<" ";cout<<endl; //输出小写字母 从a到z
char ch='a';
for( i = 'a' ;i <= 'z'; i++) //该循环利用密钥得到置换表
{
if(i < 'a' + s1.length()) //前面直接用s1代替
{
ZhiHuanBiao[i] = s1[i-'a'];
}
else //后面将剩下的"贴"上去
{
for(int j = 'a'; j < 'a' + s1.length(); j++)
{
if(ch == ZhiHuanBiao[j])
{
ch++;
j= 'a'; //每次都从头开始搜索
continue;
}
}
ZhiHuanBiao[i] = ch;
ch++;
}
cout<<ZhiHuanBiao[i]<<" "; //同时输出置换表
}
cout<<endl;
}