sexta-feira, 10 de junho de 2011
HelloWorld muito longo!
domingo, 5 de junho de 2011
Programa: Quartel de Bombeiros
import java.util.*;
class FireFighter {
String name; // nome do bombeiro
int n_eventos; // numero de eventos em que participou
int workHours; // numero de horas que trabalhou em eventos
// construtor
FireFighter(String nameFF) {
name = nameFF;
n_eventos = 0;
workHours = 0;
}
// actualiza stats do bombeiro
public void updateFireFighterStats(int duration) {
n_eventos++;
workHours += duration;
}
}
//
// Caracterizacao de um Evento
//
class Event {
int id; // identificador do evento
int n_ff; // numero de bombeiros necessarios
int n_ff_assigned; // numero de bombeiros associados ao evento
int startTime; // hora de inicio do evento
int endTime; // hora a que termina o evento
FireFighter ffs[]; // bombeiros que participaram no evento
// constructor
Event(int idFF, int nFF, int start) {
id = idFF;
n_ff = nFF;
startTime = start;
n_ff_assigned = 0;
endTime = 0;
ffs = new FireFighter[n_ff]; // reserva espaco para todos bombeiros necessarios
}
public int durassao(){
return endTime-startTime;
}
public void endTime(int fimx){
endTime=fimx;
}
public void assignFireFighter(FireFighter ff) {
ff.updateFireFighterStats(durassao());
}
// escreve os bombeiros escalados por evento
public void printAssignedFireFighters() {
System.out.println("EVENTO "+id);
if(n_ff_assigned==0){
System.out.println("Nenhum");
}else{
for(int i=0;i
FireFighter ricardo =ffs[i];
if(ricardo!=null){
System.out.println(ricardo.name);
}else{
break;
}
}
}
}
}
//
// classe principal
//
class prob95_v0 {
// lista com os bombeiros lives -- criada vazia
public static List free_ff = new List();
// lista dos eventos -- criada vazia
public static List events = new List();
// procura sequencial pelo evento com identificador id
// assume-se que o evento existe sempre
public static Event findEvent(int eventID) {
// percorrer a lista de eventos ate encontrar o eventID
for (Event ev: events)
if (ev.id== eventID)
return ev;
// nunca devera acontecer sair com null
return null;
}
// main method
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int flag, nff;
String action;
int n_eventos=0;
flag = in.nextInt();
nff = in.nextInt();
in.nextLine();
for(int i=0;i
String nome = in.nextLine();
FireFighter eugenia =new FireFighter(nome);
free_ff.addLast(eugenia);
}
String ops = in.nextLine();
while(!ops.equals("FIM")){
Scanner tiago = new Scanner(ops);
action=tiago.next();
if(action.equals("PARTIDA")){
n_eventos++;
int id=tiago.nextInt();
int n_bomb=tiago.nextInt();
int hora=tiago.nextInt();
Event xv=new Event(id,n_bomb,hora);
for(int i=0;i
FireFighter radio = free_ff.removeFirst();
if(radio!=null){
xv.ffs[i]=radio;
xv.n_ff_assigned++;
}
}
events.addLast(xv);
}else if(action.equals("CHEGADA")){
int idx = tiago.nextInt();
int fimx = tiago.nextInt();
Event evented = findEvent(idx);
evented.endTime(fimx);
for(int i =0;i
FireFighter cruel = evented.ffs[i];
if(cruel!=null){
free_ff.addLast(cruel);
evented.assignFireFighter(cruel);
}else{
break;
}
}
}
ops=in.nextLine();
}
// outputs de acordo com a flag
if (flag==1) {
System.out.printf("Ocorreram %d eventos\n",n_eventos);
}
else if (flag==2) {
System.out.println("Bombeiros Destacados");
while(!events.isEmpty()){
Event filipe=events.removeFirst();
if(filipe!=null){
filipe.printAssignedFireFighters();
}
}
}
else {
System.out.println("Listagem de Bombeiros");
while(!free_ff.isEmpty()){
FireFighter carlos= free_ff.removeFirst();
if(carlos!=null){
System.out.println(carlos.name +" "+carlos.n_eventos+" "+carlos.workHours);
}else{
break;
}
}
}
}
}
class List implements Iterable {
private int size;
private Node first;
private Node last;
public boolean isEmpty() {return size == 0;}
public int size() {return size;}
// construtor de lista vazia
List() {
size = 0;
first = last = null;
}
// remove primeiro elemento da lista
public E removeFirst() {
E rr=null;
if(!isEmpty()){
rr=first.val;
first=first.next;
size--;
if(first==null){
last=null;
}
}
return rr;
}
// adiciona no fim da lista
public void addLast(E v) {
Node novo = new Node(v,null);
if(isEmpty()){
last=first=novo;
}
last.next=novo;
last=novo;
size++;
}
// um no da lista
private class Node {
E val;
Node next;
Node(E v, Node n) {
val = v;
next = n;
}
}
// metodo iterador, devolve um objecto ListIterator
public Iterator iterator() {
return new ListIterator();
}
// definicao de iteradores sobre os elementos da lista
private class ListIterator implements Iterator {
Node current = first;
public boolean hasNext() {return current != null;}
public void remove() {
throw new UnsupportedOperationException();
}
public E next() {
if (!hasNext()) throw new NoSuchElementException();
E v = current.val;
current = current.next;
return v;
}
}
}
Programa: Avaliação de Expressões Algébricas Simples.
Avaliação de Expressões Algébricas Simples
import java.util.*;
class ppp{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int flag=scan.nextInt();
int ne = scan.nextInt();
Expr e[]= new Expr[ne];
scan.nextLine();
for(int i =0;i
String linha =scan.nextLine();
e[i]=new Expr(linha);
}
for(int y =0;y
e[y].actualiza();
}
if(flag==-1){
System.out.println("programa do tiago pereira dos Santos Silva c0816030");
}else if(flag==1){
for(int t =0;t
System.out.println("Operacoes "+e[t].n_oper+" / Numeros "+e[t].n_n);
}
}else if(flag==2){
for(int i =0;i
e[i].ExprTree.print();
}
}else{
for(int i=0;i
System.out.println(e[i].computado_val);
}
}
}
}
class Expr{
int n_oper=0;
int n_n=0;
BTree ExprTree;
int computado_val;
String linha;
Expr(String line){
linha=line;
ExprTree = new BTree();
Scanner proc = new Scanner(linha);
while(proc.hasNext()){
String ricardo = proc.next();
if(ricardo.equals("+") || ricardo.equals("-") || ricardo.equals("*") || ricardo.equals("/")){
n_oper++;
}else{
n_n++;
}
}
}
public void actualiza(){
Scanner in = new Scanner(linha);
ExprTree.insert(in);
computado_val=ExprTree.computar();
}
}
class BTNode{
Object val;
char charr='\0';
int vall=0;
BTNode esq;
BTNode dir;
BTNode(Object k){
val=k;
esq=null;
dir=null;
}
public void setChar(char k){
charr=k;
}
public void setVall(int k){
vall=k;
}
}
class BTree{
BTNode root;
int size=0;
BTree(){
root=null;
}
public void print(){
if(root!=null){
inorder(root);
System.out.println();
}
}
private void inorder(BTNode node){
if(node!=null){
inorder(node.esq);
System.out.print(node.val);
inorder(node.dir);
}
}
public void insert(Scanner in){
root=insertBTNode(in);
}
private BTNode insertBTNode(Scanner in){
if(!in.hasNext()){
return null;
}
String val=in.next();
switch(val.charAt(0)){
case '+' : case '-': case '*': case '/': case '%': case '^': case '@':{
BTNode n = new BTNode(val);
n.setChar(val.charAt(0));
n.esq=insertBTNode(in);
n.dir=insertBTNode(in);
size++;
return n;
}case '!': {
BTNode n =new BTNode(val);
n.esq=insertBTNode(in);
n.setChar(val.charAt(0));
size++;
return n;
}default:{
BTNode n=new BTNode(val);
n.setVall(new Integer(val));
size++;
return n;
}
}
}
public int computar(){
return calcular(root);
}
private int calcular(BTNode n){
//calcular os valores
if(n.charr=='+'){
return calcular(n.esq)+calcular(n.dir);
}
if(n.charr=='-'){
return calcular(n.esq)-calcular(n.dir);
}
if(n.charr=='*'){
return calcular(n.esq)*calcular(n.dir);
}
if(n.charr=='/'){
return calcular(n.esq)/calcular(n.dir);
}
if(n.charr=='%'){
return calcular(n.esq)%calcular(n.dir);
}
if(n.charr=='^'){
int rr=calcular(n.esq);
int gg = calcular(n.dir);
int yy=1;
BTNode ricardo = new BTNode(new Scanner(System.in));
for(int u=0;u
yy=yy*rr;
}
ricardo.setVall(yy);
return ricardo.vall;
}
if(n.charr=='@'){
int rr = calcular(n.esq);
int yy=0;
BTNode ricardo = new BTNode(new BTree());
for(int h =0;h
yy=yy+rr;
}
ricardo.setVall(yy);
return ricardo.vall;
}
if(n.charr=='!' && n.dir==null && n.esq != null){
int ups =calcular(n.esq);
int gaita =1;
for(int d =0;d
gaita*=ups;
ups--;
}
n.vall=gaita;
return n.vall;
}
return n.vall;
}
}