|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目是:从键盘上输入3个字符串(每个串不超过8个字符),将这些字符串按从小到大的次序排列出来 并输出排序后的结果
我自己写的代码里:
函数t作用是统计字符串中的字符个数
函数h(*x,*y)作用是当t(x)>t(y)时,用指针交换x和y的地址------这里不知我的书写对不对,很有可能是这里出错了。
我的代码:
- #include<stdio.h>
- #define N 8
- int t(char a[]) //统计一个字符串中的字符个数
- {
- int i=0,b=0;
- while(a[i]!='\0')
- {
- b++;
- i++;
- }
- return b;
- }
- void h(*x,*y)
- {
- if(t(x)>t(y))
- {
- int temp=*x;
- *x=*y;
- *y=temp;
- }
- }
- void main()
- {
- int i,m,n
- char a1[N],a2[N],a3[N]; //对三个数组初始化
- for(m=1;m<=3;m++)
- {
- for(i=0;i<n;i++)
- {
- scanf("%c",&am[i]);
- }
- }
- h(a1,a2);
- h(a2,a3);
- h(a1,a2);
- for(m=1;m<=3;m++)
- {
- for(i=0;i<N;i++)
- {
- printf("%c",am[i]);
- }
- printf("\n");
- }
- }
复制代码
把你的代码改完了。参考下: - #include <stdio.h>
- #define NUM 9 //最大为8个数的字符串,多申请1个用来存放'\0'
- void getInput(char d[]);
- int lenstr(char *p1);
- void switcharr(char **p1, char **p2, char **p3);
- void showStr(char *p1, char *p2, char *p3);
- void getInput(char d[])
- {
- int i=0;
- char ch;
- printf("请输入字符串:");
- while((ch=getchar()) != '\n')
- {
- if(i<NUM-1) //获取不超过NUM数量的字符串
- {
- d[i] = ch;
- d[i+1]='\0';
- }
- i++;
- }
- }
- int lenstr(char *p)
- {
- int i=0, count=0;
- while(p[i] != '\0')
- {
- i++;
- count++;
- }
- return count;
- }
- void switcharr(char **p1, char **p2, char **p3)
- {
- char *temp;
- if(lenstr(*p1) < lenstr(*p2))
- {
- temp=*p1;
- *p1=*p2;
- *p2=temp;
- }
- if(lenstr(*p1) < lenstr(*p3))
- {
- temp=*p1;
- *p1=*p3;
- *p3=temp;
- }
- if(lenstr(*p2) < lenstr(*p3))
- {
- temp=*p2;
- *p2=*p3;
- *p3=temp;
- }
-
- }
- void showStr(char *p1, char *p2, char *p3)
- {
- printf("%s\n", p1);
- printf("%s\n", p2);
- printf("%s\n", p3);
- }
- int main(void)
- {
- char a[NUM], b[NUM], c[NUM];
- char *p1, *p2, *p3;
- p1=a;
- p2=b;
- p3=c;
- getInput(a);
- getInput(b);
- getInput(c);
- switcharr(&p1, &p2, &p3);
- showStr(p1, p2, p3);
- return 0;
- }
复制代码
|
|