1

我有一个代码,用户必须输入油漆升的价格......但它只接受像“2.50”这样的价格,如果用户输入“2,50”,我可以让程序将逗号转换为点吗? 使用 C!

    printf ("\n How many liters of green paint we'll use? ");
    scanf ("%d", &green);
    printf ("\n How many liters of blue paint we'll use? ");
    scanf (" %d", &blue);
    printf ("\n What's the price for the liter of green paint? ");
    scanf ("%f", &priceG);
    fflush(stdin);
    printf ("\n What's the price for the liter of blue paint? ");
    scanf ("%f", &priceB);
4

2 回答 2

1

collect the input into a string and perform the operation..

Traverse each character of the string and if you come across , replace it with .

Put the traversed characters into an other string while traversing and when , is found, replace it with .

This procedure might be lengthy, but it might help and meet your requirement.

于 2013-01-05T16:35:37.050 回答
1

setlocale(LC_NUMERIC,"yourLocale")您可以使用from为小数点设置所需的语言环境#include <locale.h>。(更多信息在这里

yourLocale必须是您的机器上可用的有效语言环境字符串,并使用,小数点,例如de_DE. 您可以使用locale -a.

于 2013-01-05T16:46:22.183 回答