实验三

我们两清 提交于 2019-12-04 21:04:49
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
    float a, b, c, x1, x2;
    float delta, real, imag;
    
    printf("Enter a, b, c:  ");
    
    while(scanf("%f%f%f", &a, &b, &c)) {
        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);
            }
        }
        
        printf("Enter a, b, c:\n");
    }
    system("pause");
    return 0;
} 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int guessNumber; // 存放程序运行时生成的1~100之间的随机整数 
    int ans;     // 存放用户猜的数字 
    
    srand((time(0)));  // 以时间函数作为随机种子 
    guessNumber = 1 + rand()%100;  // 生成1~100之间的随机数 
    
    do {
        printf("your guess number is(1~100): ");
        scanf("%d", &ans);
        if(ans < guessNumber)
            printf("%d is lower than the number.\n", ans);
        else if(ans > guessNumber) 
            printf("%d higher then the number.\n", ans); 
    }while(ans != guessNumber);
    
    printf("Congratulations. you hit it~\n");    
    
    system("pause"); // 在devc中运行时,这一行可以去掉
     
    return 0;
} 

 

 

#include <stdio.h>
#include <stdlib.h>
int main() {
    int number, max, min, n;
    
    n=1;
    printf("输入第%d个数: ", n);
    scanf("%d", &number);
    max = number;
    min = number;
    
    while(n<=4) {
        n++;
        printf("输入第%d个数: ", n);
        scanf("%d", &number);    

        if(number>=max)
            max = number;
        else if(number<=min)
            min = number;
    }
    
    printf("最大数为: %d\n", max);
    printf("最小数为: %d\n", min);
    
    system("pause");
    
    return 0;
} 

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
    int isprime (int x);
    int isprime (int x){
        int y;
        y=2;
        for(;y<=sqrt(x);y++)
        if(x%y==0)
        return 0;
        return 1;
    }
    int main(){
        
        int z,m;
        z=101;
        m=0;
        for(;z<=200;z++)
        if(isprime(z)) 
        printf("%d\n",z) ,    m++;
    
        printf("101到200之间有%d个素数",m);
        
        return 0;
}

 

 

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
    long int x,y,z,m;
    y=0;
    z=1;
    printf("请输入一个整数:\n");
    scanf("%ld",&x);
    for(;x>0;)
    {
        m=x%10;
        if(m%2!=0)
        {
            y=y+m*z;
        z=z*10;
        }
        x=x/10;
    }
    printf("得到的数为%d",y);
    return 0;
    
} 

 

 

实验总结

1、学习到了for、while语句的运用,但是并没有相当透彻的理解,以后需要加强练习

2、学习到了素数的判断,并运用了函数的新知识

3、体会到了编写代码时逻辑的重要性

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!