,武汉加油!三个数比较大小,求大佬看看,是不是电脑坏了,程序是书本的
#include<stdio.h>void main()
{
void exchange(int *q1, int *q2, int *q3);
int a, b, c, *p1, *p2, *p3;
scanf_s("%d,%d,%d", &a, &b, &c);
p1 = &a;
p2 = &b;
p3 = &c;
exchange(p1, p2, p3);
printf("%d,%d,%d\n", a, b, c);
}
void exchange(int *q1, int *q2, int *q3)
{
void swap(int *pt1, int *pt2);
if (*q1 < *q2)
{
swap(q1, q2);
}
if (*q1 < *q3)
{
swap(q1, q3);
}
if (*q2 < *q3)
{
swap(q2, q3);
}
}
void swap(int *pt1, int *pt2)
{
int temp;
temp = *pt1;
*pt1 = *pt2;
*pt2 = temp;
}
输入的8,7,6
显示结果8,-858993460,-858993460
我以为vc坏了,下载stdioa也是这样的结果,我的台式机没问题啊,程序比较出来是正确的
你输入的时候 逗号是中文的逗号吧,代码里面用的是英文的逗号,格式的问题? 你输入8,7,6试试 scanf("%d,%d,%d", &a, &b, &c);你改成 scanf("%d %d %d", &a, &b, &c);测试下就没问题了 本帖最后由 love_qj 于 2020-1-27 15:30 编辑
个人觉得最后的交换应该用空指针来,temp是整型变量,而pt1交换的是地址,有可能超过了整型长度,导致乱码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void swap(int *pt1, int *pt2)
{
int temp;
temp = *pt1;
*pt1 = *pt2;
*pt2 = temp;
}
void exchange(int *q1, int *q2, int *q3)
{
if (*q1 < *q2)
{
swap(q1, q2);
}
else if (*q1 < *q3)
{
swap(q1, q3);
}
else if (*q2 < *q3)
{
swap(q2, q3);
}
}
void main()
{
int a, b, c, *p1, *p2, *p3;
printf("请输入三个数:\n");
scanf("%d%d%d", &a, &b, &c);
p1 = &a;
p2 = &b;
p3 = &c;
exchange(p1, p2, p3);
printf("%d,%d,%d\n", a, b, c);
system("pause");
}
运行正常,4楼解答是正解。scanf的格式建议百度一下,%d中间不要加逗号、空格这些
exchange函数中包含了swap函数声明,却没有定义,编译能通过吗?
页:
[1]