跳出while(!StdIn.isEmpty())问题

本文介绍了一种使用栈数据结构实现的后缀表达式求值算法,通过逐个读取输入的字符并根据运算符进行相应的计算,最终得到表达式的值。此算法适用于计算机科学中的基础算法学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ali.exercises01_03;

import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdIn;


public class EvaluatePostfix {
	
	/**
	 * @param argsa
	 */
	public static void main(String[] args) {
	    Stack<Double> vals = new Stack<>();		//操作数
	  
	    while(!StdIn.isEmpty()) {	
	    	//因为是字符串原因,输入一个数就得space一下,保证是多个字符串,而不是一个字符串
	    	//like:1 2 + 3 4 - 5 6 - * *
	    	String s = StdIn.readString();
	    	double d = 0.0;
	    	if(s.equals("+")) {
	            d = vals.pop() + vals.pop();
	            vals.push(d);
	    	}else if( s.equals("-")) {
	    		d = vals.pop() - vals.pop();
	            vals.push(d);
	    	}else if( s.equals("*")) {
	    		d = vals.pop() * vals.pop();
	            vals.push(d);
	    	}else if( s.equals("/")) {
	    		d = vals.pop() /+ vals.pop();
	            vals.push(d);
	    	}else {
	    		 vals.push(Double.parseDouble(s));
	    	}
	    	System.out.println(d);
	    }
	    System.out.println( vals.pop());
	}

}

程序如上所示,vals.pop()的打印问题,第四版书中Dijkstra算法也是类似,

好了,知道答案了...ctrl + Z。

#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; // 初始化函数 void initQueue(Node** tail) { *tail = (Node*)malloc(sizeof(Node)); (*tail)->next = *tail; // 自己连自己 形成单节点环状结构 } // 是否为空 int isEmpty(const Node* tail) { return tail->next == tail; } // 入队操作 void enqueue(Node** tail, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = value; if (!isEmpty(*tail)) { newNode->next = (*tail)->next; } else { newNode->next = *tail; // 当原队伍仅含head时 直接让它成为自己的前驱达成闭合效果. } (*tail)->next = newNode; *tail = newNode; // 移动tail至新增加的位置上标记完毕 } // 出队操作 返回true/false标识成功与否 并带回数值部分由调用方负责释放资源回收等工作 int dequeue(Node** tail, int* retValue) { if(isEmpty(*tail)){ return 0;// 触发underflow状况报告失败信息回去 } Node* oldHeadRealDataPart = (*tail)->next; *retValue=oldHeadRealDataPart->data; if ((*tail)==oldHeadRealDataPart){ // special case when there was exactly one REAL item left before deletion happened. free(oldHeadRealDataPart); return 0 ; // Now it becomes totally empty so we should inform upper level about this state change accordingly . } (*tail)->next=(oldHeadRealDataPart)->next ; free(oldHeadRealDataPart); return 1 ; } int main(){ int n,a,tmpResult,i ; while(scanf("%d",&n)!=EOF){ Node* myTail=NULL ; initQueue(&myTail); for(i=0;i<n;++i){ scanf("%d",&a); if(a>0){ enqueue(&myTail,a); }else{ if(dequeue(&myTail,&tmpResult)==0){ break ;// UnderFlow Occured ! } } } if(!isEmpty(myTail)|| i!=n ){ if(isEmpty(myTail)){ printf("UNDERFLOW\n"); }else{ tmpResult=myTail->next->data; while(1){ printf("%d",tmpResult); if(tmpResult==myTail->data){ break ; } tmpResult=((Node*)(myTail->next))->next->data; putchar(' '); } puts(""); } }else{ printf("UNDERFLOW\n"); // All processed but still got underflow at last step due to extra zero given ? }//endif }//endwhile return 0 ; }改代码运行超时了
最新发布
03-23
这段代码是一个使用固定容量栈(FixedCapacityStackOfStrings)实现的简单命令行程序。以下是对代码的详细解释: 1. `public static void main(String[] args)`:这是程序的入口点,从这里开始执行。 2. `FixedCapacityStackOfStrings s;`:声明了一个名为`s`的FixedCapacityStackOfStrings类型的变量。 3. `s = new FixedCapacityStackOfStrings(100);`:实例化一个FixedCapacityStackOfStrings对象,并将其赋值给变量`s`。这里假设FixedCapacityStackOfStrings是一个自定义的类,并且构造函数接受一个整数参数,用于指定栈的容量。 4. `while (!StdIn.isEmpty())`:当标准输入不为空时,执行循环体内的代码。 5. `String item = StdIn.readString();`:从标准输入读取一个字符串,并将其赋值给变量`item`。 6. `if (!item.equals("-")) s.push(item);`:如果读取到的字符串`item`不等于"-",则将其压入栈`s`中。这里假设栈类`s`具有名为`push`的方法,用于压入元素。 7. `else if (!s.isEmpty()) StdOut.print(s.pop() + " ");`:如果读取到的字符串`item`等于"-",并且栈`s`不为空,则从栈`s`中弹出一个元素,并将其打印到标准输出上。这里假设栈类`s`具有名为`pop`的方法,用于弹出元素。 8. `StdOut.println("(" + s.size() + " left on stack)");`:使用标准输出打印栈`s`中剩余元素的数量,并加上一些文本。 总结起来,这段代码通过循环读取标准输入的字符串,将非"-"字符串压入栈中,遇到"-"字符串则弹出栈顶元素,并将弹出的元素打印到标准输出上。最后,打印栈中剩余元素的数量。请注意,代码中使用了自定义的FixedCapacityStackOfStrings类、StdIn类和StdOut类,请确保这些类在代码运行时可用。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值