|
-
February 27th, 2008, 09:04 AM
#1
codebreaking program
Hi, im new to c++ and im trying to make a codebreaking program which works by reading a text file, working out which letter has the highest frequency and swapping it with e, etc.
Except i cant work out why it wont work lol, it runs untill the end of the codebreak function
Thanks for any help
Code:
#include <iostream>
#include <fstream>
//This section of the code includes the libaries needed to run the program
using namespace std;
// Im using standard namespaces through out the code
void codebreak(char code[512])
//This creates a function called codebreak, which uses the input code[]
{
int x, lettotal, y, i, maxno, let[25];
//This creates all the integer variables used in my program, x, y and i are used for counting
char decry[25], broke[512];
//This creates the array used to hold the correct order of the alphabet
i = 0;
maxno = 0;
x = 0;
lettotal = 0;
y = 0;
//This sets all the integers used to 0
for (int x = 0; x <=25;){
let[x] = 0;
x = x + 1;
}
//This assigns 0 to every value in the array let[25]
for (int x = 0; x <= 512; x++){
if (code[x] = 'a'){
let[0] = let[0] + 1 ;
}
if (code[x] = 'b'){
let[1] = let[1] + 1 ;
}
if (code[x] = 'c'){
let[2] = let[2] + 1 ;
}
if (code[x] = 'd'){
let[3] = let[3] + 1 ;
}
if (code[x] = 'e'){
let[4] = let[4] + 1 ;
}
if (code[x] = 'f'){
let[5] = let[5] + 1 ;
}
if (code[x] = 'g'){
let[6] = let[6] + 1 ;
}
if (code[x] = 'h'){
let[7] = let[7] + 1 ;
}
if (code[x] = 'i'){
let[8] = let[8] + 1 ;
}
if (code[x] = 'j'){
let[9] = let[9] + 1 ;
}
if (code[x] = 'k'){
let[10] = let[10] + 1 ;
}
if (code[x] = 'l'){
let[11] = let[11] + 1 ;
}
if (code[x] = 'm'){
let[12] = let[12] + 1 ;
}
if (code[x] = 'n'){
let[13] = let[13] + 1 ;
}
if (code[x] = 'o'){
let[14] = let[14] + 1 ;
}
if (code[x] = 'p'){
let[15] = let[15] + 1 ;
}
if (code[x] = 'q'){
let[16] = let[16] + 1 ;
}
if (code[x] = 'r'){
let[17] = let[17] + 1 ;
}
if (code[x] = 's'){
let[18] = let[18] + 1 ;
}
if (code[x] = 't'){
let[19] = let[19] + 1 ;
}
if (code[x] = 'u'){
let[20] = let[20] + 1 ;
}
if (code[x] = 'v'){
let[21] = let[21] + 1 ;
}
if (code[x] = 'w'){
let[22] = let[22] + 1 ;
}
if (code[x] = 'x'){
let[23] = let[23] + 1 ;
}
if (code[x] = 'y'){
let[24] = let[24] + 1 ;
}
if (code[x] = 'z'){
let[25] = let[25] + 1 ;
}
//This whole loops is used to count the frequency of letters in the given text and store the
//values in the array called let[25]
}
while (y <= 25){
lettotal = lettotal + let[y];
y = y + 1;
}
//This works out the total numbers of letters used and stores it in the integer lettotal
y = 0;
while (y <= 25){
let[y] = let[y]/lettotal;
y = y + 1;
}
while (y <= 25){
while (i <= 25){
if (let[i] > maxno){
maxno = i;
}
//This code checks to see if the item in the array is currently larger than the maximum
//number(maxno)
i = i+1;
}
if (y = 0){
decry[maxno] = 'e';
}
if (y = 1){
decry[maxno] = 't';
}
if (y = 2){
decry[maxno] = 'a';
}
if (y = 3){
decry[maxno] = 'o';
}
if (y = 4){
decry[maxno] = 'i';
}
if (y = 5){
decry[maxno] = 'n';
}
if (y = 6){
decry[maxno] = 's';
}
if (y = 7){
decry[maxno] = 'h';
}
if (y = 8){
decry[maxno] = 'r';
}
if (y = 9){
decry[maxno] = 'd';
}
if (y = 10){
decry[maxno] = 'l';
}
if (y = 11){
decry[maxno] = 'c';
}
if (y = 12){
decry[maxno] = 'u';
}
if (y = 13){
decry[maxno] = 'm';
}
if (y = 14){
decry[maxno] = 'w';
}
if (y = 15){
decry[maxno] = 'f';
}
if (y = 16){
decry[maxno] = 'g';
}
if (y = 17){
decry[maxno] = 'y';
}
if (y = 18){
decry[maxno] = 'p';
}
if (y = 19){
decry[maxno] = 'b';
}
if (y = 20){
decry[maxno] = 'v';
}
if (y = 21){
decry[maxno] = 'k';
}
if (y = 22){
decry[maxno] = 'j';
}
if (y = 23){
decry[maxno] = 'x';
}
if (y = 24){
decry[maxno] = 'q';
}
if (y = 25){
decry[maxno] = 'z';
}
let[maxno] = 0;
i = 0;
y = y + 1;
}
//This group of if statements is used to find out the correct sequence in the alphabet, by replacing the
//letter with the highest frequency with e, etc.
x = 0;
for (int x = 0; x <= 512;){
if (code[x] != 'a'){
code[x] = decry[0];
}
if (code[x] != 'b'){
code[x] = decry[1];
}
if (code[x] != 'c'){
code[x] = decry[2];
}
if (code[x] != 'd'){
code[x] = decry[3];
}
if (code[x] != 'e'){
code[x] = decry[4];
}
if (code[x] != 'f'){
code[x] = decry[5];
}
if (code[x] != 'g'){
code[x] = decry[6];
}
if (code[x] != 'h'){
code[x] = decry[7];
}
if (code[x] != 'i'){
code[x] = decry[8];
}
if (code[x] != 'j'){
code[x] = decry[9];
}
if (code[x] != 'k'){
code[x] = decry[10];
}
if (code[x] != 'l'){
code[x] = decry[11];
}
if (code[x] != 'm'){
code[x] = decry[12];
}
if (code[x] != 'n'){
code[x] = decry[13];
}
if (code[x] != 'o'){
code[x] = decry[14];
}
if (code[x] != 'p'){
code[x] = decry[15];
}
if (code[x] != 'q'){
code[x] = decry[16];
}
if (code[x] != 'r'){
code[x] = decry[17];
}
if (code[x] != 's'){
code[x] = decry[18];
}
if (code[x] != 't'){
code[x] = decry[19];
}
if (code[x] != 'u'){
code[x] = decry[20];
}
if (code[x] != 'v'){
code[x] = decry[21];
}
if (code[x] != 'w'){
code[x] = decry[22];
}
if (code[x] != 'x'){
code[x] = decry[23];
}
if (code[x] != 'y'){
code[x] = decry[24];
}
if (code[x] != 'z'){
code[x] = decry[25];
}
x = x + 1;
}
//This loop is used to replace every letter in the array code[] with the correct replacement
//according to frequency
for (int x = 0; x <=512;){
broke[x] = code[x];
x = x + 1;
}
cout << "\nAfter code breaking:\n" << broke << endl;
}
int main()
//This defines the main() function
{
char file[256];
char code[512];
int i;
i = 0;
//This defines the arrays and integers used in the main function
ifstream inFile;
//This open the stream inFile
system("cls");
//This clears the output screen
cout << " Code Breaker!" << endl;
cout << "Please enter the name and extension of the file you want to decode:" << endl;
cin.getline ( file, 512, '\n');
cout << "The name entered was : " << file << endl;
//This code gets the user to enter the name of the file that they wish to decode
inFile.open(file);
//This opens the text file specified by the user
if (!inFile) {
cerr << "Unable to open file";
exit(1);
}
//The code checks to see if the file opened correctly, if not, the program will end
while(inFile >> code[i] && i < 512){
i = i + 1;
}
//This puts the data that was in the users file, into the array code
cout << "This file contains the code : \n \n" << code << "\n" << endl;
cout << "Now performing code breaking..." << endl;
codebreak (code);
inFile.close();
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|