小编ten*_*kii的帖子

C - 比较两个字符

我在比较两个角色时遇到了麻烦.我写了一个非常基本的C问题来尝试命令行参数.

到目前为止,这是我的代码:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    char ch;
    char *type = "";
    char *gender = "";
    int baby = 0;
    int count = 0;

    /* Options:
     * -t = type of pet
     * -g = gender
     * -b = baby or adult
     */
    while ((ch = getopt(argc, argv, "t:g:b")) != EOF)
        switch (ch) {
            case 't':
                type = optarg;
                break;
            case 'g':
                gender = optarg;
                break;
            case 'b':
                baby = 1;
                break;
            default:
                fprintf(stderr, …
Run Code Online (Sandbox Code Playgroud)

c compare character

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

strstr()函数

我正在尝试编写一个程序,将用户输入的子字符串与字符串数组进行比较.

#include <stdio.h>
#include <string.h>

char animals[][20] = {
    "dogs are cool",
    "frogs are freaky",
    "monkeys are crazy"
};

int main() {
    char input[10];

    puts("Enter animal name: ");
    fgets(input, sizeof(input), stdin);

    int i;
    for(i = 0; i < 3; i++) {
        if(strstr(animals[i], input))
            printf("%s", animals[i]);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我进入青蛙时,例如它应该打印出"青蛙怪异"的信息,但它什么都没打印出来.

所以我试着写一行来每次打印出strstr()函数的值,它们都返回0,这意味着所有的比较都失败了.我不明白为什么,有人可以帮帮我吗?

c substring strstr

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

使用Stack修复我的"inorder树遍历"算法的实现

部分原因是我必须实现二阶树的顺序遍历的非递归方法.我有点卡住了.这是我到目前为止:

public void inorder(BinaryTree v) {
    Stack<BinaryTree> stack = new Stack<BinaryTree>();
    stack.push(v);
    System.out.println(v.getValue());

    while(!stack.isEmpty()) {
        while(v.getLeft() != null) {
            v = v.getLeft();
            stack.push(v);
            System.out.println(v.getValue());
        }

        while(v.getRight() != null) {
            v = v.getRight();
            stack.push(v);
            System.out.println(v.getValue());
        }
                stack.pop();
    }
}
Run Code Online (Sandbox Code Playgroud)

我注意到它只打印出我树的左侧,例如

          A
        /   \
       B     C
     /   \  /  \
    D     E F   G
   /  \
  H     I
 / \
J   K
Run Code Online (Sandbox Code Playgroud)

A B D H J

java tree stack binary-tree tree-traversal

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

XML 从另一个 XML 文件获取值

我正在尝试构建一个使用 Google Map API 的 Android 应用程序,并且我想将该项目放在 GIT 上。所以我有一个 API 密钥,此信息位于 AndroidManifest.xml 中,如下所示(我已将实际的 API 密钥替换为*key*此处

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value= *key* />
Run Code Online (Sandbox Code Playgroud)

我不希望将这些敏感信息推送到 git 上,但我无法 gitignore 这个文件。是否可以制作另一个具有密钥的 XML 文件,并让 AndroidManfiest.xml 从那里获取它(以便我可以 gitignore 这个文件)?

AndroidManifest.xml 有这段代码

<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"
android:launchMode="singleTop" />
Run Code Online (Sandbox Code Playgroud)

我注意到 label 属性从位于 res/values/strings.xml 中的另一个 XML 文件获取其值,因此:

  1. AndroidManifest.xml 如何知道在哪里找到值label
  2. 是什么@意思?
  3. 我可以将 XML 文件(称为 Config.xml)放在任何地方吗?或者它必须在res文件夹中?

xml android

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

如何使用Collection.sort()对集合进行排序

我写的是一个存储程序ArrayListPerson对象(输入是从一个文本文件).

这是Person该类的代码,我将从中创建Person对象:

import java.io.Serializable;

public class Person implements Comparable<Person>, Serializable
{
private String firstName;
private String lastName;
private int age;

public Person(String firstName, String lastName, int age)
{
    this.firstName = firstName;
    this.lastName = lastName.toUpperCase();
    this.age = age;
}

public int getAge() 
{
    return age;
}

public String getName()
{
    return firstName;
}

/**
 * @return a String of the details of a person in the format:
 *      Name: <firstName> <lastName> Age: <age> …
Run Code Online (Sandbox Code Playgroud)

java

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

C++ - 对声明枚举感到困惑

所以我正在关注tutorialspoint上的C++教程.

我真的很困惑,为什么新变量c的声明在声明一个新的枚举的同一行(对我来说看起来像一个typedef):

 enum color { red, green, blue } c;
 c = blue;
Run Code Online (Sandbox Code Playgroud)

如果我们想制作一个颜色类型的新变量,我就不能写

enum color {red,green,blue} newVar;
Run Code Online (Sandbox Code Playgroud)

正确?在C++中它是枚举颜色还是只是颜色?

c++ enums

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

指定参数

我是Haskell的新手,我正在进行一些函数式编程工作.

以下是如何指定查找两个数字的最大值的示例:

max :: Integer -> Integer -> Integer
max x y
 | x >= y = x
 | otherwise = y

max' :: Integer -> Integer -> Integer
max' x y = 
 if x >= y then x else y

prop_max1 :: Integer -> Integer -> Bool
prop_max1 x y =
 x <= max x y && y <= max' x y
Run Code Online (Sandbox Code Playgroud)

我想使用这个prop_max1函数,所以我写:

prop_max1 (max 1 2) (max' 1 2)
Run Code Online (Sandbox Code Playgroud)

返回True.

但如果我写

prop_max1((max 1 2) …
Run Code Online (Sandbox Code Playgroud)

haskell

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

标签 统计

c ×2

java ×2

android ×1

binary-tree ×1

c++ ×1

character ×1

compare ×1

enums ×1

haskell ×1

stack ×1

strstr ×1

substring ×1

tree ×1

tree-traversal ×1

xml ×1