我如何将输入的生日字符串(如02 26 1991)变成格里高利历?
我尝试先解析它,但它一直给我一个错误信息,所以我不太清楚我做错了什么.在此日期之前我还有其他输入数据.一个是另一个字符串,一个是double值.
我的程序假设计算文件中每个字符的出现,忽略大写和小写.我写的方法是:
public int[] getCharTimes(File textFile) throws FileNotFoundException {
Scanner inFile = new Scanner(textFile);
int[] lower = new int[26];
char current;
int other = 0;
while(inFile.hasNext()){
String line = inFile.nextLine();
String line2 = line.toLowerCase();
for (int ch = 0; ch < line2.length(); ch++) {
current = line2.charAt(ch);
if(current >= 'a' && current <= 'z')
lower[current-'a']++;
else
other++;
}
}
return lower;
}
Run Code Online (Sandbox Code Playgroud)
并使用以下方式打印出来:
for(int letter = 0; letter < 26; letter++) {
System.out.print((char) (letter + 'a'));
System.out.println(": " + ts.getCharTimes(file));
} …Run Code Online (Sandbox Code Playgroud) 我正在为Linux编写设备驱动程序.它创建了一个包含4个次要编号的设备.每当我们尝试以次要编号3写入设备时,我们都会想要杀死设备,目前除了打印它正在写入booga设备之外,它不会做任何其他事情.这是我当前的一些代码,如果有必要,我可以发布更多代码:
写方法:
static ssize_t booga_write (struct file *filp, const char *buf, size_t count, loff_t *f_pose) {
printk("Attempting to write to booga device\n");
/* need to protect this with a semaphore if multiple processes
will invoke this driver to prevent a race condition */
if (down_interruptible (&booga_device_stats->sem))
return (-ERESTARTSYS);
booga_device_stats->num_bytes_written += count;
up(&booga_device_stats->sem);
return count; // pretend that count bytes were written
}
Run Code Online (Sandbox Code Playgroud)
如何测试:
static void run_write_test(char *device, int bufsize)
{
char *buf;
int src;
int out;
src = open(device, O_WRONLY); …Run Code Online (Sandbox Code Playgroud) 如何获取用户输入的生日,并转换为毫秒,以便我可以计算出他们在不同星球上的年龄
这是我到目前为止的代码:
DateFormat df = new SimpleDateFormat("MM dd yyyy");
Date dateBirth = df.parse(birthdate);
Calendar calBirth = new GregorianCalendar();
calBirth.setTime(dateBirth);
Run Code Online (Sandbox Code Playgroud)
编辑1:是的我希望得到用户的出生日期和当前时间之间的毫秒数,以便按行星的一年中的天数除以
我正在编写一个程序,它使用链接列表的Hashtable来计算单词的频率.程序会计算我输入的所有单词以及频率,但是在打印哈希表后,我会收到分段错误(核心转储)错误.当我对我的程序进行valgrind时,它表明我在三个不同的地方得到了大小为8的无效读取错误.我不知道如何修复它们.以下是三个不同的地方:
void freeTable(HashTablePtr table) {
int i;
ListPtr list;
if (table == NULL)
return;
for (i = 0; i < table->size; i++) {
list = table->table[i];
freeList(list);
}
free(table->table);
free(table);
}
HashTablePtr createTable(int tableSize) {
int i;
HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr));
table->table = (ListPtr *) malloc(sizeof(ListPtr) * tableSize);
table->size = tableSize;
for (i = 0; i < table->size; i++) {
table->table[i] = createList();
}
return table;
}
void printTable(HashTablePtr table) {
ListPtr tempList;
NodePtr tempNode;
HashObjectPtr obj;
int i; …Run Code Online (Sandbox Code Playgroud) 我目前正在编写一个程序,最终将比较两个目录中的文件并显示每个文件中已更改的函数.但是,在检查目录中的内容是文件还是子目录时,我遇到了问题.现在,当我检查它是否只是一个带-d检查的目录时,它不会捕获任何子目录.我在下面发布了部分代码.
opendir newDir, $newDir;
my @allNewFiles = grep { $_ ne '.' and $_ ne '..'} readdir newDir;
closedir newDir;
opendir oldDir, $oldDir;
my @allOldFiles = grep { $_ ne '.' and $_ ne '..'} readdir oldDir;
closedir oldDir;
foreach (@allNewFiles) {
if(-d $_) {
print "$_ is not a file and therefore is not able to be compared\n\n";
} elsif((File::Compare::compare("$newDir/$_", "$oldDir/$_") == 1)) {
print "$_ in new directory $newDirName differs from old directory $oldDirName\n\n";
print OUTPUTFILE "File: $_ …Run Code Online (Sandbox Code Playgroud) 我正在编写的程序假设打开两个目录并读取其中的文件以比较这些文件的内容.然后应将文件中已更改的函数打印到文件中.该程序主要是检查.cpp文件和.h文件.
目前我正在尝试浏览目录并打开我当前的文件来打印已更改的功能.但是,我一直收到错误,指出该文件不是文件而无法打开.
这是我正在使用的当前代码的一部分
use strict;
use warnings;
use diagnostics -verbose;
use File::Compare;
use Text::Diff;
my $newDir = 'C:\Users\kkahla\Documents\Perl\TestFiles2';
my $oldDir = 'C:\Users\kkahla\Documents\Perl\TestFiles';
chomp $newDir;
$newDir =~ s#/$##;
chomp $oldDir;
$oldDir =~ s#/$##;
# Checks to make sure they are directories
unless(-d $newDir or -d $oldDir) {
print STDERR "Invalid directory path for one of the directories";
exit(0);
}
# Makes a directory for the outputs to go to unless one already exists
mkdir "Outputs", 0777 unless -d "Outputs";
# opens …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,询问用户他们的出生日期,然后计算他们在不同行星的年龄.我不假设如何输入生日,除了每个数字之间有一个空格.
我现在的代码现在不符合这些规范,我不知道如何编写它.我也有计算今天我的年龄的问题.当我输入我的出生日期并打印出年龄时,它现在告诉我,当我打印出dateBirth时,我已经是407了,今天这两个日期都是正确的.
System.out.print("Please enter your birthdate (mm dd yyyy): ");
birthdate = scan.nextLine();
DateFormat df = new SimpleDateFormat("MM dd yyyy");
Date dateBirth = df.parse(birthdate);
Calendar calBirth = new GregorianCalendar();
calBirth.setTime(dateBirth);
Calendar calDay = new GregorianCalendar();
today = calDay.getTime();
age = (today.getTime() - dateBirth.getTime()) / (1000 * 60 * 60 * 24 * 365);
Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,其中一个部分要求程序打印文件中有多少个字符(包括空格).我现在的代码虽然每次都返回0,但我不确定为什么它不计算字符.
public int getcharCount(Scanner textFile) {
int count = 0;
while(textFile.hasNext()) {
String line = textFile.nextLine();
for(int i=0; i < line.length(); i++)
count++;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我的程序的规格说我应该使用扫描仪.虽然我不确定,但我不相信它会进入for循环.当我使用相同的技术来计算文件中的行数时,它完美地工作.该代码是:
public int getLineCount(Scanner textFile) {
int lineCount = 0;
while(textFile.hasNext()) {
String line = textFile.nextLine();
lineCount++;
}
return lineCount;
}
Run Code Online (Sandbox Code Playgroud)
我们不需要检查该行是否包含任何内容.如果它出现在文本文件的中间,则应将其计为一个字符.