| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have a program in which there are some bugs. It would be very nice if someone could help me out. I have gicen the details of the program along with the code.
Write a C++ program that can read, compress (encode) and decompress (decode) binary data. The program should display a menu with the following options: a) Read Data b) Compress Data c) Decompress Data Read Data Binary data (0/1) should be read from the keyboard and stored in an array. The number of bits entered from the keyboard should be equal to the sum of last 5 digits in your student ID number. For example: If your id number is S00453345, the sum of last 5 digits is 5+3+3+4+5=20. So 20 digits (eg. 00110000011111110001) should be entered from the keyboard, read and stored in an array for compression. Compress Data: To compress/encode data, you should recognise repeated digits and replace the runs of repetition with a number representing the repetition length. For example: 000011100011000111100000 can be written as 4332345 because there are 4 zeros, 3 ones, 3 zeros, 2 ones, 3 zeros, 4 ones and 5 zeros. Decompression Data: To decompress/decode data, you should use the reverse order. For example: 3425 can be written as 00011110011111. The whole program should work in a loop to enable the user to read, compress and decompress data until the user chooses to exit. Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h> // for exit()
#include <string.h>
#include<conio.h>
class data
{
private:
char sid[20],bin[50];
int encr[50],decr[50];
public:
int sum,ctr,bin_done; /* Sum stores the number of binary values to get and ctr stores the index of encr[] */
data();
void inputsid(); /* To input the Student ID */
void computenum(); /* To find the sum of last 5 digits of the Student Id */
void menu(); /* To print the menu */
void inputbin(); /* To input the Binary data */
void encrdata(); /* To encrypt the binary data */
void decrdata(); /* To decrypt the binary data */
};
// Constructor to reset the counters
data::data()
{
sum=0;
ctr=0;
bin_done=0;
}
// To Get the Student Id
void data::inputsid()
{
clrscr();
cout<<"Enter your Student ID Number: ";
gets(sid);
computenum();
}
// To compute the num of binary digits to enter
void data::computenum()
{
int num,s=0;
sum=0;
num=strlen(sid);
char str[6];
for(int i=num-5;i<=num;i++)
{
str[s]=sid[i];
s++;
}
int tmp=atoi(str);
while(tmp!=0)
{
sum+=tmp%10;
tmp/=10;
}
}
// To print the main menu
void data::menu()
{
// clrscr();
cout<<"\n\n\n\n MENU\n";
cout<<"1. Enter Data\n";
cout<<"2. Encrypt Data\n";
cout<<"3. Decrypt Data\n";
cout<<"4. Exit\n";
}
// To get the binary Data
void data::inputbin()
{
cout<<"Enter "<<sum<<" binary digits one character at a time: ";
char tmp;
for(int i=0;i<sum;)
{
cout<<"\nEnter character no "<<i+1<<":";
cin>>tmp;
if(tmp=='0' || tmp=='1')
{
bin[i]=tmp;
i++;
}
else
{
cout<<tmp<<"\nInacceptable Value. Please enter 1 or 0 only";
}
}
bin_done=1;
}
// To Encrypt the data
void data::encrdata()
{
ctr=0;
int counter=0;
for(int i=0;i<sum;i++)
{
if(i==0)
{
if(bin[i]=='1')
{
encr[ctr]=counter;
ctr++;
counter++;
}
else
{
counter++;
}
}
else
{
if(bin[i]==bin[i-1])
{
counter++;
}
else
{
encr[ctr]=counter;
ctr++;
counter=1;
}
}
}
encr[ctr]=counter;
ctr++;
cout<<"\nThe Encrypted data is"<<ctr<<" : ";
for(i=0;i<ctr;i++)
{
cout<<encr[i];
}
}
// To Decrypt the data
void data::decrdata()
{
//int j=0;
int val=0;
cout<<"\nDecrypted data is: ";
for(int i=0;i<ctr;i++)
{
for(int k=0;k<encr[i];k++)
{
// decr[j]=val;
cout<<val;
// j++;
}
if(val==0)
{
val=1;
}
else
{
val=0;
}
}
}
void main()
{
data d; /* Creating an object of the data class */
d.sum=0;
d.ctr=0;
d.bin_done=0;
char ch;
clrscr();
d.inputsid();
do
{
d.menu();
cout<<"\nENTER YOUR CHOICE: ";
cin>>ch;
switch (ch)
{
case '1':
{
d.inputbin();
break;
}
case '2':
{
if(d.bin_done==0)
{
cout<<"Please enter the binary data first.";
getch();
break;
}
else
{
d.encrdata();
break;
}
}
case '3':
{
if(d.ctr==0)
{
cout<<"Please encrypt the binary data first.";
getch();
break;
}
else
{
d.decrdata();
break;
}
}
case '4':
{
exit(0);
break;
}
default:
cout<<"Invalid choice";
}
getch();
}while(1);
}
|
|
#2
|
||||
|
||||
|
what specifically is your problem?
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
Quote:
Write a C++ program that can read, compress (encode) and decompress (decode) binary data. The program should display a menu with the following options: a) Read Data b) Compress Data c) Decompress Data Read Data Binary data (0/1) should be read from the keyboard and stored in an array. The number of bits entered from the keyboard should be equal to the sum of last 5 digits in your student ID number. For example: If your id number is S00453345, the sum of last 5 digits is 5+3+3+4+5=20. So 20 digits (eg. 00110000011111110001) should be entered from the keyboard, read and stored in an array for compression. Compress Data: To compress/encode data, you should recognise repeated digits and replace the runs of repetition with a number representing the repetition length. For example: 000011100011000111100000 can be written as 4332345 because there are 4 zeros, 3 ones, 3 zeros, 2 ones, 3 zeros, 4 ones and 5 zeros. Decompression Data: To decompress/decode data, you should use the reverse order. For example: 3425 can be written as 00011110011111. The whole program should work in a loop to enable the user to read, compress and decompress data until the user chooses to exit. |
|
#4
|
|||
|
|||
|
well, it doesn't do any good to repeat your OP. To rephrase B-Con's post:
What are you having trouble with? What, specifically, don't you understand or want our help with? |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Binary Number Encryption/ Decryption in C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|