- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
基本上,我必须在所有数字上打印一个带有正确标志的等式。我当前的代码是:
printf("%dx^2+%dx+%d=0", a, b, c);
考虑到我已经有了 a、b 和 c 的值,我希望它能起作用。但是,负数会把事情搞砸,因为如果我设置
a = 2, b = 2, c = -2
(只是一个例子),它会输出
2x^2+2+-2=0
这显然看起来不对,那么如果它是负数,我该如何设置它,使加号不再存在?我唯一的想法是删除所有加号,但我会得到
2x^22-2=0
这也行不通。我知道这可能是一个简单的解决方法,但我对此并不陌生,如有任何帮助,我们将不胜感激。谢谢。
最佳答案
您可以使用 printf
标志符 '+'
轻松完成您想要的输出。具体来自 man 3 printf
:
Flag characters
+ A sign (+ or -) should always be placed before a number produced
by a signed conversion. By default, a sign is used only for
negative numbers. A + overrides a space if both are used.
例如:
#include <stdio.h>
int main (void) {
int a = 2, b = 2, c = -2;
printf ("%dx^2%+dx%+d = 0\n", a, b, c);
}
示例使用/输出
$ ./bin/printfsign
2x^2+2x-2 = 0
检查一下,让我知道这是否是您想要的。
关于c - 如何在C中打印带有正确符号的等式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52885546/
我正在尝试建立一个模型,该模型将根据是否存在某些组件(在我的情况下为流体端口)具有略微不同的方程。 如下代码将不起作用: parameter Boolean use_component=false;
首先采用,kludge 解决方案,sentinel 方法(您的程序必须不允许输入 sentinel 值): select coalesce(a, -2147483648) = coalesce(b,
我有一个类型族,它确定某物是否位于类型级列表的开头。 type family AtHead x xs where AtHead x (x ': xs) = True AtHead y
我试图找出为什么在下面的方法中,数组“8”中的最后一个值会满足“(8 & 7) == 0”的 where 子句。 public class Test { public static voi
我是 C 语言的初学者。我正在使用在线示例代码并尝试分析以下内容, int x = 0, y = 16; *x_ptr = &x 这是我要确保我在语法上理解的第二行。我刚刚遇到指针的概念,并试图从概念
我想知道geom_density()到底在做什么,所以我证明了图的合理性,以及是否有任何方法可以提取为绘制的每条曲线生成的函数或点。 谢谢 最佳答案 键入get("compute_group", gg
我试图用 Haskell 实现一个 Braun 树,定义如下: {-# LANGUAGE GADTs #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE PolyK
==有什么区别和 === ? 松散到底是怎么做的==对比工作? 严格怎么做===对比工作? 什么是一些有用的例子? 最佳答案 ==之间的区别和 === 松散的区别==相等运算符和严格 ===相同的运算
==有什么区别和 === ? 松散到底是怎么做的==对比工作? 严格怎么做===对比工作? 什么是一些有用的例子? 最佳答案 ==之间的区别和 === 松散的区别==相等运算符和严格 ===相同的运算
在理解下面的代码中发生了什么时遇到问题。数组 c 和 d 的行为是我所期望的。但是 a 和 b 是怎么回事? (我也用普通的标量变量尝试过这个,在任何一种情况下都没有发生令人惊讶的事情。) 输出被复制
我正在尝试使用 Java Swing 为数学方程式创建一个 GUI 5((θ/β) - cos(2πθ/β)) . 最初我开始使用一个简单的余弦函数并创建了 GUI,它工作正常。这是我的余弦函数程序:
我正在尝试将这条曲线作为我目前正在开发的一款小游戏的升级系统的一部分。方程如下 f(x) = -e^-((-log(7)/100)*(100-x))+7 在python中可以定义为 f=lambda
我是一名优秀的程序员,十分优秀!