小编Kat*_*tie的帖子

当前日期和时间为字符串

我写了一个函数来获取格式的当前日期和时间:DD-MM-YYYY HH:MM:SS.它可以工作,但让我们说,它非常难看.我怎么能做同样的事情,但更简单?

string currentDateToString()
{
    time_t now = time(0);
    tm *ltm = localtime(&now);

    string dateString = "", tmp = "";
    tmp = numToString(ltm->tm_mday);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp;
    dateString += "-";
    tmp = numToString(1 + ltm->tm_mon);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp;
    dateString += "-";
    tmp = numToString(1900 + ltm->tm_year);
    dateString += tmp;
    dateString += " ";
    tmp = numToString(ltm->tm_hour);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp; …
Run Code Online (Sandbox Code Playgroud)

c++ string time datetime date

63
推荐指数
6
解决办法
19万
查看次数

如何编写bash脚本来设置全局环境变量?

最近我写了一个设置环境变量的脚本,看看:

#!/bin/bash

echo "Pass a path:"
read path
echo $path

defaultPath=/home/$(whoami)/Desktop

if [ -n "$path" ]; then
    export my_var=$path
else
    echo "Path is empty! Exporting default path ..."
    export my_var=$defaultPath
fi

echo "Exported path: $my_var"
Run Code Online (Sandbox Code Playgroud)

它工作得很好但问题是my_var只在本地可用,我的意思是在我运行脚本的控制台窗口中.

如何编写一个脚本,允许我导出可以随处看到的全局环境变量?

bash shell environment-variables

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

Java除以零不会抛出ArithmeticException - 为什么?

为什么这段代码不会抛出ArithmeticException?看一看:

public class NewClass {

    public static void main(String[] args) {
        // TODO code application logic here
        double tab[] = {1.2, 3.4, 0.0, 5.6};

        try {
            for (int i = 0; i < tab.length; i++) {
                tab[i] = 1.0 / tab[i];
            }
        } catch (ArithmeticException ae) {
            System.out.println("ArithmeticException occured!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道!

java arithmeticexception

39
推荐指数
6
解决办法
10万
查看次数

从PostgreSQL中选择日期(时间戳)作为字符串(char),注意NULL值

我想选择一个日期(我的列是时间戳类型).但是当列中的是NULL日期时,我想返回一个空字符串.这该怎么做?我写了这个:

SELECT
   CASE WHEN to_char(last_post, 'MM-DD-YYYY HH24:MI:SS') IS NULL THEN ''
      ELSE to_char(last_post, 'MM-DD-YYYY HH24:MI:SS') AS last_post END
   to_char(last_post, 'MM-DD-YYYY HH24:MI:SS') AS last_post, content
FROM topic;
Run Code Online (Sandbox Code Playgroud)

但它告诉我一些错误,不知道为什么:

ERROR:  syntax error at or near "as"
LINE 1: ...ELSE to_char(last_post, 'MM-DD-YYYY HH24:MI:SS') AS last_po...
                                                            ^
Run Code Online (Sandbox Code Playgroud)

sql postgresql select postgresql-9.1

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

元素目前不可见,因此可能无法与Selenium Dropdown Box Python进行交互

我需要浏览网页上的所有页面.所有这些页面的左上角都有一个下拉框,包含所有可用的城市.我想通过选择此下拉框中的每个位置来访问每个页面.下拉框有一个滚动条,当我想选择它下面的选项时,它会给我异常消息:

Message: Element is not currently visible and so may not be interacted with
Stacktrace:
    at fxdriver.preconditions.visible (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
Run Code Online (Sandbox Code Playgroud)

下面是代码:

#!/bin/env/python
# -*- coding: utf-8 -*-

import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

def get_browser():
    return webdriver.Firefox()


main_page_url = "http://example.com/"
basic_url = 'http://example.com/ogloszenia-kobiet.html'

def …
Run Code Online (Sandbox Code Playgroud)

python selenium

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

AssertionError:不兼容的大小:参数'height'必须是长度2或标量(Matplotlib,Python 2.7,绘制图表)

不幸的是,新的一天带来了Python的新问题:/

我有一个用我用Java编写的其他应用程序生成的文件.这个应用程序生成带有一些数据的文件,它是一种随机的东西,因为我不能说每个文件有多少行.示例文件如下所示:

3   Sat Jan 21 00:00:00 2012
7   Sun Mar 11 00:00:00 2012
5   Fri Jan  1 00:00:00 2010
4   Sat Feb  5 00:00:00 2011
8   Sun Apr 11 00:00:00 2010
4   Wed Aug 24 00:00:00 2011
8   Sat Feb 20 00:00:00 2010
3   Thu Oct 13 00:00:00 2011
9   Fri Dec 17 00:00:00 2010
4   Tue Jul 20 00:00:00 2010
8   Fri Dec  2 00:00:00 2011
6   Mon May 31 00:00:00 2010
5   Mon May 16 00:00:00 2011 …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

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

如何在bash脚本中运行ac程序并给它2个参数?

我有一个C程序,它有2个参数,文件名和文本.我想在bash中编写一个脚本,它还带有2个参数,路径和文件扩展名,将遍历给定路径中的所有文件,并在C中作为参数文件提供给我的程序,仅包含givenextension和text.

继承我的C程序,真的没什么特别的:

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

int main(int argc, char **argv)
{
    if(argc < 3)
    {
        fprintf(stderr, "Give 2 args!\n");
        exit(-1);
    }

    char *arg1 = argv[1];
    char *arg2 = argv[2];

    fprintf(stdout, "You gave: %s, %s\n", arg1, arg2);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

和我的bash脚本:

#!/bin/bash

path=$1
ext=$2
text=$3

for file in $path/*.$ext
do
    ./app | 
    {
        echo $file 
        echo $text
    }
done
Run Code Online (Sandbox Code Playgroud)

我像这样使用它:./script /tmp txt hello它应该将txt来自/tmp和'hello'的所有文件作为参数提供给我的C程序.不,它只显示Give 2 args!:(请,帮助.

c bash shell

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

如何整理TreeMap <String,Integer>?

我有一张地图:TreeMap<String, Integer> m = new TreeMap<>();我有一个完整的字母和值,显示我的文字中每个字母的次数.

我想按递减计数顺序对该地图进行排序; 也就是说,最常见的字母在第一行,最后一行表示最不频繁的字母.如果两个字母具有相同的频率,那么字母表中首先出现的字母必须首先出现.怎么做?

我试过Comparator:

public int compare(String a, String b) {
        if (base.get(a) >= base.get(b) && a.compareToIgnoreCase(b) < 0) {
            return -1;
        } else {
            return 1;
        }
    }
Run Code Online (Sandbox Code Playgroud)

但仍然,不是它,输出是:

D 3
E 3
A 2
S 5
Run Code Online (Sandbox Code Playgroud)

伙计们......之前发现这个,这根本没有帮助.好的输出应该是:

S 5
D 3
E 3
A 2
Run Code Online (Sandbox Code Playgroud)

java treemap

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

如何用Python中的2个词典创建一个dict?

我现在正在处理Python dicts.我写了一个代码:

import random

categories = {1 : "Antics", 2 : "Tickets", 3: "Moviez",
              4 : "Music", 5 : "Photography", 6 : "Gamez", 7 : "Bookz",
              8 : "Jewelry", 9 : "Computers", 10 : "Clothes"}

items = {"Picture" : 1, "Clock" : 1, "Ticket for Mettalica concert" : 2,
         "Ticket for Iron Maiden concert" : 2, "Ticket for Placebo concert" : 2,
         "The pianist" : 3, "Batman" : 3, "Spider-Man" : 3,
         "WoW" : 6, "Cabal" : 6, "Diablo 3" …
Run Code Online (Sandbox Code Playgroud)

python dictionary

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

未定义的引用和非虚拟的thunk

我有这样的课程:

class Product
    {
        public :
            virtual double getPrice();
            virtual void setPrice(double price);
    };

class MusicProduct
    {
        protected:

            string author;
            double price;

        public :

            virtual string getAuthor();
            virtual void setAuthor(string author);
            ~MusicProduct();
    };

class CD : public MusicProduct, public Product
    {
        public :

            string getAuthor();
            void setAuthor(string author);
            double getPrice();
            void setPrice(double price);
    };

string CD::getAuthor()
    {
        return MusicProduct::author;
    }

    void CD::setAuthor(string author)
    {
        MusicProduct:author = author;
    }

    void setPrice(double price)
    {
    MusicProduct::price = price;
    }

    double getPrice()
    {
        return …
Run Code Online (Sandbox Code Playgroud)

c++ abstract-class

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