C++ 如何将已编译文件从TXT文本输出到屏幕?
我正在做一个关于“凯撒密码”的作业。此任务要求我: 编写一个程序,要求用户输入文件名。然后,您的程序应要求用户输入“加密”或“解密”。 如果用户输入任何其他内容,您的程序应打印错误消息并退出。 如果他们输入“encrypt”,您的程序应该打开文件并通过将每个字符(包括空格)的3个值向右移动来加密它。 如果他们输入“decrypt”,你的程序应该打开文件,并通过将每个字符(包括空格)的3个值向左移动来解密 如果文件无法打开,您的程序应打印一条错误消息并退出 解密或加密的消息应显示在屏幕上,并写入名为“消息”的输出文件。 还要报告输入信息中元音(A、E、I、O、U、Y)的频率 我想我已经完成了一半,但是我没有办法将处理后的“c”值打印到屏幕上,每次我尝试运行它时,我都会跳过我想要打印的编译文本的中间部分C++ 如何将已编译文件从TXT文本输出到屏幕?,c++,C++,我正在做一个关于“凯撒密码”的作业。此任务要求我: 编写一个程序,要求用户输入文件名。然后,您的程序应要求用户输入“加密”或“解密”。 如果用户输入任何其他内容,您的程序应打印错误消息并退出。 如果他们输入“encrypt”,您的程序应该打开文件并通过将每个字符(包括空格)的3个值向右移动来加密它。 如果他们输入“decrypt”,你的程序应该打开文件,并通过将每个字符(包括空格)的3个值向左移动来解密 如果文件无法打开,您的程序应打印一条错误消息并退出 解密或加密的消息应显示在屏幕上,并写入名
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;
}
intmain(){
//数据抽象
bool goodFile=true;
bool goodCommand=true;
字符串文件名;
字符串命令;
字符串消息;
ifstream输入文件;
流输出文件;
int-shift=3;
int numA=0,numE=0,numI=0,numO=0,numU=0;
字符c;
//输入
cout>文件名;
cout我将此作为一个答案发布,以使代码可读
toupper(c)
将字符c
转换为大写;它永远不会等于'a'
,'e'
等。请使用'a'
您需要检查输入
中的元音,因此在编码/解码之前进行测试。并在适当的情况下增加计数器
:
while (inputFile.get(c)) {
switch (toupper(c)) {
case 'A':
numA++;
break;
case 'E':
numE++;
break;
case 'I':
numI++;
break;
case 'O':
numO++;
break;
case 'U':
numU++;
break;
}
c += shift;
cout << c;
// ...
}
while(inputFile.get(c)){
开关(顶部(c)){
案例“A”:
numA++;
打破
案例“E”:
numE++;
打破
案例“I”:
numI++;
打破
案例“O”:
numO++;
打破
案例“U”:
numU++;
打破
}
c+=移位;
你不能缺少一些头文件,否则这将无法编译。这个:c+=shift;c=static_cast(c+shift);
将移动两次。而且,你的开关什么也不做。这是我所知道的忽略“aeiou”情况的唯一方法。如果这不起作用,我该怎么办@VladFeinstein@ZhizhongLiu (1)这并没有忽略任何东西,它实际上什么也不做。(2)为什么要忽略“aeiou”
?我需要忽略五个字母“aeiou”的大小写,因为我需要计算TXT文本中这五个字母(大写和小写)的出现次数并输出它们。@VladFeinsteinThank you。它确实有效。