Penulis Topik: [C++] Kalkulator  (Dibaca 17142 kali)

Offline Zai

  • Admin
  • Pro500
  • *******
  • Tulisan: 2.892
  • Reputation: 58629
  • Jenis kelamin: Pria
    • Lihat Profil
    • Zainuddin Nafarin Blog
[C++] Kalkulator
« pada: Januari 20, 2010, 07:51:24 PM »
Kode ini saya buat sendiri dari scratch (dari 0) dengan mengikuti langkah-langkah buku VC++ 2008 Ivon Horton.

Contoh :
Input : 2*3+3*(1+2/(1+1))
Output : 12

Kode: [Pilih]
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

void eatspaces(char* str);
double expr(char* str);
double term(char* str, int& index);
double number(char* str, int& index);

const int MAX = 80;

int main()
{
char buffer[MAX] = {0};

cout << endl
<< "Welcome to the C++alc"
<< endl;

for (;;)
{
cout << "Expression = ";
cin.getline(buffer,sizeof buffer);
eatspaces(buffer);

if (!buffer[0])
return 0;

cout << " = " << expr(buffer)
<< endl << endl;
}
}

void eatspaces(char* str)
{
int i=0,j=0;

while ((*(str+i)=*(str+j++)) != '\0')
if (*(str+i) != ' ')
i++;

return;
}

double expr(char* str)
{
double value = 0.0;
int index = 0;

value = term(str,index);

for(;;)
{
switch(*(str+index++))
{
case '\0':
return value;
case '+':
value += term(str,index);
break;
case '-':
value -= term(str,index);
break;
default:
cout << endl << "what the error?" << endl;
exit(1);
}
}

}

double term(char* str, int& index)
{
double value = 0.0;

value = number(str,index);

while (*(str+index) == '*' || (*(str+index) == '/'))
{
if (*(str+index)=='*')
value *= number(str,++index);

if (*(str+index)=='/')
value /= number(str,++index);
}

return value;
}

double number(char* str, int& index)
{
double value = 0.0;

if (*(str+index) == '(')
{
char* pstr = 0;
char buffer[MAX];
int nbuff=0;
int idbracket = 1;
char ctrial;

while (idbracket)
{
ctrial = *(str + ++index);
if (ctrial==')')
idbracket--;
else if (ctrial=='(')
idbracket++;

if (!idbracket)
ctrial = '\0';

buffer[nbuff++] = ctrial;
}

pstr = new char[nbuff];
strcpy_s(pstr,nbuff,buffer);

index++;
value = expr(pstr);
delete [] pstr;
}
else
{
while (isdigit(*(str+index)))
value = 10*value + (*(str+index++)-'0');

if (*(str+index)!= '.')
return value;

double factor = 1.0;

while (isdigit(*(str + ++index)))
{
factor*=0.1;
value += (*(str+index)-'0')*factor;
}
}

return value;
}
« Edit Terakhir: Januari 20, 2010, 07:59:19 PM oleh Zai »
4 main functions of Smadav :
(1) Additional protection for your PC, 100% compatible with other Antivirus!
(2) Best USB Antivirus, no more infection from USB!
(3) Best for offline use and low resource Antivirus
(4) Manual cleaner & tools to fight malwares

Offline s4nji

  • Pro500
  • ******
  • Tulisan: 3.089
  • Reputation: 8869
  • Jenis kelamin: Pria
  • -- Inactive Account --
    • Lihat Profil
    • s4nji.com
Re: [C++] Kalkulator
« Jawab #1 pada: Januari 20, 2010, 07:58:04 PM »
Maap OOT, tapi C++ yang dicompile tampilannya gimana yah ? ???
-- Account Inactive --
Last logged in: 18 April 2014

http://www.s4nji.com

Offline Zai

  • Admin
  • Pro500
  • *******
  • Tulisan: 2.892
  • Reputation: 58629
  • Jenis kelamin: Pria
    • Lihat Profil
    • Zainuddin Nafarin Blog
Re: [C++] Kalkulator
« Jawab #2 pada: Januari 20, 2010, 08:00:04 PM »
@s4nji
Ini console
4 main functions of Smadav :
(1) Additional protection for your PC, 100% compatible with other Antivirus!
(2) Best USB Antivirus, no more infection from USB!
(3) Best for offline use and low resource Antivirus
(4) Manual cleaner & tools to fight malwares

Offline s4nji

  • Pro500
  • ******
  • Tulisan: 3.089
  • Reputation: 8869
  • Jenis kelamin: Pria
  • -- Inactive Account --
    • Lihat Profil
    • s4nji.com
Re: [C++] Kalkulator
« Jawab #3 pada: Januari 20, 2010, 08:03:40 PM »
Tampilannya kayak command prompt gitu?
-- Account Inactive --
Last logged in: 18 April 2014

http://www.s4nji.com

Offline RIZON BARNS MAGNOXIDE

  • Pro10
  • ***
  • Tulisan: 28
  • Reputation: 77
  • Jenis kelamin: Pria
  • THE MAGNOXIDE INZERNO
    • Lihat Profil
    • THE INZERNO 56th
Re: [C++] Kalkulator
« Jawab #4 pada: Januari 20, 2010, 09:11:57 PM »
Console itu sepertinya memang beda dengan GUI (Graphical Interface User), memang benar Console itu seperti DOS atau Command Prompt lah sebutan kerennya  :D.

Offline Pieter Poker

  • Pro10
  • ***
  • Tulisan: 42
  • Reputation: 80
    • Lihat Profil
Re: [C++] Kalkulator
« Jawab #5 pada: September 16, 2012, 09:55:12 AM »
gak.mudeng

Offline NTZDarkEnergonAV

  • Pro1
  • *
  • Tulisan: 2
  • Reputation: 32
    • Lihat Profil
Re: [C++] Kalkulator
« Jawab #6 pada: November 04, 2012, 03:25:48 PM »
Kode ini saya buat sendiri dari scratch (dari 0) dengan mengikuti langkah-langkah buku VC++ 2008 Ivon Horton.

Contoh :
Input : 2*3+3*(1+2/(1+1))
Output : 12

Kode: [Pilih]
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

void eatspaces(char* str);
double expr(char* str);
double term(char* str, int& index);
double number(char* str, int& index);

const int MAX = 80;

int main()
{
char buffer[MAX] = {0};

cout << endl
<< "Welcome to the C++alc"
<< endl;

for (;;)
{
cout << "Expression = ";
cin.getline(buffer,sizeof buffer);
eatspaces(buffer);

if (!buffer[0])
return 0;

cout << " = " << expr(buffer)
<< endl << endl;
}
}

void eatspaces(char* str)
{
int i=0,j=0;

while ((*(str+i)=*(str+j++)) != '\0')
if (*(str+i) != ' ')
i++;

return;
}

double expr(char* str)
{
double value = 0.0;
int index = 0;

value = term(str,index);

for(;;)
{
switch(*(str+index++))
{
case '\0':
return value;
case '+':
value += term(str,index);
break;
case '-':
value -= term(str,index);
break;
default:
cout << endl << "what the error?" << endl;
exit(1);
}
}

}

double term(char* str, int& index)
{
double value = 0.0;

value = number(str,index);

while (*(str+index) == '*' || (*(str+index) == '/'))
{
if (*(str+index)=='*')
value *= number(str,++index);

if (*(str+index)=='/')
value /= number(str,++index);
}

return value;
}

double number(char* str, int& index)
{
double value = 0.0;

if (*(str+index) == '(')
{
char* pstr = 0;
char buffer[MAX];
int nbuff=0;
int idbracket = 1;
char ctrial;

while (idbracket)
{
ctrial = *(str + ++index);
if (ctrial==')')
idbracket--;
else if (ctrial=='(')
idbracket++;

if (!idbracket)
ctrial = '\0';

buffer[nbuff++] = ctrial;
}

pstr = new char[nbuff];
strcpy_s(pstr,nbuff,buffer);

index++;
value = expr(pstr);
delete [] pstr;
}
else
{
while (isdigit(*(str+index)))
value = 10*value + (*(str+index++)-'0');

if (*(str+index)!= '.')
return value;

double factor = 1.0;

while (isdigit(*(str + ++index)))
{
factor*=0.1;
value += (*(str+index)-'0')*factor;
}
}

return value;
}
Maap OOT,Dimana ya meletakkan code diatas .di stdafx.h,targetver.h,stdafx.cpp.

please saya masih newbie ni

Offline yupix80

  • Pro10
  • ***
  • Tulisan: 43
  • Reputation: 46
    • Lihat Profil
Re: [C++] Kalkulator
« Jawab #7 pada: Maret 21, 2013, 02:35:13 PM »
kalkulator gimana cara'a buat?