69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#define LIM 10
#define SIZE 81
void output(char * strings[], int num);
void asc_output(char * strings[], int num);
void len_output(char * strings[], int num);
void word_len_output(char * strings[], int num);
int first_word_len(char * strings);
char * s_gets(char * st, int n);
int main(void)
{
int ct;
char input[LIM][SIZE];
char * ptstr[LIM];
char ch;
statement:while(1)
{
printf("Options:\n");
printf("a)original strings; b)ASCII;\n");
printf("c)lengths of lines; d)length of first word;\n");
printf("q)quit.\n");
while((ch=getchar())!='q')
{
ct=0;
printf("Input up to %d lines.\n",LIM);
while( ct<LIM&&s_gets(input[ct],SIZE)!=NULL&&input[ct][0]!=EOF)
{
ptstr[ct]=input[ct];
ct++;
}
switch (ch)
{
case 'a' :output(ptstr,ct); goto statement;
case 'b' :asc_output(ptstr,ct); output(ptstr,ct);goto statement;
case 'c' :len_output(ptstr,ct); output(ptstr,ct);goto statement;
case 'd' :word_len_output(ptstr,ct);output(ptstr,ct); goto statement;
default : printf("only a,b,c or d is available, please try again!\n");
continue;
}
}
return 0;
}
}
void output(char * strings[], int num)
{
int i;
for(i=0;i<num;i++)
{
fputs(strings[i],stdout);
printf("\n");
}
return;
}
void asc_output(char * strings[], int num)
{
char * temp;
int i;
int top,seek;
for(top=0;top<num-1;top++)
for(seek=top+1;seek<num;seek++)
if(strcmp(strings[top],strings[seek])>0)
{
temp=strings[top];
strings[top]=strings[seek];
strings[seek]=temp;
}
}
void len_output(char * strings[], int num)
{
char * temp;
int top,seek;
for(top=0;top<num-1;top++)
for(seek=top+1;seek<num;seek++)
if(strlen(strings[top])>strlen(strings[seek]))
{
temp=strings[top];
strings[top]=strings[seek];
strings[seek]=temp;
}
}
void word_len_output(char * strings[], int num)
{
char * temp;
int top,seek;
for(top=0;top<num-1;top++)
for(seek=top+1;seek<num;seek++)
if(first_word_len(strings[top])>first_word_len(strings[seek]))
{
temp=strings[top];
strings[top]=strings[seek];
strings[seek]=temp;
}
}
int first_word_len(char * strings)
{
int i=0,j=0;
for(i=0;isblank(strings[i]);i++)
{}
for(j=i;isalpha(strings[j]);j++)
{}
return j-i;
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i=0;
ret_val=fgets(st,n,stdin);
if(ret_val)
{
while (st[i]!='\n'&&st[i]!='\0')
i++;
if(st[i]=='\n')
st[i]='\0';
else
while( getchar()!='\n')
continue;
}
}
#include <stdio.h>
#include <string.h>
#define LIM 10
#define SIZE 81
void output(char * strings[], int num);
void asc_output(char * strings[], int num);
void len_output(char * strings[], int num);
void word_len_output(char * strings[], int num);
int first_word_len(char * strings);
char * s_gets(char * st, int n);
int main(void)
{
int ct;
char input[LIM][SIZE];
char * ptstr[LIM];
char ch;
while(1)
{
printf("Options:\n");
printf("a)original strings; b)ASCII;\n");
printf("c)lengths of lines; d)length of first word;\n");
printf("q)quit.\n");
bool flag = true;
while(flag && (ch=getchar())!='q')
{
ct=0;
printf("Input up to %d lines.\n",LIM);
while( ct<LIM&&s_gets(input[ct],SIZE)!=NULL&&input[ct][0]!=EOF)
{
ptstr[ct]=input[ct];
ct++;
}
switch (ch)
{
case 'a' :output(ptstr,ct); flag = false; break;
case 'b' :asc_output(ptstr,ct); output(ptstr,ct); flag = false; break;
case 'c' :len_output(ptstr,ct); output(ptstr,ct); flag = false; break;
case 'd' :word_len_output(ptstr,ct);output(ptstr,ct); flag = false; break;
default : printf("only a,b,c or d is available, please try again!\n");
continue;
}
}
return 0;
}
}
void output(char * strings[], int num)
{
int i;
for(i=0;i<num;i++)
{
fputs(strings[i],stdout);
printf("\n");
}
return;
}
void asc_output(char * strings[], int num)
{
char * temp;
int i;
int top,seek;
for(top=0;top<num-1;top++)
for(seek=top+1;seek<num;seek++)
if(strcmp(strings[top],strings[seek])>0)
{
temp=strings[top];
strings[top]=strings[seek];
strings[seek]=temp;
}
}