The aim of our research has been to investigate the equilibrium state of chemical and electrochemical systems and cells, half cells, fuel cells, secondary and primary batteries. An equilibrium problem, where both the chemical composition of the system and the potentials of the electrodes are unknown, is studied. Starting from the basic laws of thermodynamics we show that the minimization of the total Gibbs energy, including the contribution of electrostatic energy, gives the equilibrium state of the cell, charges and potentials of the electrodes and the chemical composition of the system. A simple model for the electrostatic energy is presented, which also enables us to calculate the charges of the electrodes in the equilibrium state. A discharge equation is introduced, with the aid of which the total electrochemical cells can be studied systemically at various discharge degrees. The present method, which is applicable to all equilibrium problems of chemical and electrochemical systems, makes no distinction among the constitutent species, for example the electrons transferred to and from the electrodes are treated like any other constituents of the system. On the basis of this theory, which can also be used as such for purely theoretical considerations, we have developed a computer program that can be used for theoretical and analytical purposes. The basic theory behind the program is described in detail, and some representative numerical examples of the use of the program for different applications are presented.