我正在開發一個程序,用凱撒密碼對.txt文件進行加密和解密。 (鍵盤輸入是可選的)我做了一個努力,但很難過。凱撒密碼/文件輸入不起作用
我現在所擁有的是一個基本的或我猜想已經有點精心設計的菜單結構。 我已經到了我應該從文件中調用信息的地方,它只是調用垃圾或垃圾是一種方式或另一種方式。
這是我的代碼,它運行到輸入部分,然後在輸入後結束。
其選項1選項1然後任意數字,然後它運行文件輸入,然後結束。
在此先感謝。我幾乎到處都找,並嘗試了3或4種不同的方式都無濟於事。
所以我想知道的是我應該怎麼做才能修復文件輸入和建議如何採取該數組或字符串如何建議並通過移位值更改每個字符的ascii值。
提前致謝。
#include <iostream>
#include <fstream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <dos.h>
#include <stdio.h>
#include <windows.h>
#include <cstdlib>
using namespace std;
int decryptce();
int encryptcefile();
int encryptce();
int outputce();
int encmenu();
int decmenu();
int main()
{
int t;
//main menu
cout << "-----------------------------------------------------"<<endl;
cout << "This is a Caesar Cipher, please select [1] or [2]" << endl;
cout << "-----------------------------------------------------"<<endl;
cout << "[1] Encrypt"<<endl;
cout << "[2] Decrypt"<<endl;
cout << "What is your choice:\t";
cin >> t;
cout << "\n-----------------------------------------------------"<<endl;
// menu switch statement
switch (t)
{
case 1:
{
cout << "\n running encryption:\n\n";
encmenu();
break;
}
case 2:
{
cout << "\n running decryption:\n\n";
decmenu();
break;
}
default:
{
cout<< "Thats not a 1 or 2"<<endl;
cout<< "Relaunch the program"<<endl;
system ("pause");
return 0;
}
}
return 0;
}
int encmenu()
{
int t;
cout << "-----------------------------------------------------"<<endl;
cout << "You selected: Encrypt with Caesar Cypher"<<endl;
cout << "-----------------------------------------------------"<<endl;
cout <<"Do you want to enter from a file or the keyboard?"<<endl;
cout <<"Enter [1] From file, or [2] From Keyboard"<<endl;
cout <<"Run option:\t";
cin >> t;
cout << "\n-----------------------------------------------------"<<endl;
// encrypt menu switch
switch (t)
{
case 1:
{
encryptcefile();
break;
}
case 2:
{
encryptce();
break;
}
default:
{
cout<< "Thats not a 1 or 2"<<endl;
cout<< "Relaunch the Program"<<endl;
system ("pause");
return 0;
}
}
return 0;
}
// decrypt main menu
int decmenu()
{
int h;
cout << "-----------------------------------------------------"<<endl;
cout << "You selected: Decrypt with Caesar Cypher"<<endl;
cout << "-----------------------------------------------------"<<endl;
cout << "Reading from Data.txt in main file directory:";
cout << "\n"<<endl;
cout << "Ofset is:\t";
cin >> h;
cout << "\n-----------------------------------------------------"<<endl;
return h;
}
int decryptce()
{
return 0;
}
int encryptcefile()
{
// for figuring out what to do with negatives
/*
letter = x;
letter = (letter + shift + 26) % 26;
// add 26 in case the shift is negative
letter += x; // back to ascii code
*/
char c,i;
int num=0;
int shift;
ofstream ofile;
ifstream ifile;
ifile.open("data.txt");
ofile.open("Encrypted.txt");
if(!ifile)
{
cout << "Error Opening File" << endl;
return 0;
}
if(!ofile)
{
cout << "Error Opening File" << endl;
return 0;
}
while (ifile.good())
{
c = ifile.get();
if (c=='\n')num++;
{
cout << "Is: " << num << " Long. \r";
Sleep(1);
fflush (stdin);
}
}
cout << "File is: " << num << " characters Long.\n"<<endl;
cout << "\n-----------------------------------------------------"<<endl;
cout << "What is the shift for the encryption:\t";
// ---------------------working here-------------------
cin >> shift;
const int xx = num+1;
char *arraye;
char *arrayce;
int j;
arraye = new char [xx];
arrayce = new char [xx];
arrayce[xx];
ifile.read(arrayce,xx);
for(j=0;j<xx;j++)
{
arraye[j]=arrayce[j];
cout << arrayce[j]<< endl;
}
return 0;
}
int encryptce()
{
return 0;
}
int outputce()
{
return 0;
}
請不要解析整個代碼,只有相關的部分,最好是*最低工作對不起*例子。你發佈的大部分代碼與問題完全無關,而且令人分心。 –