본문 바로가기

Contact English

【코딩】 C 언어로 계산기(+, -, ×, ÷) 구현하기

 

C 언어로 계산기(+, -, ×, ÷) 구현하기

 

추천글 : 【C 언어】 C 언어 목차


#include <stdio.h>
#include <stdlib.h>
#define Max 100
/* This source is for calculation at practical input situation */
// Devision on this calculator prints quota. 
// If you want real Devision, you should change the format (Now the format is 'int')

int Calculator_I(char *Array, int Array_Size); // seperator; seperate by each monomials; + and -
int Calculator_II(char *Array, int Array_Size); // calculate a monomial; * and /

int Calculator_I(char *Array, int Array_Size){ // 'a*(b+c) + d'  >>  'a*(b+c)' and 'd'
	if(Array_Size == 0)	return 0; // for the case that initial number < 0
	int i = 0; 
	int j, k;	
	int count = 0; // count = 0  "="  peripheral state 
	int count2; // for '-' operation
	while(1){
		if(i > Array_Size - 1){	// only monomial can reach this stage
			return Calculator_II(Array, Array_Size);
		}						
		if(Array[i] == '(')	count ++;
		else if(Array[i] == ')') count --;
		else if(count == 0 && 
				(Array[i] == '+' || Array[i] == '-')){ // only polynomial can reach this stage
			if(Array[i] == '+'){
				char Array_2[Array_Size - i - 1]; // number(i+1 ~ Array_Size-1)
				for(j = i + 1; j < Array_Size; j++){
					Array_2[j - i - 1] = Array[j]; // if j = i + 1, j - i - 1 = 0
				}				
				return Calculator_I(Array, i) + Calculator_I(Array_2, Array_Size - i - 1);	
			}
			else if(Array[i] == '-'){
				j = i + 1;
				count2 = 0;
				while(1){
					if(j > Array_Size - 1){	// only 'A-B' can reach this stage
						char Array_2[Array_Size - i - 1]; // number(i+1 ~ Array_Size-1)
						for(k = i + 1; k < Array_Size; k ++){
							Array_2[k - i - 1] = Array[k];
						}
						return Calculator_I(Array, i) - Calculator_I(Array_2, Array_Size - i - 1);			
					}
					if(Array[j] == '(')    	count2 ++;
					else if(Array[j] == ')')	count2 --;
					else if(count2 == 0 && Array[j] == '+'){ // only 'A-B+C...' can reach this stage
						char Array_2[j - i - 1]; // number(i+1 ~ j-1)
						char Array_3[Array_Size - j - 1]; // number(j+1 ~ Array_Size-1)
						for(k = i + 1; k < j; k ++){
							Array_2[k - i - 1] = Array[k];
						}
						for(k = j + 1; k < Array_Size; k ++){
							Array_3[k - j - 1] = Array[k];
						}
						return Calculator_I(Array, i) - Calculator_I(Array_2, j - i - 1)
									+ Calculator_I(Array_3, Array_Size - j - 1);
					}
					j ++;
				}
				char Array_2[Array_Size - i - 1 + 2]; // '2' indicates '(-1)' and '*'
				Array_2[0] = '0' - 1;
				Array_2[1] = '*';				
				for(j = i + 1; j < Array_Size; j ++){
					Array_2[j - i - 1 + 2] = Array[j];	
				}
				return Calculator_I(Array, i) + Calculator_I(Array_2, Array_Size - i - 1 + 2);			
			}			
		}	
		i ++;
	}
}

int Calculator_II(char *Array, int Array_Size){
	int i = 0;
	int j, k;	
	int count = 0; // count = 0  "="  peripheral state	
	while(1){	
		if(i > Array_Size - 1){ // only '()' or number can reach this stage
			if(Array[0] == '('){ //    (---)    →    ---
				char Array_2[Array_Size - 2];	
				for(j = 1; j <= Array_Size - 2; j ++){
					Array_2[j - 1] = Array[j];
				}
				return Calculator_I(Array_2, Array_Size - 2);
			}
			int test = 1; // only number can reach this stage
			int test2 = 0; // below commands print the number as it is
			for(j = 0; j < Array_Size - 1; j ++)	test *= 10;
			for(j = 0; j < Array_Size; j ++){
				test2 += (Array[j] - '0') * test;
				test /= 10;
			}
			return test2;
		}
		if(Array[i] == '(')    	count ++;
		else if(Array[i] == ')')	count --;
		else if(count == 0 && Array[i] == '*'){	// only 'A*B...' can reach this stage
			char Array_2[Array_Size - i - 1]; // number(i+1 ~ Array_Size-1)
			for(j = i + 1; j < Array_Size; j ++){
				Array_2[j - i - 1] = Array[j]; // if j = i + 1, j - i - 1 = 0
			}
			return Calculator_I(Array, i) * Calculator_I(Array_2, Array_Size - i - 1);			
		}
		else if(count == 0 && Array[i] == '/'){
			j = i + 1;
			while(1){
				if(j > Array_Size - 1){ // only 'A/B' can reach this stage
					char Array_2[Array_Size - i - 1]; // number(i+1 ~ Array_Size-1)
					for(k = i + 1; k < Array_Size; k ++){
						Array_2[k - i - 1] = Array[k];
					}	
					return Calculator_I(Array, i) / Calculator_I(Array_2, Array_Size - i - 1);
				}	
				if(Array[j] == '*'){ // only 'A/B*C...' can reach this stage
					char Array_2[j - i - 1]; // number(i+1 ~ j-1)
					char Array_3[Array_Size - j - 1]; // number(j+1 ~ Array_Size-1)
					for(k = i + 1; k < j; k ++){
						Array_2[k - i - 1] = Array[k];
					}
					for(k = j + 1; k < Array_Size; k ++){
						Array_3[k - j - 1] = Array[k];
					}					
					return Calculator_I(Array, i) / Calculator_I(Array_2, j - i - 1) 
													* Calculator_I(Array_3, Array_Size - j - 1);					
				}
				j ++;				
			}
		}
		i ++;
	}
}

int main(int argc, char *argv[]) {
	char Array[Max];
	int i;
	scanf("%s", Array);
	for(i = 0; 1; i ++){
		if(Array[i] == 0)	break;
	}
	printf("%d", Calculator_I(Array, i)); // number(1 ~ i-1)
	return 0;
}

 

입력: 2013.07.11 21:52

수정: 2023.06.16 11:46