小编iro*_*ion的帖子

如何用C中的逗号格式化货币?

我希望将Long Float格式化为C中的货币.我想在开头放置一个美元符号,逗号在十进制之前迭代每三位数,并在十进制之前放置一个点.到目前为止,我一直在打印这样的数字:

printf("You are owed $%.2Lf!\n", money);

它返回了类似的东西

You are owed $123456789.00!

数字看起来应该是这样的

$123,456,789.00
$1,234.56
$123.45

任何答案都不需要在实际代码中.你没有勺子喂食.如果有与c相关的细节会有所帮助,请提及.其他伪代码也没问题.

谢谢.

c formatting

4
推荐指数
1
解决办法
2万
查看次数

在C中包含外部库

我正在尝试将C库用于哈佛大学的开放课程.可以在此处找到教师有关设置外部库的说明.

我正在按照ubuntu特有的说明进行操作,因为我试图在我的ubuntu盒子上使用这个库.我按照页面上的说明进行设置,但是当我helloWorld.c使用cs50库函数运行一个简单的程序时,gcc不想播放.

例:

helloWorld.c

#include <stdio.h>
#include <cs50.h>

int
main(void){
        printf("What do you want to say to the world?\n");
        string message = GetString();
        printf("%s!\n\n", message);
}
Run Code Online (Sandbox Code Playgroud)

$ gcc helloWorld.c

/tmp/ccYilBgA.o: In function `main':
helloWorld.c:(.text+0x16): undefined reference to `GetString'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我按照指示中的说明按照说明进行了操作,但它们对我不起作用.我正在运行ubuntu 12.04.如果我能进一步澄清我的问题,请告诉我.

c cs50

4
推荐指数
2
解决办法
2万
查看次数

使用自签名证书在android中创建安全的客户端 - 服务器连接

我正在开发一个企业Android应用程序,因此有必要在我的测试阶段在客户端(android模拟器/测试手机)和服务器之间创建一个安全的连接,即使服务器的证书是自签名的,而正在购买合法的证书由公司(我现在控制的东西).

我需要信任服务器的自签名证书及其证书授权,当然,它本身不受Android操作系统的信任.我正在遵循谷歌关于在这个场景中几乎逐字创建HTTPS环境的建议.

我目前面临的问题是,我无法.crt从Google的示例中访问此行中的文件:

InputStream caInput = new BufferedInputStream(
    new FileInputStream("load-der.crt"));
Run Code Online (Sandbox Code Playgroud)

代替上述,我使用:

InputStream caInput = new BufferedInputStream(
getResources().openRawResource(R.raw.mycrtfile));
Run Code Online (Sandbox Code Playgroud)

打开文件所在的InputStream派生自.但是,我得到了一条线. mycrtfile.crt.crt/res/raw/mycrtfile.crtNullPointerException

有没有更好的方式来存储和访问,我需要加载作为的证书文件InputStreamFileInputStream不是存储在内部的原始资源res目录?

security ssl android

3
推荐指数
1
解决办法
1万
查看次数

Fibonacci序列子程序返回的值太高

#!/usr/bin/perl -w
use strict;

sub fib {
    my($num) = @_;  #give $num to input array
    return(1) if ($num<=1);  #termination condition
    return($num = &fib($num-1) + &fib($num-2));  #should return sum of first "n" terms in the fibonacci sequence
}

print &fib(7)."\n";  #should output 20
Run Code Online (Sandbox Code Playgroud)

该子程序应该输出第一个"x"项的总和,由sub的参数指定.但是,它太高了.这与递归有关吗?

谢谢.

algorithm math recursion perl fibonacci

2
推荐指数
2
解决办法
440
查看次数

为什么当我溢出分配的字符数组时,我的C程序不会崩溃?

我有一个简单的C文件I/O程序,它演示了逐行读取文本文件,并将其内容输出到控制台:

/**
* simple C program demonstrating how
* to read an entire text file
*/

#include <stdio.h>
#include <stdlib.h>

#define FILENAME "ohai.txt"

int main(void)
{
    // open a file for reading
    FILE* fp = fopen(FILENAME, "r");

    // check for successful open
    if(fp == NULL)
    {
        printf("couldn't open %s\n", FILENAME);
        return 1;
    }

    // size of each line
    char output[256];

    // read from the file
    while(fgets(output, sizeof(output), fp) != NULL)
        printf("%s", output);

    // report the error if we didn't reach …
Run Code Online (Sandbox Code Playgroud)

c memory arrays stack

2
推荐指数
1
解决办法
248
查看次数

我不明白引用计数中的内存错误

我正在实现一个人为的例子,以遵循本教程中的引用计数:

struct Bar
{
    Bar () : refs(1) {}
    int x;
    int y;
    int z;

    unsigned refs;
};

class Foo
{
    public:
        Foo () 
        {
            bar = new Bar;
            bar->x = 5;
            bar->y = 10; 
            bar->z = 15;
        }

        Foo (const Foo &other) : bar(other.bar)
        {
            ++bar->refs;
        }

        Foo& operator = (const Foo &other)
        {
            if (&other != this)
            {
                if (--bar->refs < 1)
                    delete bar;
                bar = other.bar;
                // this would fix it: ++bar->refs;
            }

            return *this; …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind reference-counting

1
推荐指数
1
解决办法
90
查看次数

无法使用点运算符编写Haskell函数

我正在写一个简单的函数来转换Integer为其各个数字的列表:

toDigits :: Integer -> [Integer]
toDigits 0 = [0]
toDigits n = [toInteger (digitToInt (show n !! x)) | x <- [0..(length (show n) - 1)]]
Run Code Online (Sandbox Code Playgroud)

我想链接

toInteger (digitToInt (show n !! x))

像点运算符一样:

toInteger . digitToInt . show . (!!) n x
Run Code Online (Sandbox Code Playgroud)

但是我觉得我对如何用点运算符编写这些函数有误解,因为这会产生难以理解的(对我的haskell-brain)类型错误

haskell

-1
推荐指数
1
解决办法
90
查看次数