zoukankan      html  css  js  c++  java
  • 第七章

    1.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。

    #include <stdio.h>
    #include <math.h>
    int gcd(int m,int n){
        int t,r;
        if(m<n){
            t=m;
            m=n;
            n=t;
        }
        r=m%n;
        while(r!=0){
            m=n;
            n=r;
            r=m%n;
        }
        return n;
    }
    int gbs(int m,int n){
        int t=gcd(m,n);
        return m*n/t;
    }
    int main(){
        printf("please input m and n:
    ");
        int m,n;
        scanf("%d%d",&m,&n);
        printf("m=%d,n=%d的最大公因数=%d,最小公倍数=%d
    ",m,n,gcd(m,n),gbs(m,n));
        return 0;
    } 
     
    View Code

    2.求方程aX*x+b*x+c=0的根,用3个函数分别求当:b*b-4*a*c。大于0,等于0,小于0的根并输出结果

    #include <stdio.h>
    #include <math.h>
    void OverZero(double a,double b,double c){
        double disc=b*b-4*a*c;
        double x1,x2;
        x1=(-b-sqrt(disc))/(2*a); 
        x2=(-b+sqrt(disc))/(2*a);
        printf("a=%lf,b=%lf,c=%lf时解为x1=%lf,x2=%lf
    ",a,b,c,x1,x2);
    }
    void EquealZero(double a,double b,double c){
        double disc=b*b-4*a*c;
        double x=(-b-sqrt(disc))/(2*a);
        printf("a=%lf,b=%lf,c=%lf时解为x=%lf
    ",a,b,c,x);
    }
    void LessZero(double a,double b,double c){
        double disc=-(b*b-4*a*c);
        double p=-b/(2*a);
        double q=sqrt(disc)/(2*a);
        printf("a=%lf,b=%lf,c=%lf时解为x1=%lf+%lfi,x2=%lf-%lfi
    ",a,b,c,p,q,p,q);
    }
    int main(){
        printf("please input a,b,c:
    ");
        double a,b,c,disc;
        scanf("%lf%lf%lf",&a,&b,&c);
        disc=b*b-4*a*c;
        if(disc>0){
           OverZero(a,b,c);    
        }else if(disc==0){
           EquealZero(a,b,c);
        }else{
           LessZero(a,b,c);    
        } 
        return 0;
    } 
    View Code

    3.写一个判素数的函数,在主函数输入一个整数,输出是否为素数的信息。

    #include <stdio.h>
    int prime(int n);
    int main(){
        int n;
        printf("input an integer:");
        scanf("%d",&n);
        if(prime(n)){
            printf("%d is a prime.
    ");
        }else{
            printf("%d is not a prime.
    ");
        }
        return 0;
    }
    int prime(int n){
        int flag=1,i;
        for(i=2;i<n/2&&flag==1;i++)
          if(n%i==0) flag=0;
        return flag;
    } 
    View Code

    4.写一个函数,使给定的一个3X3的二维数组转置,即行列互换 

    #include <stdio.h>
    #include <math.h>
    #define N 3
    void cover(int a[][3],int b[][3]){
        int i,j;
        for(i=0;i<N;i++){
            for(j=0;j<N;j++)
             b[i][j]=a[j][i];
        }
    }
    int main(){
        printf("请输入一个3X3的数组:
    ");
        int i,j,a[N][N],b[N][N];
        for(i=0;i<N;i++){
            for(j=0;j<N;j++)
            scanf("%d",&a[i][j]);
        }
        cover(a,b);
        printf("The cover info:
    ");
        for(i=0;i<N;i++){
            for(j=0;j<N;j++)
            printf("%5d",b[i][j]);
            printf("
    ");
        }
        return 0;
    }
    View Code

    5.写一个函数,使输入一个字符串按反序存放,在主函数中输入和输出字符 

    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    void inverse(char str[]){
        char t;
        int i,j;
        for(i=0,j=strlen(str);i<(strlen(str)/2);i++,j--){
            t=str[i];
            str[i]=str[j-1];
            str[j-1]=t;
        }
    }
    int main(){
        char str[100];
        printf("input string:");
        scanf("%s",str);
        inverse(str);
        printf("inverse string:%s
    ",str);
        return 0;
    }
    View Code

    6.写一个函数,将两个字符串连接 

    #include <stdio.h>
    #include <string.h>
    void concatenate(char string1[],char string2[],char string[]){
        int i,j;
        for(i=0;string1[i]!='';i++)
           string[i]=string1[i];
        for(j=0;string2[j]!='';j++)
            string[i+j]=string2[j];
        string[i+j]='';
    }
    int main(){
        char s1[100],s2[100],s[100];
        printf("input string1:");
        scanf("%s",s1);
        printf("input string2:");
        scanf("%s",s2);
        concatenate(s1,s2,s);
        printf("
     The new string is %s
    ",s);
        return 0;
    }
    View Code

    未完待续.........

  • 相关阅读:
    阿里云oss怎么上传文件夹
    HTTP文件上传服务器-支持超大文件HTTP断点续传的实现办法
    让富文本编辑器支持复制doc中多张图片直接粘贴上传
    CSDN-markdown 文字样式设置(字体, 大小, 颜色, 高亮底色)
    网络编程基础知识点
    inet_addr 和inet_ntoa函数作用
    sockaddr和sockaddr_in的区别
    简析TCP的三次握手与四次分手
    TCP三次握手和四次挥手原理分析
    htons(), ntohl(), ntohs(),htons() 函数功能
  • 原文地址:https://www.cnblogs.com/byczyz/p/13848579.html
Copyright © 2011-2022 走看看