/** (c) 2012 Michał (Foxbond) Chraniuk */
#include <iostream>
#include <string>
#include <conio.h>
#include <Windows.h>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#define BLUE 1
#define GREEN 2
#define RED 4
#define PURPLE 5
#define GOLD 6
#define LIGHTBLUE 9
#define WHITE 15
using namespace std;
void changeColor (int color){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void cls () { system("cls"); }
string fillNumber(string liczba);
string dodawanie(string p, string d);
string odejmowanie(string p, string d);
string mnozenie(string p, string d);
string dzielenie(string p, string d);
int main (){
SetConsoleTitle((LPCWSTR)"SHIT-PAC");
_setmode(_fileno(stdout), _O_U16TEXT);
int action=0;
string pLiczba, dLiczba, wynik, symbol;
do {
cls();
wcout << L"Wybierz działanie:\n0. Wyjście\n1. Dodawanie\n2. Odejmowanie\n3. Mnożenie\n4. Dzielenie\n5. Wszystko\n";
cin >> action;
if (action == 0){
break;
}
cls();
do{
wcout << L"Podaj pierwszą liczbę:";
cin >> pLiczba;
}while (pLiczba.length() > 16 || pLiczba.length() <= 0);
do{
wcout << L"\nPodaj drugą liczbę:";
cin >> dLiczba;
}while (dLiczba.length() > 16 || dLiczba.length() <= 0);
cls();
pLiczba = fillNumber(pLiczba);
dLiczba = fillNumber(dLiczba);
if (action == 1){
wynik = dodawanie(pLiczba, dLiczba);
symbol = "+";
}else if (action == 2){
wynik = odejmowanie(pLiczba, dLiczba);
symbol = "-";
}else if (action == 3){
wynik = mnozenie(pLiczba, dLiczba);
symbol = "*";
}else if (action == 4){
wynik = dzielenie(pLiczba, dLiczba);
//wyswietlic dzielenie
}else if (action == 5){
//wyswietlic wszystko
}
if (action != 4 && action != 5){
wynik = fillNumber(wynik);
wcout << " "<<pLiczba.c_str()<<endl<<symbol.c_str()<<" "<<dLiczba.c_str()<<endl<<L"------------------"<<endl<<L"="<<wynik.c_str()<<endl;
}
_getch();
}while(true);
return 0;
}
int toi (char l){
if ((int)l == 49) {
return 1;
}else if ((int)l == 48){
return 0;
}else {
cls();
changeColor(RED);
wcout << "Unexpected error! Calc will exit...";
_getch();
exit(1);
}
}
string fillNumber(string liczba){
int fillLen = 16-liczba.length();
string r;
if (fillLen < 0){
//liczba.resize(16);
return liczba;
}
for (short i=0;i<fillLen;i++){
r += "0";
}
return (r+liczba);
};
string dodawanie (string p, string d){
string w = "00000000000000000";
for (short i=15;i>=0;i--){
if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 3){
w[(i+1)] = '1';
w[i] = '1';
}else if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 2){
w[(i+1)] = '0';
w[i] = '1';
}else if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 1){
w[(i+1)] = '1';
}else if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 0){
w[(i+1)] = '0';
}else{
cls();
changeColor(RED);
wcout << "Unexpected error! Calc will exit...";
_getch();
exit(1);
}
}
return w;
}
string odejmowanie (string p, string d){
string w = "000000000000000000";
p = "0"+p;
d = "0"+d;
for (short i=16;i>=0;i--){
if (toi(d[i]) == 1){
d[i] = '0';
}else if (toi(d[i]) == 0){
d[i] = '1';
}else{
cls();
changeColor(RED);
wcout << "Unexpected error! Calc will exit...";
_getch();
exit(1);
}
}
for (short i=15;i>=0;i--){
if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 3){
w[(i+1)] = '1';
w[i] = '1';
}else if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 2){
w[(i+1)] = '0';
w[i] = '1';
}else if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 1){
w[(i+1)] = '1';
}else if ((toi(p[i])+toi(d[i])+toi(w[(i+1)])) == 0){
w[(i+1)] = '0';
}else{
cls();
changeColor(RED);
wcout << "Unexpected error! Calc will exit...";
_getch();
exit(1);
}
}
wcout << " "<<p.c_str()<<endl<<" "<<d.c_str()<<endl<<w.c_str()<<endl;
_getch();
/*
for (short i=16;i>0;i--){
if (toi(w[i]) == 1){
w[i] = '0';
}else if (toi(w[i]) == 0){
w[i] = '1';
}else{
cls();
changeColor(RED);
wcout << "Unexpected error! Calc will exit...";
_getch();
exit(1);
}
}
*/
return w;
}
string mnozenie (string p, string d){
string w = "00000000000000000";
for (short i=15;i>=0;i--){
for (short ii=15;ii>=0;ii--){
}
}
return w;
}
string dzielenie (string p, string d){
return "010101010";
}
> Back