这个是我练习输入输出流时写的程序,感觉有很多地方不足和错误,程序很乱,很繁琐~ 希望大家帮我改改
#include<iostream>
#include<cstring>
#include<fstream>
#include<strstream>
#include<cstdlib>
using namespace std;
void show()
{int pus;
cout<<endl;
cout<<" 错误啦!其实不是这么用滴!"<<endl;
cout<<endl;
cout<<" 正确的使用方法是这样的:"<<endl;
cout<<endl;
cout<<" atxt 命令 文本文件名 密钥"<<endl;
cout<<endl;
cout<<" 例如加密时:atxt a xxx.txt key"<<endl;
cout<<endl;
cout<<" 解密时:atxt x xxx.txt key"<<endl;
cout<<endl;
cout<<" 注意:密钥必须为一个连续的字符串!";
cout<<endl;
cout<<endl;
cout<<" ";system("pause");}
int main(int argc,char *argv[])
{try{char *a1=argv[1],*a2=argv[2],*a3=argv[3];
if(strlen(a1)<=0)
throw;
else if(strlen(a2)<=0)
throw;
else if(strlen(a3)<=0)
throw;}
catch(...)
{show();return 0;}
strstream textfile;char ch;int key,i=0;
char qwl[100]="此文档已经加密,请解密后查看!";
char qwlt[100];
int len=0,lem=strlen(qwl);
char *cmda=argv[1],*cmd2=argv[2],*cmd3=argv[3];
char cmd1=cmda[0];char a='a',A='A',x='x',X='X';
if((strlen(cmda)<=0)||(strlen(cmd2)<=0)||(strlen(cmd3)<=0))
{show();return 0;}
while(cmd3[i]!='\0')
{key+=int(cmd3[i]);i++;
}
key=key%10+1;
int ad=0,ex=0,ex1=1;
if(int(cmd1)==int(a)||int(cmd1)==int(A))
{ad=1;}
else if(int(cmd1)==int(x)||int(cmd1)==int(X))
{ex=1;}
else
{show();return 0;}
ifstream in(cmd2);
if (!in)
{cout<<"文件 "<<cmd2<<" 打开失败"<<endl;return 0;}
else{cout<<"文件 "<<cmd2<<" 打开成功!"<<endl;}
textfile<<in.rdbuf();
if(ex)
{textfile.getline(qwlt,100);
textfile.seekg(0,ios::beg);
if(strcmp(qwlt,qwl)!=0)
{cout<<cmd2<<"不是本程序加密文件!"<<endl;return 0;}
}
ofstream out(cmd2);
if (!out)
{cout<<"不能写入"<<cmd2<<endl;return 0;}
else{cout<<"写入"<<cmd2<<"成功"<<endl;}
if(ad)
{
out.setf(ios::right);
out.width(1);
out<<qwl<<endl;
}
while(textfile.get(ch))
{out.setf(ios::right);
out.width(1);
if(ad)
{ch=char(int(ch)-key);ch^=7;out<<ch;}
else if(ex)
{if(len>lem){ch^=7;ch=char(int(ch)+key);out<<ch;}}
len++;}
if(ad)
{cout<<"加密成功!!";}
else if(ex)
{
cout<<"解密成功!!";
}
cout<<" 感谢使用!"<<endl;
return 0;}
[此贴子已经被作者于2007-9-6 23:14:00编辑过]