求一个C语言程序题:编写一个函数SWAP()实现交换两个数位置的功能 编写c语言程序,实现交换两个变量值的操作。
C\u8bed\u8a00\u7f16\u7a0b\u9898\uff1a\u7f16\u5199\u7a0b\u5e8f\u5b9e\u73b0\u4e24\u4e2a\u6574\u6570\u503c\u7684\u4ea4\u6362\uff0c\u5176\u4e2dswap()\u4e3a\u4ea4\u6362\u51fd\u6570\u3002(\u8981\u6c42\uff1a\u4e24\u503c\u771f\u6b63\u4ea4\u6362)#include "stdio.h"
int swap(int *p,int *q)
{
int t;
t=*p;*p=*q;*q=t;
}
main()
{
int a,b;
scanf("%d,%d",&a,&b);
swap(&a,&b);
printf("%d\n%d\n",a,b);
}
/*
\u7f16\u5199c\u8bed\u8a00\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u503c\u7684\u64cd\u4f5c\u3002
\u8981\u6c42\uff1a\u4f7f\u7528\u5f62\u53c2\u4e3a\u6307\u9488\u7684\u51fd\u6570swap\u5b8c\u6210\u4ea4\u6362\u529f\u80fd\u3002
\u4e3b\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528\u6574\u578b\u6307\u9488\u4ece\u952e\u76d8\u8f93\u5165\u4e24\u4e2a\u6574\u6570\uff0c\u901a\u8fc7\u8c03\u7528swap\u5b8c\u6210\u4ea4\u6362
\u8f93\u51fa\u4ea4\u6362\u524d\u540e\u7684\u53d8\u91cf\u503c\u8fdb\u884c\u6bd4\u8f83\u3002
\u8f93\u5165\u8f93\u51fa\u793a\u4f8b\uff1a
Input two number\uff1a3 4
Before change\uff1aa=3\uff0cb=4
After change:a=4,b=3
*/
//File: swap.c
#include
#include
void swap(int *a, int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
int main()
{
int *a=NULL,*b=NULL;
a=(int *)calloc(1,sizeof(int));
if(!a)
{
perror("calloc a err.");
}
b=(int *)calloc(1,sizeof(int));
if(!b)
{
perror("calloc b err.");
}
printf("Input two number\uff1a");
scanf("%d %d",a,b);
printf("Before change\uff1aa=%d\uff0cb=%d\n",*a,*b);
swap(a,b);
printf("After change:a=%d\uff0cb=%d\n",*a,*b);
return 0;
}
C:\mypro>gcc -g -Wall swap.c -o swap
C:\mypro>swap
Input two number\uff1a3 4
Before change\uff1aa=3\uff0cb=4
After change:a=4\uff0cb=3
C:\mypro>
\u7a0b\u5e8f\u5df2\u8c03\u597d\uff0c\u76f4\u63a5\u8fd0\u884c\u5373\u53ef\u3002
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);
因为那只是临时变量,函数调用完后,便释放空间,两个值并没有交换。
/*交换程序*/
#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; //判断该函数是否被调用
}
/*传引用调用,改变了实参的值,所以得以交换*/
#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; //判断该函数是否被调用
}
绛旓細//鐢变簬涓涓暟鐨勫洜瀛愪細灏忎簬鎴栫瓑浜庢湰韬,鎵浠ュ彲浠//浠ユ璁惧畾璺冲嚭鏉′欢,鐢ㄩ亶鍘嗙殑鎬濇兂鍘讳粠2寮濮嬮愪釜瀵绘壘鍥犲瓙//濡傛灉閭d釜鏁版槸鍥犲瓙涔嬩竴鍒欏彲浠ヨ鏁撮櫎if((x%i)==0)//浠ユ浣滀负鍒ゆ柇鏉′欢,鑰屼竴涓暟鐨勫洜瀛愬彲鑳戒細閲嶅//鎵浠ユ瘡鎵惧埌涓涓氨瑕佹湁閲嶅ご寮濮嬪啀鍒ゆ柇涓杈//i=2;灏辨槸浣滀负澶嶄綅鐨,鍏跺疄杩欎釜绋嬪簭鍜岀畻娉曞緢绠鍗//浣犺嚜宸卞鐪嬩袱...
绛旓細include<stdio.h> int a[5];//瀹氫箟鍏ㄥ眬鍙橀噺 int fun1(int x)//绱犳暟鍒ゆ柇鍑芥暟 { if(x<2)return 0;for(int i=2;i*i<=x;i++)if(x%i==0)return 0;return 1;} int fun_2(int *x){ int i=*x,k=0;for(;;i++)if(fun1(i)&&i>*x)// { a[k++]=i;if(k==5)//瀛...
绛旓細include <stdio.h> include <string.h> int letter,number,blank,other;void count(char str[]){ int i;for(i=0;str[i]!='\0';i++){ if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))letter++;else if(str[i]>='0'&&str[i]<='9')number++;else...
绛旓細for (i=L-1;i>=0;i--) if (str[i] == '\\') { str[i] ='\0'; break;}; // 鍘绘帀鏂囦欢鍚嶅氨鏄矾寰刾rintf("Path= %s",str);return 0;}---鍐欐垚鍑芥暟鍜岃皟鐢:#include <stdio.h>void fun(char *str){int L,i;L = strlen(str);for (i=L-1;i>=0;i--) if (str[i] == '\\'...
绛旓細1銆侀鍏堬紝闅忎究鍒涘缓涓涓湁main鏂规硶鐨勭被銆2銆佸畾涔変竴涓瓧绗︿覆鍙橀噺锛岃繖閲岀粰鍙橀噺璧嬪硷紝鍓嶅悗閮界暀鏈変竴涓绌烘牸銆3銆佸厛杩愯涓閬嶏紝鐪嬭緭鍑烘晥鏋滐紝鍙互鐪嬪埌鐧惧害鍖呮嫭绌烘牸鐨勫嚑涓瓧绗﹂兘鎵撳嵃鍑烘潵浜嗐4銆佷负浜嗘洿鍔犳竻鏅帮紝鐢ㄥ瓧绗︿覆瀵硅薄鍙﹀涓涓柟娉曗渓ength()鈥濆緱鍒版瀛楃涓茬殑闀垮害涓6锛 璇存槑鐨勭‘鏄滅櫨搴︹濆姞涓や釜绌烘牸鐨勯暱搴...
绛旓細func(int x, int y){if (x > y) {return 1;}else if (x < y) {return -1;}else {return 0;}}int main(){int x, y, ret;printf("璇疯緭鍏,y鐨勫硷細");scanf("%d%d", &x, &y);ret = func(x, y);printf("%d\n", ret);system("pause");return 0;}杩愯缁撴灉锛...
绛旓細include<stdio.h>#include <stdlib.h>#include <string.h>void fun(char a[]){ int i; int t=strlen(a); for(i=0;i<t;i++) { if(i%2!=0) { if(toascii(a[i])>=97&&toascii(a[i])<=122) { a[i]=(char)(toascii(a[i])-32); } ...
绛旓細long fact(int n){ if(n<1) return 1;return n*fact(n-1);} else long fact(int n){ long t=1;for(int i=2;i<=n;i++)t*=i;return t;} endif main(){ long s=0;for(int i=1;i<=10;i++)s+=fact(i);printf("%ld\n",s);} 2銆俰nclude "stdio.h"bool prime(int...
绛旓細include <stdio.h> include <math.h> include <string.h> int change(char *a){ int i;int num=0;for(i=0;a[i];i++)num+=(a[i]-'0')*pow(10,(strlen(a)-1-i));return num;} main(){ char a[10];int num;gets(a);num=change(a);printf("%d",num);} ...
绛旓細= EOF){ printf("鍦嗙殑闈㈢Н鏄%lf\n",Square(r)); printf("鍦嗙殑鍛ㄩ暱鏄%lf\n",Perimeter(r)); printf("璇疯緭鍏ュ渾鐨勫崐寰勶細\n"); } return 0;}杩愯缁撴灉锛氳杈撳叆鍦嗙殑鍗婂緞锛1鍦嗙殑闈㈢Н鏄3.141590鍦嗙殑鍛ㄩ暱鏄6.283180璇疯緭鍏ュ渾鐨勫崐寰勶細2 鍦嗙殑闈㈢Н鏄12.566360鍦嗙殑鍛ㄩ暱鏄12.566360...