atofjava的简单介绍

博主:adminadmin 2022-11-22 08:19:09 70

本篇文章给大家谈谈atofjava,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

怎么用java写一个简易的整数计算器,输入一道算式,然后给出结果,用C语言也行。

#include stdio.h

#include iostream

#include stack

#include string

#include vector

#include sstream

#include stdlib.h

 

using namespace std;

 

vectorpairint, string  f(string s)

{

    vectorpairint, string  v;

    char ch[100];

    string str;

    int i=0, j;

    while (s[i] != '\0')

    {

        if (s[i] == '(')

        {

            str = s[i++] + '\0';

            v.push_back(make_pairint, string (1, str));

        }

        else if (s[i] == ')')

        {

            str = s[i++] + '\0';

            v.push_back(make_pair  int, string(2, str));

        }

        else if (s[i] == '+' || s[i] == '-')

        {

            str = s[i++] + '\0';

            v.push_back(make_pair  int, string(3, str));

        }

        else if (s[i] == '*' || s[i] == '/')

        {

            str = s[i++] + '\0';

            v.push_back(make_pair  int, string(4, str));

        }

        else if(s[i] == ' ')

        {

            i++;

            continue;

        }

        else if (s[i] = '9'  s[i] = '0' || s[i] == '.')

        {

            bool dian;

            if(s[i] == '.')

                dian = true;

            j = 1;

            ch[0] = s[i++];

            int k = 1;

            while(s[i] = '9'  s[i] = '0' || s[i] == '.')

            {

                if(s[i] == '.'  dian)

                {

                    cout"输入2个小数点了"endl;

                    while(1);

                }

                ch[j] = s[i];

                ++j;

                ++i;

            }

            ch[j] = '\0';

            v.push_back(make_pair  int, string (0, ch));

            dian = false;

        }

    }

    return v;

}

 

bool bijiao(int n, string a)

{

    int m;

    if(a == "+" || a == "-")

        m=3;

    else if(a == "*" || a == "/")

        m=4;

    else if(a == "#")

        m=-1;

    return nm?true:false;

}

 

stackstring f_nbl(vectorpairint, string  v)

{

    stackstring s, f;

    f.push("#");

    for(vectorpairint, string ::iterator xi = v.begin(); xi != v.end(); ++xi)

    {

        if(xi-first == 0)

            s.push(xi-second);

        else

        {

            if(xi-first == 1)

                f.push(xi-second);

            else if(xi-first == 2)

            {

                while(f.top() != "(")

                {

                    s.push(f.top());

                    f.pop();

                }

                f.pop();

            }

            else if(f.top() == "(")

                f.push(xi-second);

            else

            {

                if(f.top() == "#")

                {

                    f.push(xi-second);

                }

                else

                {

                    if(bijiao(xi-first, f.top()))

                        f.push(xi-second);

                    else

                    {

                        while(!bijiao(xi-first, f.top()))

                        {

                            s.push(f.top());

                            f.pop();

                        }

                        f.push(xi-second);

                    }

                }

            }

        }

    }

    while(f.top() != "#")

    {

        s.push(f.top());

        f.pop();

    }

    f.pop();

    while(!s.empty())

    {

        f.push(s.top());

        s.pop();

    }

    return f;

}

 

double f_js(stackstring s)

{

    double a, b;

    string ch;

    stackdouble p;

    while(!s.empty())

    {

        while(s.top() != "+"  s.top() != "-"  s.top() != "*"  s.top() != "/")

        {

            p.push(atof(s.top().c_str()));

            if (s.size())

                s.pop();

        }

        ch = s.top();

        if (s.size())

            s.pop();

        b = p.size() ? p.top() : 0;

        if (p.size())

            p.pop();

        a = p.size() ? p.top() : 0;

        if (p.size())

            p.pop();

        switch(ch[0])

        {

        case '+' : p.push(a + b); break;

        case '-' : p.push(a - b); break;

        case '*' : p.push(a * b); break;

        case '/' : if(b==0.0)

            {

                cout"除数不能为0"endl;

                while(1);

            }

            else

                p.push(a / b);

            break;

        }

    }

    return p.size() ? p.top() : 0;

}

 

int main()

{

    string s;

    while (s != "quit")

    {

        cout"请输入算式:"endl;

        cins;

        couts"="f_js(f_nbl(f(s)))endl;

    }

    return 0;

}

atofjava的简单介绍

怎样把char转换为int(c++或java?

atoi(将字符串转换成整型数)相关函数atof,atol,atrtod,strtol,strtoul表头文件#includestdlib.h 定义函数int atoi(const char *nptr);函数说明atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。 返回值返回转换后的整型数。 附加说明atoi()与使用strtol(nptr,(char**)NULL,10);结果相同。 范例/* 将字符串a 与字符串b转换成数字后相加*/ #includestdlib.h mian(){char a[]=”-100”;

这样的java方法怎么打印出j,k,m值啊?

你可以在int m=arratOfint[j]的后面加上一条语句:System.out.println("j="+j+",k="+k+",m="m);

如何把一个字符串转成数字,float会好多小数点

如果是Java:

转换为浮点型:

使用Double或者Float的parseDouble或者parseFloat方法进行转换

转换为整型:

使用Integer的parseInt方法进行转换。

如果是C语言:

C语言有atoi、atol、atof等库函数,可分别把ASCII编码的字符串转化为int、long、float类型的数字。

头文件:stdlib.h

函数原型:int atoi(const char* nptr);

(另外两个类似)

举个例子:

char *str="123";

int num=atoi(str);

执行后,num的值被初始化为123

C语言中有没有求绝对值的函数啊?谢谢!

C语言中求绝对值的函数有两个:abs()和fabs()。

1、abs()  是用来求整型表达式的绝对值,比如:abs(x)  就是求整型变量x的绝对值。

2、fabs()  是用来求浮点型表达式的绝对值,比如:abs(x)  就是求浮点型变量(float型或double型)x的绝对值。

根据你的数据是int,long,还是double型,要用不同的函数

#include math.h

iy   =   abs(   ix   );  //int型

ly   =   labs(   lx   );  //long型

dy   =   fabs(   dx   );  //double型

扩展资料:

C语言其他常用函数:

1、atof:将str指向的字符串转换为一个double型的值,返回双精度计算结果。

2、atoi:将str指向的字符串转换为一个int型的值,返回转换结果。

3、exit:中止程序运行。将status的值返回调用的过程。

4、labs:计算long型整数num的绝对值,返回计算结果。

5、random:产生0到num之间的随机数,返回一个随机(整)数。

函数是一组一起执行一个任务的语句。每个 C 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数。

您可以把代码划分到不同的函数中。如何划分代码到不同的函数中是由您来决定的,但在逻辑上,划分通常是根据每个函数执行一个特定的任务来进行的。

函数声明告诉编译器函数的名称、返回类型和参数。函数定义提供了函数的实际主体。

C 标准库提供了大量的程序可以调用的内置函数。例如,函数 strcat() 用来连接两个字符串,函数 memcpy() 用来复制内存到另一个位置。

函数还有很多叫法,比如方法、子例程或程序,等等。

定义函数

C 语言中的函数定义的一般形式如下:

return_type function_name( parameter list )

{

body of the function

}

参考资料来源:百度百科-C语言函数

libvirt-java怎么获得虚拟机内存使用率

最近在用Nagios监控Xen PV虚拟机的时候出现了问题,在被监控的服务器上是采用nrpe来采集数据的。但是在进程里无法看到PV虚拟机的进程,虽然可以通过xm top vpsname的方式来获取名为vpsname虚拟机的cpu使用率情况,但是不便于采集数据,通过xm list可以采集到cpu时间,根据CPU时间的差值,可以计算CPU使用率,可是该命令只能root执行,因为该命令可以进行关闭,重启虚拟机等重要操作,所以如果把权限给了nrpe,将可能造成严重的安全问题。

幸好livirt提供了API,所以我打算尝试用API写一个Nagios的插件来满足的我的需求,我的想法就是分别2次获得虚拟机的CPU时间,并分别记录2次取得数据时的系统时间,然后根据差值来计算,在理论上是存在一些误差的。

1.要使用API,首先需要安装libvirt-devel

[root@aikaiyuan ~]# yum -y install libvirt-devel

2.我的代码如下,文件名为vCpu.c

/**

* Program Name: vCpu.c

* Author: steptodream

* Description:A simple plugin to get vps cpu usage

* for nagios(nrpe) by libvirt api

* Compile:gcc -o vCpu vCpu.c -lvirt

*/

#include stdlib.h

#include stdio.h

#include libvirt/libvirt.h

/* define the exit status for nagios */

#define OK 0

#define WARNING 1

#define CRITICAL 2

#define UNKNOWN 3

/* get cpu time of the given name */

double getCpuTime(char *vpsName,virConnectPtr conn) {

virDomainInfo info;

virDomainPtr domain = NULL;

int ret;

/* Find the domain of the given name */

domain = virDomainLookupByName(conn, vpsName);

if (domain == NULL) {

printf("Failed to find the vps called %sn", vpsName);

exit(OK);

}

/* Get the information of the domain */

ret = virDomainGetInfo(domain, info);

virDomainFree(domain);

if (ret 0) {

printf("Failed to get information for %sn", vpsName);

exit(OK);

}

return info.cpuTime;

}

int main(int argc,char * argv[])

{

char *vpsName; /* vps name */

int interval = 1; /* check interval */

double warning; /* warning value */

double critical; /* critical value */

double cpuUsage; /* cpu usage of the vps */

struct timeval startTime; /* time of the first time to get cpu time */

struct timeval endTime; /* time of the second time to get cpu time */

int realTime; /* real interval between two times */

long long startCpuTime; /* cpu time of the first time */

long long endCpuTime; /* cpu time of the second time */

int cpuTime; /* value of startCpuTime - endCpuTime */

char *output; /* output data for nagios */

int ret; /* exit status for nagios */

virConnectPtr conn; /* connection pointer */

switch (argc){

case 5:

interval = atoi(argv[4]);

case 4:

vpsName = argv[1];

warning = atof(argv[2]);

critical = atof(argv[3]);

break;

default:

printf("Usage:vCpu vName warning critical [interval]nn");

return OK;

}

/* connect to local Xen Host */

conn = virConnectOpenReadOnly(NULL);

if (conn == NULL) {

printf("Failed to connect to local Xen Hostn");

return OK;

}

/* get cpu time the first time */

startCpuTime = getCpuTime(vpsName, conn);

/* get start time */

if (gettimeofday(startTime, NULL) == -1) {

printf("Failed to get start timen");

return OK;

}

/* wait for some seconds */

sleep(interval);

/* get cpu time the second time */

endCpuTime = getCpuTime(vpsName, conn);

/* get end time */

if (gettimeofday(endTime, NULL) == -1) {

printf("Failed to get end timen");

return OK;

}

/* colose connection */

virConnectClose(conn);

/* calculate the usage of cpu */

cpuTime = (startCpuTime - endCpuTime) / 1000;

realTime = 1000000 * (startTime.tv_sec - endTime.tv_sec) +

(startTime.tv_usec - endTime.tv_usec);

cpuUsage = cpuTime / (double)(realTime);

/* display cpuUsage by percentage */

cpuUsage *= 100;

/* make output data and exit status for nagios*/

if (cpuUsage critical) {

output = "CRITICAL";

ret = CRITICAL;

} else if (cpuUsage warning){

output = "WARNING";

ret = WARNING;

} else {

output = "OK";

ret = OK;

}

printf("%s CPU:%.2f%|CPU=%.2f",output,cpuUsage,cpuUsage);

return ret;

}

3.编译测试,根据我的需求,我设置了3个必须参数和1个可选参数,分别是虚拟机名称vpsName,警告值warning(百分比值),危急值critical(百分比值)和检查间隔interval(秒)

[root@aikaiyuan ~]# gcc -o vCpu vCpu.c -lvirt

[root@aikaiyuan ~]# ./vCpu vmtest 1 2

OK CPU:0.20%|CPU=0.20

当然了,你同时可以再打开一个终端,用xm top vmtest来获取vmtest的cpu使用率,然后对比一下取值是否接近一致。我们再来看看返回值是否正常,因为Nagios是靠这个来判断服务状态的。

[root@aikaiyuan ~]# echo $?

注意,我的具体要求是检测指定名称的虚拟机的CPU使用率,如果超过了规定的warning或者critical值,就给使用者发邮件,所以在没有得到数据或者程序出错的情况,我都是以正常状态退出程序的。

另外,本人开发经验薄弱,所以代码里难免存在错误和不合理的地方以及不完善的地方(比如参数的合法性检验),仅供参考。

最后,关于代码中时间,我是以微秒(us)为统一单位来计算的,得到的cpu的时间是纳秒(ns),这个在结构体virDomainInfo定义中可以看到:

struct virDomainInfo{

unsigned char state : the running state, one of virDomainState

unsigned long maxMem : the maximum memory in KBytes allowed

unsigned long memory : the memory in KBytes used by the domain

unsigned short nrVirtCpu : the number of virtual CPUs for the domain

unsigned long long cpuTime : the CPU time used in nanoseconds

}

而通过gettimeofday取得存取到timeval结构体里的时间,包含tv_sec(s秒)和tv_usec(us微秒)这2种单位,可以从timeval结构体的定义中看到:

struct timeval {

time_t tv_sec; /* seconds */

suseconds_t tv_usec; /* microseconds */

};

关于atofjava和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。