반응형


#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define PLUS 43
#define MINUS 45
#define MUL 42
#define DIV 47

int main()
{
 char data1[100];
 char data2[100];
 char inputsource[100]; // 입력받는 문자열
 
 int oper_loc = 0, oper = 0, len = 0;// 연산자 위치 , 연산자, 입력문자의 길이
 
 while (1) {
  printf("입력 해 주세요 : ");
  scanf_s("%s", &inputsource, sizeof(inputsource));
  len = strlen(inputsource); // 전체 길이 구하는 함수 (strlen)


  //연산자 위치
  for (int i = 0; i < 100; i++) {
   if (inputsource[i] >= MUL && inputsource[i] <= DIV) { // 연산자 찾기
    oper = inputsource[i];// 연산자 저장
    oper_loc = i; // 연산자의 위치를 저장
    break;
   }
  }

  //변수 분리
  // 첫번 째 숫자 분리해서 data1[100] 배열에 저장
  for (int i = 0; i < oper_loc; i++) {
   data1[i] = inputsource[i];
  }
  data1[oper_loc] = 0;

  // 두번 째 숫자 분리해서 data1[100] 배열에 저장
  for (int i = oper_loc + 1; i < len; i++) {
   data2[i - oper_loc - 1] = inputsource[i];
  }
  data2[len - oper_loc - 1] = 0;

  //계산
  if (oper == PLUS)printf("%d\n", atoi(data1) + atoi(data2));
  if (oper == MINUS)printf("%d\n", atoi(data1) - atoi(data2));
  if (oper == MUL)printf("%d\n", atoi(data1) * atoi(data2));
  if (oper == DIV)printf("%d\n", atoi(data1) / atoi(data2));
 }
 return 0;
}

반응형

+ Recent posts