实验3 C语言分支语句和循环语句编程应用(友好猜日期^_^)

实验任务一

#include <stdio.h>
#include <math.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;
}

 

 

实验任务二

#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;
        printf("%3d",x);
    }while(n<N);
    printf("\n");
    return 0;
}

 

 

实验任务三

#include <stdio.h>
int main (){
    int x=101;
    int n=200;
    int num=0;
    for (;x<=200;x++){
        int t,flag;
        flag=1;
        for (t=2;t<x;t++)
        {
            if (x%t==0)
            {flag=0;
            }
        }
        if (flag){
            printf("  %d",x);
            num++;
            if (num%5==0) printf("\n");
        }
    }
    printf("\n101~200之间共有%d个素数",num);
    return 0;
}

 

 

实验任务四

#include <stdio.h>
int main(){
    long s;
    printf("Enter a number: ");
    while (scanf("%ld",&s)!=EOF){
        long sum,t;
        sum=0;
        int a,b;
        b=0;
        t=10;
        long j=10;
        if (s<1000000000){
            while (s/t!=0){
                a=(s%t-b)/(t/10);
                b=b+a*(t/10);
                if(a%2!=0){
                    sum=sum+a*(j/10);
                    j=j*10;
                }
                t=t*10;
            }
            a=(s%t-b)/(t/10);
            b=b+a*(t/10);
            if(a%2!=0){        
                sum=sum+a*(j/10);
            }
        }else{
            int x;
            for (x=1;x<=9;x++){
                a=(s%t-b)/(t/10);
                b=b+a*(t/10);
                if(a%2!=0){
                    sum=sum+a*(j/10);
                    j=j*10;
                }
                t=t*10;
            }
            a=s/1000000000;
            if(a%2!=0){
                sum=sum+a*(j/10);
            }
        }
    
        printf("new number is: %ld\n\n",sum);
        printf("Enter a number: ");
    }
    return 0;
}

 

 

实验任务五

#include <stdio.h>
int main(){
    int n;
    printf ("Enter n(1~10):");
    while (scanf ("%d",&n)!=EOF){
        int t,f,T;
        f=1;
        T=1;
        double s=0.0;
        for (t=1;t<=n;t++){
            T=t*T;
            s=s+f*(1.0/T);
            f=f*(-1);
        }
        printf("n = %d, s = %f\n\n",n,s);
        printf ("Enter n(1~10):");
    }
}

 

 

实验任务六

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main (){
    printf("猜猜2020年12月哪一天会是你的luck day\n\n");
    int n= rand()%31+1;
    int t=1;
    printf ("开始喽,你有三次机会,猜吧(1~31):");
    int a;
    for (t=1;t<=3;t++){
        scanf("%d",&a);
        if(a<n){
            printf("小伙子真着急,你猜的日期早了,luck day还妹到呢\n\n"); 
        }
        if (a>n){
            printf("你猜的日期晚啦,luck已经莫得了\n\n");
        }
        if (a==n){
            printf("这也能猜中??这么好的luck还需要luck day干嘛,速速去买彩票\n\n"); 
            break;
        }
    }
    if(a!=n){
        printf("次数用光啦。。。偷偷告诉你:");
        Sleep(1000);
        printf("12月");
        for (t=1;t<=3;t++){
            Sleep(500);
            printf(""); 
        }
        Sleep(1000);
        printf("每天都是luck day!!!");
    }
}

 

posted @ 2020-11-12 15:10  deemmo  阅读(173)  评论(1编辑  收藏  举报