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


    1.实验任务1

    #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.
    
    ");
            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
    
    ", x1, x2);
               }
               else {
                   real = -b/(2*a);
                   imag = sqrt(-delta) / (2*a);
                   printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi
    
    ", real,
    imag, real, imag);
                }
             }
             printf("Enter a, b, c: ");
        }
       return 0;
     }

    2.实验任务2

    #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("
    ");
        return 0;
    }

    3.实验任务3

    #include<stdio.h>
    #include<math.h>
    int isprime(int n);
    int main()
    {
        int i,m=0,t=0;
        for(i=101;i<=200;i++)
        {
            if(isprime(i))
            {
                m=m+1;
                t++;
                if(t%5==0)
                printf("%4d
    ",i);
                else
                printf("%4d",i);}
            
        }printf("
    ");
        printf("101~200之间共有%d个素数
    ",m);
        return 0;
    }
    int isprime(int n)
    {
        int k;
        for(k=2;k<=sqrt(n);k++)
            if(n%k==0)
                return 0;
            return 1;    
    }

    4.实验任务4

    以a=s%10;(a%2==1)取奇数  

    以t=0;t=a*pow(10,n-1)+t实现高位与低位问题

    #include<stdio.h>
    #include<math.h>
    int main(){
        long int s,a,n=0,t=0;
        printf("Enter a number:");
        while(scanf("%ld",&s)!=EOF){
            do{ 
                a=s%10;
                if(a%2==1){
                    n++;
                    t=a*pow(10,n-1)+t;
                }
                s=s/10;    
            }while(s!=0);
            printf("new number is:%ld
    
    ",t);
            printf("Enter a number:");
            t=0;
            n=0;     
        }
        return 0;
    }

    5.实验任务5

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

    6.实验任务6

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main(){
        int x,date,i,a=0;
        srand(time(0));
        date=rand()%31;
        printf("猜猜2020年12月哪一天会是你的lucky day
    开始喽,你有三次机会,猜吧(1~31):"); 
        for(i=1;i<=3;i++){
            scanf("%d",&x);
            if(x>date)printf("
    你猜的日期晚了,lucky day悄悄溜到前面啦");
            else if(x<date) printf("
    你猜的日期早了,lucky day还没到呢");
            else {
                a=1;
                printf("
    你猜中了!");
                break;
            };
             
            if(i!=3)printf("
    再猜(1~31): "); 
            
        }
        if(a==0)printf("
    
    次数用完啦。偷偷告诉你:12月,你的lucky day是%d号",date);
        
        return 0;
        
    }

  • 相关阅读:
    Bootstrap 3的box-sizing样式导致UEditor控件的图片无法正常缩放
    Npm install failed with “cannot run in wd”
    JQuery使用deferreds串行多个ajax请求
    使用HTML5的History API
    Christmas Trees, Promises和Event Emitters
    Node.js开发者最常范的10个错误
    Mongoose Schemas定义中timestamps选项的妙用
    Ubuntu上安装Robomongo及添加到启动器
    [nodemon] Internal watch failed: watch ENOSPC错误解决办法
    介绍两个Ubuntu上的桌面小工具
  • 原文地址:https://www.cnblogs.com/qiansen/p/13967035.html
Copyright © 2020-2023  润新知