指针

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()可以避免因字符未填满定义的内存而出现的乱码现象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值