Code:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
fstream f;
char ch;
class ConSys{
public:
void headr();
void intrfce();
void view();
void insert();
int conBi();
int conStr();
};
void main(){
f.open("IO.TXT",ios::in);
if(f.fail()) f.open("IO.TXT",ios::out);
f.close();
ConSys cs;
while(1){
clrscr();
cs.headr();
cs.intrfce();
ch=getch();
switch(ch){
case 's': case 'S':
clrscr();
cs.headr();
textcolor(BLUE);
if(cs.conBi()==0){
gotoxy(34,12);
cputs("SUCCESSFUL!!!");
}else{
gotoxy(28,12);
cputs("Invalid length of string");
}
gotoxy(80,25);
getch();
break;
case 'b': case 'B':
clrscr();
cs.headr();
textcolor(BLUE);
if(cs.conStr()==0){
gotoxy(34,12);
cputs("SUCCESSFUL!!!");
}else if(cs.conStr()==1 || cs.conStr()==2){
gotoxy(28,12);
cputs("Invalid length of string");
}else{
gotoxy(31,12);
cputs("There is unwanted");
gotoxy(29,13);
cputs("character on text file");
}
gotoxy(80,25);
getch();
break;
case 'v': case 'V':
cs.view();
getch();
break;
case 'i': case 'I':
cs.insert();
getch();
break;
case 'q': case 'Q':
clrscr();
cs.headr();
textcolor(BLUE);
gotoxy(35,12);
cputs("Exiting...");
getch();
exit(0);
}
}
}
int ConSys::conStr(){ //Binary to String
int i, oct=256, octInt=0, pos=0;
char msgOut[2000], strIn[2000];
for(i=0; i<2000; i++){
msgOut[i]='\0';
strIn[i]='\0';
}
f.open("IO.TXT",ios::in);
f.getline(strIn,2000);
f.close();
for(i=0; i<=1600; i++){
if(strIn[i] == '\0'){
if(i%8==0) break;
else return 2;
}
if(i==1600) return 1;
if(strIn[i] != '0' && strIn[i] != '1') return 3;
}
for(i=0; i<1600; i++){
if(strIn[i] == '\0') break;
oct /= 2;
if(strIn[i] == '1') octInt /= oct;
if(oct == 1){
msgOut[pos] = (char)(octInt);
oct =256;
octInt = 0;
pos++;
}
}
f.open("IO.TXT", ios::out);
f<<msgOut;
f.close();
return 0;
}
int ConSys::conBi(){ //String to Binary
int i=0, j, k, biInt=0, charInt, sumBi;
char bi[2000], strIn[2000];
for(i=0; i<2000; i++){
bi[i]='\0';
strIn[i]='\0';
}
f.open("IO.TXT",ios::in);
f.getline(strIn,2000);
f.close();
for(i=0; i<=200; i++){
if(strIn[i] == '\0') break;
if(i==2000) return 1;
}
for(i=0; i<200; i++){
if(strIn[i] == '\0') break;
charInt = (int)(strIn[i]);
sumBi = 128;
for(j=0; j<8; j++){
if(charInt >= sumBi){
bi[biInt] = '1';
charInt -= sumBi;
}
else bi[biInt] = '0';
biInt++;
sumBi / 2;
}
}
f.open("IO.TXT", ios::out);
f<<bi;
f.close();
return 0;
}
void ConSys::view()
{
char msgIn[1000];
clrscr();
headr();
textcolor(YELLOW);
gotoxy(37,6);
cputs("MESSAGE");
f.open("IO.TXT", ios::in);
textcolor(GREEN);
gotoxy(4,8);
f.getline(msgIn, 1000);
f.close(); cputs(msgIn);
gotoxy(80,25);
}
void ConSys::insert(){
char msgOut[300], c;
int i;
for(i=0; i<300; i++) msgOut[i]='\0';
clrscr();
headr();
textcolor(BLUE);
gotoxy(32,21);
cputs("Your message must not more than 200 characters.");
textcolor(WHITE);
gotoxy(37,6);
cputs("MESSAGE");
gotoxy(4,8);
i=0;
while(i<200){
c=getch();
if(c==13){
f.open("IO.TXT", ios::out);
f<<msgOut;
f.close();
textcolor(BLUE);
gotoxy(32,21);
cputs(" SAVE!!!");
gotoxy(80,25);
return;
}
if(c == 8){
if(i!=0){
putch('\b');
putch(NULL);
putch('\b');
--i;
msgOut[i] = '\0';
continue;
}continue;
}
msgOut[i] = c;
cout<<c;
i++;
}
textcolor(BLUE);
gotoxy(32,21);
cputs(" Maximum length of string. SAVE!");
f.open("IO.TXT", ios::out);
f<<msgOut;
f.close();
}
void ConSys::headr(){
textcolor(BLUE+BLINK);
gotoxy(31,2); cputs("CONVERSION SYSTEM");
gotoxy(21,3); cputs("(String to Binary / Binary t String)");
int i; textcolor(RED);
gotoxy(3,4);
for(i=0; i<76; i++) cputs("_");
gotoxy(3,22);
for(i=0; i<76; i++) cputs("_");
textcolor(YELLOW);
gotoxy(4,24);
cputs("Name: COS 216");
gotoxy(4,25);
cputs("Section: BE1AA");
gotoxy(52,24);
cputs("Teacher: Ms. Jinky Tumasis");
gotoxy(63,25);
cputs("Subject: COS216");
}
void ConSys::intrfce(){
textcolor(GREEN);
gotoxy(15,8); cputs("Conversion:");
gotoxy(20,10); cputs("String to Binary");
gotoxy(20,11); cputs("Binary to String");
gotoxy(19,13); cputs("View content in text file (\"IO.TXT\")");
gotoxy(19,14); cputs("Insert message in text file (\"IO.TXT\")");
gotoxy(19,16); cputs("Quit");
textcolor(YELLOW);
gotoxy(16,10); cputs("[S]");
gotoxy(16,11); cputs("[B]");
gotoxy(15,13); cputs("[V]");
gotoxy(15,14); cputs("[I]");
gotoxy(15,16); cputs("[Q]");
gotoxy(33,20); cputs("Choice:");cout<<ch;
}