日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字典应用例子

發(fā)布時間:2024/1/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字典应用例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


#define MAXWORD 50
#define DICTSIZ 100


char dict[DICTSIZ][MAXWORD+1];


//字典中字母數
int nwords = 0;
int nextin(char* cmd, char* word);
int initw();
int insertw(const char* word);
int deletew(const char* word);
int lookupw(const char* word);


int main(int argc, char* argv[])
{
char word[MAXWORD+1];
char cmd;
int wrdlen;
while(1)
{
wrdlen = nextin(&cmd, word);
if (wrdlen<0)
{
exit(0);
}
switch(cmd)
{
case 'I':
initw();
printf("Dictionary initalized to empty\n");
break;


case 'i':
insertw(word);
printf("%s inserted\n", word);
break;


case 'd':
if (deletew(word))
{
printf("%s deleted \n", word);
}
else
{
printf("%s not found \n", word);
}
break;


case 'l':
if (lookupw(word))
{
printf("%s was found\n", word);
}
else
{
printf("%s was not found\n", word);
}
break;
case 'q':
printf("program quits\n");
exit(0);
default:
printf("command %c invalid\n", cmd);
break;
}
}
return 0;
}


int nextin(char* cmd, char* word)
{
int i, ch;
ch=getc(stdin);
while(isspace(ch))
{
ch=getc(stdin);
}
if(ch==EOF)
{
return -1;
}
*cmd=(char)ch;
ch=getc(stdin);
while(isspace(ch))
{
ch=getc(stdin);
}
if(ch==EOF)
{
return -1;
}
if ('\n'==ch)
return 0;


i=0;
while(!isspace(ch))
{
if (MAXWORD < ++i)
{
printf("error: word too long\n");
exit(1);
}
*word++ =ch;
ch=getc(stdin);
}
*word++=0;
return i;
}


int initw()
{
nwords=0;return 1;
}


int insertw(const char* word)
{
strcpy(dict[nwords], word);
nwords++;
return nwords;
}


int deletew(const char* word)
{
int i;
for(i=0;i<nwords;i++)
{
if (0==strcmp(word, dict[i]))
{
nwords--;
strcpy(dict[i], dict[nwords]);
return 1;
}
}
return 0;
}


int lookupw(const char* word)
{
int I;
for(I=0;nwords>I;I++)
{
if (0==strcmp(word, dict[I]))
{
return 1;
}
}
return 0;
}

總結

以上是生活随笔為你收集整理的字典应用例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。