【实验名称】函数的定义与使用
【实验目的】
1、熟悉C++函数的定义;
2、理解C++函数的使用;
3、掌握C++的函数重载。
【实验原理】
函数是一组一起执行一个任务的语句。每个 C++ 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数。我们把代码划分到不同的函数中。如何划分代码到不同的函数中是由您来决定的,但在逻辑上,划分通常是根据每个函数执行一个特定的任务来进行的。
C++ 允许多个函数拥有相同的名字,只要它们的参数列表不同就可以,这就是函数的重载。借助重载,一个函数名可以有多种用途。
【实验内容】
一、设有置换加密算法:将大小写字母转换为其后的第i个字母,例如,i=4时,A->E…Z->D,a->e… z->d,非字母不变。编写程序,能够选择加密或解密功能以及i值,输入字符串后给出结果。
代码(具体功能解释可见代码中注释部分):
#include<iostream>
using namespace std;
//加密
void jiami(char *point1, char *point2,int shift)
{
strcpy(point2, point1);
for (int i = 0; i < strlen(point2); i++)
{
//大写字母
if (point2[i] >= 65 && point2[i] <= 90)
{
point2[i] = point2[i] + shift;
//若溢出
if (point2[i] > 90)
{
point2[i] = (point2[i] % 91) + 65;
}
}
//小写字母
else if(point2[i] >= 97 && point2[i] <= 122)
{
point2[i] = point2[i] + shift;
//若溢出
if (point2[i] > 122)
{
point2[i] = (point2[i] % 123) + 97;
}
}
}
return;
}
//解密
void jiemi(char *point1, char *point2, int shift)
{
strcpy(point2, point1);
for (int i = 0; i < strlen(point2); i++)
{
//大写字母
if (point2[i] >= 65 && point2[i] <= 90)
{
point2[i] = point2[i] - shift;
//若溢出
if (point2[i] < 65)
{
point2[i] = 91 - (65 - point2[i]);
}
}
//小写字母
else if (point2[i] >= 97 && point2[i] <= 122)
{
point2[i] = point2[i] - shift;
//若溢出
if (point2[i] < 97)
{
point2[i] = 123 - (97 - point2[i]);
}
}
}
return;
}
int main()
{
//freopen("in.txt", "r", stdin);
char plaintext[100], ciphertext[100];
int judge = 0; int shift;
cout << ".................................." << endl;
while (true)
{
memset(plaintext, '\0', sizeof(plaintext));
memset(ciphertext, '\0', sizeof(ciphertext));
cout << "请选择功能:1、加密 2、解密 3、退出 " << endl;
cin >> judge;
//退出
if (judge == 3)
{
cout << ".................................." << endl;
break;
}
cout << "请输入位移量:" << endl;
cin >> shift;
//加密
if (judge == 1)
{
cout << "请输入待加密明文:" << endl;
cin >> plaintext;
jiami(plaintext, ciphertext, shift);
cout << "加密后的密文为:\n" << ciphertext << endl;
cout << ".................................." << endl;
}
//解密
else
{
cout << "请输入待解密密文:" << endl;
cin >> ciphertext;
jiemi(ciphertext, plaintext, shift);
cout << "解密后的明文为:\n" << plaintext << endl;
cout << ".................................." << endl;
}
}
return 0;
}
运行结果:
二、使用重载,设计同名函数,计算原点与一维/二维/三维空间中任意一点之间的距离,并在主函数中测试。(参数可以是类类型,也可以使用普通类型)
代码(具体功能解释可见代码中注释部分):
#include<iostream>
#include<cmath>
using namespace std;
//一维点
double distance(double x)
{
double result = sqrt(x*x);
return result;
}
//二维点
double distance(double x,double y)
{
double result = sqrt(x*x + y * y);
return result;
}
//三维点
double distance(double x, double y, double z)
{
double result = sqrt(x*x + y * y + z * z);
return result;
}
int main()
{
//freopen("in.txt", "r", stdin);
double x, y, z;
int judge;
cout << "......................................................" << endl;
while (true)
{
double result;
cout << "请选择功能:1、一维点 2、二维点 3、三维点 4、退出" << endl;
cout << " ";
cin >> judge;
//程序结束判断
if (judge == 4)
{
cout << "......................................................" << endl;
break;
}
cout << "请输入点的坐标:";
//一维点
if (judge == 1)
{
cin >> x;
result = distance(x);
}
//二维点
else if (judge == 2)
{
cin >> x >> y;
result = distance(x, y);
}
//三维点
else if (judge == 3)
{
cin >> x >> y >> z;
result = distance(x, y, z);
}
//输出计算结果
cout << "该点距离原点的距离为:";
cout << result << endl;
cout << "......................................................" << endl;
}
return 0;
}
运行结果:
【小结或讨论】
对于第一题,这不禁让我联想到了密码学,这正是最基础的移位密码。主要的思路就是逐个读取输入进来的字符,若是字母就进行加密/解密,要注意移位时可能发生的溢出,即超过/小于字母的ASCALL码范围,需要用到模运算。若不是字母则不做处理。这里有一个小坑,还让我debug了好一会。就是模运算后应该还要加上一个基础值。比如有大写字母移位到91,这超出了大写字母的表示范围,如果我们直接模运算就直接模成了0,所以我们一个还要加上一个大写字母的基础值,即A的ASCALL码65,这样才能使移位操作围成一个首尾相连的圈。
对于第二题,题目本身不算太难,其中重载函数的使用让我感觉还是比较新鲜的。毕竟之前C语言中要求函数名是不能重复的。这样根据参数的不同自动匹配相应的子函数,我感觉还是非常的方便的,也让我感到了C++语言较之C语言的灵活。