第十一周作业

作业头

这个作业属于那个课程 C语言程序设计II
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/homework/3201
我在这个课程的目标是 递归函数
这个作业在那个具体方面帮助我实现目标 熟练的运用递归函数
参考文献 c语言程序设计p221

基础作业

7-1 汉诺塔问题* (10 分)

汉诺塔是一个源于印度古老传说的益智玩具。据说大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘,大梵天命令僧侣把圆盘移到另一根柱子上,并且规定:在小圆盘上不能放大圆盘,每次只能移动一个圆盘。当所有圆盘都移到另一根柱子上时,世界就会毁灭。
请编写程序,输入汉诺塔圆片的数量,输出移动汉诺塔的步骤。
输入格式
圆盘数 起始柱 目的柱 过度柱
输出格式
移动汉诺塔的步骤
每行显示一步操作,具体格式为:
盘片号: 起始柱 -> 目的柱
其中盘片号从 1 开始由小到大顺序编号。
输入样例

3
a c b

输出样例

1: a -> c
2: a -> b
1: c -> b
3: a -> c
1: b -> a
2: b -> c
1: a -> c

1)实验代码

#include<stdio.h>
void hanoi(int n, char a, char b, char c);//递归函数
int main()
{
	int n;
	char a,b,c;

	//输入
	scanf("%d
", &n);
	scanf("%c %c %c", &a, &b, &c);

	hanoi(n, a,b,c);  //调用函数

	return 0;
}

void hanoi(int n, char a, char b, char c)
{
	if (n == 1)  //只剩一个盘子
	{
		printf("%d: %c -> %c
",n,a,b);  //从x号柱移动到y号柱
	}
	else
	{
		hanoi(n - 1, a, c, b);
		printf("%d: %c -> %c
", n, a, b);
		hanoi(n - 1, c, b, a);
	}
}

1)设计思路

1)调试中出现的问题及解决方案

1)运行结果截图

7-2 估值一亿的AI核心代码 (20 分)

以上图片来自新浪微博。
本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:

无论用户说什么,首先把对方说的话在一行中原样打印出来;
消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
把原文中所有大写英文字母变成小写,除了 I;
把原文中所有独立的 can you、could you 对应地换成 I can、I could—— 这里“独立”是指被空格或标点符号分隔开的单词;
把原文中所有独立的 I 和 me 换成 you;
把原文中所有的问号 ? 换成惊叹号 !;
在一行中输出替换后的句子作为 AI 的回答。

输入格式:
输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
输出格式:
按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。
输入样例:

6
Hello ?
 Good to chat   with you
can   you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

输出样例:

Hello ?
AI: hello!
 Good to chat   with you
AI: good to chat with you
can   you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know

1)实验代码

#include<stdio.h>
#include<string.h>
int main()
{
    char str[10][1001], str1[1001];
    int n, i;
    static int a, b, c,flag;
    scanf("%d
", &n);

    for(i=0;i<n;i++)
	{
        gets(str[i]);
    }

    for (i = 0; i < n; i++)
    {
        a = 0;
        b = 0;
        flag = 0;
        while (str[i][a] != '')
        {
            if (str[i][a] == ' ' && flag == 1)
            {
                str1[b] = ' ';
                b++;
                flag = 0;
            }

            if (str[i][a] != ' ')
            {
                if ((str1[b - 1] == ' ') && ((str[i][a]<'a' || str[i][a]>'z') && (str[i][a]<'A' || str[i][a]>'Z')) && ((str[i][a] < '0') || (str[i][a] > '9')))
                {
                    if (str[i][a] == '?')
                    {
                        str1[--b] = '!';
                        b++;
                        flag = 1;
                    }
                    else
                    {
                        str1[--b] = str[i][a];
                        b++;
                        flag = 1;
                    }
                }
                else
                {
                    if (str[i][a] <= 'Z' && str[i][a] >= 'A'&& str[i][a] != 'I')
                    {
                        str1[b] = str[i][a] - 'A' + 'a';
                        b++;
                        flag = 1;
                    }
                    else
                    {
                        if (str[i][a] == '?')
                        {
                            str1[b] = '!';
                            b++;
                            flag = 1;
                        }
                        else
                        {
                            str1[b] = str[i][a];
                            b++;
                            flag = 1;
                        }
                    }
                }
            }
            a++;
        }

        str1[b] = '';
        if (str1[b - 1] == ' ')
        {
            str1[b - 1] = '';
        }

        a = 0;
        while (str1[a] != '')
        {
            if (str1[a] == 'c' && str1[a + 1] == 'a' && str1[a + 2] == 'n' && str1[a + 3] == ' ' && str1[a + 4] == 'y' && str1[a + 5] == 'o' && str1[a + 6] == 'u')
            {
                if (((str1[a - 1]<'a' || str1[a - 1]>'z') && (str1[a - 1]<'A' || str1[a - 1]>'Z')) && ((str1[a + 7]<'a' || str1[a + 7]>'z') && (str1[a + 7]<'A' || str1[a + 7]>'Z')))
                {
                    str1[a + 0] = 'I';
                    str1[a + 1] = ' ';
                    str1[a + 2] = 'c';
                    str1[a + 3] = 'a';
                    str1[a + 4] = 'n';
                    str1[a + 5] = '8';
                    str1[a + 6] = '8';
                    a += 7;
                    continue;
                }
            }

            if (str1[a] == 'c' && str1[a + 1] == 'o' && str1[a + 2] == 'u' && str1[a + 3] == 'l' && str1[a + 4] == 'd' && str1[a + 5] == ' ' && str1[a + 6] == 'y' && str1[a + 7] == 'o' && str1[a + 8] == 'u')
            {
                if (((str1[a - 1]<'a' || str1[a - 1]>'z') && (str1[a - 1]<'A' || str1[a - 1]>'Z')) && ((str1[a + 9]<'a' || str1[a + 9]>'z') && (str1[a + 9]<'A' || str1[a + 9]>'Z')))
                {
                    str1[a + 0] = 'I';
                    str1[a + 1] = ' ';
                    str1[a + 2] = 'c';
                    str1[a + 3] = 'o';
                    str1[a + 4] = 'u';
                    str1[a + 5] = 'l';
                    str1[a + 6] = 'd';
                    str1[a + 7] = '8';
                    str1[a + 8] = '8';
                    a += 7;
                    continue;
                }
            }
            if (str1[a] == 'I' && ((str1[a - 1]<'a' || str1[a - 1]>'z') && (str1[a - 1]<'A' || str1[a - 1]>'Z')) && ((str1[a + 1]<'a' || str1[a + 1]>'z') && (str1[a + 1]<'A' || str1[a + 1]>'Z')))
            {
                str1[a] = '7';
            }
            if ((str1[a] == 'm' && str1[a + 1] == 'e') && ((str1[a - 1]<'a' || str1[a - 1]>'z') && (str1[a - 1]<'A' || str1[a - 1]>'Z')) && ((str1[a + 2]<'a' || str1[a + 2]>'z') && (str1[a + 2]<'A' || str1[a + 2]>'Z')))
            {
                str1[a] = '5';
                str1[a + 1] = '6';
                a++;
            }
            a++;
        }
        printf("%s
AI: ", str[i]);
        c = strlen(str1);
        for (int t = 0; t < c; t++)
        {
            if (str1[t] == '8')
                continue;
            else if (str1[t] == '5' && str1[t + 1] == '6')
            {
                printf("you");
                t++;
                continue;
            }
            else if(str1[t]=='7')
            {
                printf("you");
            }
            else
            {
                printf("%c", str1[t]);
            }
        }
        printf("
");
    }
    return 0;
}

1)设计思路

这份代码借鉴了别人的,现在还在琢磨中

1)调试中出现的问题及解决方案

1)运行结果截图

原文地址:https://www.cnblogs.com/xirfly/p/10847266.html