实验三11.14

  • EX1
// 一元二次方程求解 
// 重复执行, 直到按Ctrl+Z结束 
#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;
} 

备注:
①while(变量 != EOF)用法

EOF是一个计算机术语,为EndOfFile的缩写,在操作系统中表示资料源无更多的资料可读取。在while循环中以EOF作为文件结束标志,这种以EOF作为文件结束标志的文件,必须是文本文件。在文本文件中,数据都是以字符的ASCII代码值的形式存放。我们知道,ASCII代码值的范围是0~127,不可能出现-1,因此可以用EOF作为文件结束标志。还有很多文件处理函数处错误后的返回值也是EOF,因此常被用来判断调用一个函数是否成功。在终端(黑框)中手动输入时,系统并不知道什么时候到达了所谓的“文件末尾”,因此需要用<Ctrl + z>组合键然后按 Enter 键的方式来告诉系统已经到了EOF,这样系统才会结束while。

  • EX2
//ex2
#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;
}

复习:

① #define  标识符  常量   //注意, 最后没有分号

备注:

①srand函数

srand函数是随机数发生器的初始化函数,srand和rand()配合使用产生伪随机数序列。

②x = rand()%10

对10取余数 这样0≤x≤9

 

③关于srand(time(0))的解释

计算机没有办法产生真正的随机数的,是用算法模拟,所以你只调用rand,每次出来的东西是一样的。设置一个种子后,根据种子的不同,就可以产生不同的数了。而怎么保证种子的不同呢?最简单的办法当然是用永远在向前的时间。

srand(time(0)) ;  //先设置种子
rand();  //然后产生随机数
Srand是种下随机种子数,你每回种下的种子不一样,用Rand得到的随机数就不一样。为了每回种下一个不一样的种子,所以就选用Time(0),Time(0)是得到当前时时间值(因为每时每刻时间是不一样的了)。

  • EX3
//ex3
#include <stdio.h>
#include <math.h>
int main()
{
    int i, m, n, x;
    
     x = 0;
        
    
    for(n = 101; n <= 200; n++) 
    {
         m = sqrt(n);
         for(i = 2; i <= m; i++)
             if(n % i == 0)break;
    
         if(i > m)
         {
            printf("%5d",n);
            x++;
            if(x % 5 == 0)
            printf("\n");
         }
    
    }
     printf("\n");
     printf("100~200之间共有%d个素数",x);
     return 0;
 } 

  • EX4
//ex4
#include <stdio.h>
#include <stdlib.h>
int main()
{
    long a, b, c;
    
    printf("输入一个数字:");
    
    while(scanf("%ld",&a) != EOF)
    {
        long d = 0, sum = 0;
        while(a != 0)
        {
            b = a%10;
            a = a/10;
            
            if(b % 2 != 0)
               c = c*10+b;    
        }
        while(c != 0)
        {
             d = c%10;
             c = c/10;
             
             if(d % 2 != 0)
               sum = sum*10+d;
        }
     printf("新的数字是:%ld\n",sum);
     printf("输入一个数字:");
     } 
     
    
    
    return 0; 
 } 

设计思路:运用%和/,将输入的数字最后一位不断取出,若为奇数则保存,并用“c=c*10+b”来组成新的数,但此时新数字高低位排列与原数字相反,需要再次抽取组成新数,此时不用再判断奇偶数。

  • EX5
//ex5
#include <stdio.h>
#include <math.h>

int main()
{
    int n, i, factorial = 1;
    double s = 0, sign = 1.00000;
    
    printf("输入n(1~10):");
    
    while(scanf("%d",&n) != EOF)
    {
    
        for(i=1;i<=n;i++)
        {
            factorial = factorial * i;
            sign = pow(-1,i+1);
            s = s + sign/factorial;
            continue;
        }
        
        printf("n = %d  s = %f\n", n, s);
        
        s = 0;
        factorial = 1;
        sign = 1.00000;
        
        printf("输入n(1~10):");
    }
      return 0;
} 

备注:一定要注意整型数据和浮点型数据在计算里的区别!

  • EX6

 

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

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

 

posted @ 2020-11-17 17:07  热心市民小杨  阅读(110)  评论(1编辑  收藏  举报