指针

来源:互联网 发布:免费数据库系统有哪些 编辑:程序博客网 时间:2024/05/20 20:03

2011-05-15 23:01:42

1.输入三个整数,按由小到大的顺序输出。

程序代码:

#include<stdio.h>
void swap(int *p1,int *p2);
int main(void)
{
 int a,b,c;

 printf("Please input three numbers:");
 scanf("%d%d%d",&a,&b,&c);
 if(a>b)
  swap(&a,&b);
 if(a>c)
  swap(&a,&c);
 if(b>c)
  swap(&b,&c);
 printf("the right sequence is:%d %d %d/n",a,b,c);
}
void swap(int *p1,int *p2)
{
 int temp;
 temp=*p1;
 *p1=*p2;
 *p2=temp;
}

2.输入3个字符串,按由小到大的顺序输出。

程序代码:

#include<string.h>
#include<stdio.h>
#define N 20
void swap(int *p1,int *p2);
int main(void)
{
 char *a[N],*b[N],*c[N];

 printf("Please input three charactices:/n");
 gets(a);
 gets(b);
 gets(c);
 if(strcmp(a,b)>0)
  swap(a,b);
 if(strcmp(a,c)>0)
  swap(a,c);
 if(strcmp(b,c)>0)
  swap(b,c);
 printf("the right order is:/n%s/n%s/n%s/n",a,b,c);
}
void swap(char *p1,char *p2)
{
 char *temp[N];
 strcpy(temp,p1);
 strcpy(p1,p2);
 strcpy(p2,temp);
}

输入函数gets()和scanf均可以实现从键盘输入,但是使用gets()可以避免因字符未填满定义的内存而出现的乱码现象。

原创粉丝点击