C语言上机实验六——函数

注意:部分函数名我运用的是名字缩写(zxr),请自行更改。

1. 编写一个函数,由实参传来一个整数n,将它各个位上的数字逆序输出,例如输入

123,输出为321。

#include <stdio.h>

int main()

{

    int shunxu(int x);

    int n;

    printf("please input a number:");

    scanf("%d",&n);

    shunxu(n);

 return 0;

}

int shunxu(int x)

{

    int i=0;

    while(x != 0)

    {

        i = i*10 + x%10;

        x = x/10;

    }

    printf("%d",i);

    return 0;

}

2. 求方程ax 2 +bx+c=O的根,用3个函数分别求当:b2-4ac大于0、等于0和小于0时的根并输出结果。从主函数输入a、b、c 的值。

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

float math1(float x,float y,float z);

float math2(float x,float y,float z);

void math3();

float math4(float x,float y,float z);

float d;

int main()

{

    float a,b,c,x1,x2;

    printf("please input three number a b c:");

scanf("%f %f %f",&a,&b,&c);

d=b*b-4*a*c;

if(d>0)

{

x1=math1(a,b,c);

x2=math2(a,b,c);

printf("The solution to this equation:x1=%f x2=%f",x1,x2);

}

else if(d<0)

{

math3();

}

else

{

x1=math4(a,b,c);

printf("The solution to this equation:x1=x2=%f",x1);

}

system("pause");

return 0;

}

extern float d;

float math1(float x,float y,float z)

{

float n;

n=((-1)*y+sqrt(d))/(2*x);

return n;

}

extern float d;

float math2(float x,float y,float z)

{

float n;

n=((-1)*y+sqrt(d))/(2*x);

return n;

}

extern float d;

void math3()

{

printf("Equation has no solution");

}

extern float d;

float math4(float x,float y,float z)

{

float n;

n=((-1)*y)/(2*x);

return n;

}

3.编写一个函数,由实参传来一个矩阵A[M][N],按列输出它每一列上最大值和最小值

的差。

#include <stdio.h>

#include <stdlib.h>

void zxr(int s[10][10],int m,int n)

{

int i,j,max,min, t;

    for(j=0;j<n;j++)

{

        max=s[0][j];

        min=s[0][j];

        for(i=0;i<m;i++)

{

            if(s[i][j]>max)

{

    max=s[i][j];

}

            if(s[i][j]<min)

{

      min=s[i][j];

    }

        }

        t=max-min;

        printf("%dn",t);

    }

}

int main()

{

int M,N,a[10][10];

int c,b;

printf("please input the array M and N:");

scanf("%d %d",&M,&N);

printf("input this array:");

for(c=0;c<M;c++)

{

for(b=0;b<N;b++)

{

scanf("%d",&a[c][b]);

}

}

zxr(a,M,N);

return 0;

}

4. 编写一个函数,从键盘输入m,输出大于m且紧随m的素数。

#include <stdio.h>

#include <stdlib.h>

int zxr(int a)

{

int i,v;

for(i=a+1;;i++)

{

for(v=2;v<i;v++)

{

if(i%v==0)

{

break;

}

}

if(v==i)

{

    return(i);

        }

}

}

int main(int argc, char *argv[])

{

int m;

printf("please input a number:");

scanf("%d",&m);

printf("%d",zxr(m));

return 0;

}

  1. 编写一个函数,删除字符串s中从下标k开始的n个字符(n和k从键盘输入)。

例如,字符串内容为:ChinaABC,k中的值为:5,n中的值为:3,结果为:China。

 #include <stdio.h>

#include <stdlib.h>

#include<string.h>

void zxr(char a[],int b,int c)

{

int i;

for(i=b;i<strlen(a)-c;i++)

{

a[i]=a[i+c];

}

a[i]='';

    puts(a);

}

int main()

{

int n,k;

char st[1000];

printf("please input the string:");

gets(st);

printf("input 'k' and 'n':");

scanf("%d %d",&k,&n);

zxr(st,k,n);

return 0;

}

 

6.编写计算最小公倍数的函数,试由主函数输入两个正整数a和b调用之。计算最小公

倍数的公式为:

lcm(u,v)=u*v/gcd(u,v) (u,v≥0)

其中,gcd(u,v)是u、v的最大公约数。lcm(u,v)是u、v的最小公倍数。

#include <stdio.h>

#include <stdlib.h>

int gcd(int u,int v);

int lcm(int u,int v);

int main()

{

    int a,b,x;

    printf("please input 'a' and 'b':");

    scanf("%d %d",&a,&b);

    x=lcm(a,b);

    printf("the lcm:%d",x);

}

int gcd(int u,int v)

{

int c,i,g;

c=u<v? u:v;

for(i=1;i<=c;i++)

{

if(u%i==0&&v%i==0)

{

g=i;

}

}

return g;

}

int lcm(int u,int v)

{

return (u*v)/gcd(u,v);

}

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>