Advertisement
Guest User

Untitled

a guest
May 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <stdlib.h>
  6. #include <algorithm>
  7. #include <sstream>
  8. using namespace std;
  9.  
  10.  
  11. struct student{
  12. string name;
  13. int id;
  14. string email;
  15. string courses[3];
  16. int grades[3];};
  17.  
  18. int count_students(string filename);
  19.  
  20. void read_student(string fname,student*s,int n);
  21.  
  22. string encrypt(string word , int x);
  23.  
  24. string dencrypt(string word);
  25.  
  26. void write1_student(string fname,student*s,int n);
  27.  
  28. void write2_student(string fname,student*s,int n);
  29. string convert(int x);
  30.  
  31.  
  32.  
  33. int main()
  34. {
  35.  
  36.     int n,ch;
  37.     cout<< "1=encrypt,2=dencrypt"<<endl;
  38.     cin>>ch;
  39.     string fname;
  40.     if(ch==1){
  41.         cout<<"Enter file name ( ex: file_name.txt )"<<endl;
  42.         cin>>fname;
  43.         n=count_students(fname);
  44.         student*st=new student[n];
  45.         read_student(fname,st,n);
  46.  
  47.         write1_student("data.txt",st,n);
  48.     }else if(ch==2){
  49.         cin>>fname;
  50.          n=count_students(fname);
  51.         student*st=new student[n];
  52.         read_student(fname,st,n);
  53.         write2_student("data.txt",st,n);
  54.     }
  55.  
  56.     return 0;
  57. }
  58. string convert(int x){
  59. stringstream ss;
  60. ss<<x;
  61. string y;
  62. y= ss.str();
  63. return y;
  64.  
  65. }
  66.  
  67.  
  68. int count_students(string filename){
  69. int count=0;
  70. string dummy;
  71. ifstream infile;
  72. infile.open(filename.c_str());
  73. while(!infile.eof()){
  74.     getline(infile,dummy);
  75.     count++;
  76. }infile.close();
  77. return count/6;
  78. }
  79. void read_student(string fname,student*s,int n){
  80.     string dummy;
  81.     ifstream infile;
  82.     infile.open(fname.c_str());
  83.     for(int i=0;i<n;i++){
  84.         getline(infile,s[i].name);
  85.         infile>>s[i].id;
  86.         infile>>s[i].email;
  87.         for(int j=0;j<3;j++){
  88.             infile>>s[i].courses[j];
  89.  
  90.             infile>>s[i].grades[j];
  91.         }
  92.  
  93.         getline(infile,dummy);
  94.         infile.ignore();
  95.     }
  96. }
  97. string encrypt(string word , int x){
  98.     for(int i=0;i<word.size();i++){
  99.         if(word[i]!=' ')
  100.           word[i]+=x;
  101.  
  102.     }
  103.     return word;
  104. }
  105. string dencrypt(string word){
  106.     int x;
  107.     cin>>x;
  108.     for(int i=0;i<word.size();i++){
  109.         if(word[i]!=' ')
  110.           word[i]-=x;
  111.  
  112.     }
  113.     return word;
  114. }
  115.  
  116. void write1_student(string fname,student*s,int n){
  117.     int num;
  118.     cout<<"How many characters do you want to shift?"<<endl;
  119.     cin>>num;
  120.     string x;
  121.     ofstream infile;
  122.     infile.open(fname.c_str());
  123.     for(int i=0;i<n;i++){
  124.             string e_name=encrypt(s[i].name,num);
  125.         infile<< e_name <<endl;
  126.         x=convert(s[i].id);
  127.     string e_id=encrypt(x,num);
  128.     infile<< e_id <<endl;
  129.     string e_email=encrypt(s[i].email,num);
  130.     infile<< e_email <<endl;
  131.     for(int j=0;j<3;j++){
  132.         string e_courses=encrypt(s[i].courses[j],num);
  133.         infile<< e_courses;
  134.         x=convert((*s).grades[j]);
  135.         string e_grades=encrypt(x,num);
  136.         infile<<' '<< e_grades<<endl;
  137.         if(!(i==n-1&&j==2))
  138. ;    }
  139.     infile<<endl;
  140.     }
  141. }void write2_student(string fname,student*s,int n){
  142.     stringstream ss;
  143.     ss<<(*s).id;
  144.     ss<<(*s).grades[3];
  145.     string str=ss.str();
  146.     string x=ss.str();
  147.     ofstream infile;
  148.     infile.open(fname.c_str());
  149.     for(int i=0;i<n;i++){
  150.             string e_name=dencrypt(s[i].name);
  151.         infile<< e_name <<endl;
  152.     string e_id=dencrypt(x);
  153.     infile<< e_id <<endl;
  154.     string e_email=dencrypt(s[i].email);
  155.     infile<< e_email <<endl;
  156.     for(int j=0;j<3;j++){
  157.         string e_courses=dencrypt(s[i].courses[j]);
  158.         infile<< e_courses << ":";
  159.         string e_grades=dencrypt(x);
  160.         infile<< e_grades;
  161.         if(!(i==n-1&&j==2))
  162. ;    }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement