当前位置:网站首页>Experiment 4

Experiment 4

2020-12-06 21:31:44 Lin Jingye

//  Solving quadratic equation of one variable  ( Function implementation mode ) 
//  repeat ,  Until you press Ctrl+Z end  

#include <math.h>
#include <stdio.h>

//  Function declaration 
void solve(double a, double b, double c);

//  The main function  
int main() {
    double a, b, c;
    
    printf("Enter a, b, c: ");
    while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
        solve(a, b, c);  //  Function call  
        printf("Enter a, b, c: ");
    }
    
    return 0;
}

//  Function definition 
//  function : Solving a quadratic equation of one variable , printout 
//  Formal parameters :a,b,c Is the coefficient of quadratic equation of one variable  
void solve(double a, double b, double c) {
    double x1, x2;
    double delta, real, imag;
    
    if(a == 0) 
        printf("not quadratic equation.\n");
    else {
        delta = b*b - 4*a*c;
        
        if(delta >= 0) {
            x1 = (-b + sqrt(delta)) / (2*a);
            x2 = (-b - sqrt(delta)) / (2*a);
            printf("x1 = %f, x2 = %f\n", x1, x2);
        }
        else {
            real = -b/(2*a);
            imag = sqrt(-delta) / (2*a);
            printf("x1 = %f + %fi, x2 = %f - %fi\n", real, imag, real, imag);
        }
    }    
}

Can not be , Because there are two roots of a quadratic equation of one variable .

//  Using parts static Variable computes factorials 
 
#include <stdio.h>
long long fac(int n);  //  Function declaration  

int main() {
    int i,n;
    
    printf("Enter n: ");
    scanf("%d", &n);
    
    for(i=1; i<=n; ++i) 
        printf("%d! = %lld\n", i, fac(i)); 
    
    return 0;
}

//  Function definition  
long long fac(int n) {
    static long long p = 1;
    printf("p = %lld\n", p);
    p = p*n;
    
    return p;
}

Local static The characteristic of a variable is to repeat the last number , And talk about the corresponding operations .

//  practice : Local static Variable characteristics  

#include<stdio.h>
int func(int, int);

int main() {
    int k=4,m=1,p1,p2;
    
    p1 = func(k,m) ; 
    p2 = func(k,m) ;
    
    printf("%d,%d\n",p1,p2) ;
    
    return 0;    
}

int func(int a,int b) {   
    static int m=0,i=2;
    
    i += m+1;
    m = i+a+b;  
    
    return (m);
}

After program running , The initial value of the second operation is the final value at the end of the first operation .

// Find all primes between two integers ( Including these two integers ), Save the results in an array bb in , Function returns the number of primes .
//  for example , Input 6 and 21, The output of :7 11 13 17 19.

#include <stdio.h>

#define N 1000
int fun(int n,int m,int bb[N]) {
    int i,j,k=0,flag;
    
    for(j=n;j<=m;j++) {
          flag = 4;   
        for(i=2;i<j;i++)
            if(j % i == 0) {  
               flag=0;
               break;
        }
        if(flag == 4) 
           bb[k++]=j;
    }
    return k;
}

int main(){
    int n=0,m=0,i,k,bb[N];
    
    scanf("%d",&n);
    scanf("%d",&m);
    
    for(i=0;i<m-n;i++)
        bb[i]=0;
        
    k=fun(n, m, bb); 
    
    for(i=0;i<k;i++)
        printf("%4d",bb[i]);
        
    return 0;
}

#include <stdio.h>
long long fun(int n);   //  Function declaration  

int main() {
    int n;
    long long f;
    
    while(scanf("%d", &n) != EOF) {
        f = fun(n);  //  Function call  
        printf("n = %d, f = %lld\n", n, f);
    }
    
    return 0;
}

//  Function definition 
//  Make up ... 
long long fun(int n) {
    long long int s = 2;
    if(n == 1)
        s = s - 1;
    else if(n >= 2 && n <=32)
        s = s*(fun(n-1)+1)-1;
        
        return s;
}

#include <stdio.h>

void draw(int n, char symbol);  //  Function declaration  

#include <stdio.h> 
int main() {
    int n, symbol;
    
    while(scanf("%d %c", &n, &symbol) != EOF) {
        draw(n, symbol);  //  Function call  
        
        printf("\n");
    }
    
    return 0;
} 


//  Function definition 
//  Complement code ... 
void draw(int n, char symbol) {
    int i, a;
    for(i = 1;i <= n; ++i) {
        for(a = n-i;a > 0;a--)
            printf(" ");
        for(a = 2*i-1;a > 0;a--)
            printf("%c", symbol);
            printf("\n");
    }
}

版权声明
本文为[Lin Jingye]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201206213051715b.html