跳出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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值