Skip to content

Instantly share code, notes, and snippets.

@chasinglogic
Created December 5, 2013 15:22
Show Gist options
  • Save chasinglogic/7807348 to your computer and use it in GitHub Desktop.
Save chasinglogic/7807348 to your computer and use it in GitHub Desktop.
Attempt at making a dumb little calculator to help me learn c type conversions.
#include <stdio.h>
int add(int, int);
int analyze(char*);
int operate(char, int, int);
int main() {
char input[10];
printf("Welcome to mat's calculator 0.0.0.1\n");
scanf("%s", input);
printf("%d\n", analyze(input));
}
int add(int x, int y) {
return x + y;
}
int analyze(char s[]){
int i, operated;
char operator;
int num1, num2;
for(i = 0; i < 10; ++i){
printf("%c\n", s[i]);
if(s[i] == '+'){
operator = '+';
operated = 1;
printf("Operated, Operator == %c\n", operator);
} else if (s[i] == ' ') {
;
} else {
if (operated == 1) {
num2 = (num2 * 10) + (s[i] - '0');
} else {
num1 = (num1 * 10) + (s[i] - '0');
}
}
}
return operate(operator, num1, num2);
}
int operate(char op, int v, int z) {
switch(op){
case '+':
return add(v, z);
default:
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment