⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encode.cpp

📁 testing of file to be insert into the application
💻 CPP
字号:
//Name: i'll be back(Parttime)
//Assign 2- task 2b
//Program that encode text file and store into another file

#include <fstream>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <iomanip>
using namespace std;

#define TOTCHARS 33
#define TOTSSPECIALCHARS 8
#define BUFSIZE 10000
#define LINEBREAK "000"
#define MAXFILENAMESIZE 20
#define SPECIALCHARS " .,!?:;"


struct EncryptCode{
	char ch;
	int code;
	int value;
};

void generateCodes(EncryptCode *, char *);
void printCodes(EncryptCode *);
int convertChar(char ,EncryptCode *);


int main(){
	
	
	char specialChars[TOTSSPECIALCHARS]=SPECIALCHARS;	
	EncryptCode secretCode[TOTCHARS];
	char buf[BUFSIZE]="";
	char ifilename[MAXFILENAMESIZE]="";
	char ofilename[MAXFILENAMESIZE]="";
	char *p;
	int temp ;
	
	//filestreams
	ifstream infile;
	ofstream outfile;
	
	//to generate the list of secretcodes
	generateCodes(secretCode, specialChars);
	//printCodes(secretCode);
	
	//Prompt user for filename to read and encode
	while (strlen(ifilename)==0){
		system("cls");
		cout << "Please enter the File Name you would like to encode " 
			 << "\n(Note that you should include extension and file "
			 << "should be on same directory as program)" << endl;
		cout << "File Name : ";
		fflush(stdin);
		cin.getline(ifilename,MAXFILENAMESIZE,'\n');
	}	 
	
	infile.open(ifilename);
	if (!infile.good()){
		cout << "File not found. Program will exit now." << endl;	 
		exit(1);
	}
	
	
	//Prompt user for filename to save te encoded text as
	system("cls");
	cout << "File name to encode : " << ifilename << endl;
	cout << "\nPlease enter the File Name you would like to"
		 << "save the encoded file as : " << endl;
	cout << "File Name : ";
	fflush(stdin);
	cin.getline(ofilename,MAXFILENAMESIZE,'\n');
	
	
	outfile.open(ofilename);
	if (!infile.good()){
		cout << "Internal Error. Program will exit now." << endl;	 
		exit(1);
	}
	system("cls");
	cout << "File name to encode : " << ifilename << endl;
	cout << "\nEncoding " << ifilename << " in progress...Please wait" << endl;

	//Keep reading till file reaches EOF
	while(!infile.eof()){
		infile.getline(buf,BUFSIZE,'\n');
		p=buf;
		while(*p!='\0')
		{
			if(isalpha(*p))
			{ 
				//If current ch is a alphabet	
				if(isupper(*p))
				{	 
					//check for upper case
					outfile << "1";
				}
				else
				{
					//check for lower case
					outfile << "0";
				}
					
				//convert the character to lower case	 
				*p=tolower(*p);
			}
			else
			{
				//to attend to special characters
				outfile << "9";	   	   	   
			}
			
			//encode the current ch to secret code
			temp=convertChar(*p,secretCode);
			
			if(temp==-1){
				//if the character does not match any secret code
				//write the original text
				outfile << setfill('0') << setw(2) << *p << " ";
			}
			else
			{
				//current ch is encoded successfully
				outfile << setfill('0') << setw(2) << convertChar(*p,secretCode) << " ";
			}
			p++;
		}
		
		//Include a linebreak
		outfile << LINEBREAK << " ";
	}
	
	//close file streams
	outfile.close(); 
	infile.close();
	
	cout << "\nFile has been encoded successfully and save as " 
		 << ofilename << endl;
	cout << "\nProgram will exit now." << endl;
		 	 	 
	return 0;
}

//######################################################
//# To print out the list of encrypted codes for testing
//######################################################
void printCodes(EncryptCode *p){
	for(int k=0;k<TOTCHARS;k++){
		cout << p->ch << "," << p->code << "," << p->value << endl;
		//cout << p->ch << "," << p->code << endl;
		p++;	
	}	 	 	  
}

//######################################################
//# To generate the list of encrypted codes
//######################################################
void generateCodes(EncryptCode *p,char *s){
	int tempCnt=1;
	
	//a to z
	for(int n=97;n<=122;n++){	 	 
		p->ch=static_cast<char>(n);	 
		p->code=tempCnt;
		p->value=n;
		p++;
		tempCnt++;
	}
	
	tempCnt=1;
	
	//special characters
	while(*s!='\0'){
	//	  cout << "::" << *s <<endl;
		p->ch=*s;
		p->code=tempCnt;
		p->value=static_cast<int>(*s);
		tempCnt++;
		s++;
		p++;
	}
}

//######################################################
//# To convert the character ch to secret code
//######################################################

int convertChar(char ch,EncryptCode *p){
	for(int z=0;z<TOTCHARS;z++){
		if(p->ch==ch){
			return p->code;	   
		}
		p++;
	}
	return -1;	  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -