C 语言教程 在线

1595c-examples-quadratic-roots

一元二次方程可能有两个实数解,或者一个实数解,或者无实数解。

根据输入的三个系数 a、b、c 的判断解的情况:

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

int main(void)
{
    double a ,b , c;
    double delat;
    double x1, x2;
    char ch;
    do
    {

        
        
        printf("请输入一元二次方程的三个系数:\n");
        
        printf("请输入系数 a = \t");
        scanf("%lf",&a);
        
        printf("请输入系数 b = \t");
        scanf("%lf",&b);
        
        printf("请输入系数 c = \t");
        scanf("%lf",&c);
        
        delat = b*b-4*a*c;
        
        if(delat>0)
        {
            x1= ( -b+sqrt(delat) )/2;
            x2= ( -b-sqrt(delat) )/2;
            printf("有2个实数解:x1 = %lf  x2 = %lf\n",x1,x2);
        }
        else if(delat==0)
        {
            x1=( -b+sqrt(delat) )/2;
            printf("有2个相等实数解:x1=x2 = %lf\n",x1);
        }
        else
            printf("无实数解\n");

        printf("是否继续吗:Y/N\n");
        scanf(" %c",&ch);
    }while(ch=='y'||ch=='Y');
    return 0;
}

1594c-examples-largest-number-three

参考方法:

#include <stdio.h>
/*
    判断三个数中的最大数
*/
int getMax(int a, int b, int c)
{
    int max=a;
    if(b>max)
    {
        max = b;
    }
    if(c>max)
    {
        max = c;
    }
    return max;
}


int main()
{
    int a, b, c;
    printf("请输入三个整数,用空格分隔:");
    scanf("%d %d %d",&a, &b, &c);
    printf("最大值是:%d",getMax(a, b, c));
    return 0;
}

1593c-examples-largest-number-three

用三元表达式判断:

#include <stdio.h>

int main()
{
  int a, b, c, max;
  printf("请输入三个数,用空格分割: ");
  scanf("%d %d %d", &a, &b, &c);
  max =  a > b ? ( a > c ? a : c ) : ( b  > c ? b : c);
  printf("最大值是:%d", max);
  return 0;
}

1592c-examples-largest-number-three

参考方法:

#include<stdio.h>

int main()
{
    int a,b,c,max;
    printf("请输入三个数,用空格隔开:");
    scanf("%d %d %d",&a,&b,&c);
    if(a>b){
        max=a;
    } else {
        max=b;
    }
    if(max>c){
        printf("最大值是%d",max);
    } else{
        max=c;
        printf("最大值是%d",max);
    }
    return 0;
}

1591c-examples-vowel-consonant

完善一楼多个字符输入错误的问题:

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

int main()
{
    char cLetter,b,a;
    int iInRange, iCheck, i, iLetter;
    i = 1;
    while (i)
    {
        printf("输入一个字母, 判断元音与辅音. 如果想退出,请输入“0”。\n");
        scanf("%c", &a);
        cLetter=a;
    scanf("%c", &a);

        while(a!=10 )
        {  

        scanf("%c", &a);
        }
        //getchar();//getchar is must get a key input and CR
        printf("----------------------------------------------------------------------\n");
        iLetter = (int)cLetter;
        iCheck = isalpha(iLetter);
        if (iCheck)
        {
            iInRange = (cLetter == 'a') || (cLetter == 'e') || (cLetter == 'i') || (cLetter == 'o') || (cLetter == 'u') || (cLetter == 'A') || (cLetter == 'E') || (cLetter == 'I') || (cLetter == 'O') || (cLetter == 'U');
            if (iInRange)
            {
                printf("字母 %c 是元音字母!\n", cLetter);
                printf("----------------------------------------------------------------------\n");
                continue;
            }
            else
            {
                printf("字母 %c 是辅音字母!\n", cLetter);
                printf("----------------------------------------------------------------------\n");
                continue;
            }
        }
        else if (iCheck == 0)
        {
            if (iLetter != 48)
            {
                printf("Error input!\n");
                printf("----------------------------------------------------------------------\n");
                continue;
            }
            else if ((int)cLetter == 48)
            {
                printf("Bye bye~~~!\n");
                printf("----------------------------------------------------------------------\n");
                i = (int)cLetter - 48;
            }
        }
    }
    return 0;
}