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

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

废话不多说,先上代码

#include 
#include 
//转换大小写
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;
}

你可能感兴趣的:(c语言,c++)