Write a program using the given functions 用给定函数编写程序
Must use a structure type 必须使用结构类型
#include<stdio.h>
#include<stdlib.h>
#include<CONIO.H>
typedef struct String{
char string[100];
}SqList;
int count = 0; //统计数据长度
void Print(SqList *L)
{
for(int i=0;L->string[i]!='\0';i++){
printf("%2c",L->string[i]);
}
printf("\n");
}
void Creat_List ( SqList *L )
{
printf("Input a string:");
scanf( "%s",&L->string);
}//录入数据完成
//
void Count(SqList *L)
{
count = 0;
for(int i=0;L->string[i]!='\0';i++){
count++;
}
}
void LoopUp(SqList* L)
{
printf("Find a character :");
char ch;
int m=0;
int i=0;
Count(L);
getchar();
scanf( "%c",&ch);
for(i = 0;i<count;)
{
if(ch == L->string[i]) //如果找到
{
i++;
printf("%3c is in the list\n",ch);
break;
} else
{
i++;
m++; //m用来表示break的时候是否<=i
}
}
if(m==i)
{
printf("%3c is not in the list\n",ch);
}
}
int main ()
{
int k;
SqList L;
while( 1 ){
printf( "-------------------MENU-------------------\n" );
printf( "1.String to list \n" );
printf( "2.Show the list \n" );
printf( "3.LoopUp \n" );
printf( "4.Count \n" );
printf( "5.Exit \n" );
printf("Choose the item(1~5):");
scanf( "%d",&k );
switch( k )
{
case 1:
{
Creat_List(&L);
}break;
case 2:
{
printf("List:");
Print(&L);
}break;
case 3:
{
LoopUp(&L);
}break;
case 4:
{
Count(&L);
printf("String length : %d\n",count);
}break;
case 5:
exit(1);break;
default:printf("Error");
}
}
return 0;
}
运行结果:
-------------------MENU-------------------
1.String to list
2.Show the list
3.LoopUp
4.Count
5.Exit
Choose the item(1~5):1
Input a string:abcdefg
-------------------MENU-------------------
1.String to list
2.Show the list
3.LoopUp
4.Count
5.Exit
Choose the item(1~5):2
List: a b c d e f g
-------------------MENU-------------------
1.String to list
2.Show the list
3.LoopUp
4.Count
5.Exit
Choose the item(1~5):3
Find a character :a
a is in the list
-------------------MENU-------------------
1.String to list
2.Show the list
3.LoopUp
4.Count
5.Exit
Choose the item(1~5):3
Find a character :z
z is not in the list
-------------------MENU-------------------
1.String to list
2.Show the list
3.LoopUp
4.Count
5.Exit
Choose the item(1~5):4
String length : 7
-------------------MENU-------------------
1.String to list
2.Show the list
3.LoopUp
4.Count
5.Exit
Choose the item(1~5):5
Press any key to continue
创建一个list链表结构,之后就是对链表的遍历了
就是一个菜单的显示技术,自己google下有很多。
是这个意思,很久前写过一个操作二叉树的,可以向其中插入节点,然后可以不同方法遍历,可以转置,主要是用来排序的,明天找找看
可以定义一个结构体,用来作为存储字符串的节点,然后创建链表,各个函数的作用相当于向链表插入字符串,遍历打印链表,遍历字符串求长度,字符的匹配查找,封装成四个函数。输入1.2.3.4选择可以放在主函数中用一个switch语句实现。每个case语句下调用相应的函数即可。