小编joc*_*oce的帖子

C中带有%g的字符串格式

如何创建一个字符串,以便它将浮点数格式化为没有尾随的小数点或数字,当它是一个整数时,但不要切换到更大数字的科学记数法?

当我做:

float myFloat= 15.6f;
float myInt = 5.0f;
float myLarge = 7000000.0f;
sprintf(out, "my float is %g", myFloat);
sprintf(out, "my int is %g", myInt);
sprintf(out, "my large is %g", myLarge);
Run Code Online (Sandbox Code Playgroud)

我有类似的东西:

my float is 15.6
my int is 5
my large is 7e+07f

我想要一个单独的格式字符串,它将给出15.6,5和700000.

编辑原因评论不做格式化:

那正是我所想.但是包装器非常不方便,因为格式字符串嵌入在更长格式的字符串中:

sprintf(buf, "%d %g", myInt, myFloat);
Run Code Online (Sandbox Code Playgroud)

怎么包裹那个?

sprintf(buf, "%d %g", myInt, Wrapper(myFloat));??
Run Code Online (Sandbox Code Playgroud)

Wrapper应该返回什么?或者更糟:

sprintf(buf, "%d %s", myInt, Wrapper(myFloat));?? 
Run Code Online (Sandbox Code Playgroud)

c string format numbers

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

使用linq到xml选择具有给定属性的元素

我有以下XML结构:

<artists>
    <artist> 
        <name></name> 
        <image size="small"></image> 
        <image size="big"></image> 
    </artist>
</artists>
Run Code Online (Sandbox Code Playgroud)

我需要选择具有给定属性的名称和图像(size = big).

var q = from c in feed.Descendants("artist")
        select new { name = c.Element("name").Value, 
                     imgUrl = c.Element("image").Value };
Run Code Online (Sandbox Code Playgroud)

如何在上面的查询中指定所需的图像属性(size = big)?

linq-to-xml

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

学习金字塔(python)并且正在与@view_config装饰器进行斗争.它应该开箱即用吗?

我还在学习金字塔,而我正在努力学习如何使用装饰器.下面是我的测试视图可调用的副本.

from pyramid.response import Response
from pyramid.view import view_config
from pyramid.renderers import render_to_response

def my_blog(request):
    return {'project':'tricky'}

@view_config( renderer='templates/foo.pt' )
def foo_blog(request):
    return {'name':'tricky'}
Run Code Online (Sandbox Code Playgroud)

根据我对view_config装饰器的理解,它可用于设置应用程序配置,而无需在配置文件中实际设置它们.在本例中,我将渲染器设置为templates/foo.pt.这不起作用.

但是,如果我在配置文件(init .py)中设置渲染器,则:

config.add_route( 'foo_blog' , '/blog/{foo}' ,  view='tricky.views.Blog.blog.foo_blog' renderer='tricky:templates/mytemplate.pt' )
Run Code Online (Sandbox Code Playgroud)

它会工作.

我做错了什么阻止我能够使用装饰器.谢谢!

python decorator pyramid

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

c ++模板构造函数的问题

码:

#include<iostream>

using namespace std;

template<class T, int N> class point {
    T coordinate[N];
public:
    point(const point<T,N>&);
    const double& operator[](int i) const {
        return coordinate[i];
    }
};

template<class T, int N> point<T,N>::point(const point<T,N>&p)
{
    for(int i=0;i<N;i++)
        coordinate[i]=p.coordinate[i];
};

int main() {
    point<int,2> P2;
    point<double,3> P3;
    cout<<P2[0]<<P3[1];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

prog.cpp: In function ‘int main()’:
prog.cpp:17: error: no matching function for call to ‘point<int, 2>::point()’
prog.cpp:11: note: candidates are: point<T, N>::point(const point<T, N>&) [with T =
             int, int N = …
Run Code Online (Sandbox Code Playgroud)

c++ templates constructor

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

c ++将宏变量转换为wchar字符串文字的简便方法

在下面的示例中,我想删除该std::wstring(std::widen(...))部分,但'#'宏只返回一个char字符串文字 - 有没有办法容纳wchar?

#define FOO_MACRO(className)\
struct className##Factory : public OtherClass {\
// does some stuff here\
} className##Factory;\
someMap->add(std::wstring(std::widen(#className), className##Factory)))
Run Code Online (Sandbox Code Playgroud)

我怎么用wchar做同样的事情?

c++ macros

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

即使存在多个不同的递归调用,函数是否可以针对尾递归进行优化?

正如我在最近的SO问题中所提到的那样,我正在通过项目Euler问题来学习F#.

我现在对问题3有一个功能正常的答案,如下所示:

let rec findLargestPrimeFactor p n = 
    if n = 1L then p
    else
        if n % p = 0L then findLargestPrimeFactor p (n/p)
        else findLargestPrimeFactor (p + 2L) n

let result = findLargestPrimeFactor 3L 600851475143L
Run Code Online (Sandbox Code Playgroud)

但是,由于有2个执行路径可能导致不同的调用findLargestPrimeFactor,我不确定它是否可以针对尾递归进行优化.所以我想出了这个:

let rec findLargestPrimeFactor p n = 
    if n = 1L then p
    else
        let (p', n') = if n % p = 0L then (p, (n/p)) else (p + 2L, n)
        findLargestPrimeFactor p' n' …
Run Code Online (Sandbox Code Playgroud)

f# tail-recursion

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

BitmapImage.ImageOpened Not Firing

我正在尝试下载图像,并在完成时触发事件.我用这个:

BitmapImage btest = new BitmapImage(new Uri("http://www.google.com/images/srpr/logo4w.png"));
btest.ImageOpened += btest_ImageOpened;

void btest_ImageOpened(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)

但是,该ImageOpened事件不会发生.如果我将Image Control的源设置为BitmapImageusing:

image.Source = btest;
Run Code Online (Sandbox Code Playgroud)

它确实很火.ImageOpened除非将BitmapImage发件人设置为图像的来源,为什么不会触发事件?

c# bitmapimage windows-phone-7 windows-phone-8

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

创建虚拟分区

我正在尝试使用C++在USB驱动器上创建一个隐藏的虚拟分区.

我可以在Windows中使用虚拟磁盘API吗?

c++ windows

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

C++从文件末尾向后读取文件

我正在尝试编写一个带有菜单的程序,该菜单可以通过几种不同的方式从文本文件中读取.我只是在处理菜单选项#2(从文件末尾向后读),但我无法理解我做错了什么.我已经在这几天了,而且找不到任何好的资源来帮助解决这个问题.任何帮助,将不胜感激.

#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

using namespace std;

const int SIZE = 20;                     
typedef char String[SIZE];

//prototypes
void Menu(int &);
void ReadFile(ifstream &);
void BackwardFile(ifstream &,long &);
long CountFile(ifstream &,long &);

int main()
{  
    char filename[]= "grades.txt";
    int choice;
    long numBytes;

    ifstream InList;
    InList.open(filename);

    if(InList.good())
    {
        do
        {         
            Menu(choice); 
            switch(choice)
            {
                case 1:  
                    ReadFile(InList);
                    break;
                case 2:
                    CountFile(InList,numBytes);
                    BackwardFile(InList,numBytes);
                    break;
                case 3:
                    cout << "Pick a start position between 0 - " …
Run Code Online (Sandbox Code Playgroud)

c++ eof seekg

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

Python和ReportLab:PDF表格中的列宽和对齐错误

我使用Python 2.7(iOS Pythonista App)和reportlab 2.7模块来创建带有表格的PDF.一切正常.RepotLab自动格式化列的宽度.但是在两种情况下,我无法理解为什么reportlab以它的方式格式化输出以及如何获得我想要的格式.

案例1:几乎我想要的一切......

+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Day | Date     | Time          | Duration | Notes                                                                                                         |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Tue | 01.04.14 | 14:00 - 17:15 | 3.25     | Here are some notes.                                                                                          |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Wed | 02.04.14 | 18:00 - 20:15 | 2.25     | Sometime these notes are a little longer text so there must be a line break to let the whole note be visible! |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Thu | 02.04.14 | 14:00 - 17:15 | …
Run Code Online (Sandbox Code Playgroud)

python reportlab

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