求一个C语言程序题:编写一个函数SWAP()实现交换两个数位置的功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求一个C语言程序题:编写一个函数SWAP()实现交换两个数位置的功能相关的知识,希望对你有一定的参考价值。

编写一个函数SWAP()实现交换两个数位置的功能
另外还要每一步的说明 解释一下每一步的意思

#include <stdio.h>
void swap(int *p, int *q);//用传地址的方法交换
void main()

int i, j;
scanf("%d%d", &i, &j);//从键盘输入两个交换的数
swap(&i, &j);//传入i,j的地址
printf("i = %d, j = %d\n", i, j);//输出交换后的两个值

void swap(int *p, int *q)

int temp;//定义临时变量 存放中间值
temp = *p;
*p = *q;
*q = temp;

注意:不能直接写void swap(int i, int j);
因为那只是临时变量,函数调用完后,便释放空间,两个值并没有交换。
参考技术A /*交换程序*/
#include <iostream>
using namespace std;
void swap(int &x,int &y);
int main()


int i=8,j=9;
swap(i,j);
cout <<"i=" <<i <<endl;
cout <<"j=" <<j <<endl;
return 0;

void swap(int &x,int &y)

int temp;
temp=x;
x=y;
y=temp;
cout<<"This swap is used!"<<endl; //判断该函数是否被调用

/*传引用调用,改变了实参的值,所以得以交换*/
参考技术B #include <iostream>
using namespace std;
void swap(int &x,int &y);函数声明
/*传引用(就是用&号的原因)调用,改变了实参的值,所以得以交换*/
int main()


int i=8,j=9;
swap(i,j); //函数调用
cout <<"i=" <<i <<endl;
cout <<"j=" <<j <<endl;
return 0;

void swap(int &x,int &y)

int temp;
temp=x;
x=y;
y=temp; //数值交换
cout<<"This swap is used!"<<endl; //判断该函数是否被调用

求高手,两道c语言编写题

第一题,头文件是
#include <math.h>

第二题,对于不超过3位的int x,x/100是它的百位,x%10是它的个位,x/10%10是它的十位。

做题求严谨,输入x以后判断它是不是在范围内,不在直接退出。
参考技术A 1.第一个图的解答如下,由于引用了“math.h”头文件,在linux下编译的时候需引用数学库,
加上-lm参数,假设源文件名为test.c,则编译语句为gcc test.c -lm,生成a.out
可执行文件,也可用-o指明可执行文件名称,如gcc -o test test.c -lm。
#include "math.h"
#include "stdio.h"

int main()

float x = 0 ;
double result = 0.0;
do

printf("please input x (Ctrl+C for exit):");
scanf("%f",&x);//从键盘读取x值
if(x <= 1)

result = exp(x) + 1;
printf("e^%f+1=%lf\\n",x,result );

else if(x >1 && x <=5 )

result = (-4)*x + 5;
printf("-4*%f+5=%lf\\n",x,result );

else if(x >5 && x <=10 )

result = 2*x -1;
printf("2*%f-1=%lf\\n",x,result );

else

result = 3*x - 11;
printf("3*%f-11=%lf\\n",x,result );

while(1);
return 0;

2.图二答案如下,有注释解释,不懂欢迎提问

#include "stdio.h"
#include "stdlib.h"
int main()

int num;
int ret;//返回值
unsigned char a;//存放百位数
unsigned char b;//存放十位数
unsigned char c;//存放个位数
unsigned char num_count;//位数统计
int num_invert;//逆序
do

printf("please input a num between(0,999](Ctrl+C for exit):");
ret = scanf("%d",&num);
/*输入不是整数,强制退出*/
if(ret != 1)

printf("please input a int num,now is to exit\\n");
exit(1);

/*输入超过范围,重新输入*/
if(num <= 0 || num > 999)

printf("input invalid,please input again\\n");
continue;//回到循环开始的地方

else

if(num/100 != 0)

num_count = 3;
a=num/100; //除以100得到百位数
b=(num/10)%10; //先除以10去掉个位数,然后取10余数就是十位数
c=num%10; //直接取10余数就是个位数
num_invert = c*100 + b*10 +a; //逆序数

else if(num/10 != 0)

num_count =2;
a = 0;
b = num/10;
c = num%10;
num_invert = c*10 + b;

else

num_count = 1;
a = 0;
b = 0;
c = num;
num_invert = c;

printf("%d 是 %d 位数,每一位的和是:%d,逆序打印为:%d\\n",num,num_count,a+b+c,num_invert );


while(1);
return 0;

本回答被提问者采纳
参考技术B 第一个用if else语句就可以吧

以上是关于求一个C语言程序题:编写一个函数SWAP()实现交换两个数位置的功能的主要内容,如果未能解决你的问题,请参考以下文章

谁能帮我做下这道C语言编程题,明天就要交了,好心人帮帮忙,谢谢了~~~~求救呀·~~~

编写c语言程序用三个函数求10个同学的平均分及成绩排序

c语言编程题 利用指针编写函数用选择法对整数数组排序(降序)。 求大神帮忙啊!!!谢谢

swap 函数的编写

急!!![80分]求一C语言程序

一道高一的集合类的题,求解答。