sábado, 15 de junio de 2013

Proyecto unidad 3(maquina de dulces y refrescos)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Proyecto_PilasColas_Prototipo_
{
    class cola
    {
        int[] vec;
        int p, u, tam;

        public cola(int max)
        {
            tam = max;
            vec = new int[tam];
            p = u = -1;
        }

        public int this[int v]
        {
            set
            { vec[v] = Convert.ToInt16(value); }
            get
            { return vec[v]; }
        }

        public bool esta_llena()
        {
            if (u >= tam - 1)
            {
                return true;
            }
            return false;
        }

        public bool esta_vacia()
        {
            if (p == -1)
            {
                return true;
            }
            return false;
        }

        public bool agregar(int dato)
        {
            if (!esta_llena())
            {
                vec[++u] = dato;
                if (u == 0)
                {
                    p = 0;
                }
                return true;
            }
            return false;
        }

        public bool extraer(ref int dato)
        {
            if (!esta_vacia())
            {
                dato = vec[p];
                if (p == u)
                {
                    p = -1;
                    u = p;
                }
                else
                {
                    p++;
                }
                return true;
            }
            return false;
        }
    }
}

========================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Proyecto_PilasColas_Prototipo_
{
    class PILA<T>
    {
        T[] vec;
        int tam, tope, tam2, tope2;

        bool vacia, llena, vacia2, llena2;

        public PILA(int n)
        {
            tam = n;
            vec = new T[tam];
            tope = 0;
            vacia = true;
            llena = false;
            vacia2 = true;
            llena2 = false;
            tam2 = n;
            tope2 = 0;
        }

        public void Push(T valor)
        {
            vacia = false;
            vec[tope++] = valor;
            if (tope == tam)
                llena = true;
        }

        public void Push2(T valor)
        {
            vacia2 = false;
            vec[tope2++] = valor;
            if (tope2 == tam2)
                llena2 = true;
        }

        public T Pop()
        {
            if (--tope == 0)
                vacia = true;
            llena = false;
            return vec[tope];
        }

        public T Pop2()
        {
            if (--tope2 == 0)
                vacia2 = true;
            llena2 = false;
            return vec[tope2];
        }

        public bool EstaVacia
        {
            get { return vacia; }
        }

        public bool EstaVacia2
        {
            get { return vacia2; }
        }

        public bool EstaLlena
        {
            get { return llena; }
        }

        public bool EstaLlena2
        {
            get { return llena2; }
        }

        public T Tope
        {
            get { return vec[tope - 1]; }
        }
    }
}


================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Proyecto_PilasColas_Prototipo_
{
    public partial class Form1 : Form
    {
        PILA<string> p;
        cola col;

        int refrescos = 10;
        int clientes = 5;
        int dulces = 10;
        int cont1 = 0;
        int cont2 = 0;
        string ex = "";
        string res = "";
        string c = "";

        int re, rc, dul, dulc, cc, cr;

        public Form1()
        {
            InitializeComponent();
        }

        private void butComprar1_Click(object sender, EventArgs e)
        {
            if (!p.EstaVacia)
            {
                label2.Text += Convert.ToString(dataGridView1[0, re - (dulces + 1)].Value) + " ";
                p.Pop();
                dataGridView1[0, re - (dulces + 1)].Value = " ";
                dulces++;
                cont1++;
            }
            else
            {
                MessageBox.Show("LA PILA ESTA VACIA");
            }
        }

        private void butComprar2_Click(object sender, EventArgs e)
        {
            if (!p.EstaVacia2)
            {
                label1.Text += Convert.ToString(dataGridView3[0, dul - (refrescos + 1)].Value) + " ";

                p.Pop2();
                dataGridView3[0, dul - (refrescos + 1)].Value = " ";
                refrescos++;
                cont2++;
            }
            else
            {
                MessageBox.Show("LA PILA ESTA VACIA");
            }
        }

        private void butRellenarMaqDul_Click(object sender, EventArgs e)
        {
            if (!p.EstaLlena)
            {
                ex = Convert.ToString("CHOCOLATES");
                dataGridView1[0, re - dulces].Value = ex;
                dulces--;
                p.Push(ex);
            }
            else
            {
                MessageBox.Show("LA PILA ESTA LLENA");
            }
        }

        private void butRellenarRefrescos_Click(object sender, EventArgs e)
        {
            if (!p.EstaLlena2)
            {
                res = Convert.ToString("COCA");
                dataGridView3[0, re - refrescos].Value = res;
                refrescos--;
                p.Push2(res);
            }
            else
            {
                MessageBox.Show("LA PILA ESTA LLENA");
            }
        }

        private void butClientes_Click(object sender, EventArgs e)
        {
            if (col.agregar(clientes + cc))
            {
                c = Convert.ToString("CLIENTE");
                dataGridView2[cc - clientes, 0].Value = c;
                clientes--;
            }
            else
            {
                MessageBox.Show("DESBORDAMIENTO COLA LLENA");
            }

            butComprar1.Enabled = true;
            butRellenarMaqDul.Enabled = true;
            butComprar2.Enabled = true;
            butRellenarRefrescos.Enabled = true;
            dataGridView1.Visible = true;
            dataGridView3.Visible = true;
        }

        private void butSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void butIniciar_Click(object sender, EventArgs e)
        {
            butComprar1.Enabled = false;
            butRellenarMaqDul.Enabled = false;
            butComprar2.Enabled = false;
            butRellenarRefrescos.Enabled = false;
            re = 10;
            rc = 1;
            dul = 10;
            dulc = 1;
            cc = 5;
            cr = 1;

            dataGridView2.RowCount = cr;
            dataGridView2.ColumnCount = cc;
            dataGridView3.RowCount = re;
            dataGridView3.ColumnCount = rc;
            dataGridView1.RowCount = dul;
            dataGridView1.ColumnCount = dulc;
            dataGridView1.Visible = false;
            dataGridView3.Visible = false;

            p = new PILA<string>(10);
            col = new cola(5);//clientes
        }

        private void butExtraer_Click(object sender, EventArgs e)
        {
            try
            {
                label3.Text += ((cont1 * (Convert.ToInt32(7))) + (cont2 * Convert.ToInt32(9))) + ".00";
                label3.Visible = true;
                MessageBox.Show(" nueva compra" + " cliente extraido");

                label2.Text = "";
                label3.Text = "";

                int d = 0;
                if (col.extraer(ref d))
                {
                    dataGridView2[cc - (clientes + 5), 0].Value = "";
                    clientes--;
                }
                else
                {
                    MessageBox.Show("DESBORDAMIENTO COLA VACIA");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void butPagar_Click(object sender, EventArgs e)
        {
            try
            {
                int d = 0;
                if (col.extraer(ref d))
                {
                    dataGridView2[cc - (clientes + 5), 0].Value = "";
                    clientes--;
                }
                else
                {
                    MessageBox.Show("DESBORDAMIENTO COLA VACIA");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void butAcercaDe_Click(object sender, EventArgs e)
        {
            Form2 aux = new Form2();
            aux.Show();
        }
    }

}

No hay comentarios:

Publicar un comentario