c语言单词本的新增、删除、查询按顺序显示功能

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");
}

结果如下

到此这篇关于c语言单词本的新增,删除,查询,按顺序显示的文章就介绍到这了,更多相关c语言单词本内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C语言线性表的顺序表示与实现实例详解

    1.概述 通常来说顺序表是在计算机的内存中以数组的形式保存的线性表,是用一组地址连续的存储单元依次存储数据元素的线性数据结构.线性表采用顺序存储的方式存储就称之为顺序表.顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中. 将表中元素一个接一个的存入一组连续的存储单元中,这种存储结构就是顺序结构. 采用顺序存储结构的线性表简称为" 顺序表".顺序表的存储特点是:只要确定了起始位置,表中任一元素的地址都通过下列公式得到:LOC(ai)=LOC(a1)+(i-1)*L 1≤

  • c语言单词本的新增、删除、查询按顺序显示功能

    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()

  • layui table表格数据的新增,修改,删除,查询,双击获取行数据方式

    layui table利用参数新增,修改,删除,查询,双击数据行获取本行数据等.模块化使用layui table表格,通过二次封装实现语法上的解耦使用layui table,使用参数就可以以及外部的方法调用即可实现对layui table表格的操作,通过封装,把更多自主权交给页面自定义,layui:v:2.3.0.感谢layui的作者贤心. 演示效果如下: 以上json显示不完整是录屏的原因,请见谅! var Table = function(ops){ this.dataList = [];

  • C语言中建立和删除文件连接的相关函数讲解

    C语言link()函数:建立文件连接 头文件: #include <unistd.h> 定义函数: int link (const char * oldpath, const char * newpath); 函数说明:link()以参数newpath 指定的名称来建立一个新的连接(硬连接)到参数oldpath 所指定的已存在文件. 如果参数newpath 指定的名称为一已存在的文件则不会建立连接. 返回值:成功则返回0, 失败返回-1, 错误原因存于errno. 附加说明:link()所建立

  • Bootstrap table中toolbar新增条件查询及refresh参数使用方法

    我们想要在bootstrap-table中自定义查询条件如何实现呢?这些自定义的按钮.输入框是定义在哪个位置呢?还记得上一节中我们在配置中有这样一个属性: //工具按钮用哪个容器 toolbar: '#toolbar', <div id="toolbar"></div> 我们定义的查询条件就是放入到这个div中的,先看一下我们期望的效果: 要实现这样的效果,我们首先要新增查询表单: <div class="container">

  • Java反射 JavaBean对象自动生成插入,更新,删除,查询sql语句操作

    通过反射根据提供的表名.POJO类型.数据对象自动生成sql语句. 如名为 User 的JavaBean与名为 user 的数据库表对应,可以提供一个封装有数据的User对象user,根据user中含有的数据自动生成sql语句. 1.生成插入语句(插入user中包含的非空数据的语句): String insertSql = getInsertSql("user", User.class, user); 2.生成更新语句(user中id不能为空): String updateSql =

  • C语言数组添加和删除元素的实现

    数组不擅长插入(添加)和删除元素.数组的优点在于它是连续的,所以查找数据速度很快.但这也是它的一个缺点.正因为它是连续的,所以当插入一个元素时,插入点后所有的元素全部都要向后移:而删除一个元素时,删除点后所有的元素全部都要向前移. 插入算法 # include <stdio.h> int main(void) { int a[23] = {1, 5, 66, 8, 55, 9, 1, 32, 5, 65, 4, 8, 5, 15, 64, 156, 1564, 15, 1, 8, 9, 7,

  • R语言入门教程之删除指定数据的方法

    引言 在R学习中经常用到的是按着某种逻辑值提取数据集.本文来讲一下利用索引的手法删除数据集合. 数据准备 > Data 英雄 职业 熟练等级 使用频次 胜率 1 后裔 射手 5 856 0.64 2 孙尚香 射手 5 211 0.10 3 狄仁杰 射手 5 324 0.20 4 李元芳 射手 4 75 0.30 5 安琪拉 法师 5 2324 0.40 6 张良 法师 4 755 0.50 7 不知火舞 法师 4 644 0.60 8 貂蝉 法师 3 982 0.70 9 <NA> &l

  • C语言 详解如何删除有序数组中的重复项

    目录 删除有序数组中的重复项Ⅰ a.思路 b.图解 c.代码 d.思考 删除有序数组中的重复项Ⅱ a.思路 b.图解 c.代码 d.思考 删除有序数组中的重复项Ⅰ a.思路 定义变量 int dest=0,cur=1,nums[cur]与nums[dest]逐一比较. nums[cur]!=nums[dest],将nums[cur]放入dest下一个位置,更新dest. nums[cur]!=nums[dest],cur移动. cur==numsSize,结束.返回dest+1. b.图解 c.

  • go语言实现Elasticsearches批量修改查询及发送MQ操作示例

    目录 update_by_query批量修改 索引添加字段 查询es发送MQ update_by_query批量修改 POST post-v1_1-2021.02,post-v1_1-2021.03,post-v1_1-2021.04/_update_by_query { "query": { "bool": { "must": [ { "term": { "join_field": { "val

  • C语言编程简单却重要的数据结构顺序表全面讲解

    目录 前言 一.线性表定义 二.顺序表实现 1概念及结构 2静态顺序表 2.1实现顺序表接口,第一步要对顺序表进行初始化 2.2对顺序表的增删查改的接口函数(以尾插为例) 3动态顺序表 3.1动态顺序表初始化 3.2动态顺序表-尾插 3.3动态顺序表-头插 3.4动态顺序表-尾删 3.5动态顺序表-头删 3.6动态顺序表-任意位置插入数据 3.7动态顺序表-任意位置删除数据 结束 前言 本文主要介绍顺序表的定义和常见静态顺序表的用法. 一.线性表定义 线性表(line list)是n个具有相同特

随机推荐