c語言單詞本的新增、刪除、查詢按順序顯示功能
更新時(shí)間:2021年12月10日 14:10:12 作者:starmultiple
這篇文章主要介紹了c語言單詞本的新增、刪除、查詢按順序顯示功能,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
c語言單詞本的新增,刪除,查詢,按順序顯示
#include<stdio.h> #include<string.h> #define SIZE 100 int addword(char p[][20], int n); int findword(char p[][20], int n, char *f); int delword(char p[][20], int n, char *f); void display(char p[][20], int n); void menu(); int main() { char myword[100][20]; char word[20]; char choice; int count = 0; int pos = -1; do { menu(); printf("Please input your choice:"); scanf("%c", &choice); getchar(); switch (choice) { case '1': count = addword(myword, count); break; case '2': printf("Please input what you are looking for:"); gets(word); pos = findword(myword, count, word); if (pos != -1) printf("It's the %d word\n", pos + 1); break; case '3': printf("Please input what you want to delete:"); gets(word); count = delword(myword, count, word); break; case '4': display(myword, count); break; case '0':choice='0';break; default: printf("Error input,please input your choice again!\n"); } } while (choice); return 0; } void menu( ) { printf("----------1.增加單詞------------\n"); printf("----------2.查詢單詞------------\n"); printf("----------3.刪除單詞------------\n"); printf("----------4.顯示單詞------------\n"); printf("-------------0.退出-------------\n"); } int addword(char p[][20], int n) { int i, j; char pos = -1; char flag = 'y'; char tmp[20]; while (flag == 'y' || flag == 'Y') { if (n == SIZE) { printf("Word list is full\n"); break; } else { printf("Iput your word:"); gets(tmp); pos = findword(p, n, tmp); if (pos != -1) { printf("the word exits!\n"); break; } else { if (n) { for (i = 0;i < n && strcmp(tmp, p[i])>0;i++); for (j = n;j > i;j--) strcpy(p[j], p[j - 1]); strcpy(p[i], tmp); n++; } else { strcpy(p[0], tmp); n = 1; } } } printf("Another word?(y/n):"); scanf("%c", &flag); getchar(); } return n; } int findword(char p[][20], int n, char *f) { int i; int pos = -1; for (i = 0;i < n;i++) { if (!strcmp(p[i], f)) { pos = i; break; } } return pos; } int delword(char p[][20], int n, char *f) { int i; int pos = -1; pos = findword(p, n, f); if (pos == -1) printf("It'not in myword list!\n"); else { for (i = pos;i < n - 1;i++) { strcpy(p[i], p[i + 1]); } n = n - 1; } return n; } void display(char p[][20], int n) { int i; if (n) { for (i = 0;i < n;i++) puts(p[i]); } else printf("There is no word in myword list!\n"); }
結(jié)果如下
到此這篇關(guān)于c語言單詞本的新增,刪除,查詢,按順序顯示的文章就介紹到這了,更多相關(guān)c語言單詞本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
C++標(biāo)準(zhǔn)模板庫(kù)vector的常用操作
今天小編就為大家分享一篇關(guān)于C++標(biāo)準(zhǔn)模板庫(kù)vector的常用操作,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Matlab實(shí)現(xiàn)繪制立體玫瑰花的示例代碼
這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)繪制更立體的玫瑰花,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下2023-02-02