| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 796 人关注过本帖
标题:赏高分100分不知,实参的值哪里来的,具体请看,
只看楼主 加入收藏
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:9 
赏高分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
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:1 
分不少
2013-09-21 21:33
youngdavid
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:107
专家分:698
注 册:2012-9-24
收藏
得分:1 
分略多~
2013-09-21 21:46
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:10 
cpp中对象的操作有很多是隐含进行的,根据编译器的不同还有不同的行为,即使不初始化,也会有默认的初始化动作。

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-09-21 21:48
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
收藏
得分:0 
回复 4楼 pauljames
这是整型的,默认的初始化的值是0,它不是,它的值是有用的,你认真看看,

编写的程序,不能改变世界,却可以改变自己...
2013-09-21 22:04
NBABOY
Rank: 4
等 级:业余侠客
威 望:4
帖 子:129
专家分:281
注 册:2013-5-2
收藏
得分:18 
有赋值啊,这好像是一个计算器的程序。在switch~case语句里,作为计算结果的变量,
2013-09-22 00:16
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:70 
你自己看看這個畫面,發現什麽問題沒有?
图片附件: 游客没有浏览图片的权限,请 登录注册

授人以渔,不授人以鱼。
2013-09-22 11:01
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:0 
本人表示不乐意在一大堆代码中找某一个变量,LZ给出一大堆代码,要intResult,至少也要标记出来,没人喜欢在一大堆代码里找一个小小的变量,懂不?

思考赐予新生,时间在于定义
2013-09-22 11:14
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
收藏
得分:0 
回复 7楼 TonyDeng
嗯,我错了,没有认真看啊,

编写的程序,不能改变世界,却可以改变自己...
2013-09-22 16:13
胡振杰
Rank: 2
等 级:论坛游民
帖 子:41
专家分:63
注 册:2012-9-14
收藏
得分:0 
分略高。。代码略长。。。
2013-09-23 23:01
快速回复:赏高分100分不知,实参的值哪里来的,具体请看,
数据加载中...
 
   



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

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