求助下这个单词接龙的程序用C语言怎么写

Python016

求助下这个单词接龙的程序用C语言怎么写,第1张

int main()

{

    char first[20] = {0}

    char second[20] = {0}

    char key[20] = {0}

    //input

    printf("please input first word:")

    scanf("%s", first)

    printf("please input second word:")

    scanf("%s", second)

    //begin

    bool found = false

    for (int i = 0 i < 20 i ++)

    {

        char tmp = first[i]

        if (tmp == second[0])

        {

            found = true

        }

        if (found == true)

        {

            int len = 0

            int l = i

            int j = 0

            for ( l < 20, j < 20 l ++, j ++)

            {

                if (first[l] != 0)

                {

                    key[j] = first[l]

                    len ++

                }

                else

                    break

            }

            for (j = 0 j < len j ++)

            {

                if (key[j] != second[j])

                {

                    found = false

                    memset(key, 0x00, sizeof(key)/sizeof(key[0]))

                }

            }

            if (found)

            {

                printf("the max keyword is: %s \n", key)

                break

            }

            else

                printf("can not found the key word!! \n")

        }

    }

    return 0

}

//囧,代码写好回来一看居然已经有推荐答案了.....

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <stdlib.h>

#include <windows.h>

#define MAX 80

char buff[MAX+1]

char first[4]

char last

bool spellCheck()

{

int p = 0

while(buff[p] != 0)

{

if(buff[p] >'z'||buff[p] <'a')

return false

p++

}

return true

}

bool syntaxCheck()

{

int len = strlen(first)

if(len == 3)

{

if(strncmp(first,buff,3) == 0)

return true

else if(strncmp(&first[1],buff,2) == 0)

return true

else if(strncmp(&first[2],buff,1) == 0)

return true

}

else if(len == 2)

{

if(strncmp(&first[0],buff,2) == 0)

return true

else if(strncmp(&first[1],buff,1) == 0)

return true

}

else if(len == 1)

{

if(strncmp(&first[0],buff,1) == 0)

return true

}

return false

}

void getWord()

{

do

{

printf("请输入一个全小写单词,和%s匹配\n",first)

scanf("%s",buff)

if(!spellCheck())

{

printf("拼写错误,请输入一个全小写的单词......\n")

continue

}

if(strlen(buff) == 0)

{

printf("请输入一个单词......\n")

continue

}

return

}while(true)

}

void start()

{

FILE *fp = fopen("D:\\1.txt","w")//<--------这里改输出文件的路径

int len

first[0] = rand()%26+'a'

first[1] = 0

last = rand()%26+'a'

printf("a1:%c a2:%c\n",first[0],last)

while(true)

{

getWord()

if(!syntaxCheck())

{

printf("输入的单词不符合要求,请重新输入......\n")

continue

}

else

{

fprintf(fp,"%s\n",buff)

len = strlen(buff)

if(buff[len-1] == last)

{

printf("%s末尾和a2:%c匹配,所以游戏结束\n",buff,last)

break

}

else

{

if(len >= 3)

strcpy(first,&buff[len-3])

else

strcpy(first,buff)

printf("匹配成功,请继续输入......\n")

}

}

}

fclose(fp)

}

int main()

{

srand(GetTickCount())

start()

system("pause")

}