如何使 getch() 函数采用双字符而不是一个字符?

how to make getch() function takes double character instead of one?

我只想通过 (get char) 或任何其他方式放置多个字符,这是一个使用 switch cases 条件的简单计算器程序,但无法选择具有多个输入到 getch 函数例如:10,11....

#include <stdio.h>
#include <conio.h>
void plus_func();
void menus_func();
void mul_func();
void div_func();
void modulus_func();
void shiftleft_func();
void shiftright_func();
void and_func();
void or_func();
void not_func();
void doubleand_func();
void doubleor_func();
void notequal_func();
void xor_func();
void main(void){

int x,y, res;
int m;
int op1;
int op2;
int op3;
int i=0;
int k;
while(i==0){
 printf("Enter any key to start the calculator function program or escape to exit it \n");
 k=getch();
 if(k!=0x1b)
{
 printf("enter the first num or press escape to exit \n"); 
 x=getch();
 if(x==0x1b)
 {
    break;
 }
 else
 {
    op1=x-48;
 }
  printf("enter the second num or press escape to exit \n"); 
  y=getch();

  if(y==0x1b)
  {
    break;
  }
  else
  {
    op2=y-48;
  }
  printf("enter the type of the operation \n",m);
  m=getch();
  if(m==0x1b)
  {
    break;
  }
  else
  {
    op3=m-48;
  }
  switch(op3)
  {

     case 1: plus_func(op1,op2);break;

     case 2: menus_func(op1,op2);break;

     case 3: mul_func(op1,op2);break;

     case 4: div_func(op1,op2);break;

     case 5: modulus_func(op1,op2);break;

     case 6: shiftleft_func(op1,op2);break;

     case 7: shiftright_func(op1,op2);break;

     case 8: and_func(op1,op2);break;

     case 9: or_func(op1,op2);break;

     case 10: not_func(op1,op2);break;

     case 11: doubleand_func(op1,op2);break;

     case 12: doubleor_func(op1,op2);break;

     case 13: notequal_func(op1,op2);break;

     case 14: xor_func(op1,op2);break;

     default: printf("error");
     break;
  }
}
  else
  {
    break;
  }
}
}

  void plus_func(int a, int b)
{
    int c;
    c=a+b;
    printf("the result is %d \n",c);
}

  void menus_func(int a, int b)
{
    int c;
    c=a-b;
    printf("the result is %d \n",c);
}

  void mul_func(int a, int b)
{
    int c;
    c=a*b;
    printf("the result is %d \n",c);
}
  void div_func(int a, int b)
{
    int c;
    c=a/b;
    printf("the result is %d \n",c);
}
  void modulus_func(int a, int b)
{
    int c;
    c=a%b;
    printf("the result is %d \n",c);
}
  void shiftleft_func(int a, int b){
    int c;
    c=a<<b;
    printf("the result is %d \n",c);
}
  void shiftright_func(int a, int b)
{
    int c;
    c=a>>b;
    printf("the result is %d \n",c);
}
  void and_func(int a, int b)
{
    int c;
    c=a&b;
    printf("the result is %d \n",c);
}
  void or_func(int a, int b)
{
    int c;
    c=a|b;
    printf("the result is %d \n",c);
}
  void not_func(int a, int b)
{
    int c;
    c=a+b;
    c=~c;
    printf("the result is %d \n",c);
}
  void doubleand_func(int a, int b)
{
    int c;
    c=a&&b;
    printf("the result is %d \n",c);
}
  void doubleor_func(int a, int b)
{
    int c;
    c=a||b;
    printf("the result is %d \n",c);
}
  void notequal_func(int a, int b)
{
    int c;
    c=a+b;
    c=!c;
    printf("the result is %d \n",c);
}
  void xor_func(int a, int b)
{
    int c;
    c=a^b;
    printf("the result is %d \n",c);
}
scanf("%d",&k);

就是你需要的。

更安全:

scanf_s("%d",&k);

嗯,getch ()不能超过两个字符。您可以使用 getline (),但我认为这不是您所需要的。您正在阅读数字,而不是字符。也一样

scanf ("%d", &k);

或使用 @DivinCodino 的回答中提到的 scanf_s。另外,如果您想阅读字符,请如上所述使用 getline 或使用 scanf:

scanf (" %c", &k);

并将 k 声明为

char k;

此外,您当前的程序将无法正常工作,因为它正在尝试使用 getch 读取整数,而 getch 专为读取字符而设计。

进行上述修改后,代码为:

#include <stdio.h>
#include <conio.h>
void plus_func();
void menus_func();
void mul_func();
void div_func();
void modulus_func();
void shiftleft_func();
void shiftright_func();
void and_func();
void or_func();
void not_func();
void doubleand_func();
void doubleor_func();
void notequal_func();
void xor_func();
void main(void){

int x,y, res;
int m;
int op1;
int op2;
int op3;
int i=0;
int k;
while(i==0){
 printf("Enter any key to start the calculator function program or escape to exit it \n");
 k=getch();
 if(k!=0x1b)
{
 printf("enter the first num or press escape to exit \n"); 
 scanf ("%d", &x);
 if(x==0x1b)
 {
    break;
 }
 else
 {
    op1=x-48;
 }
  printf("enter the second num or press escape to exit \n"); 
  scanf ("%d", &y);

  if(y==0x1b)
  {
    break;
  }
  else
  {
    op2=y-48;
  }
  printf("enter the type of the operation \n",m);
  scanf ("%d", &m);
  if(m==0x1b)
  {
    break;
  }
  else
  {
    op3=m-48;
  }
  switch(op3)
  {

     case 1: plus_func(op1,op2);break;

     case 2: menus_func(op1,op2);break;

     case 3: mul_func(op1,op2);break;

     case 4: div_func(op1,op2);break;

     case 5: modulus_func(op1,op2);break;

     case 6: shiftleft_func(op1,op2);break;

     case 7: shiftright_func(op1,op2);break;

     case 8: and_func(op1,op2);break;

     case 9: or_func(op1,op2);break;

     case 10: not_func(op1,op2);break;

     case 11: doubleand_func(op1,op2);break;

     case 12: doubleor_func(op1,op2);break;

     case 13: notequal_func(op1,op2);break;

     case 14: xor_func(op1,op2);break;

     default: printf("error");
     break;
  }
}
  else
  {
    break;
  }
}
}

  void plus_func(int a, int b)
{
    int c;
    c=a+b;
    printf("the result is %d \n",c);
}

  void menus_func(int a, int b)
{
    int c;
    c=a-b;
    printf("the result is %d \n",c);
}

  void mul_func(int a, int b)
{
    int c;
    c=a*b;
    printf("the result is %d \n",c);
}
  void div_func(int a, int b)
{
    int c;
    c=a/b;
    printf("the result is %d \n",c);
}
  void modulus_func(int a, int b)
{
    int c;
    c=a%b;
    printf("the result is %d \n",c);
}
  void shiftleft_func(int a, int b){
    int c;
    c=a<<b;
    printf("the result is %d \n",c);
}
  void shiftright_func(int a, int b)
{
    int c;
    c=a>>b;
    printf("the result is %d \n",c);
}
  void and_func(int a, int b)
{
    int c;
    c=a&b;
    printf("the result is %d \n",c);
}
  void or_func(int a, int b)
{
    int c;
    c=a|b;
    printf("the result is %d \n",c);
}
  void not_func(int a, int b)
{
    int c;
    c=a+b;
    c=~c;
    printf("the result is %d \n",c);
}
  void doubleand_func(int a, int b)
{
    int c;
    c=a&&b;
    printf("the result is %d \n",c);
}
  void doubleor_func(int a, int b)
{
    int c;
    c=a||b;
    printf("the result is %d \n",c);
}
  void notequal_func(int a, int b)
{
    int c;
    c=a+b;
    c=!c;
    printf("the result is %d \n",c);
}
  void xor_func(int a, int b)
{
    int c;
    c=a^b;
    printf("the result is %d \n",c);
}

此代码仍然不起作用,但除了将 getch 更改为 scanf 之外,我没有做任何更改。

此函数将捕获多个数字并将它们连接成一个值。为了保存 space,只有 plus_func 包含在代码和开关中。该函数接受位数、符号标志和指向该值的指针。它 returns 一个整数,表示按下了 ESC。

有了这个,每个输入都是固定宽度。要输入 3,必须输入 03

#include <stdio.h>
#include <conio.h>

int getint ( unsigned int digits, int sign, int *value) {
    int ch = 0;
    unsigned int first = digits;

    *value = 0;//set to zero

    while ( digits) {
        while ( ( ch = getch()) < '0' || ch > '9') {
            if ( ch == '-' && sign == 1 && first == digits) {// allow for negative numbers
                sign = -1;
                putchar ( '-');
            }
            if ( ch == 0x1b) {
                return 1;
            }
        }
        putchar ( ch);
        *value *= 10;
        *value += ch - '0';//concatenate the digits
        digits--;
    }
    if ( sign) {
        *value *= sign;
    }
    return 0;
}

void plus_func(int a, int b)
{
    int c = 0;
    c = a + b;
    printf ( "\nthe result is %d\n", c);
}

int main()
{
    int i = 0;
    int op1 = 0;
    int op2 = 0;
    int op3 = 0;
    unsigned int digits = 2;

    do {
        printf("\nenter the first %u digit num or press escape to exit \n", digits);
        if ( getint ( digits, 1, &op1)) {
            break;
        }

        printf("\nenter the second %u digit num or press escape to exit \n", digits);
        if ( getint ( digits, 1, &op2)) {
            break;
        }

        printf("\nenter the type of the operation from 01 to 14\n");
        if ( getint ( digits, 0, &op3)) {
            break;
        }

        switch ( op3) {
            case 1:
                plus_func ( op1, op2);
                break;

            default:
                printf ( "error\n");
                break;
        }

    } while ( i == 0);
    return 0;
}