C 언어 및 프로그램 설계 초보 절차 - 24if 문장의 삽입

1952 단어
하 선생님 강의 링크 C 언어 및 프로그램 설계 초보 본 강의
응용 프로그램: 세그먼트 함수
#include <stdio.h>
int main()
{
    float x, y;
    scanf("%f", &x);
    if(x<2)
    {
        y=x;
    }
    else if(x<6)
    {
        y=x*x+1;
    }
    else if(x<10)
    {
        y=sqrt(x+1);
    }
    else
    {
        y=1/(x+1);
    }
    printf("%f
", y); return 0; }
//     
#include <stdio.h>
int main()
{
    float x, y;
    scanf("%f", &x);
    if(x<2)
    {
        y=x;
    }
    if(x>=2&&x<6)
    {
        y=x*x+1;
    }
    if(x>=6&&x<10)
    {
        y=sqrt(x+1);
    }
    if(x>=10) 
    {
        y=1/(x+1);
    }
    printf("%f
", y); return 0; }

일원 이차 방정식의 뿌리를 구하기(ax2+bx+c=0)
#include <stdio.h>
int main()
{
  float a,b,c,x1,x2;
  scanf("%f %f %f", &a, &b, &c);
  if ((b*b-4*a*c)>=0)
  {
    if ((b*b-4*a*c)>0)
    {
      x1=(-b+sqrt(b*b-4*a*c))/(2*a);
      x2=(-b-sqrt(b*b-4*a*c))/(2*a);
      printf("       :x1=%.2f x2=%.2f
", x1, x2); } else { x1=-b/(2*a); printf(" ,x1=x2=%.2f
", x1); } } else { printf(" !
"); } return 0; }

1원 2차 방정식의 루트 - 개선된 코드 구하기
#include <stdio.h>
int main(){
    float a,b,c,x1,x2,delta;
    scanf("%f %f %f", &a, &b, &c);
    delta = b*b-4*a*c;  //   
    if (delta>=0){
        if (delta>0){
            x1=(-b+sqrt(delta))/(2*a);
            x2=(-b-sqrt(delta))/(2*a);
            printf("       :x1=%.2f x2=%.2f
", x1, x2); } else{ x1=-b/(2*a); printf(" ,x1=x2=%.2f
", x1); } } else{ printf(" !
"); } }

좋은 웹페이지 즐겨찾기