将1000s数字与10s数字交换(C)

将1000s数字与10s数字交换(C),c,floating-point,double,C,Floating Point,Double,我尝试切换例如:输入54321.987,然后4和2应该切换,所以输出将是52341.987。54321.777应变为52341.777。如果为2345.777,则应为4325.777。任何我不在乎的事情。但如果是888886543.777,则只有第二个和第四个数字应该从逗号前最右边的部分切换。因此,它将成为888884563.777 因此,正如LearningC所建议的,我正在尝试将1000秒的数字与10秒的数字进行交换 但无论我尝试什么,我都会出错。我不能通过这些错误。我该怎么做 到目前为止,

我尝试切换例如:输入54321.987,然后4和2应该切换,所以输出将是52341.987。54321.777应变为52341.777。如果为2345.777,则应为4325.777。任何我不在乎的事情。但如果是888886543.777,则只有第二个和第四个数字应该从逗号前最右边的部分切换。因此,它将成为888884563.777

因此,正如LearningC所建议的,我正在尝试将1000秒的数字与10秒的数字进行交换


但无论我尝试什么,我都会出错。我不能通过这些错误。我该怎么做

到目前为止,我所做的实际工作就是:

   int main(int argc, char** argv) {
        double x;
        scanf("%lf", &x);

        double tens = ((int) (x / 10)) % 10;
        double thousands = ((int) (x / 1000)) % 10;

        printf("%09.3f", x += (tens - thousands) * 990.0);
        return 0;
    }

上面的代码现在可以工作了。

首先,您必须确定这些数字

你可以这样做

double tens = ((int)(x / 10)) % 10;
double thousands = ((int)(x / 1000)) % 10;
这使您能够

x = x - (tens * 10.0) - (thousands * 1000.0) + (tens * 1000.0) + (thousands * 10.0);
在原始位置减去它们,然后以交换的方式重新添加它们

您可以对此进行优化以

x = x + tens * (1000.0 - 10.0) - thousands * (1000.0 - 10.0);
而且,再一次,这是为了

x += (tens - thousands) * 990.0;

首先,您必须确定这些数字

你可以这样做

double tens = ((int)(x / 10)) % 10;
double thousands = ((int)(x / 1000)) % 10;
这使您能够

x = x - (tens * 10.0) - (thousands * 1000.0) + (tens * 1000.0) + (thousands * 10.0);
在原始位置减去它们,然后以交换的方式重新添加它们

您可以对此进行优化以

x = x + tens * (1000.0 - 10.0) - thousands * (1000.0 - 10.0);
而且,再一次,这是为了

x += (tens - thousands) * 990.0;

使用字符串操作:

char string[20];

snprintf(string, sizeof(string), "%09.3f", x);
char *dot = strchr(string, '.');
assert(dot != 0 && dot > string + 4);
char old_4 = dot[-4];
char old_2 = dot[-2];
dot[-2] = old_4;
dot[-4] = old_2;

/* If you need a float back */
sscanf(string, "%lf", &x);
double frac_part;
double int_part;

frac_part = modf(x, &int_part);

long value = int_part;
int  dig_2 = (int_part / 10) % 10;
int  dig_4 = (int_part / 1000) % 1000;
assert(dig_4 != 0);
value -= 10 * dig_2 + 1000 * dig_4;
value += 10 * dig_4 + 1000 * dig_2;
int_part = value;
x = int_part + frac_part;
使用算术操作:

char string[20];

snprintf(string, sizeof(string), "%09.3f", x);
char *dot = strchr(string, '.');
assert(dot != 0 && dot > string + 4);
char old_4 = dot[-4];
char old_2 = dot[-2];
dot[-2] = old_4;
dot[-4] = old_2;

/* If you need a float back */
sscanf(string, "%lf", &x);
double frac_part;
double int_part;

frac_part = modf(x, &int_part);

long value = int_part;
int  dig_2 = (int_part / 10) % 10;
int  dig_4 = (int_part / 1000) % 1000;
assert(dig_4 != 0);
value -= 10 * dig_2 + 1000 * dig_4;
value += 10 * dig_4 + 1000 * dig_2;
int_part = value;
x = int_part + frac_part;

这两种操作的顺序都不是最小的,但它们相当直接。

使用字符串操作:

char string[20];

snprintf(string, sizeof(string), "%09.3f", x);
char *dot = strchr(string, '.');
assert(dot != 0 && dot > string + 4);
char old_4 = dot[-4];
char old_2 = dot[-2];
dot[-2] = old_4;
dot[-4] = old_2;

/* If you need a float back */
sscanf(string, "%lf", &x);
double frac_part;
double int_part;

frac_part = modf(x, &int_part);

long value = int_part;
int  dig_2 = (int_part / 10) % 10;
int  dig_4 = (int_part / 1000) % 1000;
assert(dig_4 != 0);
value -= 10 * dig_2 + 1000 * dig_4;
value += 10 * dig_4 + 1000 * dig_2;
int_part = value;
x = int_part + frac_part;
使用算术操作:

char string[20];

snprintf(string, sizeof(string), "%09.3f", x);
char *dot = strchr(string, '.');
assert(dot != 0 && dot > string + 4);
char old_4 = dot[-4];
char old_2 = dot[-2];
dot[-2] = old_4;
dot[-4] = old_2;

/* If you need a float back */
sscanf(string, "%lf", &x);
double frac_part;
double int_part;

frac_part = modf(x, &int_part);

long value = int_part;
int  dig_2 = (int_part / 10) % 10;
int  dig_4 = (int_part / 1000) % 1000;
assert(dig_4 != 0);
value -= 10 * dig_2 + 1000 * dig_4;
value += 10 * dig_4 + 1000 * dig_2;
int_part = value;
x = int_part + frac_part;
这两种操作的顺序都不是最简单的,但它们相当直接。

我刚刚写了以下内容:

double swapDigits(double in)
{
    return in + ((int)(in / 10) % 10 - (int)(in / 1000) % 10) * 990.0;
}
我刚刚写了这样一句话:

double swapDigits(double in)
{
    return in + ((int)(in / 10) % 10 - (int)(in / 1000) % 10) * 990.0;
}

但无论我尝试什么,我都会出错。我无法传递错误,所以我尝试了各种方法,但都出现了错误,我从上面的代码中删除了这些错误,因为它们不相关。最简单的方法是对格式化字符串使用snprintf和一些字符操作。输入123.456的输出应该是什么-是143.256吗?1234.567的输出应该是什么-是1432.567吗?987654.321的输出应该是什么-是967854.321吗?@F4LLCON表示您只想交换第10位和第1000位的数字?因此您想切换10s数字和1000s数字;有趣的是,我是从左端开始计数,而不是从小数点开始倒数,因为……嗯,因为这是正常的计数方式。这也说明了为什么不止一个例子有帮助。@F4LLCON然而,它是含糊不清的,所以澄清通常是有帮助的。但无论我做什么,我都会出错。我无法传递错误,所以我尝试了各种方法,但都出现了错误,我从上面的代码中删除了这些错误,因为它们不相关。最简单的方法是对格式化字符串使用snprintf和一些字符操作。输入123.456的输出应该是什么-是143.256吗?1234.567的输出应该是什么-是1432.567吗?987654.321的输出应该是什么-是967854.321吗?@F4LLCON表示您只想交换第10位和第1000位的数字?因此您想切换10s数字和1000s数字;有趣的是,我是从左端开始计数,而不是从小数点开始倒数,因为……嗯,因为这是正常的计数方式。它还说明了为什么不止一个示例有帮助。@F4LLCON然而,它是不明确的,因此澄清通常是有帮助的。注意:当x>10*INT_MAX时,此方法失败。建议INT数千=INT fmodx,10000/1000;注:此方法在x>10*INT_MAX时失败。建议INT数千=INT fmodx,10000/1000;