实验3 C语言分支语句、循环语句、函数综合应用编程

Task1:

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

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

X%n(n为常数)的意思就是[0,n-1]的整数,即[0,n-1]以内的数字     由取余实现

 

 Task2:

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {    
srand(time(0));
int luckyday = rand()%31+1;
int a;
int time=1;
printf("猜一猜2021年5月哪一天会是你的Lucky day?\n开始咯,你有三次机会,猜吧(1~31)"); 
do{
scanf("%d",&a);
time++;
if(a>luckyday){
    printf("你猜的日期晚了, lucky day悄悄溜到前面啦,再猜猜(1~31):\n");
    }
else if (a<luckyday){
    printf("你猜的日期早了, lucky day还没到呢,再猜猜(1~31):\n") ;}
else if (a==luckyday){    
    printf("恭喜你,猜中啦!");
   break;}
}
    while(time<=3);
    if(a!=luckyday){
    printf("次数用完啦,偷偷告诉你:五月,你的lucky day是%d",luckyday);
}else{printf("\n");
}
    return 0;
} 

 

Task3:

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

Task4:有一说一,“一元二次方程的根,能否设计成以函数返回值的方式返回给主调函数?“这句话是什么意思我都看不懂  我就说个否吧 ,等我日后功底雄厚再来回答。

// 一元二次方程求解(函数实现方式)
// 重复执行, 直到按下Ctrl+Z结束 

#include <math.h>
#include <stdio.h>

// 函数声明
void solve(double a, double b, double c);

// 主函数 
int main() {
    double a, b, c;
    
    printf("Enter a, b, c: ");
    while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
        solve(a, b, c);  // 函数调用 
        printf("Enter a, b, c: ");
    }
    
    return 0;
}

// 函数定义
// 功能:求解一元二次方程,打印输出结果
// 形式参数:a,b,c为一元二次方程系数 
void solve(double a, double b, double c) {
    double x1, x2;
    double delta, real, imag;
    
    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 = %.2f, x2 = %.2f\n", x1, x2);
        }
        else {
            real = -b/(2*a);
            imag = sqrt(-delta) / (2*a);
            printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n", real, imag, real, imag);
        }
    }    
}

Task5:

#include <stdio.h>
double fun(int n); // 函数声明

int main() {
int n;
double s;
printf("Enter n(1~10): ");
while(scanf("%d", &n) != EOF) {
s = fun(n); // 函数调用
printf("n = %d, s= %f\n\n", n, s);
printf("Enter n(1~10): ");
}
return 0;
}
// 函数定义
double fun(int n) {
double x;
int i=1,y=1;
double t=0;
for(i=1;i<=n;i++){
y=i*y;
if(i%2==0)
x=(-1/(double)y);
else
    x=(1/(double)y);
t=t+x;
}
return t;
}

 

Task6:

#include <stdio.h>

int main()
{
    int x;
    int a=0;
    for ( x=101; x<=200; x++ ) {
        int i;
        int isPrime = 1;
        for ( i=2; i<x; i++ ) {
            if ( x % i == 0 ) {
                isPrime = 0;
                break;
            }
        }
        if ( isPrime == 1 ) {
            printf("%d ", x);
            a++;        
             if(a%5==0){
                printf("\n");
        } 
            }    
        }
    printf("\n在101~200之间,共有素数%d个",a); 
    return 0;
}

 

Task7:娱乐娱乐