c++里面有没有什么办法做到 判断某个给定的未知数是double类型还是int类型 呢?...

c++里面有没有什么办法做到 判断某个给定的未知数是double类型还是int类型 呢?

如果只是double和int, 可以用sizeof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using  namespace  std;
#include <stdlib.h>
int  main()
{
     double  a;
     double  b;
     float  c = 3.0;
      
      b=1, a=b+5/2;
     cout <<  sizeof  a << endl;
     cout <<  sizeof  b << endl;
     cout <<  sizeof   c<< endl;
     cout <<  sizeof  3 << endl;
     cout <<  sizeof  3.0 << endl;
     cout <<  sizeof (( int )3)  << endl;
     cout <<  sizeof (( long )3)  << endl;
     cout <<  sizeof (( float )3)  << endl;
     cout <<  sizeof (( double )3)  << endl; 
     system ( "pause" );
     return  0;
}

85861-20170707150237159-388727745.png


c++里面有没有什么办法做到 判断某个给定的未知数是int类型,long类型,还是float类型 呢?


 貌似做不到


参考

--------------------------------
1.sizeof 运算符 - cppreference.com

2.若已定义x和y为double类型,则表达式:x=1,y=x+3/2的值是______.(A、1B、2C、2.0D、2.5_百度作业帮



== 以上 2017/7/7 下午3:00:20




转载于:https://www.cnblogs.com/liango/p/7132257.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我是一名语言模型,无法为您编写代码。但是,我可以提供一些思路和建议: 1. 将中缀表达式转换为后缀表达式。 2. 使用栈来计算后缀表达式。 3. 在计算过程中,遇到除数为0或开负数的平方根时,返回-114514。 4. 在计算过程中,如果出现x,则用给定的值代替x。 5. 可以使用库函数<math.h>中的函数来计算幂和开方。 以下是一个简单的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAX_EXPR_LEN 100 #define STACK_SIZE 100 typedef struct { double data[STACK_SIZE]; int top; } Stack; void initStack(Stack *s) { s->top = -1; } void push(Stack *s, double x) { if (s->top == STACK_SIZE - 1) { printf("Stack Overflow!\n"); exit(1); } s->data[++(s->top)] = x; } double pop(Stack *s) { if (s->top == -1) { printf("Stack Underflow!\n"); exit(1); } return s->data[(s->top)--]; } double calculate(double x, char *expr) { char postfix[MAX_EXPR_LEN]; int i, j, n = strlen(expr); char c; double a, b, result, tmp; Stack s; initStack(&s); // 将中缀表达式转换为后缀表达式 for (i = j = 0; i < n; i++) { c = expr[i]; if (c >= '0' && c <= '9') { postfix[j++] = c; } else if (c == 'x') { tmp = x; do { postfix[j++] = (int)tmp % 10 + '0'; tmp /= 10; } while (tmp > 0); postfix[j++] = '#'; // 用#标记x的结束位置 } else if (c == '(') { push(&s, c); } else if (c == ')') { while ((c = pop(&s)) != '(') { postfix[j++] = c; } } else if (c == '+' || c == '-') { while (s.top >= 0 && s.data[s.top] != '(') { postfix[j++] = pop(&s); } push(&s, c); } else if (c == '*' || c == '/') { while (s.top >= 0 && (s.data[s.top] == '*' || s.data[s.top] == '/' || s.data[s.top] == '^')) { postfix[j++] = pop(&s); } push(&s, c); } else if (c == '^' || c == 's' || c == 'c' || c == 't' || c == 'q') { while (s.top >= 0 && s.data[s.top] == '^') { postfix[j++] = pop(&s); } push(&s, c); } } while (s.top >= 0) { postfix[j++] = pop(&s); } postfix[j] = '\0'; // 计算后缀表达式 for (i = 0; postfix[i]; i++) { c = postfix[i]; if (c >= '0' && c <= '9') { push(&s, c - '0'); } else if (c == '#') { push(&s, x); } else if (c == '+') { b = pop(&s); a = pop(&s); push(&s, a + b); } else if (c == '-') { b = pop(&s); a = pop(&s); push(&s, a - b); } else if (c == '*') { b = pop(&s); a = pop(&s); push(&s, a * b); } else if (c == '/') { b = pop(&s); if (b == 0) { return -114514; } a = pop(&s); push(&s, a / b); } else if (c == '^') { b = pop(&s); a = pop(&s); push(&s, pow(a, b)); } else if (c == 's') { a = pop(&s); push(&s, sin(a)); } else if (c == 'c') { a = pop(&s); push(&s, cos(a)); } else if (c == 't') { a = pop(&s); push(&s, tan(a)); } else if (c == 'q') { a = pop(&s); if (a < 0) { return -114514; } push(&s, sqrt(a)); } } result = pop(&s); return result; } int main() { char expr[MAX_EXPR_LEN]; double x, result; printf("Enter the expression: "); scanf("%s", expr); printf("Enter the value of x: "); scanf("%lf", &x); result = calculate(x, expr); if (result == -114514) { printf("Error!\n"); } else { printf("Result: %lf\n", result); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值