| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 448 人关注过本帖
标题:[求助]指针数组
只看楼主 加入收藏
独角龙
Rank: 1
等 级:新手上路
帖 子:221
专家分:0
注 册:2006-5-5
收藏
 问题点数:0 回复次数:2 
[求助]指针数组

用指针数组存放多个单词。当输入一个单词,在内存中查找该单词,并确认是否删除或替换。

要求:程序能够根据用户的需求,完成只删除(替换)一个或删除(替换)多个或删除(替换)所有的被查找的数据,给出被删除(替换)的数据个数,如果没有查找到,则给出没有查找到信息。

#include <stdio.h>
#include <string.h>
#define N 80
#define M 20

void instruction(void); //界面介绍函数
void append(char *str[N]); //添加单词函数
void delete(char *str[N]); //删除单词函数
void displace(char *str[N]); //替换单词函数

int main(void)
{
int n;
char *str[N]; //定义大小为N的指针数组

while(1) //无限循环
{
instruction();
printf("Please input your choice:\n"); //输入操作选择
scanf("%d", &n);

switch(n)
{
case 1:
append(&str[N]); //添加单词
break;
case 2:
delete(&str[N]); //删除单词
break;
case 3:
displace(&str[N]); //替换单词
break;
case 0:
exit(0); //退出
}
}

return 0;
}

void instruction(void) //界面介绍
{
printf("1--添加单词\n");
printf("2--删除单词\n");
printf("3--替换单词\n");
printf("0--退出\n");
printf("\n\n");
}

void append(char *str[N]) //添加单词
{
int i = 0;


printf("Input the append word:");
scanf("%s", &str[i ++]);

}

void delete(char *str[N]) //删除单词
{
int i, k, n;
char key[M], *p, *q;

printf("Input the delete word:"); //输入要删除的单词
scanf("%s", key);

while (1)
{
if (strcmp(str[i], key) != 0) //未找到 k = 0
{
k = 0;
i ++;
continue;
}

else //找到 k = 1
{
k = 1;
break;
}
}

if (!k)
{
printf("The word is not found!\n");
}

else
{
printf("The word is found!\n");
printf("Are you sure delete this word?\n"); //确定是否要删除
printf("1----delete\n");
printf("2----not delete\n");
scanf("%d", &n);
}

if (n == 1)
{
p = str[i];

for (q = str[i]; ; q ++) //移动元素
{
*(q + 1) = *q;
}
}

else
exit(0);

}

void displace(char *str[N]) //替换单词
{
int i, k, n;
char key[M], *newstr;

printf("Input the displace word:"); //输入要替换的单词
scanf("%s", key);

for (i = 0; ; i ++)
{
if (strcmp(str[i], key) != 0) //未找到 k = 0
{
k = 0;
continue;
}

else //找到 k = 1
{
k = 1;
break;
}
}

if (!k)
{
printf("The word is not found!\n");
}

else
{
printf("The word is found!\n");
printf("Are you sure displace this word?\n"); //确定是否要替换
printf("1----displace\n");
printf("2----not displace\n");
scanf("%d", &n);
}

if (n == 1)
{
printf("Input new word:"); //输入新单词
scanf("%s", &newstr);

strcpy(str[i], newstr);
}

else
exit(0);
}

搜索更多相关主题的帖子: 指针 
2006-06-25 11:57
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-06-25 13:28
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
space, space, malloc space

日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-06-25 16:58
快速回复:[求助]指针数组
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017366 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved