简体   繁体   English

如何在 C 控制台程序中将字符串用户输入与 int 变量匹配

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

i want to make a simple program like shop system.我想做一个简单的程序,比如商店系统。 long story short i want my user to input code (which is var name with int price value (example input: "a1 a2 b3... ")) then it can calculate its final price (fp = a1+a2+a3+...) and add some "artificially auto" progress or loading bar长话短说,我希望我的用户输入代码(这是带有 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;
}
}

this "fp = " section is I want to know any reference would be accepted, any suggestion would be appreciated any option also accepted like using call function, etc这个“fp =”部分是我想知道任何参考都会被接受,任何建议都会被接受任何选项也被接受,比如使用调用 function 等

thank you very much非常感谢您

I usually go in small steps.我通常小步走 go。 You already got to the int fp = <???>;你已经到了int fp = <???>; step... so, keeping in mind that having lots of functions is good:步骤...所以,请记住,拥有很多功能是好的:

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

and, for version 0.0.0.0.1并且,对于版本 0.0.0.0.1

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

check that it works, then add functionality.检查它是否有效,然后添加功能。
version 0.0.0.0.2版本 0.0.0.0.2

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

keep changing the function until it "works" for a bunch of testing inputs.不断更改 function 直到它“工作”用于一堆测试输入。

This is clearly a matrix manipulation exercise.这显然是一个矩阵操作练习。

You should get product prices from a table (matrix), not from single variables.您应该从表格(矩阵)中获取产品价格,而不是从单个变量中获取。 Create a table with the prices:创建一个包含价格的表:

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"
};

Also, you should initialize all your variables to zero and ensure your variable sel contains enough space to store user input.此外,您应该将所有变量初始化为零,并确保您的变量sel包含足够的空间来存储用户输入。 I changed its declaration to contain 1024 characters (this is an arbitrary value).我将其声明更改为包含 1024 个字符(这是一个任意值)。

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

scanf("%s", sel) doesn't limit the number of characters read and doesn't include whitespaces, so change it to fgets(sel, 1024, stdin); scanf("%s", sel)不限制读取的字符数,也不包含空格,所以改成fgets(sel, 1024, stdin); . .

Now, create a function to get prices (from product_prices_table ) based on the product code.现在,创建一个 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;
}

In your main function, loop the input and call your function with each product code and accumulate in your fp variable.在您的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]; 
    }   
}   

So now fp has the total price of all provided products.所以现在fp有了所有提供的产品的总价格。

Here's the complete code:这是完整的代码:

#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