c - 如何在c中指向数组指针(数组[x])

标签 c arrays function pointers

我刚刚完成了一个我一直在开发的程序,它工作正常,但我想通过消除重复的代码来改进它。所以我想我可以通过指向数组指针本身来做到这一点,但我不太确定如何做到这一点。我的教授没有详细讨论这一点。

我在想,如果我用类似于 balance [x] = Deposit_choice - 1 的内容更改 balance [0-4] 的出现,我会在消除重复代码方面走在正确的轨道上,但没有任何效果像我预期的那样。我尝试过创建两个不同的函数;一个用于更改 balance [x] 的值,一个用于运行算法,但我有点不知道该做什么。

由于它们工作得不太好,这里是算法的第一次迭代未改变:

case 3:
          show_account_menu(balance, 5);
          printf("An account balance of -1.00 means your account has not been opened yet.\n\n");
          printf("\nWhat account would you like to deposit in?\n" );// inputprompt
          scanf(" %d", &deposit_choice);
          if ((deposit_choice > 5) || (account_choice <1)){ //can only deposit in accounts 1-5
            printf("\n***  Invalid account selection!!  ***\n");
          }

            // start of account 1
            if(deposit_choice == 1){
              if(balance [0] == -1.00 ){
                printf("You have not created an account yet.\n");
                break;

              }else if (balance [0] >= 0.00){
                  printf("Enter an amount to deposit: ");//input
                  scanf("%d", &deposit);
                  printf("\n-------------------------------\n");
                    if(deposit >= 0){
                      printf("\n* Your new balance is: %.2f *\n\n",balance[0]=balance[0]+deposit);
                      break;
                    }else{
                      printf("Invalid input!\n\n");//deposit validation
                      break;
                    }
                  }

                //start of account 2
              }else if(deposit_choice == 2){

如果有人能帮助我,我将非常感激。我觉得如果我能理解如何解决这个问题,我就能够巩固如何使用指针、数组,甚至函数。

如果你想在这里查看整个代码,它是:

      #include <stdio.h>

    void show_bank_menu(void){
      printf("\n**-**-**-**-**-**-**-**-Welcome to Scrubs Bank-**-**-**-**-**-**-**-**-\n");
      printf("**-**-**-**-**-**-**-**How may I help you today**-**-**-**-**-**-**-**-\n");
      printf("********************~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~********************\n");
      printf("**********$$$$$$****~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~******$$$$$$$*******\n");
      printf("********$$$$$$$$$***  1. View Accounts...          *****$$$$$$$$$******\n");    /*introduction*/
      printf("********$$$$**$$$$**  2. Open Accounts...          ****$$$$**$$$$******\n");
      printf("********$$$$********  3. Deposit...                **********$$$$******\n");
      printf("***$$$$**$$$********  4. Withdraw...               **********$$$***$$$*\n");
      printf("***$$$$$$$$$********  5. Exit                      **********$$$$$$$$**\n");
      printf("****$$$$$$$*********~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~***********$$$$$$***\n");
      printf("********************~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~********************\n");
      printf("**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n");
      printf("**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n");
    }

    void show_account_menu(float array[ ], int size ){
      int x;
      for( x = 0 ; x < size ; x++ ) {
        printf("%d: %.2f\n", x+1, array[x]);
      }
    }



    int main (void) {
      int choice, account_choice, deposit_choice, withdraw_choice;
      float balance [5]= {-1.00,-1.00,-1.00,-1.00,-1.00};
      int deposit=0;
      int withdraw=0;

        //bank menu selection
        show_bank_menu();
        printf("\nchoice: ");
        scanf("%d", &choice);

        while (choice !=5){
          switch(choice){
            case 1:
    //show account
            show_account_menu(balance,5);
            printf("An account balance of -1.00 means your account has not been opened yet.\n\n");
              break;

              case 2:
              show_account_menu(balance,5);
              printf("An account balance of -1.00 means your account has not been opened yet.\n\n");
                printf("Which account would you like to open?\n" );
                scanf("%d", &account_choice );
                if ((account_choice > 5) || (account_choice <1)){
                  printf("\n***  Invalid account selection!!  ***\n");
                }
    //open account
                  if(account_choice == 1){
                    printf("\n");
                    balance [0]=0.00;
                    show_account_menu(balance,5);
                  }else if(account_choice == 2){
                    printf("\n");
                    balance [1]=0.00;
                    show_account_menu(balance,5);
                  }else if(account_choice == 3){
                    printf("\n");
                    balance [2]=0.00;
                    show_account_menu(balance,5);
                  }else if(account_choice == 4){
                    printf("\n");
                    balance [3]=0.00;
                    show_account_menu(balance,5);
                  }else if(account_choice == 5){
                    printf("\n");
                    balance [4]=0.00;
                    show_account_menu(balance,5);
                  }
                  break;
    //deposit
                  case 3:
                  show_account_menu(balance, 5);
                  printf("An account balance of -1.00 means your account has not been opened yet.\n\n");
                  printf("\nWhat account would you like to deposit in?\n" );// inputprompt
                  scanf(" %d", &deposit_choice);
                  if ((deposit_choice > 5) || (account_choice <1)){
                    printf("\n***  Invalid account selection!!  ***\n");
                  }

                    // start of account 1
                    if(deposit_choice == 1){
                      if(balance [0] == -1.00 ){
                        printf("You have not created an account yet.\n");
                        break;

                      }else if (balance [0] >= 0.00){
                          printf("Enter an amount to deposit: ");//input
                          scanf("%d", &deposit);
                          printf("\n-------------------------------\n");
                            if(deposit >= 0){
                              printf("\n* Your new balance is: %.2f *\n\n",balance[0]=balance[0]+deposit);
                              break;
                            }else{
                              printf("Invalid input!\n\n");//deposit validation
                              break;
                            }
                          }

                        //start of account 2
                      }else if(deposit_choice == 2){
                        if (balance [1] == -1.00){
                          printf("You have not created an account yet.\n" );
                          break;

                        }else if(balance [1] >= 0.00){
                        printf("Enter an amount to deposit: ");
                        scanf("%d", &deposit);//input
                        printf("\n-------------------------------\n");
                          if(deposit >= 0){
                            printf("\n* Your new balance is: %.2f *\n\n",balance[1]=balance[1]+deposit);
                            break;
                          }else{            //validation
                            printf("Invalid input!\n\n");
                            break;
                          }
                        }


                      }else if(deposit_choice == 3){//start account 3
                        if(balance [2] == -1.00){
                          printf("You have not created an account yet.\n");
                          break;

                        }else if(balance [2] >= 0.00){
                        printf("Enter an amount to deposit: ");
                        scanf("%d", &deposit);                      //input
                        printf("\n-------------------------------\n");
                          if(deposit >= 0){
                            printf("\n* Your new balance is: %.2f *\n\n",balance[2]=balance[2]+deposit);
                            break;
                          }else{
                            printf("Invalid input!\n\n");//validation
                            break;
                          }
                        }

                      }else if(deposit_choice == 4){
                        if (balance [3] == -1.00){
                          printf("You have not created an account yet.\n" );
                          break;

                        }else if(balance [3] >= 0.00){
                        printf("Enter an amount to deposit: ");
                        scanf("%d", &deposit);//input
                        printf("\n-------------------------------\n");
                          if(deposit >= 0){
                            printf("\n* Your new balance is: %.2f *\n\n",balance[3]=balance[3]+deposit);
                            break;
                          }else{            //validation
                            printf("Invalid input!\n\n");
                            break;
                          }
                        }


                      }else if(deposit_choice == 5){// start account 5
                        if (balance [4] == -1.00){
                          printf("You have not opened the account yet.\n");
                          break;

                        }else if(balance [4] >= 0.00){
                        printf("Enter an amount to deposit: ");
                        scanf("%d", &deposit);                  //input
                        printf("\n-------------------------------\n");
                          if(deposit >= 0){
                            printf("\n* Your new balance is: %.2f *\n\n",balance[4]=balance[4]+deposit);
                            break;
                          }else{
                            printf("Invalid input!\n\n");    //validation
                            break;
                           }
                         }
                       }
    //end of withdraw options
                    break;
    //withdraw
                    case 4:
                    show_account_menu(balance, 5);
                    printf("An account balance of -1.00 means your account has not been opened yet.\n\n");
                    printf("\nWhat account would you like to withdraw out of?\n" );// inputprompt
                    scanf(" %d", &withdraw_choice);
                    if ((withdraw_choice > 5) || (account_choice <1)){
                      printf("\n***  Invalid account selection!!  ***\n");
                    }

                      // start of account 1
                      if(withdraw_choice == 1){
                        if(balance [0] == -1.00 ){
                          printf("You have not created an account yet.\n");
                          break;

                        }else if (balance [0] >= 0.00){
                            printf("Enter an amount to withdraw: ");//input
                            scanf("%d", &withdraw);
                            printf("\n-------------------------------\n");
                              if(withdraw > balance [0] ){
                                printf("\nInsufficient funds!\n");
                                break;
                              }else if(withdraw >= 0){
                                printf("\n* Your new balance is: %.2f *\n\n",balance[0]=balance[0]-withdraw);
                                break;
                              }else{
                                printf("Invalid input!\n\n");//deposit validation
                                break;
                              }
                            }

                          //start of account 2
                        }else if(withdraw_choice == 2){
                          if (balance [1] == -1.00){
                            printf("You have not created an account yet.\n" );
                            break;

                          }else if(balance [1] >= 0.00){
                            printf("Enter an amount to withdraw: ");
                            scanf("%d", &withdraw);//input
                            printf("\n-------------------------------\n");
                              if( withdraw > balance [1] ){
                                printf("Insufficient funds!\n");
                                break;
                              }else if(withdraw >= 0){
                                printf("\n* Your new balance is: %.2f *\n\n",balance[1]=balance[1]-withdraw);
                                break;
                              }else{            //validation
                                printf("Invalid input!\n\n");
                                break;
                              }
                            }

                          //start of account 3
                        }else if(withdraw_choice == 3){
                          if (balance [2] == -1.00){
                          printf("You have not created an account yet.\n");
                          break;

                          }else if(balance [2] >= 0.00){
                            printf("Enter an amount to withdraw: ");
                            scanf("%d", &withdraw);//input
                            printf("\n-------------------------------\n");
                              if(withdraw > balance [2] ){
                                printf("Insufficient funds!\n");
                                break;
                              }else if(withdraw >= 0){
                                printf("\n* Your new balance is: %.2f *\n\n",balance[2]=balance[2]-withdraw);
                                break;
                              }else{            //validation
                                printf("Invalid input!\n\n");
                                break;
                            }
                          }
                        //start of account 4
                      }else if(withdraw_choice == 4){
                        if (balance [3] == -1.00){
                        printf("You have not created an account yet.\n");
                        break;

                        }else if(balance [3] >= 0.00){
                          printf("Enter an amount to withdraw: ");
                          scanf("%d", &withdraw);//input
                          printf("\n-------------------------------\n");
                          if(withdraw > balance [3] ){
                            printf("Insufficient funds!\n");
                          }else if(withdraw >= 0){
                              printf("\n* Your new balance is: %.2f *\n\n",balance[3]=balance[3]-withdraw);
                              break;
                            }else{            //validation
                              printf("Invalid input!\n\n");
                              break;
                            }
                          }
                        //start of account 5
                      }else if(withdraw_choice == 5){
                        if (balance [4] == -1.00){
                        printf("You have not created an account yet.\n");
                        break;

                        }else if(balance [4] >= 0.00){
                          printf("Enter an amount to withdraw: ");
                          scanf("%d", &withdraw);//input
                          printf("\n-------------------------------\n");
                            if(withdraw > balance [4] ){
                              printf("Insufficient funds!\n");
                            }else if(withdraw >= 0){
                                printf("\n* Your new balance is: %.2f *\n\n",balance[4]=balance[4]-withdraw);
                                break;
                            }else{            //validation
                              printf("Invalid input!\n\n");
                              break;
                            }
                          }
                        }


                  /*open accounts*/
                        break;

                          case 5:
                            printf("\nGood day!\n");
                          return 0;


                    default:
                    printf("-----------------------");
                    printf("\n Not an option!\n");
                    printf("-----------------------\n");

        }// end switch

        show_bank_menu();

        printf("choice: ");
        scanf("%d", &choice);
        printf("\n");




      }//end while

      return 0;
    }//end main

最佳答案

这是您要找的吗?请注意,balance[0] 的实例已替换为 balance[deposit_choice - 1]:

          if(balance [deposit_choice - 1] == -1.00 ){
              printf("You have not created an account yet.\n");
              break;

          }else if (balance [deposit_choice - 1] >= 0.00){
              printf("Enter an amount to deposit: ");//input
              scanf("%d", &deposit);
              printf("\n-------------------------------\n");
              if(deposit >= 0){
                printf("\n* Your new balance is: %.2f *\n\n",balance[deposit_choice - 1] = balance[deposit_choice - 1] + deposit);
                break;
              }else{
                printf("Invalid input!\n\n");//deposit validation
                break;
              }
          }

附注你的压痕很奇怪。您应该确保您的缩进量始终相同,此外,为 block 的内部添加缩进(即我注意到您缩进了 if 语句,但缩进应该从后面的行开始)。

关于c - 如何在c中指向数组指针(数组[x]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33881185/

相关文章:

C - 扫雷 AI 不点击图 block

c - 如何在纯C中将字符放入字符串的空指针中

c - 数组的 Sizeof...它是如何工作的?

for 循环中的 C# 数组产生相同的随机整数值

javascript - Javascript 函数中的对象

c - 如何使用 fwrite() 多次写入默认结构值

javascript - 不同条件下的 ng-model

javascript - 从输入修改数组然后返回输出

function - Oracle函数和查询返回不同的结果

php - 如何将多个函数调用成一个常量代码结构?