实验三

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

任务二
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int x,ans,i; srand(time(0)); x=rand()%31+1;//产生1~31之间的随机数 printf("Guess your lucky day of May!!\n"); printf("You have three chances.Enter your answer:"); scanf("%d",&ans); for (i = 0; i < 2; i++) { if (ans > x) { printf("Too late. Try again:"); scanf("%d", &ans); } else if (ans < x) { printf("Too early. Try again:"); scanf("%d", &ans); } else { printf("You`re right!!"); break; } } if (i>=2) printf("Your lucky day is May %d",x);//出循环后判断是答对还是用完次数 return 0; }

任务三
#include<stdio.h> #include<math.h> int main(){ long outcome,x,number; int y,count,m; printf("Enter a number:"); while(scanf("%ld",&number)){ count=1; outcome=0; for(;number!=0;) { x=number%10; if(x%2!=0) { m=1; for(y=1;y<count;y++) { m*=10; } outcome+=x*m; count++; } number/=10; } printf("\nnew number is:%ld\n\n",outcome); printf("Enter a number:"); } return 0; }

任务四
// 一元二次方程求解(函数实现方式)
// 重复执行, 直到按下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);
        }
    }    
}

 任务五
#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 k,sign=1,s1=0;//定义一个控制符号的变量
     for (int i = 1; i<=n ; i++){
        k=1;
        for (int j = 1; j<=i ; j++) {
            k*=j;//求阶乘
        }
         s1+=sign/k;
         sign*=(-1);//每一循环改变符号
     }
    return s1;

#include <stdio.h>
int isPrime(int x);
int main() {
      int k=0,p=1;//变量k用于计数,p用于控制格式
      for (int i = 101; i <=200 ; i++) {
          if(isPrime(i)){
              printf("%4d",i);
            k++;
              p++;
         }
         if (p%6==0){      //每打印五个数字换行,由于p初值为1,所以这里对6取余
             printf("\n");
             p=1;
         }
     }
     printf("\nThe number of primes between 100 and 200 is %d",k);
     return 0;
 }
 //素数判断函数
 int isPrime(int x){
     int i,k;
     if (x<2)
        k=0;
    for (i =2 ; i <= x; ++i) {
         if (x%i==0){
             k=0;
             break;
         }
     }
     if (i>=x)
         k=1;
    return k;
 }

实验七
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void printCharMan(int line, int col);  // 函数声明 
void printSpaces(int n); // 函数声明 

int main() {
    int line, col;
    
    for(line=5, col=5; col<=60; col++) {
        printCharMan(line, col);
        Sleep(50);  // 暂停50ms 
        system("cls");  // 清除屏幕 
    }
}

// 打印n个空格 
void printSpaces(int n){
    int i;
    
    for(i=1; i<=n; i++)
        printf(" ");
}

// 在第line行第col列打印一个字符小人 
void printCharMan(int line, int col) {
    int i, j;
    
    // 打印line-1行空行
    for(i=1; i<=line-1; i++)
        printf("\n");
    
    // 打印col-1个空格
    printSpaces(col-1);
    
    // 在第line行、第col列打印字符小人的头 
    printf(" O \n");
    
    // 打印col-1个空格
    printSpaces(col-1);
    
    // 在第line行、第col列打印字符小人的身体 
    printf("<H>\n");
    
    // 打印col-1个空格
    printSpaces(col-1);
    
    // 在第line行、第col列打印字符小人的腿 
    printf("I I\n");
}

 

posted @ 2021-04-15 19:19  小夏在线学c  阅读(122)  评论(0)    收藏  举报