前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言实战项目(AI代码)

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

作者头像
一枕眠秋雨
发布2024-03-11 17:47:10
2800
发布2024-03-11 17:47:10
举报
文章被收录于专栏:司钰秘籍

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

代码语言:text
复制
 废话不多说,先上代码
 #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;
 }
 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档