簡體   English   中英

如何在 C 控制台程序中將字符串用戶輸入與 int 變量匹配

[英]how to match string user input with int variable in C console programs

我想做一個簡單的程序,比如商店系統。 長話短說,我希望我的用戶輸入代碼(這是帶有 int 價格值的 var 名稱(示例輸入:“a1 a2 b3 ...”))然后它可以計算其最終價格(fp = a1+a2+a3+.. .) 並添加一些“人工自動”進度或加載欄

#include <stdio.h>
#include <stdlib.h>

int main() {
char sel;
int i,s;
int fp;
        
        //var name and price list
int mma1 = 2500;
int mma2 = 2000;
int mma3 = 3000;
int mma4 = 3500;

int mmb1 = 10000;
int mmb2 = 12000;
int mmb3 = 13000;
int mmb4 = 10000;

int mmc1 = 7000;
int mmc2 = 7500;
int mmc3 = 6500;
int mmc4 = 5000;

int mmd1 = 2000;
int mmd2 = 1500;
int mmd3 = 3000;
int mmd4 = 3500;

int mka1 = 3000;
int mka2 = 12000;
int mka3 = 10000;
int mka4 = 11000;

int mkb1 = 6000;
int mkb2 = 5000;
int mkb3 = 7000;
int mkb4 = 5500;

int mkc1 = 12000;
int mkc2 = 15000;
int mkc3 = 15000;
int mkc4 = 13000;

int mkd1 = 45000;
int mkd2 = 50000;
int mkd3 = 55000;
int mkd4 = 60000;
        
for(i=0; i<=7; i++)
    {
        
        
        //list
        char b=64, a[8][4][50]=
    {
        "proto1 = 2500", "proto2 = 2000", "proto3 = 3000", "proto4 = 3500",
        "proto5 = 10000", "proto6 = 12000", "proto7 = 13000", "proto8 = 10000",
        "proto9 = 7000", "proto10 = 7500", "proto11 = 6500","proto12 = 5000",
        "proto13 = 2000", "proto14 = 1500", "proto15 = 3000", "proto16 = 3500",
        "proto17 = 3000", "proto18 = 12000", "proto19 = 10000", "proto20 = 11000",
        "proto21 = 6000", "proto22 = 5000", "proto23 = 7000", "24 = 5500",
        "proto25= 12000", "26 = 15000", "27 = 15000", "28 = 13000",
        "29 = 45000", "30 = 50000", "31 = 55000", "32 = 60000"
    };
    
    
            for(i=0; i<=7; i++)
    {
        
        if(i==0)
        {
            printf("================================================================================================================================");
            printf("\nMenyediakan\n");
            printf("\n mm\n");
        }   
        else
        if(i==4)
        {
            printf("================================================================================================================================");
            printf("\nMenyediakan\n");
            printf("\n mk\n");
            b=64;
        }
        b++;
        printf("type %c  \n", b);
        
        for(s=0; s<=3; s++)
        {
            printf("      %d. %s \n", s+1, a[i][s]);
            
        }
        
        printf("\n");
    }
    printf("================================================================================================================================\n");
    
    
        
  
    printf("type all code of your desired item then press enter (example 'mma1 mma3 mkb3 mkb4 ... ')\n");
    scanf("%s", sel);
    // 
    
    fp = 
    
    //
    system("pause");
    return 0;
}
}

這個“fp =”部分是我想知道任何參考都會被接受,任何建議都會被接受任何選項也被接受,比如使用調用 function 等

非常感謝您

我通常小步走 go。 你已經到了int fp = <???>; 步驟...所以,請記住,擁有很多功能是好的:

fp = finalPrice(sel); // may need more arguments

並且,對於版本 0.0.0.0.1

int finalprice(const char *input) {
    return 42;
}

檢查它是否有效,然后添加功能。
版本 0.0.0.0.2

int finalprice(const char *input) {
    if (*input == 'a') return 65;
    return -1;
}

不斷更改 function 直到它“工作”用於一堆測試輸入。

這顯然是一個矩陣操作練習。

您應該從表格(矩陣)中獲取產品價格,而不是從單個變量中獲取。 創建一個包含價格的表:

char product_prices_table[8][4][50]=
{
    /* mma */
    "2500",  "2000",   "3000",  "3500",
    /* mmb */
    "10000", "12000",  "13000", "10000",
    /* mmc */
    "7000",  " 7500",  "6500",  "5000",
    /* mmd */
    "2000",  " 1500",  "3000",  "3500",
    /* mka */
    "3000",  " 12000", "10000", "11000",
    /* mkb */
    "6000",  " 5000",  "7000",  "5500",
    /* mkc */
    "12000", "15000", "15000", "13000",
    /* mkd */
    "45000", "50000", "55000", "60000"
};

此外,您應該將所有變量初始化為零,並確保您的變量sel包含足夠的空間來存儲用戶輸入。 我將其聲明更改為包含 1024 個字符(這是一個任意值)。

int fp = 0;
char sel[1024] = {0, };

scanf("%s", sel)不限制讀取的字符數,也不包含空格,所以改成fgets(sel, 1024, stdin); .

現在,創建一個 function 以根據產品代碼獲取價格(來自product_prices_table )。

static int get_product_price(char *product_code)
{
    int product_price = 0;

    /* Ensure input is valid */
    if (   (strlen(product_code) == 4)
        && (product_code[0] == 'm')
        && (product_code[1] == 'm' || product_code[1] == 'k')
        && (product_code[2] >= 'a' && product_code[2] <= 'd')
        && (product_code[3] >= '1' && product_code[3] <= '4'))
    {
        /* Get price from the table */
        int first_index = 0;

        if (product_code[1] == 'k')
            first_index = 4;

        if (product_code[2] == 'b')
            first_index += 1;
        else if (product_code[2] == 'c')
            first_index += 2;
        else if (product_code[2] == 'd')
            first_index += 3;

        int second_index = atoi(&product_code[3]) - 1;

        product_price = atoi(product_list[first_index][second_index]);
    }
    else
    {
        printf("error: invalid product code \"%s\"\n", product_code);
    }

    return product_price;
}

在您的main function 中,循環輸入並使用每個產品代碼調用您的 function 並累積在您的fp變量中。

/* Loop through all provided product codes */
int input_size = strlen(sel);
char *product_code = sel;

int i = 0;
for (; i < input_size; i++) {

    /* Check for end of product code */
    if (sel[i] == ' ' || (i == input_size - 1)) {

        /* Discard trailing whitespace or the last newline character */
        if (sel[i] == ' ' || sel[i] == '\n')
            sel[i] = 0;

        fp += get_product_price(product_code);

        /* Point to the next product code */
        if (i + 1 < input_size)
            product_code = &sel[i + 1]; 
    }   
}   

所以現在fp有了所有提供的產品的總價格。

這是完整的代碼:

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

char product_prices_table[8][4][50]=
{
    /* mma */
    "2500",  "2000",   "3000",  "3500",
    /* mmb */
    "10000", "12000",  "13000", "10000",
    /* mmc */
    "7000",  " 7500",  "6500",  "5000",
    /* mmd */
    "2000",  " 1500",  "3000",  "3500",
    /* mka */
    "3000",  " 12000", "10000", "11000",
    /* mkb */
    "6000",  " 5000",  "7000",  "5500",
    /* mkc */
    "12000", "15000", "15000", "13000",
    /* mkd */
    "45000", "50000", "55000", "60000"
};
    
static int get_product_price(char *product_code)
{
    int product_price = 0;

    /* Ensure input is valid */
    if (   (strlen(product_code) == 4)
        && (product_code[0] == 'm')
        && (product_code[1] == 'm' || product_code[1] == 'k')
        && (product_code[2] >= 'a' && product_code[2] <= 'd')
        && (product_code[3] >= '1' && product_code[3] <= '4'))
    {
        /* Get price from the table */
        int first_index = 0;

        if (product_code[1] == 'k')
            first_index = 4;

        if (product_code[2] == 'b')
            first_index += 1;
        else if (product_code[2] == 'c')
            first_index += 2;
        else if (product_code[2] == 'd')
            first_index += 3;

        int second_index = atoi(&product_code[3]) - 1;

        product_price = atoi(product_prices_table[first_index][second_index]);
    }
    else
    {
        printf("error: invalid product code \"%s\"\n", product_code);
    }

    return product_price;
}

int main() {
    char sel[1024] = {0, };
    int i,s;
    int fp = 0;
            
    for(i=0; i<=7; i++)
        {
            
        //list
             char b=64, a[8][4][50]=
         {
             "proto1 = 2500", "proto2 = 2000", "proto3 = 3000", "proto4 = 3500",
             "proto5 = 10000", "proto6 = 12000", "proto7 = 13000", "proto8 = 10000",
             "proto9 = 7000", "proto10 = 7500", "proto11 = 6500","proto12 = 5000",
             "proto13 = 2000", "proto14 = 1500", "proto15 = 3000", "proto16 = 3500",
             "proto17 = 3000", "proto18 = 12000", "proto19 = 10000", "proto20 = 11000",
             "proto21 = 6000", "proto22 = 5000", "proto23 = 7000", "24 = 5500",
             "proto25= 12000", "26 = 15000", "27 = 15000", "28 = 13000",
             "29 = 45000", "30 = 50000", "31 = 55000", "32 = 60000"
         };

        for(i=0; i<=7; i++)
        {
            
            if(i==0)
            {
                printf("================================================================================================================================");
                printf("\nMenyediakan\n");
                printf("\n mm\n");
            }   
            else
            if(i==4)
            {
                printf("================================================================================================================================");
                printf("\nMenyediakan\n");
                printf("\n mk\n");
                b=64;
            }
            b++;
            printf("type %c  \n", b);
            
            for(s=0; s<=3; s++)
            {
                printf("      %d. %s \n", s+1, a[i][s]);
                
            }
            
            printf("\n");
        }
        printf("================================================================================================================================\n");
      
        printf("type all code of your desired item then press enter (example 'mma1 mma3 mkb3 mkb4 ... ')\n");
        fgets(sel, 1024, stdin);

        /* Loop through all provided product codes */
        int input_size = strlen(sel);
        char *product_code = sel;

        int i = 0;
        for (; i < input_size; i++) {

            /* Check for end of product code */
            if (sel[i] == ' ' || (i == input_size - 1)) {

                /* Discard trailing whitespace or the last newline character */
                if (sel[i] == ' ' || sel[i] == '\n')
                    sel[i] = 0;

                fp += get_product_price(product_code);

                /* Point to the next product code */
                if (i + 1 < input_size)
                    product_code = &sel[i + 1];
            }
        }

        printf("fp = %d\n", fp);

        //
        system("pause");
        return 0;
    }
}

暫無
暫無

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

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