求大神帮忙解决这道C语言题,用Vc6.0.不要用太难的语言,本人现阶段大一、

编写一程序P719.C实现以下功能从键盘上输入5个字符串(约定:每个字符串中字符数≤80字节),对其进行升序排序并输出。编程可用素材:printf("Input5stri... 编写一程序P719.C实现以下功能  从键盘上输入5个字符串(约定:每个字符串中字符数≤80字节),对其进行升序排序并输出。编程可用素材:printf("Input 5 strings:\n")、printf("---------------------------\n")。  程序的运行效果应类似地如图1所示,图1中的红色部分是从键盘输入的内容。 展开
 我来答
changwazipipi9
2013-12-17 · 超过12用户采纳过TA的回答
知道答主
回答量:41
采纳率:0%
帮助的人:23.3万
展开全部
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define LIM 5
#define SIZE 32
int read(char (*p)[SIZE],int);
void d_m(void); /*display menu*/
void output(char (*p)[SIZE],int);
void s_a(char (*p)[SIZE],int); /*sorted by ASCII*/
void s_l(char (*p)[SIZE],int); /*sorted by length*/
void s_l_f(char (*p)[SIZE],int); /*sorted by length of first word*/
int main(void)
{
char array[LIM][SIZE];
int count;
char ch;
int n;

n=read(array,LIM);
while(1)
{
if(n<LIM)
break;
d_m();
ch=getchar();
while(getchar()!='\n')
continue;
switch(ch)
{
case 'a':
output(array,LIM);
break;
case 'b':
s_a(array,LIM);
break;
case 'c':
s_l(array,LIM);
break;
case 'd':
s_l_f(array,LIM);
break;
case 'q':
goto bye;
}
}
bye:puts("Bye!");
system("pause");
return 0;
}

int read(char (*p)[SIZE],int n)
{
int count=0;
int size;

puts("Please input up to 5 lines.");
puts("I will handle them.");
puts("Ctrl+z to quit.");
while(count<n&&fgets(p[count],SIZE,stdin)!=NULL)
{
size=strlen(p[count]);
if(p[count][size-1]=='\n')
{
p[count][size-1]='\0';
}
else
{
p[count][size-1]='\0';
while(getchar()!='\n')
continue;
}
count++;
}
return count;
}

void d_m(void)
{
puts("Please select:");
puts("a.output strings according to the original order.");
puts("b.output strings sorted by ASCII.");
puts("c.output strings sorted by length.");
puts("d.output strings sorted by length of first word.");
puts("q.quit.");
}

void output(char (*p)[SIZE],int n)
{
int count;

for(count=0;count<n;count++)
puts(p[count]);
}

void s_a(char (*p)[SIZE],int n)
{
int top,seek;
char *temp;
char *p1[LIM];

for(top=0;top<n;top++)
p1[top]=p[top];
for(top=0;top<n-1;top++)
for(seek=top+1;seek<n;seek++)
if(strcmp(p1[top],p1[seek])>0)
{
temp=p1[top];
p1[top]=p1[seek];
p1[seek]=temp;
}
for(top=0;top<LIM;top++)
puts(p1[top]);
}

void s_l(char (*p)[SIZE],int n)
{
int top,seek;
char *temp;
char *p1[LIM];

for(top=0;top<n;top++)
p1[top]=p[top];
for(top=0;top<n-1;top++)
for(seek=top+1;seek<n;seek++)
if(strlen(p1[top])<strlen(p1[seek]))
{
temp=p1[top];
p1[top]=p1[seek];
p1[seek]=temp;
}
for(top=0;top<LIM;top++)
puts(p1[top]);
}

void s_l_f(char (*p)[SIZE],int n)
{
int top,seek;
int array[n];
char *temp;
char *p2;
char *p1[LIM];
int temp1;

for(top=0;top<n;top++)
{
p1[top]=p[top];
array[top]=0;
}
for(top=0;top<n;top++)
for(seek=0;seek<strlen(p1[top]);seek++)
{
if(isspace(p1[top][seek])||ispunct(p1[top][seek])||p1[top][seek]=='\0')
continue;
else
{
p2=&(p1[top][seek]);
while((isspace(*p2)==0)&&(ispunct(*p2)==0)&&(*p2!='\0'))
{
p2++;
array[top]++;
}
break;
}
}
for(top=0;top<n-1;top++)
for(seek=top+1;seek<n;seek++)
if(array[top]<array[seek])
{
temp=p1[top];
p1[top]=p1[seek];
p1[seek]=temp;

temp1=array[top];
array[top]=array[seek];
array[seek]=temp1;
}
for(top=0;top<n;top++)
{
puts(p1[top]);
}
}
030910304
2013-12-17 · TA获得超过146个赞
知道小有建树答主
回答量:75
采纳率:0%
帮助的人:115万
展开全部
#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main( void )
{
char input[81];
char *str[5];
char *p;
int i, j;

printf("Input 5 strings:\n");
 
for( i = 0; i < 5; i++ ){
gets( input );
str[i] =(char *)malloc( strlen(input)+1);
strcpy( str[i], input );
}

for( i = 0; i < 4; i++ )
for( j = i + 1; j < 5; j++ )
if( strcmp( str[i], str[j] ) > 0 ){
p = str[i];
str[i] = str[j];
str[j] = p;
}

for( i = 0; i < 5; i++ ){
printf( "%s\n", str[i] );
free( str[i]);
}

return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
evilstar2008
2013-12-17
知道答主
回答量:21
采纳率:0%
帮助的人:12.3万
展开全部
#include <stdio.h>
#include <string.h>
int main()
{
char a[4][80];
printf("Input 5 strings:\n");
// 输入5个字符串
for(int i=0;i<5;i++)
{
gets(a[i]);
}
printf("---------------------------\n");
// 排序
for(int i=0;i<4;i++)
{
for(int j=i+1;j<5;j++)
{
// 如果 i 串 大于 j 串,则交换
if( strcmp(a[i],a[j]) > 0)
{
char temp[80];
strncpy(temp, a[i],80);
temp[strlen(a[i])]='\0';
strncpy(a[i], a[j],80);
a[i][strlen(a[j])]='\0';
strncpy(a[j], temp,80);
a[j][strlen(temp)]='\0';
}
}
}
// 输出
for(int i=0;i<5;i++)
{
printf("%s\n",a[i]);
}
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
与子如初见
2013-12-17 · TA获得超过1638个赞
知道小有建树答主
回答量:1117
采纳率:100%
帮助的人:779万
展开全部
来 看看
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 80
void test(void)
{
char *p[5],*q;
int i = 0,j = 0;
printf("input 5 strings:\n");
for(i = 0 ; i <5; i++)
{
p[i] = (char *)malloc(sizeof(char)*N);
if(!p[i])
{
printf("p[%d]apply error\n",i);
exit(-1);
}
memset(p[i], '\0', N);
gets(p[i]);
}
for(i = 0 ; i < 5-1; i++)
{
for(j = 0;j < 5-1-i;j++)
if(strcmp(p[j],p[j+1]) > 0)
{
q = p[j];
p[j] = p[j+1];
p[j+1] = q;
}
}
printf("---------------------------\n");
for(i= 0 ;i < 5; i++)
{
puts(p[i]);
free(p[i]);
p[i] = NULL;
}
}
int main(void)
{
test();
return 0;
}
已通过运行,放心使用。欢迎采纳,欢迎追问。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
827729956xxj
2013-12-17
知道答主
回答量:8
采纳率:0%
帮助的人:5.3万
展开全部
# include<stdio.h>
# include<string.h>
void main()
{char a[5][100],c[100];
int i,d;
printf("Input 5 strings:\n");
for(i=0;i<5;i++)
gets(a[i]);
printf("-------------------\n");
for(i=0;i<5;i++)
for(d=i;d<4;d++)
{if(strcmp(a[d],a[d+1])==1)
{strcpy(c,a[d]);strcpy(a[d],a[d+1]);strcpy(a[d+1],c);}
}
for(i=0;i<5;i++)
puts(a[i]);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式