如何比较字符数组的CUDA C ++? [英] How to compare arrays of char in CUDA C++?

查看:116
本文介绍了如何比较字符数组的CUDA C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要CUDA一个函数,临危字符数组,如果他们匹配,函数返回一个值,但是当我测试这个code,总是返回0,如没有这些条件匹配。到目前为止,我想这样的:

I need a function in cuda that recieves an array of chars and if they match, the function returns a value, but when i test this code, always return 0, like none of these conditions match. so far i tried this:

__device__   float operate_1(float num, char  func[]) {

        if(func[0] == 's' && func[1] == 'i' && func[2] == 'n'  )

            return sinf(num * PI/180.0);

        else if(func[0] == 'c' && func[1] == 'o' && func[2] == 's'  )

            return cosf(num * PI/180.0);

        else if(func[0] == 'l' && func[1] == 'o' && func[2] == 'g'  )

            return logf(num);

       else if(func[0] == 'e')

            return expf (num);

        return 0; }
        //evaluates an operator.

这code太多,但像previous一个不正常工作。

and this code too, but doesn't work like the previous one.

  __device__   float operate_1(float num, char  func[]) {

        if(func == "sin")

            return sinf(num * PI/180.0);

        else if(func == "cos")

            return cosf(num * PI/180.0);

        else if(func == "log")

            return logf(num);

       else if(func == "e")

            return expf (num);

        return -0; }
        //evaluates an operator.

我可以我该怎么办?

what i can i do?

推荐答案

这个怎么样:

__device__ int my_strcmp(const char *str_a, const char *str_b, unsigned len = 256){
  int match = 0;
  unsigned i = 0;
  unsigned done = 0;
  while ((i < len) && (match == 0) && !done){
    if ((str_a[i] == 0) || (str_b[i] == 0)) done = 1;
    else if (str_a[i] != str_b[i]){
      match = i+1;
      if ((int)str_a[i] - (int)str_b[i]) < 0) match = 0 - (i + 1);}
    i++;}
  return match;
  }

这篇关于如何比较字符数组的CUDA C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆