반응형
다음과 같이 연산의 이름을 문자열로 받아서 해당 연산을 실행하는 프로그램을 작성하라. 연산을 나타내는 문자열은 "add", "sub", "mul", "div"으로 하라.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h> #include <string.h> #include <stdlib.h> #define SIZE 80 int compare(char *a); int main(void) { char a[SIZE]=""; char seps[]=" "; char *ope, *x, *y; int i,j,k; printf("연산을 입력하세요: "); gets(a); ope=strtok(a,seps); x=strtok(NULL,seps); y=strtok(NULL,seps); i=compare(ope); j=atoi(x); k=atoi(y); printf("연산의 결과: "); if(i==0) printf("%d\n", j+k); else if(i==1) printf("%d\n", j-k); else if(i==2) printf("%d\n", j*k); else if(i==3) printf("%d\n", j/k); return 0; } int compare(char *a) { if(strncmp(a,"add",3)==0) return 0; else if(strncmp(a,"sub",3)==0) return 1; else if(strncmp(a,"mul",3)==0) return 2; else if(strncmp(a,"div",3)==0) return 3; } | cs |
반응형