คือต้องทำรายงานส่งอ.อะครับ ช่วยทีครับ
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
class Calculator
{
public:
double num[20];
int ope[19];
int GetOperator(int i)
{
return ope
;
}
};
int main() {
system("TITLE George's Calculator");
system("COLOR 17");
Calculator calc;
double temp;
int i=0;
int counter=1;
double result=0;
char opr='*';
calc.num[0]=1;
bool loop=1;
cout << "You can multiply, divide, add and subtract using *,/,+,-, respectively\n\n";
cout << "Enter the = sign at the end of what you want calculated and press enter\n\n";
do {
cout << "Enter what you want to be calculated: ";
cin >> temp;
switch (opr) {
case '+':
calc.ope=1;
i++;
counter++;
calc.num=temp;
break;
case '-':
calc.ope=2;
i++;
counter++;
calc.num=temp;
break;
case '*':
calc.num=calc.num*temp;
break;
case '/':
if(temp == 0){
cout << "You can't divide by zero" << endl;
}else{
calc.num = calc.num/temp;
}
break;
default:
cout << "That is an invalid operation" << endl;
break;
}
cout << "Operator: ";
cin >> opr;
if (opr=='=') loop=0;
} while (loop);
result = calc.num[0];
i = 1;
for(i = 1; i < counter; i++) {
if (calc.ope[i-1]==1)
result = result + calc.num;
else if (calc.ope[i-1]==2)
result = result - calc.num;
else if
(calc.ope[i-1]==0) break;
else
cout << "Improper operations!";
cout << "
Operator[" << i-1 << "] = " << calc.GetOperator(i-1) << ")";
}
cout << "\nResult: " << result;
cin >> result;
return 0;
}
อยากจะทราบความหมายทุกบรรทัดของโค้ดนี้หน่อยครับ
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
class Calculator
{
public:
double num[20];
int ope[19];
int GetOperator(int i)
{
return ope;
}
};
int main() {
system("TITLE George's Calculator");
system("COLOR 17");
Calculator calc;
double temp;
int i=0;
int counter=1;
double result=0;
char opr='*';
calc.num[0]=1;
bool loop=1;
cout << "You can multiply, divide, add and subtract using *,/,+,-, respectively\n\n";
cout << "Enter the = sign at the end of what you want calculated and press enter\n\n";
do {
cout << "Enter what you want to be calculated: ";
cin >> temp;
switch (opr) {
case '+':
calc.ope=1;
i++;
counter++;
calc.num=temp;
break;
case '-':
calc.ope=2;
i++;
counter++;
calc.num=temp;
break;
case '*':
calc.num=calc.num*temp;
break;
case '/':
if(temp == 0){
cout << "You can't divide by zero" << endl;
}else{
calc.num = calc.num/temp;
}
break;
default:
cout << "That is an invalid operation" << endl;
break;
}
cout << "Operator: ";
cin >> opr;
if (opr=='=') loop=0;
} while (loop);
result = calc.num[0];
i = 1;
for(i = 1; i < counter; i++) {
if (calc.ope[i-1]==1)
result = result + calc.num;
else if (calc.ope[i-1]==2)
result = result - calc.num;
else if
(calc.ope[i-1]==0) break;
else
cout << "Improper operations!";
cout << "
}
cout << "\nResult: " << result;
cin >> result;
return 0;
}