Write a program using the given functions 用给定函数编写程序
Must use a structure type 必须使用结构类型
c语言问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- MR_Radish666 2015-11-25 06:17关注
#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本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报