实验3 循环语句

时间:2019-11-17
本文章向大家介绍实验3 循环语句,主要包括实验3 循环语句使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

part1:验证性内容

一元二次方程求解

// 一元二次方程求解
// 重复执行, 直到按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_s("%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;
}

part2补足程序,使程序符合题目要求并正确运行

编程找出5个整数中的最大数和最小数,并输出找出的最大数和最小数。

#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;
}

part3:编程练习

输出101~200之间素数和素数个数

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

int is_prime(int n)
{
    double m=sqrt((double)n);
    for(int i=2;i<=m;i++)
        if(n%i==0)return 0;
    return 1;
}

int main()
{
    int s=0;
    for(int i=101;i<=200;i++)
    {
        if(is_prime(i))
        {
            printf("%d ",i);
            s++;
        }
        else
            continue;
        if(s%5==0)printf("\n");
    }
    printf("101~200之间共有%d个素数\n",s);

    system("pause");
    return 0;
}

将一个长整型数s的每一数位上的奇数依次取出来,构成一个新的数,起高位仍在高位,低位仍在低位。

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

int main()
{
    long s;
    printf("Enter a number : ");
    scanf_s("%ld", &s);
    char a[20] = { 0 };

    _itoa_s(s, a, 10);

    for (int i = 0; i <= strlen(a); i++)
        if (a[i] % 2 != 0)printf("%c", a[i]);

    system("pause");
    return 0;
}

s=1/a+2/aa+……

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

int main()
{
    int n, a;
    printf("Enter n and a:");
    scanf_s("%d %d", &n, &a);
    int p = 0;
    double s = 0;

    for (int i = 1; i <= n; i++)
    {
        p = p * 10 + a;
        s = s + (double)i / (double)p;
    }
    printf("s=%lf", s);

    system("pause");
    return 0;
}

 实验总结与体会

将数值转化成字符串有时候特别方便

原文地址:https://www.cnblogs.com/faspk/p/11878030.html