【发布时间】:2021-03-02 19:53:39
【问题描述】:
我正在处理有关“CaesarCipher”的任务。这项任务要求我: 编写一个程序,要求用户输入文件名。然后,您的程序应要求用户输入“加密”或“解密”。 如果用户输入任何其他内容,您的程序应打印错误消息并退出。 如果他们输入“加密”,您的程序应该打开文件并通过将每个字符(包括空格)向右移动 3 个值来加密它。 如果他们输入“解密”,您的程序应该打开文件并通过将每个字符(包括空格)向左移动 3 个值来解密它。
如果文件无法打开你的程序应该打印错误信息并退出。
解密或加密的消息应显示在屏幕上,并写入名为“消息”的输出文件。 还要报告输入消息中元音(A、E、I、O、U、Y)的频率。
我想我已经完成了一半,但是我无法将处理后的“c”值打印到屏幕上,每次我尝试运行它时,我都会跳过我想要的编译文本的中间部分并打印。
int main(){
//Data Abstraction
bool goodFile = true;
bool goodCommand = true;
string fileName;
string command;
string message;
ifstream inputFile;
ofstream outputFile;
int shift = 3;
int numA = 0, numE = 0, numI = 0, numO = 0, numU = 0;
char c;
// Input
cout << "Enter File Name: ";
cin >> fileName;
cout << fileName << endl;
cout << "Enter encrypt or decrypt: ";
cin >> command;
cout << command << endl;
// Open File
inputFile.open(fileName);
outputFile.open(message);
// Identify whether output Error message
if(!inputFile){
goodFile = false;
}
if(command != "encrypt" && command != "decrypt"){
goodCommand = false;
}
if (goodCommand != true){
cout << "Error: Bad Command." << endl;
}
if(goodFile != true){
cout << "Error: File did NOT open." << endl;
}
// Identify process data or not
if (goodFile == true && goodCommand == true){
if(command == "decrypt"){
shift = -3;
}
while(inputFile.get(c)){
c+= shift;
c = static_cast<char>(c+shift);
cout << c;
switch(toupper(c)){
case 'a':
break;
case 'e':
break;
case 'i':
break;
case 'o':
break;
case 'u':
break;
}
if(c == 'a'){
numA++;
}
if(c == 'e'){
numE++;
}
if(c == 'i'){
numI++;
}
if(c == 'o'){
numO++;
}
if(c == 'u'){
numU++;
}
}
outputFile << c << endl;
outputFile << "Letter Frequency" << endl;
outputFile << " A "<< numA << endl;
outputFile << " E "<< numE << endl;
outputFile << " I "<< numI << endl;
outputFile << " O "<< numO << endl;
outputFile << " U "<< numU << endl;
inputFile.close();
outputFile.close();
cout << "Letter Frequency" << endl;
cout << " A "<< numA << endl;
cout << " E "<< numE << endl;
cout << " I "<< numI << endl;
cout << " O "<< numO << endl;
cout << " U "<< numU << endl;
}
return 0;
}
【问题讨论】:
-
您缺少一些头文件,否则将无法编译。
-
这个:
c+= shift; c = static_cast<char>(c+shift);将移动两次。此外,您的开关什么也不做。 -
这是我所知道的唯一可以忽略“aeiou”大小写的方法。如果这不起作用,那我该怎么办? @VladFeinstein
-
@ZhizhongLiu (1) 这不会忽略任何东西,它实际上什么都不做。 (2) 为什么要忽略
"aeiou"? -
我需要忽略“aeiou”这五个字母的大小写,因为我需要统计这五个字母(大写和小写)在TXT文本中出现的次数并输出。@VladFeinstein
标签: c++