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



我想制作一个类似商店系统的简单程序。长话短说,我希望我的用户输入代码(这是一个带有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("nMenyediakann");
printf("n mmn");
}   
else
if(i==4)
{
printf("================================================================================================================================");
printf("nMenyediakann");
printf("n mkn");
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=";部分是我想知道的任何引用都将被接受,任何建议都将不胜感激也接受任何选项,如使用呼叫功能等

非常感谢

我通常会循序渐进。您已经进入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;
}

保持改变函数直到它";作品";用于一堆测试输入。

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

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

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);

现在,创建一个函数来根据产品代码获取价格(来自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函数中,循环输入并使用每个产品代码调用函数,并在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("nMenyediakann");
printf("n mmn");
}   
else
if(i==4)
{
printf("================================================================================================================================");
printf("nMenyediakann");
printf("n mkn");
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 = %dn", fp);
//
system("pause");
return 0;
}
}

相关内容

  • 没有找到相关文章

最新更新