Calculator C: inputting both operator signs and integers to perform calculations

So I understand the basic concepts of making a simple calculator, such as asking user for two int values a,b and then asking them which operating sign they want to use. But I want to create something more complex and usable.

My method is to scan int values and operator signs separately, so first it will scan into int, then into string??? the input would be something like: 1 (enter) '/' (enter) 2 (enter) '+' (enter) 4(enter) and then the user can press x to end and calculate.

 int main()
{
int array_int[30];
char array_operators[30];
int hold_value = 0;
int i = 0;
printf("Enter your calculations, press enter after each number and operator is entered n");
while(1==1){
    scanf("%i",&hold_value); //Use this to decide which array to put it in.
    if(isdigit(hold_value)){
      array_int[i] = hold value // Check if input will be an int or char to decide which array to store it in??

}

I still need a way of ending the loop on user input, and I know that my logic that I put into the conditional makes no sense, but I'm new to C and I don't know all my options. Hopefully my goals were stated clear enough for you guys to help me out. Thanks


改变你当前的代码,

int main()
{
  int array_int[30]={0};
  char array_operators[30]={0}; //Initialize variables. It is a good practice
  char hold_value; //hold value must be a char
  int i = 0, j = 0;
  printf("Enter your calculations, press enter after each number and operator is entered, press Q to quit n");
  while(1){
      scanf(" %c",&hold_value); //Note the space before %c. It skips whitespace characters

      if(hold_value=='Q') //break the loop if character is Q
        break;
      if(isdigit(hold_value)){ // If input is a digit
        array_int[i++] = hold_value-'0'; //Store the integer in array_int
      }
      else{ //Input is a character
        array_operators[j++] = hold_value;
      }

  }

  //Calculate from here

  return 0;
}

If you want to end the loop when there is nothing to return just use return(0).
If you want to end the programm than exit(0).
Also, please check the following:
http://forum.codecall.net/topic/50733-very-simple-c-calculator/

链接地址: http://www.djcxy.com/p/65008.html

上一篇: 在内核OpenCL中实现FIFO的最佳方法

下一篇: 计算器C:输入操作符和整数来执行计算