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

📄 decode.cpp

📁 testing of file to be insert into the application
💻 CPP
字号:
//Name: i'll be back(Parttime)
//Assign 2- Task 2b
//Program that decode/encoded text file

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

#define TOTCHARS 33
#define TOTSSPECIALCHARS 7
#define TOTSALPHACHARS 26
#define BUFSIZE 10000
#define LINEBREAK "000"
#define MAXFILENAMESIZE 20


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

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


int main(){
	
	char specialChars[TOTSSPECIALCHARS+1]=" .,!?:;";	
	EncryptCode secretCode[TOTCHARS];
	char buf[BUFSIZE]="";
	
	char ifilename[MAXFILENAMESIZE]="";
	char *p;
	char flag=' ';
	char code[4]="";
	char tmpcode[3]="";
	int codevalue=0;
	char charvalue=' ';
	
	
	//to generate the list of secretcodes
	generateCodes(secretCode, specialChars);
	//printCodes(secretCode);
	
	ifstream infile;
	//Prompt user for filename to read and encode
	while (strlen(ifilename)==0){
		system("cls");
		cout << "Please enter the File Name you would like to decode " 
			 << "\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);
	}
	
	//Decode the filename
	cout << "\n\nDecoding " << ifilename << " in progress..";  
	cout << "\n\nDecoded Text : \n\n" << endl;
	while(!infile.eof())
	{
		//rogram will read 4 charcaters at a time eg, 010_ 
		infile >> code;
		
		if(strcmp(code,LINEBREAK)==0)
		{
			cout << endl;
		}
		else
		{
			p=code;
			while(*p!='\0')
			{
				//check whether is it a lower case, uppercase, special character
				flag=*p;
				p++;
				tmpcode[0]=char(*p);
				p++;
				tmpcode[1]=char(*p);
				p++;
				
				//convert to integer
				codevalue=atoi(tmpcode);
				
				//decode the integer
				charvalue=decodeChar(flag,codevalue,secretCode);
				
				if(flag=='0')
				{
					//is lowercase[
					cout << static_cast<char>(tolower(charvalue));
				}
				else if(flag=='1')
				{
					//is uppercase
					cout << static_cast<char>(toupper(charvalue));
				}
				else if(flag=='9')
				{	
				
					if(charvalue=='\0'){
						//if charcater does not match any
						cout << tmpcode[1];
					}
					else
					{
						//is special character	  
						cout << charvalue;
					}
				}
				else
				{
					//should nt come here
				}
				p++;
			}//endwhile
		}//endif
	}//end while
	
	infile.close();
	
	system("pause");
	cout << "\n\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++;
	}	 
}
*/

//######################################################
//# To decode int secret code to character
//######################################################

char decodeChar(char flag,int ch,EncryptCode *p){
	int start=0;
	int end=0;
	
	//check whether is it a special char or alphabet
	if(flag=='9'){
		//special character
		start=TOTSALPHACHARS;
		end=TOTCHARS;
	}
	else
	{
		//alphabet
		start=0;
		end=TOTSALPHACHARS;
	}
	p=p+start;
	
	//loop through the list of secretcodes and match
	for(int z=start;z<end;z++){
		if(p->code==ch){
			return p->ch;	 
		}
		p++;
	}
	return '\0';	
}

⌨️ 快捷键说明

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