测验题目:错题修改 #include<stdio.h> void Swap(int *x,int *y); int main(void){ float a,b; scanf("%d,%d",&a,&b);/*输入a和b的值*/ printf("Before swap:a=%d,b=%d\n",a,b);/*打印交换前a,b的值*/ Swap(a,b); /*调用函数Swap实现a值与b值得交换*/ printf("After swap:a=%d,b=%d\n",a,b);/*打印交换后的a,b*/ } /*函数功能:交换两个整型数的值*/ void Swap(int *x,int *y); { int temp; temp = *x; *x = *y; *y = temp; }
时间: 2023-08-07 16:08:28 浏览: 54
这段代码中有一些错误,请看下面的修改后的代码:
```
#include <stdio.h>
void Swap(float *x, float *y);
int main(void) {
float a, b;
scanf("%f,%f", &a, &b); /*输入a和b的值*/
printf("Before swap: a=%f, b=%f\n", a, b); /*打印交换前a,b的值*/
Swap(&a, &b); /*调用函数Swap实现a值与b值得交换*/
printf("After swap: a=%f, b=%f\n", a, b); /*打印交换后的a,b*/
return 0;
}
/*函数功能:交换两个浮点型数的值*/
void Swap(float *x, float *y) {
float temp;
temp = *x;
*x = *y;
*y = temp;
}
```
修改之后的代码做了以下几个修改:
1. `%d` 应该改成 `%f`,因为 `a` 和 `b` 声明为了 `float` 类型;
2. 在函数声明和定义中,形参类型应该改成 `float *`,因为需要传递浮点型变量的地址;
3. 在函数定义中,去掉了多余的 `;` 符号;
4. 在 `main()` 函数中添加了 `return 0;` 语句,表示程序正常结束。
这样修改之后,代码就可以正确地编译、运行了。