如何解决如何在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("\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 =”部分是我想知道的 任何参考都将被接受, 任何建议将不胜感激 也接受任何选项,如使用调用函数等
非常感谢
解决方法
我通常是小步走的。您已经到了 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",/* mka */
"3000"," 12000","11000",/* mkb */
"6000"," 5000","7000","5500",/* mkc */
"12000","15000",/* 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","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,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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。