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