zoukankan      html  css  js  c++  java
  • 6月25日代码

     1 template<typename T>
     2 void swap(T &a,T &b)
     3 {
     4     T temp=a;
     5     a=b;
     6     b=temp;
     7 }
     8 int add(double a,double b)
     9 {
    10     return a+b;
    11 }
    12 int main()
    13 {
    14     int ia=3;
    15     double db=5.0;
    16     char s1[]="good",s2[]="better";
    17     int x=add(ia,db);
    18     /*swap(ia,db);
    19     swap(s1,s2);*/
    20 }
    21 //-------------------
    22 template<typename T>
    23 T max(T a,T b)
    24 {
    25     return a<b?b:a;
    26 }
    27 //const T&a与T const&a等价
    28 //----------------------------------------
    29 #include<iostream>
    30 template<typename T>
    31 T const& max(T const& a,T const& b)
    32 {
    33     return a<b?b:a;
    34 }
    35 int main()
    36 {
    37     int ia=3;
    38     double db=5.0;
    39     std::cout<<max<double>(ia,db)<<"
    ";
    40     std::cout<<max(static_cast<double>(ia),db)<<"
    ";
    41 }
    42 //--------------------------------------------
    43 #include<iostream>
    44 template<typename T>
    45 void swap(T &a,T&b)
    46 {
    47     T temp=a;
    48     a=b;
    49     b=temp;
    50 }
    51 int main()
    52 {
    53     int ia=3;
    54     const int cb=5;
    55     swap(ia,7);
    56     swap<int>(ia,7);
    57     swap(cb,7);
    58     swap<const int>(ia,7);
    59 }
    60 //----------------
    61 const char*const& max(const char*const&a,const char* const&b)
    62 {
    63     return std::strcmp(a,b)<0?b:a;
    64 }
    65 //------------------
    66 void f(int *ix,int *iy,double *dx,double *dy)
    67 {
    68     int *ip=max(ix,iy);
    69     double *dp=max(dx,dy);
    70 }
    71 //-----------------------------------
    72 #include<iostream>
    73 template<typename T>
    74 T const& max(T const&a,T const&b)
    75 {
    76     return a<b?b:a;
    77 }
    78 template<typename T>
    79 T*const& max(T*const&a,T*const&b)
    80 {
    81     return *a<*b?b:a;
    82 }
    83 const char*const&max(const char* const&a,const char*const&b)
    84 {
    85     return std::strcmp(a,b)<0?b:a;
    86 }
    87 int main()
    88 {
    89     int ia=3,ib=7;
    90     char*s1="hello";
    91     char*s2="hell";
    92     std::cout<<*max(&ia,&ib)<<"
    ";
    93     std::cout<<max(s1,s2)<<"
    ";
    94     std::cout<<max(ia,ib)<<"
    ";
    95 }
    View Code
     1 //
     2 #include <iostream>
     3 #include <string>
     4 #include <fstream>
     5 #include <vector> // 向量头文件
     6 using namespace std;
     7 string co(string message,string code);
     8 string deco(string cipher,string code);
     9 int main()
    10 { 
    11     ifstream finm("message.txt"); //原文
    12     //string message;
    13     vector<string> messageAll; 
    14     string msgtemp;
    15     string cipher,decipher;
    16     string code;
    17     ifstream fin("code.txt"); //加密秘钥
    18     
    19     for ( ;getline(finm, msgtemp); )
    20     {
    21         messageAll.push_back(msgtemp); //将输入的所有字符分行读、存储
    22     }                
    23     
    24     cout<<"文件读入结束
    "<<endl;    
    25     int row=messageAll.size(); //读入了多少行    
    26     for (int i=0; i<row; i++)
    27     {
    28         msgtemp=messageAll[i];
    29         cout<<""<<i+1<<"行字符串为"<<msgtemp<<endl; //原文
    30         fin>>code; //读入一行,加密秘钥        
    31         cipher=co(msgtemp,code); //函数调用        
    32         cout<<""<<i+1<<"行字符串加密后得到"<<cipher<<endl; //加密
    33         decipher=deco(cipher,code);
    34         cout<<""<<i+1<<"行密加密后的字符串,解密后得到"<<decipher<<endl; //解密
    35         cout<<endl;
    36     }        
    37     return 0;
    38 }
    39 string co(string message,string code)
    40 {
    41     int n;
    42     n=message.length();
    43     for(int i=0;i<n;i++)
    44     {  
    45         message[i]=message[i]+code[i]-'0';
    46         if(message[i]>'z')
    47         {
    48             message[i]=message[i]-('z'-' ');            
    49         }
    50         
    51         
    52     } return message;
    53 }
    54 string deco(string cipher ,string code)
    55 {
    56     int n;
    57     n=cipher.length();
    58     for(int i=0;i<n;i++)
    59     {   
    60         cipher[i]=cipher[i]+('z'-' ');
    61         if(cipher[i]>'z')
    62         {
    63             cipher[i]=cipher[i]+('z'-' ');
    64         }
    65         cipher[i]=cipher[i]-('z'-' ');
    66         cipher[i]=cipher[i]-code[i]+'0';
    67         
    68     }    
    69     return cipher;
    70 }
    View Code
     1 //=====================================
     2 // 101_5.cpp
     3 // 字符的加密解密
     4 //=====================================
     5 #include<iostream>
     6 #include<fstream>
     7 #include<vector>
     8 #include<string>
     9 using namespace std;
    10 //---------------------------------------
    11 string encode(string &str);    //函数声明
    12 string decode(string &trans);  //函数声明
    13 const char key[]="4962873";
    14 //---------------------------------------
    15 int main(){    
    16     string str;
    17     cout<<"请输入需要加密的一行字符,以换行结束"<<endl;
    18     getline(cin, str);
    19     string trans=encode(str);//加密函数encode,加密得到trans
    20     cout<<"加密后,密文为:"<<endl;
    21     cout<<trans<<endl;
    22     if (str==decode(trans)) //解密函数decode,判断解密得到的序列是否与原始输入序列相等
    23     {    
    24         cout<<"解密成功,原文为:"<<endl;
    25         cout<<str<<endl;
    26     }
    27     else
    28         cout<<"解密失败"<<endl;    
    29     return 0;
    30 }
    31 //-------------------------------------
    32 
    33 string encode(string &str)
    34 {
    35 //    string result(str); //类似于int a(5); 将字符串类型变量result赋值为str
    36     string result;      //这种方法是通过assign函数来指定
    37     result.assign(str);//用于拷贝、赋值操作
    38     
    39     for (int i=0;i<str.length(); i++)
    40     {
    41         result[i]+=key[i%7]-'0';
    42         if (result[i]>'z')
    43             result[i]=result[i]-('z'-' '+1);//大写字母和小写字母 差 65-32+1,空格是 32  
    44         
    45     }
    46     return result;
    47 }
    48 //-------------------------------------
    49 string decode(string &str)
    50 {
    51     string result (str);
    52     for (int i=0;i<str.length(); i++)
    53     {
    54         result[i]-=key[i%7]-'0';
    55         if (result[i]<' ')
    56             result[i]=result[i]+('z'-' '+1);
    57         
    58     }
    59     return result;
    60 }
    View Code
     1 //=====================================
     2 // 钱能教材P101_5.cpp
     3 // 字符的加密解密
     4 //=====================================
     5 #include<iostream>
     6 #include<fstream>
     7 #include<vector>
     8 #include<string>
     9 using namespace std;
    10 //---------------------------------------
    11 string encode(string &str);    //函数声明
    12 string decode(string &trans);  //函数声明
    13 const char key[]="4962873";
    14 //---------------------------------------
    15 int main(){    
    16     string str;
    17     cout<<"请输入需要加密的一行字符,以换行结束"<<endl;
    18     getline(cin, str);
    19     string trans=encode(str);//加密函数encode,加密得到trans
    20     cout<<"加密后,密文为:"<<endl;
    21     cout<<trans<<endl;
    22     if (str==decode(trans)) //解密函数decode,判断解密得到的序列是否与原始输入序列相等
    23     {    
    24         cout<<"解密成功,原文为:"<<endl;
    25         cout<<str<<endl;
    26     }
    27     else
    28         cout<<"解密失败"<<endl;    
    29     return 0;
    30 }
    31 //-------------------------------------
    32 
    33 string encode(string &str)
    34 {
    35 //    string result(str); //类似于int a(5); 将字符串类型变量result赋值为str
    36     string result;      //这种方法是通过assign函数来指定
    37     result.assign(str);
    38     
    39     for (int i=0;i<str.length(); i++)
    40     {
    41         result[i]+=key[i%7]-'0';
    42         if (result[i]>'z')
    43             result[i]=result[i]-('z'-' '+1);//' '代表32," "不是32
    44         
    45     }
    46     return result;
    47 }
    48 //-------------------------------------
    49 string decode(string &str)
    50 {
    51     string result (str);
    52     for (int i=0;i<str.length(); i++)
    53     {
    54         result[i]-=key[i%7]-'0';
    55         if (result[i]<' ')
    56             result[i]=result[i]+('z'-' '+1);
    57         
    58     }
    59     return result;
    60 }
    View Code
  • 相关阅读:
    51nod 1122 机器人走方格 V4(矩阵乘法)
    51nod 1092 回文字符串
    51nod 1254 最大子段和 V2(递推)
    容斥原理求gcd为k的数对个数
    51nod 1115 最大M子段和 V3
    51nod 1053 最大M子段和 V2(贪心)
    洛谷P1792 [国家集训队]种树
    洛谷P1484 种树(反悔贪心,双向链表+堆)
    51 nod 1052 最大M子段和
    51 nod 1051 最大子矩阵和
  • 原文地址:https://www.cnblogs.com/herizai/p/3154863.html
Copyright © 2011-2022 走看看