首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C控制台程序中将字符串用户输入与int变量相匹配

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

Stack Overflow用户
提问于 2021-06-03 23:08:21
回答 2查看 77关注 0票数 1

我想做一个简单的程序,像商店系统。长话短说,我希望我的用户输入代码(这是var name和int价格值(示例输入:"a1 a2 b3 ... "))然后它可以计算出它的最终价格(fp = a1+a2+a3+...)并添加一些“人工自动”的进度条或加载条

代码语言:javascript
复制
#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 =“部分是我想知道任何参考都会被接受,任何建议都会被接受,任何选项也会被接受,比如使用call函数等

非常感谢

EN

回答 2

Stack Overflow用户

发布于 2021-06-03 23:27:51

我通常是一小步一步来的。您已经到了int fp = <???>;步骤...因此,请记住,拥有很多函数是很好的:

代码语言:javascript
复制
fp = finalPrice(sel); // may need more arguments

并且,对于版本0.0.0.0.1

代码语言:javascript
复制
int finalprice(const char *input) {
    return 42;
}

检查它是否正常工作,然后添加功能。

版本0.0.0.0.2

代码语言:javascript
复制
int finalprice(const char *input) {
    if (*input == 'a') return 65;
    return -1;
}

不断改变函数,直到它对一堆测试输入“起作用”。

票数 0
EN

Stack Overflow用户

发布于 2021-06-04 02:40:40

这显然是一个矩阵操作练习。

您应该从表(矩阵)中获取产品价格,而不是从单个变量中获取。创建一个包含价格的表:

代码语言:javascript
复制
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个字符(这是一个任意值)。

代码语言:javascript
复制
int fp = 0;
char sel[1024] = {0, };

scanf("%s", sel)不限制读取的字符数,也不包括空格,因此请将其更改为fgets(sel, 1024, stdin);

现在,创建一个函数来根据产品代码获取价格(从product_prices_table)。

代码语言:javascript
复制
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函数中,循环输入并使用每个产品代码调用您的函数,并在您的fp变量中进行累积。

代码语言:javascript
复制
/* 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有了所有提供的产品的总价。

下面是完整的代码:

代码语言:javascript
复制
#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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67824006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档