I am new to C and C++, but I wrote
this calculator in C++. It compiles okay on borland 5.5, and can do addition,
subtraction, multiplication, and division of two numbers. If you type any
characters except numbers, +,-,*,/,or e, there will be lots of beeping noises
and error messages!
Please post any ways I can improve
this code or my programming in general!
(Additions such as square roots, etc. will be appreciated)
*****Planned changes: Add a constant for pi, add squared and square root, allow
calculation of roman numerals.*****
1./*
2.Program: Fett Calculator
3.
4.File:C:\borland\bcc55\bin\cpp\calc
5.
6.Function: Calculator, very simple
7.
8.Author: Mister-Fett
9.
10.Revision: Version 1.0, distributed 9/17/04
11.*/
12.#include
<iostream.h>
13.#include
<conio.h>
14.int mc(int
x, int y) //Multiply two numbers
15.{
16.cout <<"\n\n"<< x
<<" times "<< y <<" equals ";
17.return (x*y);
18.}
19.int ac(int
a, int b) //Add two numbers
20.{
21.cout <<"\n\n"<< a
<<" plus "<< b <<" equals ";
22.return (a+b);
23.}
24.int sc(int
z, int c) //Subtract two numbers
25.{
26.cout <<"\n\n"<< z
<<" minus "<< c <<" equals ";
27.return (z-c);
28.}
29.int dc(int
o, int t) //Divide two numbers
30.{
31.cout <<"\n\n"<< o
<<" divided by "<< t <<" equals ";
32.return (o/t);
33.}
34.void
calc(char choice)
35.{
36.int on,tw,thr;
37.if (choice == '+') //This whole block
checks what the user wants to calculate, and refers to the proper routine to
calculate it.
38.{
39.cout<<"You selected
"<<choice<<". Please enter two numbers,\nsepperated by
spaces,";
40.cout<<"that you want to
add."<<endl;//print instructions for the user
41.cin>>on;//Get the value of
variable on
42.cin>>tw;//Get the value of
variable tw
43.thr=ac(on,tw);//Get the sum of on and
tw, and assign that value to thr
44.cout<<thr<<"\n\n\n\aThanks
for using my calculator!";//Print a thank you message
45.}
46.else if (choice =='-')
47.{
48.cout<<"You selected
"<<choice<<". Please enter two numbers,\nsepperated by
spaces, that you want to subtract."<<endl;
49.cin>>on;
50.cin>>tw;
51.thr=sc(on,tw);
52.cout<<thr<<"\n\n\n\aThanks
for using my calculator!";
53.}
54.else if (choice =='*')
55.{
56.cout<<"You selected
"<<choice<<". Please enter two numbers,\nsepperated by
spaces, that you want to multiply."<<endl;
57.cin>>on;
58.cin>>tw;
59.thr=mc(on,tw);
60.cout<<thr<<"\n\n\n\aThanks
for using my calculator!";
61.}
62.else if (choice =='/')
63.{
64.cout<<"You selected
"<<choice<<". Please enter two numbers,\nsepperated by
spaces, that you want to divide."<<endl;
65.cin>>on;
66.cin>>tw;
67.thr=dc(on,tw);
68.cout<<thr<<"\n\n\n\aThanks
for using my calculator";
69.}
70.else
71.{
72.cout<<"\nPlease reenter that
value.\n\a";
73.cin>>choice;
74.calc(choice,);
75.}
76.}
77.void
main()
78.{
79.clrscr();
80.int one, two, three;
81.char choice;
82.while (choice != 'e')
83.{
84.cout<<"\nPlease enter +,-,*,
or / and then two numbers,\nsepperated by spaces, that you wish
to\nadd,subtract,multiply,or divide.\nType e and press enter to exit.";
Tidak ada komentar:
Posting Komentar