網站首頁 編程語言 正文
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"); }
結果如下
原文鏈接:https://blog.csdn.net/starmultiple/article/details/121845367
相關推薦
- 2023-03-20 C#如何判斷.Net?Framework版本是否滿足軟件運行需要的版本_C#教程
- 2022-05-20 如何保證緩存和數據庫的一致性?
- 2022-08-03 C++中TinyXML讀取xml文件用法詳解_C 語言
- 2021-12-01 騰訊云服務器搭建Jenkins詳細介紹_Linux
- 2022-04-11 Tomcat訪問不到web應用報錯ORA-01882: 未找到時區的解決方案
- 2022-08-22 Go語言學習之Switch語句的使用_Golang
- 2022-03-13 C語言打印各種圖案實例代碼_C 語言
- 2022-08-16 QT布局管理詳解QVBoxLayout與QHBoxLayout及QGridLayout的使用_C 語
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支