C语言程序设计(第四版)谭浩强版 课后答案 第七章

发布时间 2023-05-17 00:28:30作者: 风中凌乱的猪头
1、辗转相除法
#include<stdio.h>
int commond(int a,int b){
    int i;
    while(b!=0){
        i = a%b;
         a=b;
         b=i;
    }
    printf("%d\n",a);
}
int commonm(int a,int b){
    int i;
    int y=b,x=a;
    while(b!=0){
        i = a%b;
        a=b;
        b=i;
    }
    printf("%d\n",x*y/a);
}
int main(){
    int a,b;
    printf("please input two menbers:");
    scanf("%d%d",&a,&b);
    commond(a,b);
    commonm(a,b);
    return 0;
}

2、求方程根

#include<stdio.h>
#include<math.h>
double positive(double a,double b,double c){
    double x1,x2;
    x1 = (-b+sqrt(b*b-4*a*c))/2;
    x2 = (-b-sqrt(b*b-4*a*c))/2;
    printf("the equation's roots are:%f %f\n",x1,x2);
}
double negative(double a,double b,double c){
    double x1,y1,n;
    n = fabs(b*b-4*a*c);
    y1 = sqrt(n)/(2*a);
    x1 = -b/(a*2);
    printf("the equation's roots are:%fi+%f,",y1,x1);
    printf("%lfi-%lf\n",y1,x1);
}
double zero(double a,double b,double c)
{
    printf(" the equation's roots are:%f %f\n",(-b)/2*a,b/(a*2));
}
int main(){
    double a,b,c,d;
    scanf("%lf%lf%lf",&a,&b,&c);
    d = b*b-4*a*c;
    if(d>0){
        positive(a,b,c);
    }
    else if (d<0)
    {
        negative(a,b,c);
    }
    else 
        zero(a,b,c);
    

    return 0;
}

3、如何定义函数的返回,一开始并不想用int型,而是用void,但是不知道if执行怎么结束返回,所有用了int型,直接return 0。如果用void可以用exit(0)让程序结束退出

#include<stdio.h>
int isprime(int a){
    int i;
    for(i=2;i<a;i++){
        if(a%i==0){
            printf("it's not a prime number!\n");
            return 0;
        }
    }
    printf("this number is a prime number\n");
    return 0;
}
int main(){
    int i;
    printf("please input a number:");
    scanf("%d",&i);
    isprime(i);
    return 0;
}

4、3*3数组转置

#include<stdio.h>
void T(int array[][3]){
    int i,j,t;
    for(i=0;i<3;i++){
        for(j=i;j<3;j++){
            t=array[i][j];
            array[i][j]=array[j][i];
            array[j][i]=t;
        }
    }
    for(i=0;i<3;i++){
          for(j=0;j<3;j++){
            printf("%d ",array[i][j]);
          }
          printf("\n");
          
    }
}
int main(){
    void T(int array[][3]);
    int a[3][3];
    int i,j;
    printf("please input numbers:");
    for(i=0;i<3;i++){
          for(j=0;j<3;j++){
            scanf("%d",&a[i][j]);
          }
    }
    for(i=0;i<3;i++){
          for(j=0;j<3;j++){
            printf("%d ",a[i][j]);
          }
          printf("\n");
          
    }
    T(a);
    return 0;
}

5、无返回类型

#include<stdio.h>
#include<string.h>
void anti(char b[20]){
    int l,i,j,t;
    l = strlen(b);
    for(i=0,j=l-1;i<l/2;i++){
       t=b[i];
       b[i]=b[j];
       b[j]=t;
       j--; 
    }
    puts(b);
}
int main(){
    void anti(char b[20]);
    char a[20];
    printf("please input string:");
    scanf("%s",a);
    anti(a);
    return 0;
}

返回指针类型

#include<stdio.h>
#include<string.h>
char* anti(char b[20]){
    int l,i,j,t;
    l = strlen(b);
    for(i=0,j=l-1;i<l/2;i++){
       t=b[i];
       b[i]=b[j];
       b[j]=t;
       j--; 
    }
    return b;
}
int main(){
    char* anti(char b[20]);
    char *b;
    char a[20];
    printf("please input string:");
    scanf("%s",a);
    b=anti(a);
    puts(b);
    return 0;
}

6、