簡體   English   中英

在 switch case 中調用函數

[英]Calling a function within a switch case

我一直在嘗試創建一個計算器或半計算器,我想知道我的代碼出了什么問題,為什么?

這是我在編譯時遇到錯誤的代碼,在一台機器上我得到不同的錯誤,而在另一台機器上,錯誤是不同的。

希望大家幫我解決一下。 它是一個小程序,可以更好地理解 C 編程。

#include<stdio.h>
int main()
{
    int operation,fc,v1,v2;
    double fcd;
    printf("input 2 values\n");
    scanf("%d%d",&v1,&v2);
    /* telling the user to choose any type of calculator */
      printf("please choose what you want to do with your values\n");
      printf("1- Sum\n");
      printf("2- substracttion\n"); 
      printf("3- multiplay\n");
      printf("4- devision\n"); 
      scanf("%d",&operation);//input a
      switch(operation)
    {
      case 1:
      int fc = sum(int v1, int v2);
        printf("sum of two values = %d\n",fc);
        break;

    case 2:
       int fc = substact(int v1, int v2);
       printf("substract of two values = %d",fc);
        break;

    case 3:
        int fc = multiplay(int v1, int v2);
        printf("multiply of two values = %d\n",fc);
        break;

    case 4:
       int fcd = devision(int v1, int v2);
       printf("division of two values = %d\n",fc);
        break;
    default:
        printf("wrong choice\n");
    }
return 0;}

int sum(int a,int b)
{
    int sum=0;
    um=a+b;
    return sum;
}
int substact(int a,int b)
{
    int sub=0;
    sub=a-b;
    return sub;
}
int multiplay(int a,int b)
{
    int mult=1;
    mult=a*b;
    return mult;
}
double devision(int a,int b)
{
    double  devi=1;
    devi=a/b;
    return devi;
}

問題1
C(C99 之前的版本)不允許在塊開始以外的任何地方在局部范圍內聲明變量。 (在執行任何語句之前)。 同樣在您的情況下,它可能會導致在同一范圍內具有相同名稱的多個變量,這是不允許的。

int fcint fcd打破了上述條件,所以應該移到函數的頂部。 (它們已經存在,只需從案例中刪除int

問題2
傳遞參數時,您不需要給出類型。 例如sum(int v1, int v2); 不好。 (從參數中刪除int

警告
始終在首次使用之前聲明(或定義)函數。 在您的程序中,在main中聲明之前使用諸如 sum、substract 等函數。

擴展閱讀:
變量內部開關
C中的函數調用
C 中的函數
為什么函數在使用前需要聲明?

您代碼中的以下語句可能會導致編譯錯誤。

 int fc = sum(int v1, int v2);

您已經聲明了 fc 變量,那么為什么還要在這里再次聲明它。 在函數內多次聲明變量會導致編譯錯誤。 還有一件事是你不能用下面的方式調用函數 int fc = sum(int v1, int v2)。 閱讀與 C 編程相關的書籍,了解如何進行函數調用。

所以修改上面的C語句如下。

       fc = sum(v1,v2);

我沒有編譯你的代碼,但我認為上面的更改應該可以解決你的編譯錯誤

編譯錯誤最有可能與函數 sum、substract 等的聲明有關。您編寫的代碼在主函數中使用后定義了函數。 如果您打算從主函數調用它們,請在主函數之前“聲明”所有函數。

我希望這個解決方案能幫助你。 在調用該函數之前,您必須先聲明。 這是示例源代碼。

   #include <stdio.h>

  /* function declaration */
  int max(int num1, int num2);

 int main () {

 /* local variable definition */
 int a = 100;
 int b = 200;
 int ret;

 /* calling a function to get max value */
 ret = max(a, b);

 printf( "Max value is : %d\n", ret );

return 0;

}

 /* function returning the max between two numbers */
 int max(int num1, int num2) {

 /* local variable declaration */
int result;

if (num1 > num2)
  result = num1;
else
  result = num2;

return result; 

}

幾個問題:

您需要在使用函數之前聲明它們。

您不能重復類型聲明。 例如,您在程序的開頭聲明了 fc,並且在 switch 語句 cases 中重復了它:“int fc =...”,只需使用“fc =...”

最后,調用函數時不要聲明類型。 下面是一個最低限度更正的代碼:

#include<stdio.h>
int sum(int a,int b);
int substact(int a,int b);
int multiplay(int a,int b);
double devision(int a,int b);

int main()
{
    int operation,fc,v1,v2;
    double fcd;
    printf("input 2 values\n");
    scanf("%d%d",&v1,&v2);
      /* telling the user to choose any type of calculator */
    printf("please choose what you want to do with your values\n");
    printf("1- Sum\n");
    printf("2- substracttion\n"); 
    printf("3- multiplay\n");
    printf("4- devision\n"); 
    scanf("%d",&operation);//input a
    switch(operation)
    {
      case 1:
        fc = sum(v1, v2);
        printf("sum of two values = %d\n",fc);
        break;

      case 2:
        fc = substact(v1, v2);
        printf("substract of two values = %d",fc);
        break;

      case 3:
        fc = multiplay(v1, v2);
        printf("multiply of two values = %d\n",fc);
        break;

      case 4:
        fcd = devision(v1, v2);
        printf("division of two values = %d\n",fc);
        break;
      default:
        printf("wrong choice\n");
    }
return 0;
}

int sum(int a,int b)
    {
    int sum=0;
    sum=a+b;
    return sum;
    }
int substact(int a,int b)
    {
    int sub=0;
    sub=a-b;
    return sub;
    }
int multiplay(int a,int b)
    {
    int mult=1;
    mult=a*b;
    return mult;
    }
double devision(int a,int b)
    {
        double  devi=1;
    devi=a/b;
    return devi;
    }

這是另一個有點不同的程序,也是理解在 switch case 中調用函數的簡單方法。 這是聲明和調用函數的最簡單方法之一

聲明所有函數並從函數外部調用

#include<stdio.h>
#include<conio.h>
int x,y;
int addi(int,int);//when call the function. The function must be integer type.
int multi(int,int);
int divi(int,int);
int square(int);
int evenOdd(int);
int substracn(int,int);

int main()
{
   int choice;   

printf("*********************************************************************\n");
printf("\t\tCalculator\n");
printf("*********************************************************************\n");

printf("Enter a choice what you want to perform");
printf("\n\t1.Addition");
printf("\n\t2.Multiplication");
printf("\n\t3.Division");


printf("\n\t4.Square");
printf("\n\t5.Even or Odd");
printf("\n\t6.Substraction\n");
scanf("%d",&choice);



switch(choice)//switch case use when more than 2 options are there. Then it is the best 
{
case 1: printf("\n\tAddition");
        printf("\nEnter two no for addition");
        scanf("\n%d\t%d",&x,&y);
        addi(x,y);//function by passing paramenter
        break;//break the statement after the execution of the function definition

case 2: printf("Enter two no's for multiplication");//multiplication
        scanf("\n%d\t%d",&x,&y);
        multi(x,y);
        break;

case 3: printf("Enter two no's for division");
        scanf("%d%d",&x,&y);
        divi(x,y);
        break;


case 4: printf("Enter one no for square of no");
        scanf("%d",&x);
        square(x);
        break;

case 5: printf("Enter the no for even odd");
        scanf("%d",&x);
        evenOdd(x);
        break;

case 6: printf("Enter the two no's for Substraction");
        scanf("%d%d",&x,&y);
        subtracn(x,y);
        break;
default:
    printf("Enter a valid option");

}

getch();
}

int addi(int a,int b)
{
    int c;
    c=a+b;
    printf("sum of no %d + %d is: %d",a,b,c);

}

int multi(int a,int b)
{
    int c;
    c=a*b;
    printf("multiplication of no %d x %d is: %d",a,b,c);

}

int divi(int a,int b)
{
    int c;
    c=a/b;
    printf("division of no's is: %d",c);

}

int square(int a)
{
    int c;
    c=a*a;
    printf("square of no %d is: %d",a,c);

}

int evenOdd(int a)
{
    if(a%2==0)
    {
    printf("The no is even : %d",a);
    }
    else
    {
    printf("The no is odd : %d",a);    
    }

}
int subtracn(int a,int b)
{
    int c;
    c=a-b;
    printf("Substraction of no's is: %d",c);

}`
    let role = "teacher";
    
    forStudent = ()=>{
        console.log("from student");
    }

    forTeacher = ()=>{
        console.log("from teacher");
    }

    forOwner = ()=>{
        console.log("from owner");
    }
    
    defFunc = ()=>{
        console.log("its default");
    }

    switch(role){
        case 'student': forStudent();
        break;
        
        case 'teacher': forTeacher();
        break;
        
        case 'owner': forOwner();
        break;
        
        default: defFunc ();
    }

就是這么簡單。

在調用它之前確保你的函數已經定義並且它已經完成

上面的代碼是用 Javascript 編寫的,但希望它對其他語言也有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM