Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

A Command Line Calculator

Rate me:
Please Sign up or sign in to vote.
2.84/5 (23 votes)
24 Nov 2005CPOL4 min read 109.9K   2.8K   28  
A command line calculator using CodeDOM.
using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Calculate
{
	/// <summary>
	/// Summary description for Variables.
	/// </summary>
	[Serializable()]
	public class Variables:ICollection,ISerializable
	{
		#region Private Members
		private SortedList list;
		private static Variables variables;
		#endregion
		
		#region Constructors
		public Variables()
		{
			//
			// TODO: Add constructor logic here
			//
			list=new SortedList();
		}
		public Variables(SerializationInfo info, StreamingContext ctxt)
		{
			list=(SortedList)info.GetValue("list", typeof(SortedList));
			
		}
		#endregion

		#region ICollection Members

		public bool IsSynchronized
		{
			get
			{
				// TODO:  Add Variables.IsSynchronized getter implementation
				return false;
			}
		}

		public int Count
		{
			get
			{
				// TODO:  Add Variables.Count getter implementation
				return list.Count;
			}
		}

		public void CopyTo(Array array, int index)
		{
			// TODO:  Add Variables.CopyTo implementation
			list.CopyTo(array,index);
		}

		public object SyncRoot
		{
			get
			{
				// TODO:  Add Variables.SyncRoot getter implementation
				return list.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable Members

		public IEnumerator GetEnumerator()
		{
			// TODO:  Add Variables.GetEnumerator implementation
			return list.GetEnumerator();
		}

		#endregion

		#region ISerializable Members

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			// TODO:  Add Variables.GetObjectData implementation
			info.AddValue("list", list);
		}
		#endregion

		#region Indexers
		public virtual double this[int Index]
		{
			get
			{
				return (double)list.GetByIndex(Index);
			}
		}
		public virtual double this[string key]
		{
			get
			{
				return (double)list[key];
			}
			set
			{
				if(list[key]==null)
				{
					list.Add(key,value);
				}
				else
				{
					list[key]=value;
				}
			}
		}
		#endregion

		#region Public Methods
		public void Add(string key,double value)
		{
			this[key]=value;
		}
		public void Clear()
		{
			list.Clear();
		}
		public string [] GetKeys()
		{
			string []keys=new string[list.Keys.Count];
			IList lst=list.GetKeyList();
			for(int i=0;i<keys.Length;i++)
			{
				keys[i]=(string)lst[i];
			}
			return keys;
		}
		#region Static Methods
		public static Variables GetVariables()
		{
			if(variables==null)
			{
				variables=new Variables();
			}
			return variables;
		}
		public static void Save()
		{
			Process p=Process.GetCurrentProcess();
			string path=System.IO.Path.GetDirectoryName(p.MainModule.FileName);
			FileStream stream=File.Create(path+@"\variables.dat");
			BinaryFormatter bformatter = new BinaryFormatter();
			bformatter.Serialize(stream, variables);
			stream.Close();
		}
		public static void Load()
		{
			try
			{
				Process p=Process.GetCurrentProcess();
				string path=System.IO.Path.GetDirectoryName(p.MainModule.FileName);
				FileStream stream=File.OpenRead(path+@"\variables.dat");
				BinaryFormatter bformatter = new BinaryFormatter();
				variables = (Variables)bformatter.Deserialize(stream);
				stream.Close();
			}
			catch
			{
				
			}
		}
		#endregion
		#endregion

		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions