- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
我一直在尝试编写将前缀表达式转换为后缀表达式的代码。
到目前为止,这是我所做的(请注意,我是新手,因此可能效率不高!)
/***************************************************
NOTE: This code fails to realise the precedence
of parentheses and $ (exponential) expressions
****************************************************/
import java.util.Scanner;
class Stack{
//Private Declarations
private int top;
private String a[] = new String [100];
//Public Declarations
public Stack(){
top = -1;
a[0] = "\0";
}
public void push(String x){
if (top != 99){
a[++top] = x;
}
else{
System.out.println("Stack overflow");
}
};
public String pop(){
if (top == -1){
System.out.println("\nStack empty!");
return "\n";
}
else{
return a[top--];
}
};
public int ret_top(){
return top;
};
}
public class Prefix2Postfix {
public static void main(String[] args) {
//Declaration
Scanner in = new Scanner (System.in);
Stack op = new Stack ();
Stack sym = new Stack ();
char ch;
int i;
String exp, str, str1, str2, str3;
//Taking input from the user
System.out.println("Enter the prefix expression : ");
exp = in.next();
for (i=0; i<exp.length(); i++){
ch = exp.charAt(i);
if((ch == '+')
||(ch == '-')
||(ch == '*')
||(ch == '/')
||(ch == '$')){
str = Character.toString(ch);
op.push(str);
}
else{
str = Character.toString(ch);
sym.push(str);
if (sym.ret_top() == 1){
str1 = sym.pop();
str2 = sym.pop();
str3 = op.pop();
str1 = str2.concat(str1);
str1 = str1.concat(str3);
sym.push(str1);
}
}
}
//Output
str = sym.pop();
System.out.println("After conversion to postfix" + ": " + str);
in.close();
}
}
正如我已经在代码中评论的那样,我的代码无法实现前缀表达式的优先级。
例如:
Infix : a + (b*c)
Prefix : +a*bc
Postfix : abc*+ //Expected output
Output I get : ab*c+
我使用的逻辑有问题还是我需要添加什么?您能否建议一些方法使我的代码可以正常工作?如果有人可以提供帮助,我将不胜感激。
注意:我们的教授建议我们编写自己的堆栈类和代码。另请注意,这是一种家庭作业。
提前致谢。
编辑:为了确保我的 STACK 正常工作,我编写了以下代码并且它可以正常工作。
import java.util.Scanner;
class STACK {
//Private Declarations
private int top;
private int elem[] = new int [100];
//Public Declarations
public STACK (){
top = -1;
elem[0] = 0;
}
public void push(int x){
if (top != 99){
elem[++top] = x;
}
else{
System.out.println("Stack overflow");
}
};
public int pop(){
if (top == -1){
System.out.println("Stack empty!");
return 0;
}
else{
return elem[top--];
}
};
}
public class StackPushPop {
public static void main(String[] args) {
STACK s = new STACK();
Scanner in = new Scanner (System.in);
int choice, x;
do{
System.out.println("Menu Options :");
System.out.println("1 -> Push an element");
System.out.println("2 -> Pop an element");
System.out.println("3 -> Empty complete stack");
System.out.println("Any other input for exit");
System.out.println("Your choice : ");
choice = in.nextInt();
switch(choice){
case 1:
System.out.println("\nEnter element : ");
x = in.nextInt();
s.push(x);
break;
case 2:
System.out.print("\nPopping element : ");
x = s.pop();
if (x != 0){
System.out.println(x);
}
break;
case 3:
System.out.println("\nEmptying stack!");
x = 1;
while (x!= 0){
x = s.pop();
if(x != 0){
System.out.print(x + " ");
}
}
break;
default:
choice = 0;
}
}while (choice != 0);
}
}
编辑我终于设法创建了一个运行良好的程序。
import java.util.Scanner;
class STACK{
private int top, MAX;
private String a[] = new String [1000];
public STACK(){
top = -1;
MAX = 1000;
a[0] = "";
}
public void push(String x){
if (top <= MAX-1){
a[++top] = x;
}
else{
System.out.println("Stack overflow");
}
};
public String pop(){
if (top == -1){
System.out.println("\nStack empty!");
return "\n";
}
else{
return a[top--];
}
};
public int getTop(){
return top;
};
}
public class Prefix2Postfix_STACK{
static boolean isOperator (char ch){
switch (ch){
case '+':
case '-':
case '*':
case '/':
case '$':
return true;
default :
return false;
}
}
public static void main(String[] args) {
//declarations
Scanner in = new Scanner (System.in);
String exp;
int i;
STACK s = new STACK ();
String exp_str[] = new String[100];
String postfix_exp = "\n";
//input
System.out.println("Enter prefix expression (No spaces or brackets) : ");
exp = in.next();
//create a string array of all characters but in reverse
for(i=0; i<=exp.length()-1; i++){
exp_str[exp.length()-1-i]=Character.toString(exp.charAt(i));
}
//computing postfix:
i=0;
do{
if (!isOperator(exp_str[i].charAt(0)))
s.push(exp_str[i]);
else{
String str1 = s.pop();
String str2 = s.pop();
str1 = str1 + str2 + exp_str[i];
postfix_exp = str1;
s.push(str1);
}
i++;
}while(s.getTop()>=0 && i!=exp.length());
//Output
System.out.println("After converting to postfix : " + postfix_exp);
in.close();
}
}
我想通过将帧划分为 8x8 子 block 并将当前帧的每个 block 与前 10 帧的相邻 block 进行比较,从前 10 帧中找到运动矢量。 我可以用单帧进行操作,但我觉得很难处理 N 帧。
我正在尝试从数据库中选择一个随机条目,但仅从最新的 100 个条目中选择。有什么想法吗?谢谢。 最佳答案 对于 MySql - SELECT * FROM (SELECT * FROM tabl
我有一个表格,其中包含职位状态。表列是: ID //which is unique incremental Post_id //liked post user_id //user
使用 Laravel Eloquent,如何从前 100 行中随机抽取 10 行(按日期排序)。 例如,我有这个: $comments = Comment::orderBy('created_at',
数据是pandas系列:我正在使用df.B=df.A.str.extract(r'')使用提取的 WHERE 单词创建 B 列df: A HI my lines are so super WHERE1
我正在使用下面的脚本从前 24 小时中提取数据并将其输出到一个 html 表中,它在 8 月的整个月都有效,但自从月份更改为 9 月后就搞砸了,现在显示第一个数据入口.. 我的“时间戳”列是 varc
最近学习了Git的基本概念。我们确实使用了一些 git-svn 来熟悉它的使用。我想和我的 friend 一起在 git 上开始我的第一个“真正的”项目。 因此我想问一下您一般使用 Git 的最佳实践
我正在编写一个脚本,我需要在标签上提取 id 中可用的值,我需要使用纯 Javascript 来完成此操作。我有类似的 jQuery 代码可用,但我无法成功完成 Javascript 代码。我的 jq
我有这个查询来选择记录号 49 和 50 SELECT id,date FROM table WHERE enabled=1 ORDER BY id DESC LIMIT 2 OFFSET 48 现在
我是一名优秀的程序员,十分优秀!