int a = 123; int b = 321; double n = a * 100 / b; n为多少
时间: 2024-03-11 10:49:57 浏览: 21
根据运算符优先级,先进行乘法运算,然后再进行除法运算。因此,表达式 `a * 100 / b` 相当于 `(a * 100) / b`。将a和b代入表达式中得到:
```
n = (123 * 100) / 321
```
计算得到结果为:
```
n = 38.316645
```
由于n是一个double类型的变量,因此结果为一个小数。如果您需要得到整数结果,可以使用强制类型转换来将结果转换为int类型。例如:
```java
int a = 123;
int b = 321;
int result = (int) (a * 100 / b); // 结果为38
```
相关问题
double a=123.49556; a=a*1000; int b=((int) a)%10; a=(int)(a/10); if(b>=5&&b<=9){ a=a+1; } a=((double)a)/100; System.out.println(a);优化
这段代码可以进行如下优化:
```java
double a = 123.49556;
a = Math.round(a * 1000.0); // 四舍五入取整
a = a / 10.0; // 将 a 除以 10
System.out.println(a);
```
这样可以直接通过Math类中的round()方法进行四舍五入取整,避免了使用条件判断语句,代码更加简洁。同时,将a除以10的操作也可以在乘以1000时进行,避免了多次类型转换和运算,提高了代码的效率。
/* 函数功能:求一个后缀表达式的值 */ double evalpost(char f[]); /* 函数功能:判断一个字符是否为运算符 */ int is_operation(char op); /* 函数功能:将一个中缀表达式e转换为与它等价的后缀表达式f */ void postfix(char e[], char f[]); /* 函数功能:求运算符的优先级 */ int priority(char op); /* 函数功能:将数字字符串转变成相应的数 */ double readnumber(char f[], int *i); /* 函数功能:判断表达式括号是否匹配 */ int match_kuohao(char c[]); 为这段代码编写测试代码
以下是对每个函数的测试代码:
1. evalpost(char f[])
```c++
#include <iostream>
#include <cstring>
#include "evalpost.h"
using namespace std;
int main() {
char f[] = "23+45+*";
double res = evalpost(f);
cout << "Result: " << res << endl;
return 0;
}
```
2. is_operation(char op)
```c++
#include <iostream>
#include <cstring>
#include "is_operation.h"
using namespace std;
int main() {
char op = '+';
int res = is_operation(op);
cout << "Is operator: " << (res ? "true" : "false") << endl;
return 0;
}
```
3. postfix(char e[], char f[])
```c++
#include <iostream>
#include <cstring>
#include "postfix.h"
using namespace std;
int main() {
char e[] = "2+3*4+5";
char f[100];
postfix(e, f);
cout << "Postfix expression: " << f << endl;
return 0;
}
```
4. priority(char op)
```c++
#include <iostream>
#include <cstring>
#include "priority.h"
using namespace std;
int main() {
char op = '*';
int res = priority(op);
cout << "Priority: " << res << endl;
return 0;
}
```
5. readnumber(char f[], int *i)
```c++
#include <iostream>
#include <cstring>
#include "readnumber.h"
using namespace std;
int main() {
char f[] = "123.45";
int i = 0;
double res = readnumber(f, &i);
cout << "Number: " << res << endl;
return 0;
}
```
6. match_kuohao(char c[])
```c++
#include <iostream>
#include <cstring>
#include "match_kuohao.h"
using namespace std;
int main() {
char c[] = "((2+3)*4)";
int res = match_kuohao(c);
cout << "Matching brackets: " << (res ? "true" : "false") << endl;
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)