实验三

时间:2019-11-18
本文章向大家介绍实验三,主要包括实验三使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
// 一元二次方程求解
// 重复执行, 直到按Ctrl+D或Ctrl+E结束
//  
#include <math.h>
#include <stdio.h>
#include <stdlib.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)) {
        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 = %f, x2 = %f\n", x1, x2);
            }
            else {
                real = -b/(2*a);
                imag = sqrt(-delta) / (2*a);
                printf("x1 = %f + %fi, x2 = %f - %fi\n", real, imag, real, imag);
            }
        }
        
        printf("Enter a, b, c:\n");
    }
    system("pause");
    return 0;
} 
// 猜数游戏
// 程序运行时自动生成1~100之间的随机数,提示用户猜
// 如果用户猜的数等于随机数,提示用户猜对了,程序结束
// 否则,如果用户猜的数小于随机数,提示用户低了,用户继续猜
// 如果用户猜的数大于随机数,提示用户高了,用户继续猜 
// 程序循环运行直到用户猜对为止

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

int main() {
    int guessNumber; // 存放程序运行时生成的1~100之间的随机整数 
    int ans;     // 存放用户猜的数字 
    
    srand((time(0)));  // 以时间函数作为随机种子 
    guessNumber = 1 + rand()%100;  // 生成1~100之间的随机数 
    
    do {
        printf("your guess number is(1~100): ");
        scanf("%d", &ans);
        if(ans < guessNumber)
            printf("%d is lower than the number.\n", ans);
        else if(ans > guessNumber) 
            printf("%d higher then the number.\n", ans); 
    }while(ans != guessNumber);
    
    printf("Congratulations. you hit it~\n");    
    
    system("pause"); // 在devc中运行时,这一行可以去掉
     
    return 0;
} 
/* 
编程找出5个整数的最大数和最小数 
《C语言程序设计教程学习指导》p122实验内容(3) 
*/ 

#include <stdio.h>
#include <stdlib.h>
int main() {
    int number, max, min, n;
    
    n=1;
    printf("输入第%d个数: ", n);
    scanf("%d", &number);
    max = number;
    min = number;
    
    while(/*补足①*/n<5) {
        n++;
        printf("输入第%d个数: ", n);
        scanf("%d", &number);    

        if(/*补足②*/number>=max)
            max = number;
        else if(/*补足③*/number<=min)
            min = number;
    }
    
    printf("最大数为: %d\n", max);
    printf("最小数为: %d\n", min);
    
    system("pause");
    
    return 0;
} 
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
    int i,m;
    for(i=101;i<=200;i++)
    {
        for(m=2;m<=sqrt(i);++m)
         if(i%m==0)break;
        if(m>sqrt(i))
        printf("%4d",i);

        
    }
    printf("\n101~200之间共有21个素数");
    return 0;
}
#include<stdio.h>
int main()
{   long int s;
    int m,i,n;
    m=0;
    n=1;
    printf("Enter a number:");
    scanf("%ld",&s);
    while(s>0){
    
        i=s%10;
        s=s/10;
        
        if(i%2!=0)
        {
            m=m+i*n;
            n=n*10;
        }
    }
    printf("new number is:%d\n",m);
    return 0;
} 
#include<stdio.h> 
int main()
{ int a,n,t=1,b=1;
  double s,m=0;
  printf("Enter n and a:\n");
  scanf("%d%d",&n,&a);
  while(t<=n){
      m=m+b*a;
      b=b*10;
      s=s+t/m;
      t++;
  }
  printf("s=%f\n",s);
  return 0;
}

原文地址:https://www.cnblogs.com/wy0302/p/11881142.html