有一行电文,已按下面规律译成密码 C++编程

2016-06-22 11:09:37  分类: c++程序设计第三版谭浩强课后答案  参与:

有一行电文,已按下面规律译成密码:
    A->Z     a->z
    B->Y     b->y
    C->X     c->x
     …      …
即第一个字母变成第26个字母,第i个字母变成第(26-i+1)个字母。非字母字符不变,要求编程序将密码译回原文,并打印出密码和原文。(用C++编写程序)

以下是此题的【c++源代码】,需要【c源代码】请点击进入

方法1,用字符数组方法,源代码如下
#include <iostream>
using namespace std;
int main()
 {int j,n;
  char ch[80],tran[80];
  cout<<"input cipher code:";
  gets(ch);
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (ch[j]!='\0')
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      tran[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
      tran[j]=219-ch[j];
    else
      tran[j]=ch[j];
    j++;
  }
  n=j;
  cout<<"original text:";
  for (j=0;j<n;j++)
    putchar(tran[j]);
  cout<<endl;
  return 0;
 }

 
方法2,不定义两个字符数组,而只用一个字符数组,源代码如下

 #include <iostream>
using namespace std;
int main()
 {int j,n;
  char ch[80];
  cout<<"input cipher code:";
  gets(ch);
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (ch[j]!='\0')
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      ch[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
      ch[j]=219-ch[j];
    else
      ch[j]=ch[j];
    j++;
  }
  n=j;
  cout<<"original text:";
  for (j=0;j<n;j++)
    putchar(ch[j]);
  cout<<endl;
  return 0;
 }

 
 方法3,用string 方法

#include <iostream>
#include <string>
using namespace std;
int main()
 {int j;
  string ch="I will visit China next week.",tran;
  tran=ch;
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (j<=ch.size())
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      tran[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
   tran[j]=219-ch[j];
    else
   tran[j]=ch[j];
    j++;
  }
  cout<<"original text:";
  cout<<tran<<endl;
  return 0;
 }

 
方法4,只用一个字符串变量ch
 #include <iostream>
#include <string>
using namespace std;
int main()
 {int j;
  string ch="I will visit China next week.";
  cout<<"cipher code:"<<ch<<endl;
  j=0;
  while (j<=ch.size())
  { if ((ch[j]>='A') && (ch[j]<='Z'))
      ch[j]=155-ch[j];
    else if ((ch[j]>='a') && (ch[j]<='z'))
   ch[j]=219-ch[j];
    j++;
  }
  cout<<"original text:";
  cout<<ch<<endl;
  return 0;
 }

 
 

来源:c++程序设计第三版谭浩强课后答案

本文链接:http://www.wb98.com/cjia/post/cjia_5.12.html


本站文章搜索:

<< 上一篇下一篇 >>

搜索

Tags列表

赞助商链接