'C'에 해당되는 글 116건

  1. 2007.11.02 do~while 반복문 by 청웨일
  2. 2007.11.02 while 반복문 by 청웨일
  3. 2007.11.02 반복문의 여러 형태 by 청웨일
  4. 2007.11.02 중첩된 if문 by 청웨일
  5. 2007.11.02 문자의 입력 by 청웨일

do~while 반복문

C : 2007. 11. 2. 16:20
 

*********************

do {

    실행문;

} while(조건식);

********************

문장이 최소 한번은 실행된다.

조건식이 참인 동안 계속 실행한다.


p.115_예제 1
#include <stdio.h>
int main(void)
{
   int a,b;
   char ch;

   printf("Do you want to:\n");
   printf("Add,Subtract, Multiply, or Divide?\n");


   do {
      printf("Enter first letter:");
      ch=getchar();
      printf("\n");
   } while(ch!='A' && ch!='S' && ch!='M' && ch!='D');
//사용자가 보기중 하나를 고를 때까지 반복한다.     

   printf("\n");

   printf("Enter first number:");
   scanf("%d", &a);
   printf("Enter second number:");
   scanf("%d", &b);


   if(ch=='A') printf("%d", a+b);
   else if(ch=='S') printf("%d", a-b);
   else if(ch=='M') printf("%d", a*b);
   else if(ch=='D' && b!=0) printf("%d", a/b);

   printf("\n\n");

   return 0;
}

p.116_예제 2
#include <stdio.h>
#include <conio.h>

int main(void)
{
 char ch;

 do{
  ch=getche();
 } while(ch!='q');

 printf("found the q");

 printf("\n\n");

 return 0;
}

p.116_1
#include <stdio.h>
#include <conio.h>

int main(void)
{
   float gallon;
   char ch;

   printf("gallon을 liter로 \nA를 누르면 프로그램이 끝납니다.");


   do{
      printf("\ngallon?");
      scanf("%f", &gallon);
      printf("\n%.1f gallon은 %.4f liter이다. enter", gallon, gallon*3.7854);
      ch=getche();
   }while(ch!='A');


   printf("\n");

   if(ch=='A') printf("End");

   return 0;
}

Posted by 청웨일

while 반복문

C : 2007. 11. 2. 16:17

* while(조건식) 실행문;

식이 참인 동안 문장이 계속 실행된다.

식이 거짓이면 문장이 한번도 실행되지 않을 수도 있다.


 

p.112_예제 1
#include <stdio.h>
#include <conio.h>

int main(void)
{
 char ch;

 ch = getche();

 while(ch!='q') ch = getche();  
//q가 입력되면 반복을 종료한다.
 printf("found the q");

 return 0;
}

#include <stdio.h>
#include <conio.h>

int main(void)
{
 char ch;

 printf("Enter your message.\n");

 ch = getche();
 while(ch!='\r') {
  printf("%c", ch+1);
  ch = getche();
 }
 return 0;
}

1_p.114
#include <stdio.h>

int main(void)
{
   float a,b;
   int c, i;

   printf("실행회수 : ");
   scanf("%d", &c);

   i=1;
   while (i<=c) {
      printf("거리입력:");
      scanf("%f", &a);
      printf("속도입력:");
      scanf("%f", &b);
      printf("걸리는 시간: %f\n", a/b);

      i++;
   }
  
   return 0;
}

2_ p.114
#include <stdio.h>
#include <conio.h>

int main(void)
{
 char ch;

 printf("Enter your message.\n");

 ch = getche();
 while(ch!='\r') {                           //\r 은 enter를 의미한다.
  printf("%c", ch-1);
  ch = getche();
 
 }
 return 0;
}

Posted by 청웨일

반복문의 여러 형태

C : 2007. 11. 2. 14:57
 

이 책에서 -알기쉽게 해설한 C (Herbert Schildt, 이한출판사) - for문의 유용성을 강조한다.

내부 식이 생략되거나 목표문을 생략하기도 한다.(C가 null문장을 허용하기 때문이다.)


*반복문 http://blog.naver.com/kkochi82/140036663480
*for반복문 http://blog.naver.com/kkochi82/140037112782


p.109_1
#include <stdio.h>
#include <conio.h>

int main(void)
{
 int i;
 char ch;

 ch='a';               //ch에 초기값을 치환
 
 for(i=0; ch!='q'; i++) {
  printf("pass: %d\n", i);
  ch = getche();
 }
 printf("\n\n");
 return 0;
}

p.110_예제 2
#include <stdio.h>

int main(void)
{
 printf("Enter an integer:");
 scanf("%d", &i);

 for(; i; i--) printf("%d", i);    //초기화부분 생략

 return 0;
}

p.110_예제 3
#include <stdio.h>

int main(void)
{
 char ch;

 for(ch=getche(); ch!='q'; ch=getche());       //목표문 생략
 printf("found the q");

 return 0;
}

p.111_예제 5
#include <stdio.h>

int main(void)
{
 int i;

 for(i=0; i<10; ) {
  printf("%d", i);
  i++;                               //증가식을 밖으로 내보낸다.
 }
 printf("\n\n");
 return 0;
}

연습문제 1 p.112
#include <stdio.h>

int main(void)
{
   float a,b;
   int c, i;

   printf("실행횟수:");
   scanf("%d", &c);

   for(i=1; i<=c; i++) {
      printf("거리입력:");
      scanf("%f", &a);
      printf("속도입력:");
      scanf("%f", &b);
  
      printf("걸리는 시간: %f\n", a*b);
   }
   return 0;
}

연습문제 2 p.112
#include <stdio.h>

int main(void)
{
 int i;

 printf("숫자입력:");
 scanf("%d", &i);


 for(i; i; i--) ;
 printf("\a");

 return 0;
}

연습문제 3
#include <stdio.h>

int main(void)
{
 int i;
 for(i=1; i<1001; i=i+i) printf("%d ", i);

 return 0;
}

Posted by 청웨일

중첩된 if문

C : 2007. 11. 2. 13:24

if(조건문) {

   if(조건문)  실행문;

}

- 중첩은 15단계까지 허용한다.(ANSI 표준 컴파일러)


if문이 다른 if나 else의 목표문이 될때.

  if(조건식)문장
  else
     if(조건식)문장
  else
     if(조건식)문장
  .
  .
  .
     else 문장


//참인 조건식을 만날때까지 조건 검사.
//모든 조건식이 거짓이면 마지막 else 문장이 실행된다.
//마지막 else 문장이 없으면 아무것도 실행하지 않는다.

//일치되는 문자를 만나면 남은 if문을 건너뛸 수 있다.
//불필요한 연산에 시간을 낭비하지 않는다.

 

p.107_1
#include <stdio.h>
int main(void)
{
   int a,b;
   char ch;

   printf("Do you wantto:\n");
   printf("Add, Suvtract, Muktiply, or Divide?\n");
   printf("Enter first letter:");

   ch = getchar();
   printf("\n");

   printf("Enter first number:");
   scanf("%d", &a);
   printf("Enter second number:");
   scanf("%d", &b);

   if(ch=='A') printf("%d", a+b);
   else if(ch=='S') printf("%d", a-b);
   else if(ch=='M') printf("%d", a*b);
   else if(ch=='D' && b!=0) printf("%d", a/b);
  
   return 0;
}

p.107_2
#include <stdio.h>
int main(void)
{
 int answer, count;

 for(count=1; count<11; count++) {
     printf("What is %d+%d?", count, count);
  scanf("%d", &answer);
  if(answer == count+count) printf("Right");
  else {
   printf("Sorry, you're wrong\n");
   printf("Try again.\n");
   printf("\nWhat is %d + %d?", count, count);
   scanf("%d", &answer);

   if(answer == count+count) printf("Right\n");
   else
    printf("Wrong, the answer is %d\n", count+count);
  }
 }
 return 0;
}

p.108_2
#include <stdio.h>

int main(void)
{
   float a, b;
   int c;

   printf("1:원의 넓이, 2:사각형의 넓이, 3:삼각형의 넓이\n");
   printf("어떤 것의 넓이를 구할 것인지 선택하세요.:");
   scanf("%d", &c);


  if(c == 1) {
      printf("반지름을 입력 : ");
      scanf("%f", &a);
      printf("\n원의 넓이는 %.2f입니다.", (a*a)*3.14);
   }
   else if(c == 2) {
      printf("가로입력 :");
      scanf("%f", &a);
      printf("세로입력 :");
      scanf("%f", &b);
      printf("\n사각형의 넓이는 %.2f입니다.", a*b);
   }
   else if(c == 3) {
      printf("밑변입력:");
      scanf("%f", &a);
      printf("높이입력:");
      scanf("%f", &b);
      printf("\n삼각형의 넓이는 %.2f입니다.", (a*b)/2);
   }
   return 0;
}

Posted by 청웨일

문자의 입력

C : 2007. 11. 2. 11:33
 

getchar()  -  입력된 한 문자를 반환,   하나의 키를 누를 때까지 기다린다.

getche()   -   하나의 키를 누르면 즉시 반환한다. 
                   라인 버퍼 입력.  헤더 파일 <conio.h> 필요.


p.101
#include <stdio.h>
int main(void)
{
 char ch;

 ch = getchar();
 printf("you typed : %c, ch);

 return 0;
}

예제 1 p.103
#include <conio.h>
#include <stdio.h>

int main(void)
{
 char ch;
 printf("enter a character: ");
 ch = getche();
 printf("\nIts ASCII code is %d", ch);
 
 printf("\n\n");

 return 0;
}

ㄴgetchar()
#include <stdio.h>

int main(void)
{
 char ch;
 printf("enter a character: ");
 ch = getchar();
 printf("\nIts ASCII code is %d", ch);

 printf("\n\n");

 return 0;
}

예제2 p.104
#include <stdio.h>
int main(void)
{
 int a, b;
 char ch;
 printf("Do you want to:\n);
 printf("Add, Subtract, Multiply, or Divide?\n");
 printf("Enter first letter:");
 ch = getchar();
 printf("\n");

 printf("Enter first number:");
 scanf("%d", &a);
 printf("Enter second number:");
 scanf("%d", &b);

 if(ch=='A') printf("%d", a+b);
 if(ch=='S') printf("%d", a-b);
 if(ch=='M') printf("%d", a*b);
 if(ch=='D' && b!=0) printf("%d", a/b);

 return 0;
}

연습문제 1  p.105
#include <stdio.h>
#include <conio.h>

int main(void)
{
 int i;
 char ch, j='z';

 printf("문자 10 개 입력:\n");
 
 for(i=1; i<11; i++) {
  ch = getche();
  if(ch<j) j=ch;
 }
 printf("%c", j);

 printf("\n\n");
 return 0;
}

Posted by 청웨일