C语言实战项目(AI代码)

是的,你没有听错,用C语言编写一个简单的AI代码,功能十分简单,仅供娱乐,重要的是其中有几个实用性较强的自定义函数

废话不多说,先上代码

#include <stdio.h>
#include <string.h>
//转换大小写
void RemoveLetter(char* str);
//删除多余空格
void RemoveSpace(char* str);
//疑问词转换
void TurnInterrogativewords(char* str);
//问号转成感叹号
void TurnSymbol(char* str);
//第一人称改第二人称
void TurnFirstperson(char* str);
 
void RemoveLetter(char* str)
{
    for (int i = 0; str[i] != '\0'; i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z' && str[i] != 'I')
        {
            str[i] += 32;
        }
    }
}
void RemoveSpace(char* str)
{
    char* p = str;
    int i = 0;
    while (*p)
    {
        if (str[0] == ' ')
            p++;
        if ((*(p + 1) < 'a' || *(p + 1) > 'z') && (*(p + 1) < 'A' || *(p + 1) > 'Z') && (*(p + 1) < '0' || *(p + 1) > '9'))
        {
            if (*p == ' ')
                p++;
        }
        if (*(p + 1) == '\0' && *p == ' ')
            break;
        str[i++] = *p;
        if (*p == ' ')
        {
            int count = 0;
            int j = 0;
            for (j = 0;; j++)
            {
                if (*(p + j) == ' ')
                    count++;
                else break;
            }
            p += count - 1;
        }
        p++;
    }
    str[i] = '\0';
}
void TurnInterrogativewords(char* str)
{
    char* p = str;
    int k = 0;
    while (*p)
    {
        str[k++] = *p;
        if ((*p == 'c' || *p == 'C') && (*(p - 1) == ' ' || (*(p - 1) < 'a' || *(p - 1) > 'z') && (*(p - 1) < 'A' || *(p - 1) > 'Z') && (*(p - 1) < '0' || *(p - 1) > '9')))
        {
            if (*(p + 1) == 'a' && *(p + 2) == 'n' && *(p + 3) == ' ' && *(p + 4) == 'y' && *(p + 5) == 'o' && *(p + 6) == 'u' && (*(p + 7) == ' ' || (*(p + 7) < 'a' || *(p + 7) > 'z') && (*(p + 7) < 'A' || *(p + 7) > 'Z') && (*(p + 7) < '0' || *(p + 7) > '9')))
            {
                char ch1[] = "I can  ";
                int i = 0;
                for (i = 0; ch1[i] != '\0'; i++)
                {
                    *(p + i) = ch1[i];
                }
            }
            if (*(p + 1) == 'o' && *(p + 2) == 'u' && *(p + 3) == 'l' && *(p + 4) == 'd' && *(p + 5) == ' ' && *(p + 6) == 'y' && *(p + 7) == 'o' && *(p + 8) == 'u' && (*(p + 9) == ' ' || (*(p + 9) < 'a' || *(p + 9) > 'z') && (*(p + 9) < 'A' || *(p + 9) > 'Z') && (*(p + 9) < '0' || *(p + 9) > '9')))
            {
                char ch2[] = "I could  ";
                int i = 0;
                for (i = 0; ch2[i] != '\0'; i++)
                {
                    *(p + i) = ch2[i];
                }
            }
        }
        p++;
    }
    str[k] = '\0';
}
void TurnSymbol(char* str)
{
    char* p = str;
    int i = 0;
    while (*p)
    {
        if (*p == '?')
            *p = '!';
        str[i++] = *p;
        p++;
    }
    str[i] = '\0';
}
void TurnFirstperson(char* str)
{
    char* p = str;
    int j = 0;
    while (*p)
    {
        char ch[] = "you";
        if (*p == 'I' && ((*(p + 1) < 'A' || *(p + 1) > 'Z') && (*(p + 1) < 'a' || *(p + 1) > 'z') || *(p + 1) == ' '))
        {
            char str1[1000];
            for (int i = 0; ; i++)
            {
                str1[i] = *(p + 1 + i);
                if (*(p + 1 + i) == '\0')
                    break;
            }
            for (int i = 0; ch[i] != '\0'; i++)
            {
                *(p + i) = ch[i];
            }
            for (int i = 0; ; i++)
            {
                *(p + 3 + i) = str1[i];
                if (str1[i] == '\0')
                    break;
            }
        }
        if (*p == 'm' && *(p + 1) == 'e' && ((*(p + 2) < 'A' || *(p + 2) > 'Z') && (*(p + 2) < 'a' || *(p + 2) > 'z') && (*(p - 1) < 'A' || *(p - 1) > 'Z') && (*(p - 1) < 'a' || *(p - 1) > 'z') || (*(p + 2) == ' ' && *(p - 1) == ' ')))
        {
            char str2[1000];
            for (int i = 0; ; i++)
            {
                str2[i] = *(p + 2 + i);
                if (*(p + 1 + i) == '\0')
                    break;
            }
            for (int i = 0; ch[i] != '\0'; i++)
            {
                *(p + i) = ch[i];
            }
            for (int i = 0; ; i++)
            {
                *(p + 3 + i) = str2[i];
                if (str2[i] == '\0')
                    break;
            }
        }
        str[j++] = *p;
        p++;
    }
    str[j] = '\0';
}
int main()
{
    char chat[1000];
    char str[1000];
    getchar();
    while (1)
    {
        gets(chat);
        strcpy(str, chat);
        printf("%s\n", str);
        RemoveSpace(str);
        TurnFirstperson(str);
        TurnInterrogativewords(str);
        TurnSymbol(str);
        RemoveSpace(str);
        RemoveLetter(str);
        printf("AI: %s\n", str);
    }
    return 0;
}

 

 

  • 65
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枕眠秋雨>o<

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值