| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 796 人关注过本帖
标题:赏高分100分不知,实参的值哪里来的,具体请看,
取消只看楼主 加入收藏
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:2 
赏高分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;
}
搜索更多相关主题的帖子: 源文件 include function 
2013-09-21 21:18
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
收藏
得分:0 
回复 4楼 pauljames
这是整型的,默认的初始化的值是0,它不是,它的值是有用的,你认真看看,

编写的程序,不能改变世界,却可以改变自己...
2013-09-21 22:04
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
收藏
得分:0 
回复 7楼 TonyDeng
嗯,我错了,没有认真看啊,

编写的程序,不能改变世界,却可以改变自己...
2013-09-22 16:13
快速回复:赏高分100分不知,实参的值哪里来的,具体请看,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017568 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved