30函数实现

计算理论导引作业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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355