Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstdlib>
- #include <stdlib.h>
- #include <algorithm>
- #include <sstream>
- using namespace std;
- struct student{
- string name;
- int id;
- string email;
- string courses[3];
- int grades[3];};
- int count_students(string filename);
- void read_student(string fname,student*s,int n);
- string encrypt(string word , int x);
- string dencrypt(string word);
- void write1_student(string fname,student*s,int n);
- void write2_student(string fname,student*s,int n);
- string convert(int x);
- int main()
- {
- int n,ch;
- cout<< "1=encrypt,2=dencrypt"<<endl;
- cin>>ch;
- string fname;
- if(ch==1){
- cout<<"Enter file name ( ex: file_name.txt )"<<endl;
- cin>>fname;
- n=count_students(fname);
- student*st=new student[n];
- read_student(fname,st,n);
- write1_student("data.txt",st,n);
- }else if(ch==2){
- cin>>fname;
- n=count_students(fname);
- student*st=new student[n];
- read_student(fname,st,n);
- write2_student("data.txt",st,n);
- }
- return 0;
- }
- string convert(int x){
- stringstream ss;
- ss<<x;
- string y;
- y= ss.str();
- return y;
- }
- int count_students(string filename){
- int count=0;
- string dummy;
- ifstream infile;
- infile.open(filename.c_str());
- while(!infile.eof()){
- getline(infile,dummy);
- count++;
- }infile.close();
- return count/6;
- }
- void read_student(string fname,student*s,int n){
- string dummy;
- ifstream infile;
- infile.open(fname.c_str());
- for(int i=0;i<n;i++){
- getline(infile,s[i].name);
- infile>>s[i].id;
- infile>>s[i].email;
- for(int j=0;j<3;j++){
- infile>>s[i].courses[j];
- infile>>s[i].grades[j];
- }
- getline(infile,dummy);
- infile.ignore();
- }
- }
- string encrypt(string word , int x){
- for(int i=0;i<word.size();i++){
- if(word[i]!=' ')
- word[i]+=x;
- }
- return word;
- }
- string dencrypt(string word){
- int x;
- cin>>x;
- for(int i=0;i<word.size();i++){
- if(word[i]!=' ')
- word[i]-=x;
- }
- return word;
- }
- void write1_student(string fname,student*s,int n){
- int num;
- cout<<"How many characters do you want to shift?"<<endl;
- cin>>num;
- string x;
- ofstream infile;
- infile.open(fname.c_str());
- for(int i=0;i<n;i++){
- string e_name=encrypt(s[i].name,num);
- infile<< e_name <<endl;
- x=convert(s[i].id);
- string e_id=encrypt(x,num);
- infile<< e_id <<endl;
- string e_email=encrypt(s[i].email,num);
- infile<< e_email <<endl;
- for(int j=0;j<3;j++){
- string e_courses=encrypt(s[i].courses[j],num);
- infile<< e_courses;
- x=convert((*s).grades[j]);
- string e_grades=encrypt(x,num);
- infile<<' '<< e_grades<<endl;
- if(!(i==n-1&&j==2))
- ; }
- infile<<endl;
- }
- }void write2_student(string fname,student*s,int n){
- stringstream ss;
- ss<<(*s).id;
- ss<<(*s).grades[3];
- string str=ss.str();
- string x=ss.str();
- ofstream infile;
- infile.open(fname.c_str());
- for(int i=0;i<n;i++){
- string e_name=dencrypt(s[i].name);
- infile<< e_name <<endl;
- string e_id=dencrypt(x);
- infile<< e_id <<endl;
- string e_email=dencrypt(s[i].email);
- infile<< e_email <<endl;
- for(int j=0;j<3;j++){
- string e_courses=dencrypt(s[i].courses[j]);
- infile<< e_courses << ":";
- string e_grades=dencrypt(x);
- infile<< e_grades;
- if(!(i==n-1&&j==2))
- ; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement