计算理论导引作业2020/7/9交。
递归函数30个程序的实现。
#include<iostream>
#include<string>
#include<cstdio>
#include <vector>
using namespace std;
#define NUM 8
unsigned int aCantor[NUM][NUM];
//1
unsigned int add(unsigned int x, unsigned int y) {
unsigned int res;
res = x + y;
return res;
}
//2
unsigned int plusxy(unsigned int x, unsigned int y) {
unsigned int res;
res = x * y;
return res;
}
//3
unsigned int fac(int x)
{
unsigned int i, f = 1;
for (i = 1; i <= x; i++)
f *= i;
return f;
}
//4
unsigned int powxy(unsigned int x, unsigned int y) {
unsigned int res;
res = pow(x, y);
return res;
}
//5
unsigned int px(unsigned int x) {
unsigned int res;
if (x > 0)
res = x - 1;
else
res = 0;
return res;
}
//6
unsigned int sub(unsigned int x, unsigned int y) {
unsigned int res;
if (x >= y)
res = x - y;
else
res = 0;
return res;
}
//7
unsigned int absxy(unsigned int x, unsigned int y) {
unsigned int res;
res = sub(x, y) + sub(y, x);
return res;
}
//8
unsigned int alpha(unsigned int x) {
unsigned int res;
res = sub(1, x);
return res;
}
unsigned int f(unsigned int* X, unsigned int n, unsigned int t) {
unsigned int res = 0;
for (int i = 0; i < n; i++) {
res += X[i];
}
res = sub(res, t);
return res;
}
//9
unsigned int addf(unsigned int *X, unsigned int n, unsigned int Y) {
unsigned int res = 0;
for (int t = 0; t <= Y; t++) {
res += f(X,n, t);
}
return res;
}
//10
unsigned int plusf(unsigned int* X, unsigned int n, unsigned int Y) {
unsigned int res = 0;
for (int t = 0; t <= Y; t++) {
res *= f(X, n, t);
}
return res;
}
//11
unsigned int d(unsigned int x, unsigned int y) {
unsigned int res;
res = alpha(alpha(absxy(x, y)));
return res;
}
//12
unsigned int xequaly(unsigned int x, unsigned int y) {
unsigned int res;
if (x == y)
res = 0;
else
res = 1;
return res;
}
//`13
unsigned int xbiggery(unsigned int x, unsigned int y) {
unsigned int res;
res = alpha(sub(x,y));
return res;
}
//14
unsigned int xsmalequa(unsigned int x, unsigned int y) {
unsigned int res;
res = alpha(alpha(xequaly(x,y)) || (x < y));
return res;
}
//15
unsigned int ycanx(unsigned int x, unsigned int y) {
unsigned int res;
for (int i = 0; i <= x; i++) {
if (i * y == x) {
res = 0;
break;
}
else
res = 1;
}
return res;
}
//16
unsigned int ydivx(unsigned int x, unsigned int y) {
unsigned int res = 0;
for (int i = 0; i <= y; i++) {
if ((i +1) * x > y) {
res = i;
break;
}
}
return res;
}
//17
unsigned int prim(unsigned int x) {
unsigned int res = 0;
if (x <= 1)
res = 1;
else {
for (int t = 2; t <x; t++) {
res += alpha(ycanx(x, t));
}
res = alpha(alpha(res));
}
return res;
}
//18
unsigned int p(unsigned int x) {
unsigned int res = 0;
int i = -1,j = 1;//第0个素数是2
while (i != x ) {
j++;
if (prim(j) == 0) {
i++;
}
}
res = j;
return res;
}
//19
unsigned int R(unsigned int x, unsigned int y) {
unsigned int res;
res = (x%y);
return res;
}
//20
unsigned int t(unsigned int x) {
unsigned int res = 0,i = 0,count = 0;
while (x !=1) {//x一直对素数相除
if (x % p(i) == 0) {//如果可以被这个素数整除
while (x % p(i) == 0) {//就一直整除下去
x = x / p(i);
}
}
else {
count++;//零指个数加一
}
i++;
}
res = i - count;
return res;
}
//21
unsigned int xi(unsigned int x, unsigned int i) {
unsigned int res = 0, j = 0, count = 0;
if (i >= 0) {
while (x != 1) {//x一直对素数相除
if (x % p(j) == 0) {//如果可以被这个素数整除
while (x % p(j) == 0) {//就一直整除下去
x = x / p(j);
if (j == i)
count++;
}
}
j++;
}
res = count;
}
else
res = 0;
return res;
}
//22
unsigned int Lt(unsigned int x) {
unsigned int res = 0, i = 0, count = 0;
while (x != 1) {//x一直对素数相除
if (x % p(i) == 0) {//如果可以被这个素数整除
while (x % p(i) == 0) {//就一直整除下去
x = x / p(i);
}
res = i + 1;
}
i++;
}
return res;
}
//23
bool GN(unsigned int x) {
bool res = true;
unsigned int i = 0;
while (x != 1) {//x一直对素数相除
if (x % p(i) == 0) {//如果可以被这个素数整除
while (x % p(i) == 0) {//就一直整除下去
x = x / p(i);
}
}
else
res = false;
i++;
}
return alpha(res);
}
//24
vector<unsigned int> an(unsigned int x) {
vector<unsigned int> res;
unsigned int i = 0;
while (x != 1) {//x一直对素数相除
unsigned int count = 0;
if (x % p(i) == 0) {//如果可以被这个素数整除
while (x % p(i) == 0) {//就一直整除下去
x = x / p(i);
count++;
}
}
res.push_back(count);
i++;
}
return res;
}
//25
vector<unsigned int> XGodelY(vector<unsigned int> X, vector<unsigned int> Y) {
vector<unsigned int> res;
vector <unsigned int>::iterator tmp;
for (tmp = Y.begin(); tmp != Y.end(); tmp++) {
X.push_back(*tmp);
}
//a.insert(a.end(), b, begin(), b.end());其实用insert更好
return res = X;
}
//26
unsigned int sharpax(unsigned int x, unsigned int a) {
unsigned int res = 0;
vector<unsigned int> anRes = an(x);
vector <unsigned int>::iterator tmp;
for (tmp = anRes.begin(); tmp != anRes.end(); tmp++) {
if (*tmp == a)
res++;
}
return res;
}
//27
unsigned int match(unsigned int x, unsigned int y) {
unsigned int res = 0;
return res = aCantor[x][y];
}
//28
unsigned int r(unsigned int z) {
unsigned int res = 0;
for (int i = 0; i < NUM; i++) {
for (int j = 0; j < NUM - i; j++) {
if (match(i, j) == z)
res = j;
}
}
return res;
}
//29
unsigned int l(unsigned int z) {
unsigned int res = 0;
for (int i = 0; i < NUM; i++) {
for (int j = 0; j < NUM - i; j++) {
if (match(i, j) == z)
res =i;
}
}
return res;
}
//30
unsigned int PROG(unsigned int x) {
unsigned int res = 0;//默认谓词为真
vector<unsigned int> rn= an(x);
vector<unsigned int> in;
vector<unsigned int> jn;
vector <unsigned int>::iterator tmp0;
vector <unsigned int>::iterator tmp1;
for (tmp0 = rn.begin(); tmp0 != rn.end(); tmp0++) {
for (int i = 0; i < NUM; i++) {
for (int j = 0; j < NUM - i; j++) {
if (aCantor[i][j] == *tmp0) {
if (j == 0)//如果出现j为0就一定不成立
{
return 1;
}
else {//没有的话就继续执行
in.push_back(i);
jn.push_back(j);
}
}
}
}
}
//已经得到了“二维”数组
for (tmp0 = in.begin(); tmp0 == in.end(); tmp0++) {
for (tmp1 = jn.begin(); tmp1 == jn.end(); tmp1++) {
//如果两者都不为0
if (in[*tmp0] != 0 && in[*tmp1] != 0) {
if (in[*tmp0] == in[*tmp1]) {//两者相等
return 1;
}
}
}
}
return res;
}
int main()
{
//初始数据(默认数据)
unsigned int x = 128, y = 3, i = 1, n = 7, Y = 7, z = 18;
unsigned int X[7] = { 1,1,1,1,1,1,1 };
int xTmp = 1;//27
int a = 2;//26
vector <unsigned int> anRes;//24
vector <unsigned int>::iterator tmp;
vector <unsigned int> xn, yn;//25
vector <unsigned int> XGodelYRes = XGodelY(xn, yn);//25
//cantor矩阵
int count = 0, count1 = 0;
int iac = 0, jac = 0;
for (count = 0; count < NUM; count++) {
for (jac = 0; jac <= count; jac++) {
iac = count - jac;
aCantor[iac][jac] = count1++;
//验证cantor矩阵的正确性
//cout << iac << " " << jac << endl;
//cout << aCantor[iac][jac] << endl;;
}
}
int choice1;
cout << "选择1、自行输入数据测试;2、直接看结果" << endl;
cin >> choice1;
int choice2;
int stopPro = 1;
if (choice1 == 1) {
while (stopPro == 1) {
cout << "请选择你要测试的程序(1-30)【如果要退出测试请按0】" << endl;
cin >> choice2;
switch (choice2) {
case 1:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "1. x + y = " << add(x, y) << endl;
break;
case 2:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "2. x * y = " << plusxy(x, y) << endl;
break;
case 3:
cout << "请输入x" << endl;
cin >> x;
cout << "3. x! = " << fac(x) << endl;
break;
case 4:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "4. pow(x,y) = " << powxy(x, y) << endl;
break;
case 5:
cout << "请输入x" << endl;
cin >> x;
cout << "5. p(x) = " << px(x) << endl;
break;
case 6:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "6. sub(x,y) = " << sub(x, y) << endl;
break;
case 7:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "7. abs(x,y) = " << absxy(x, y) << endl;
break;
case 8:
cout << "请输入x" << endl;
cin >> x;
cout << "8. alpha(x) = " << alpha(x) << endl;
break;
case 9:
cout << "本次测试中X[7] = { 1,1,1,1,1,1,1 }, Y = 7" << endl;
cout << "9. addf(x1,x2...xn,t(0-Y)) = " << addf(X, n, Y) << endl;
break;
case 10:
cout << "本次测试中X[7] = { 1,1,1,1,1,1,1 }, Y = 7" << endl;
cout << "10. plusf(x1,x2...xn,t(0-Y)) = " << plusf(X, n, Y) << endl;
break;
case 11:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "11. d(x,y) = " << d(x, y) << endl;
break;
case 12:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "12. x = y = " << xequaly(x, y) << endl;
break;
case 13:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "13. x > y = " << xbiggery(x, y) << endl;
break;
case 14:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "14. x <= y = " << xsmalequa(x, y) << endl;
break;
case 15:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "15. y | x = " << ycanx(x, y) << endl;
break;
case 16:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "16. y / x = " << ydivx(x, y) << endl;
break;
case 17:
cout << "请输入x" << endl;
cin >> x;
cout << "17. prim(x) =" << prim(x) << endl;
break;
case 18:
cout << "请输入x" << endl;
cin >> x;
cout << "18. p(x) = " << p(x) << endl;
break;
case 19:
cout << "请输入x, y" << endl;
cin >> x >> y;
cout << "19. R(x,y) = " << R(x, y) << endl;
break;
case 20:
cout << "请输入x" << endl;
cin >> x;
cout << "20. t(x) = " << t(x) << endl;
break;
case 21:
cout << "请输入x,i" << endl;
cin >> x >> i;
cout << "21. x(i) = " << xi(x, i - 1) << endl;
break;
case 22:
cout << "请输入x" << endl;
cin >> x;
cout << "22. Lt(x) = " << Lt(x) << endl;
break;
case 23:
cout << "请输入x" << endl;
cin >> x;
cout << "23. GN(x) = " << GN(x) << endl;
break;
case 24:
cout << "请输入x" << endl;
cin >> x;
cout << "24.an(x) = ";
anRes = an(x);
for (tmp = anRes.begin(); tmp != anRes.end(); tmp++) {
cout << *tmp << " ";
}
cout << endl;
break;
case 25:
cout << "规定X = {0,1,2,3,4},Y = {5,6,7,8,9}" << endl;
for (int i = 0; i < 5; i++) {
xn.push_back(i);
yn.push_back(i + 5);
}
cout << "25. XGodelY = ";
XGodelYRes = XGodelY(xn, yn);
for (tmp = XGodelYRes.begin(); tmp != XGodelYRes.end(); tmp++) {
cout << *tmp << " ";
}
cout << endl;
break;
case 26:
cout << "请输入a,X" << endl;
cin >> a >> x;
cout << "26. #(a,X) = " << sharpax(x, a) << endl;
break;
case 27:
cout << "请输入x,y(由于cantor矩阵在本程序中的限制,注意x和y的和不能超过NUM = 7)" << endl;
cin >> x >> y;
cout << "27. <X,Y>= " << match(x, y) << endl;
break;
case 28:
cout << "输入Z" << endl;
cin >> z;
cout << "28. r(Z)= " << r(z) << endl;
break;
case 29:
cout << "输入Z" << endl;
cin >> z;
cout << "29. l(Z)= " << l(z) << endl;
break;
case 30:
cout << "输入x" << endl;
cin >> x;
cout << "30. PROG(X)= " << PROG(x) << endl;
break;
default:
stopPro = 0;
break;
}
cout << endl;
}
}
else {
cout << "初始化的数据:" << endl;
cout << "x = " << x << endl << "y = " << y << endl << "n = " << n << endl << "z = " << z << endl;
cout << "Y = " << Y << endl;;
cout << "X : ";
for (int k = 0; k < Y; k++)
cout << " " << X[k];
cout << endl;
cout << "1. x + y = " << add(x, y) << endl;
cout << "2. x * y = " << plusxy(x, y) << endl;
cout << "3. x! = " << fac(x) << endl;
cout << "4. pow(x,y) = " << powxy(x, y) << endl;
cout << "5. p(x) = " << px(x) << endl;
cout << "6. sub(x,y) = " << sub(x, y) << endl;
cout << "7. abs(x,y) = " << absxy(x, y) << endl;
cout << "8. alpha(x) = " << alpha(x) << endl;
cout << "9. addf(x1,x2...xn,t(0-Y)) = " << addf(X, n, Y) << endl;
cout << "10. plusf(x1,x2...xn,t(0-Y)) = " << plusf(X, n, Y) << endl;
cout << "11. d(x,y) = " << d(x, y) << endl;
cout << "12. x = y = " << xequaly(x, y) << endl;
cout << "13. x > y = " << xbiggery(x, y) << endl;
cout << "14. x <= y = " << xsmalequa(x, y) << endl;
cout << "15. y | x = " << ycanx(x, y) << endl;
cout << "16. y / x = " << ydivx(x, y) << endl;
cout << "17. prim(x) =" << prim(x) << endl;
cout << "18. p(x) = " << p(x) << endl;
cout << "19. R(x,y) = " << R(x, y) << endl;
cout << "20. t(x) = " << t(x) << endl;
cout << "21. x(i) = " << xi(x, i - 1) << endl;
cout << "22. Lt(x) = " << Lt(x) << endl;
cout << "23. GN(x) = " << GN(x) << endl;
cout << "24.an(x) = ";
anRes = an(x);
for (tmp = anRes.begin(); tmp != anRes.end(); tmp++) {
cout << *tmp << " ";
}
cout << endl;
for (int i = 0; i < 5; i++) {
xn.push_back(i);
yn.push_back(i + 5);
}
cout << "25. XGodelY = ";
XGodelYRes = XGodelY(xn, yn);
for (tmp = XGodelYRes.begin(); tmp != XGodelYRes.end(); tmp++) {
cout << *tmp << " ";
}
cout << endl;
cout << "26. #(a,X) = " << sharpax(x, a) << endl;
cout << "27. <X,Y>= " << match(xTmp, y) << endl;//注意xTmp和y的和不能超过NUM
cout << "28. r(Z)= " << r(z) << endl;
cout << "29. l(Z)= " << l(z) << endl;
cout << "30. PROG(X)= " << PROG(x) << endl;
}
getchar();
return 0;
}
网友评论