实验3 C语言分支语句和循环语句编程应用

 

 

 

实验任务1
 
// 一元二次方程求解 
// 重复执行, 直到按Ctrl+Z结束 
//
#include <math.h> 
#include <stdio.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) != EOF) { 
        if(a == 0) 
            printf("not quadratic equation.\n\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 = %.2f, x2 = %.2f\n\n", x1, x2); 
            }
                else {
                    real = -b/(2*a); 
                    imag = sqrt(-delta) / (2*a); 
                    printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real, imag, real, imag); 
                }
        }
        
        printf("Enter a, b, c: ");
    }
    
    return 0; 
}

 

 

实验任务2
 
// 生成N个0~9之间的随机整数,并打印输出 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#define N 5 

int main() { 
    int x, n; 
    
    srand(time(0)); // 以当前系统时间作为随机种子 
    n = 0; 
    do {
        n++; 
        x = rand()%10; // 生成一个0~9之间的随机整数 
        printf("%3d", x); 
    }while(n<N); 
    
    printf("\n"); 
    
    return 0; 
}

 

 

实验任务3
 
#include <stdio.h>
#include <math.h>
int main(){
    int i,j,k,count=0;
    for(i=101;i<=200;i++)
    {
        k=sqrt(i);
        for(j=2;j<=k;j++)
         if(i%j==0)
          break;
        if(j>k)
        {printf("%5d",i);
        count++;
        if(count%5==0)
        printf("\n");
        }
    }
    printf("\n 100~200之间共有 %d 个素数.\n",count);
    return 0;
}

 

 

 

实验任务4
 
#include <stdio.h>
int main(){
    long s,t=0,a,b=1;
    
    printf("Enter a number: ");
    
    while(scanf("%ld", &s) != EOF) {
        
        if(s==0)
        printf("invalid input.\n\n");
        
        else { 
            
            
            while(s){
                
            a=s%10;    
            
            if(a%2!=0)
            {t+=a*b;
            b*=10;}
            
            s=s/10;
            
            }
            
        printf("new number is:%ld\n\n",t);
    
        } 
        t=0,b=1;
        printf("Enter a number: ");
         
    }
    
    return 0;
} 

 

 
实验任务5
 
 
#include <stdio.h>
int main(){
    int n,i;
    float s=0.0,j=-1.0;
    
    printf("Enter n (1~10): ");
    
    while(scanf("%d", &n) != EOF) {
        
        if(n<1||n>10)
        printf("invalid input.\n\n");
        
        else { 
        
        for(i=1;i<=n;i++){
            j*=-1.0/i;
            s+=j;
        }
            

            
        printf("n=%d,s=%f\n\n",n,s);
    
        } 
        
        s=0.0,j=-1;
        printf("Enter n (1~10):");
         
    }
    
    return 0;
} 

 

 
实验任务6
 
 
#include<stdio.h>
#include<stdlib.h>
#include<time.h> 
int main(){

    int a,b=3,n=1;
    srand(time(0));
    a=rand()%31+1; 
    
    printf("猜猜2020年12月哪一天会是你的luck day.\n\n开始喽,你有三次机会,猜吧(1~31): ");
    scanf("%d",&n);
    
    while(b>0){
        
        if(n<1||n>31)
        {printf("???请不要开玩笑,好吗?\n\n再来一次吧:");
        scanf("%d",&n);
        }
        
        else if(n==a)
        {printf("恭喜,猜中啦!"); 
        b=-1;
        break;}
        
        else {
            if(n<a){
            printf("你猜的日期早了,luck day还没到呢~\n\n");
            b--;
                if(b>0){
                printf("再猜(1~31): ");
                scanf("%d",&n);    
                }
            }
            else if(n>a){
            printf("你猜的日期晚了,luck day悄悄溜到前面啦~\n\n");
            b--;
                if(b>0){
                printf("再猜(1~31): ");
                scanf("%d",&n);
            }
            }
        }
    }
    
    if(b==0) printf("次数用完啦。偷偷告诉你:12月,你的luck day是%d号",a);
    
    return 0;
    
}

 

 
 
posted @ 2020-11-16 22:53  迷你糖ovo  阅读(75)  评论(1)    收藏  举报