赏高分100分不知,实参的值哪里来的,具体请看,
源文件,operate.cpp中的,intResult,的值是哪里来的(它作实参传给ChangeTheNumberSystem),它没有初始化,但我调试输出该变量的值,却是一个10,很奇怪啊,主源文件:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "menu.h"
#include "change.h"
#include "operate.h"
using namespace std;
using std::string;
//Declare the prototypes of the functions:
//The main function:
int main()
{
Operate operation1;
int i;
string MenuItem[] = {
"1:Binary Addition", "2:Binary Subtraction",
"3:Binary Multiplication", "4:Binary Division",
"5:Binary Remainder","6: End the program"
};
menu thisMenu(6);
for (i = 0; i < 6; i++) thisMenu.ChangeMenuItem(i, MenuItem[i]);
do
{
thisMenu.DisplayMenu();
i = thisMenu.ReturnChoice();
operation1.Operation(2, i );
// operation1.Operation(8, i );
// operation1.Operation(16, i );
}while (i!= 6);
return 0;
}
operate.h
class Operate
{
public:
void Operation(int numSystem, int operation);
};
operate.cpp
#include <string>
#include "stdafx.h"
#include <iostream>
#include "menu.h"
#include "operate.h"
#include "change.h"
using namespace std;
void Operate::Operation(int numSystem, int operation) {
Change changer;
string strNumber1, strNumber2;
int intResult;
do {
cout<<endl<<"Please input the first number:";
cin>>strNumber1;
}while (!changer.VerifyInput(numSystem, strNumber1));
do {
cout<<endl<<"Please input the second number:";
cin>>strNumber2;
}while (!changer.VerifyInput(numSystem, strNumber2));
switch (operation) {
case 1:
intResult = changer.ChangeTheNumberSystem(numSystem, strNumber1)
+ changer.ChangeTheNumberSystem(numSystem, strNumber2);
cout<<endl<<strNumber1<<" +"<<strNumber2<<" = "
<<changer.ChangeTheNumberSystem(numSystem, intResult)<<endl;
break;
case 2:
intResult = changer.ChangeTheNumberSystem(numSystem, strNumber1)
- changer.ChangeTheNumberSystem(numSystem, strNumber2);
cout<<endl<<strNumber1<<" -"<<strNumber2<<" = "
<<changer.ChangeTheNumberSystem(numSystem, intResult)<<endl;
break;
case 3:
intResult = changer.ChangeTheNumberSystem(numSystem, strNumber1)
* changer.ChangeTheNumberSystem(numSystem, strNumber2);
cout<<endl<<strNumber1<<" ×"<<strNumber2<<" = "
<<changer.ChangeTheNumberSystem(numSystem, intResult)<<endl;
break;
case 4:
if (strNumber2 == "0")
cout<<endl<<"The divisor can not set as 0. The operation will be cancel..."<<endl;
else
{
intResult = changer.ChangeTheNumberSystem(numSystem, strNumber1)
/ changer.ChangeTheNumberSystem(numSystem, strNumber2);
cout<<endl<<strNumber1<<" ÷"<<strNumber2<<" = "
<<changer.ChangeTheNumberSystem(numSystem, intResult)<<endl;
}
break;
case 5:
int Remain = changer.ChangeTheNumberSystem(numSystem, strNumber1)
% changer.ChangeTheNumberSystem(numSystem, strNumber2);
cout<<endl<<strNumber1<<" %"<<strNumber2<<" = "
<<changer.ChangeTheNumberSystem(numSystem, Remain)<<endl;
}
}
menu.h
#include <string>
using std::string;
class menu {
private:
int itemCount;
string *item;
public:
menu(int);
void ChangeMenuItem(int, string);
void ChangeMenuItemCount(int);
void DisplayMenu();
int ReturnChoice();
int GetItemCount();
};
menu.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
#include "menu.h"
using namespace std;
using std::string;
menu::menu(int itemNumber) {
int i;
itemCount = itemNumber;
item =new string[itemNumber];
for (i = 0; i < itemCount; i++) item[i] = "";
}
void menu::ChangeMenuItem(int itemIndex, string itemString) {
if (itemIndex < itemCount)
item[itemIndex] = itemString;
}
void menu::ChangeMenuItemCount(int itemNumber) {
if (itemNumber <=20) itemCount = itemNumber;
}
void menu::DisplayMenu() {
int i;
cout<<endl;
for (i = 0; i < itemCount; i++)
cout<<item[i]<<endl;
}
int menu::ReturnChoice() {
string choice;
int n;
do {
cout<<endl<<"Please select the peration:";
cin>>choice;
if (choice.size() == 1) n = choice[0] - 48;
else {
if (choice.size() == 2)
n = (choice[0] - 48) * 10 + (choice[1] - 48);
else n = -1;
}
}while (n <= 0 || n > itemCount);
return n;
}
int menu::GetItemCount()
{
return itemCount;
}
change.h
class Change
{
public:
string ChangeTheNumberSystem(int numSystem, int x) ;
int ChangeTheNumberSystem(int numSystem, string s);
bool VerifyInput(int numSystem, string s);
};
change.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
#include "menu.h"
#include "change.h"
using namespace std;
//The function that transfer a decimanl value into the string
//of the target number system.
string Change::ChangeTheNumberSystem(int numSystem, int x) {
int d = 1, i;
char f[] = {48, 0};
string s = "", sign = "";
if (x == 0) return "0";
if (x < 0) {
sign = "-";
x *= -1;
}
else s = "";
while ( x != 0) {
i = x % numSystem;
if (i > 9) f[0] = i + 55;
else f[0] = i + 48;
s = f + s;
x /= numSystem;
}
return sign + s;
}
//The function that transfers the string of a number system
//into the decimal value:
int Change::ChangeTheNumberSystem(int numSystem, string s) {
int n = 0, d = 1, i;
for (i = s.length(); i > 0; i--){
if (s[i - 1] == '-'){
n *= -1;
}
else {
if (s[i - 1] > 57) {
s[i - 1] = s[i - 1] | 96;
n += (s[i - 1] - 87) * d;
d *= 16;
}
else {
n += (s[i - 1] - 48) * d;
d *= numSystem;
}
}
}
return n;
}
///////////////////////////////////////////////////////////////////////
//The function that verifies the user's input:
//numSystem: 2 = Binary; 8 = Octal; 16 = Hexadecimal.
bool Change::VerifyInput(int numSystem, string s) {
int i;
if (s[0] == '-') i = 1;
else i = 0;
for (i; i<s.length(); i++) {
switch (numSystem) {
case 2:
if (s[i] != 48 && s[i] != 49) {
cout<<"You input the wrong number! please try to input again!";
return false;
}
break;
case 8:
if (s[i] < 48 || s[i] > 55) {
cout<<"You input the wrong number! please try to input again!";
return false;
}
break;
case 16:
if (s[i] >= 48 && s[i] <= 57) break;
else {
s[i] = s[i] | 96;
if (s[i] >= 97 && s[i] <= 102) break;
else {
cout<<"You input the wrong number! please try to input again!";
return false;
}
}
}
}
return true;
}