You are basically looking at two problems here, (1)how you want to implement your lookup table, and then (2)how to perform the calculations:
1. The easiest version of a lookup table would simply to make an array of structures, whereas the structures contained two members: the conversion "name" and then the formula.
2. The second part of your problem is processing the formula, which is essentially just a "create a calculator" problem. There are many ways to do this, you could look at how RPN is calculated to give you an idea.
Here is an example that uses a very simple calculator just to give you the idea:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct table{
char conversion[50];//user inputted conversion
char formula[50];//holds 50 char to implement formula
};
int simpleCalculator(char [], int);
int main(void){
char myInput[10]={0}, inputbuffer[100]={0};
int num1=0, answer=0;
//implement our lookup table, this would be loading from a file
struct table LookupTable[]= {{"c->k","+ 273"},
{"km->m","* 1000"}};
//obtaining user input, this would be cmd line args entered
//like: m->km 3000
printf("Enter cmdLine args:");
fgets(inputbuffer,sizeof(inputbuffer),stdin);
sscanf(inputbuffer,"%s %d", myInput, &num1);
//------------------------------------------------
//now we find the value in our lookup table and call our calculator
for(int i = 0; i < (sizeof(LookupTable)/sizeof(table));i++){
if(strcmp(LookupTable[i].conversion,myInput)==0){
answer=simpleCalculator(LookupTable[i].formula, num1);
break;
}
}
printf("Answer = %d", answer);
getchar();
return (0);
}
int simpleCalculator(char formula[],int input){
int factor=0, answer=0;
char operand;
sscanf(formula,"%c %d", &operand, &factor);
switch(operand){
case '*':
answer = input * factor;
break;
case '+':
answer = input + factor;
break;
}
return answer;
}