linux+命令++传参,linux 上命令行传参数有关问题

C/C++ code#include "stdio.h"

#include "stdlib.h"

#include "ctype.h"

#define MAX 100

struct Stack

{

double *base;

int top;

};

void init (struct Stack *s);

void push (struct Stack *s, double val);

double pop (struct Stack *s);

int my_atof (char *str, double *val);

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

{

int c, i;

double val, op;

struct Stack *S = (struct Stack *)malloc (sizeof (struct Stack));

init (S);

if (argc == 1)

{

printf ("Please input expression!\n");

return 0;

}

i = 1;

for (; i < argc; i ++)

{

printf ("%s\n", argv[i]);

}

while (i < argc && (c = my_atof (*(argv + i), &val)) )

{

switch (c)

{

case 1:

push (S, val);

break;

case '+':

push (S, pop (S) + pop (S));

break;

case '-':

op = pop (S);

push (S, pop (S) - op);

break;

case '*':

push (S, pop (S) * pop (S));

break;

case '/':

op = pop (S);

if (op == 0)

{

printf ("Expression is invalid!\n");

return 0;

}

push (S, pop (S) / op);

break;

default :

break;

}

i ++;

}

if (c == 0)

{

printf ("Expression is invalid\n");

}

if (i == argc)

{

printf ("%f\n", pop (S));

}

return 0;

}

void init (struct Stack *S)

{

S->base = (double *)malloc (MAX * sizeof (double));

S->top = 0;

}

void push (struct Stack *S, double val)

{

if (S->top >= MAX)

{

printf ("Stack is full !\n");

}

else

{

S->base[S->top ++] = val;

}

}

double pop (struct Stack *S)

{

if (S->top == 0)

{

printf ("Stack is empty!\n");

}

else

{

S->top --;

return S->base[S->top];

}

}

int my_atof (char *str, double *val)

{

double power;

int sign;

while (isspace (*str))

{

str ++;

}

if (*str == '*' || *str == '/')

{

str ++;

if (*str != '\0')

{

return 0;

}

if (*str == '\0')

{

return *(str - 1);

}

}

sign = (*str == '-') ? -1 : 1;

if (*str == '-' || *str == '+')

{

str ++;

if (! isdigit (*str) && *str != '\0')

{

return 0;

}

if (*str == '\0')

{

return *(str - 1);

}

}

*val = 0;

while (isdigit (*str))

{

*val = *val * 10 + *str - '0';

str ++;

}

if (*str == '.')

{

power = 1;

while (isdigit (*++str))

{

*val = *val * 10 + *str - '0';

power *= 10;

str ++;

}

*val /= power;

}

if (*str != '\0')

{

return 0;

}

*val *= sign;

return 1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值