找回密码
 立即注册
搜索
查看: 2555|回复: 34

哎.....又来求助了...C++.....

[复制链接]
发表于 2009-5-21 14:46 | 显示全部楼层 |阅读模式


/////////////////////////////////////////////////////////////////
// This is a program that reads in big integers, add them up
// and gives out an addition.
// precondition:
//                    It will give an erro message when input
//                    integer is over 20 digits.
//                    It will also give an erro message when the
//                    addition is over 20 digits.
/////////////////////////////////////////////////////////////////

#include <iostream>

using namespace std;

//the constent number of digits of an individual input.
const int MAX_NUM = 20;
//the constent number of how many integers will be accepted.
const int MAX = 10;

//A function that add and prints the addition of all the numbers.
//@param the array that stores all the integer
//@param the size of the array.
//@param the numbers of integers were stored.
void count(int,int);

void count(int bigInt[][MAX_NUM], int x)
{      
        //initial an array to store the addition.
        int result[MAX_NUM];
       
        //set an integer to store the addition of those digits which are in different
        //integer but in the same position.
        int sum= 0;
        //set an integer to store the extra values.
        int up = 0 ;
       
        //a loop that adds all the integers.
        for(int i=0; i<=MAX;i++)
        {
                        //a loop that adds up the digits in the same position which
                        //are in different integers array.
                        for(int j=0; j < MAX; j++)
                        {
                                //test if it`s an available number in that position
                                if(bigInt[j]<10 && bigInt[j]>=0)
                                {
                                        //gets the value of those digits in the same position.
                                        sum = sum + bigInt[j];
                                }
                        }
                        //add the exact value and the carry from the last position together.
                        int temp = sum%10 +up;
                        //sight the value into the addition array.
                        result =temp%10;
                        //puts the carry value into up and carry it to the next position.
                        up = temp/10;
       
        }
        cout << "The addition of those " << x ;
        cout <<" big integers is: " <<endl;
        //output the value.
        for (int i = 0; i <20 ; i++)
        {
                if (result != '\n' || result>=0 || result<10)
                        cout << result;
        }
}

int main()
{
        //initial an array to store all the numbers.
        int bigInt[MAX][MAX_NUM];
        //a counter respects to numbers of integers
        int x = 0;  

       
        cout << "It`s a program that counts the addition of integers."<<endl;
        cout << "Please enter your big integer." << endl;
       
        //a loop that gets all the input integers.
        while (true)
        {
                cout << "(it should be positive and less than 20 digits with no blank)"<< endl;
               
                //a counter respects to numbers of digits.
                int y = 0;
                //set a charactor for cin.
                char ch;
               
                //a loop that gets digits for a integer.
                while (true)
                {
                        //cin digits as charator.
                        cin.get(ch);
                       
                        //test to see if he finish an integer input or the input is wrong.
                        if(ch  == '\n' || ch == '\t')
                                break;
                        else if (y >19)
                        {
                                cout << "Sorry, you have more than 20 digits, it`s too much."<<endl;
                                break;
                        }
                        //fits the charactor in integer and stores into the array.
                        else
                                bigInt[x][y] = int(ch)-int('0');
                       
                        //increase the digits counter.
                        y++;
                }
               
                //a loop that moves digits in the array to the back.
                for(int i=MAX_NUM-1;i >= MAX_NUM-1-y;i--)
                {
                        bigInt[x]=bigInt[x][y];
                        y--;
                }
               
                //ask for if any following integers are need to be add.
                char ans;
                cout << "Would you like to start to enter another big int?(y/n)"<< endl;
                cin >> ans;
                cout << endl;
                //stops the loop when the user say no more integers.
                if (ans == 'n' || ans == 'N')
                {
                        cout<<"OK"<<endl;
                        break;
                }
                //stops the loop when there`re too many integers.
                else if (x >= MAX)
                {
                        cout << "Sorry, you have enter too much integers!"<<endl;
                        break;
                }
                //increase the integer counter.
                x++;
               
                cout <<endl;
                cout <<x<<endl<<y<<endl;
        }
      
    //call the count function.
    count(bigInt,x);
   
    return 0;
}


就是要读取大的整数并且做加法...

我怎么感觉没错呢=   =#
回复

使用道具 举报

头像被屏蔽
     
发表于 2009-5-21 14:53 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

 楼主| 发表于 2009-5-21 14:57 | 显示全部楼层
cin.get(ch);
....
bigInt[x][y] = int(ch)-int(\'0\');

这样的表述方式不对吗??

我输入了2..想把它读成\'2\'这个char..然后再用bigInt[x][y] = int(ch) - int(\'0\')来把它换成整数2存到array里..错了吗?错了吗??
回复

使用道具 举报

     
发表于 2009-5-21 15:04 | 显示全部楼层
没错,写ch-‘0’就可以了

另外,lz你这代码满篇都是int* 到int的赋值错误
回复

使用道具 举报

 楼主| 发表于 2009-5-21 15:09 | 显示全部楼层
楼上..举个例子啊..

=   =####

另外..那个bigInt[x][y]真的没吃到正确的数字哦...

                while (true)
                {
                        //cin digits as charator.
                        cin.get(ch);
                       
                        //test to see if he finish an integer input or the input is wrong.
                        if(ch  == \'\\n\' || ch == \'\\t\')
                                break;
                        else if (y >19)
                        {
                                cout << \"Sorry, you have more than 20 digits, it`s too much.\"<<endl;
                                break;
                        }
                        //fits the charactor in integer and stores into the array.
                        else if (int(ch)-int(\'0\') >=0 || int(ch)-int(\'0\')<10)
                                bigInt[x][y] = int(ch)-int(\'0\');
                       
                        //increase the digits counter.
                        y++;
                        cout<<endl<<\"bigInt is \"<< bigInt[x][y] << endl << \"y is \" << y <<endl;
                }

这个是用来读一个数组的while loop然后那个cout 的结果是些奇怪的东西
1
bigInt is 132008
y is 1
2
bigInt is 127
y is 2
3
bigInt is 0
y is 3
4
bigInt is 2896432
y is 4
回复

使用道具 举报

 楼主| 发表于 2009-5-21 15:11 | 显示全部楼层
原帖由 PabloPicasso 于 2009-5-21 14:53 发表
unsigned int的最大取值范围不超过10位的2^32

你这超过20位才报警

数据类型应该是long long吧


不许用long..这个烂program就是要求做一些array来实现char 和int 的转换做加减法..好像很弱智..但是我还是做砸了...恼火中
回复

使用道具 举报

     
发表于 2009-5-21 15:18 | 显示全部楼层
------ Build started: Project: ttt, Configuration: Debug Win32 ------
Compiling...
ttt.cpp
e:\\mycode\\update\\ttt\\ttt.cpp(45) : error C2446: \'<\' : no conversion from \'int\' to \'int *\'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\\mycode\\update\\ttt\\ttt.cpp(45) : error C2040: \'<\' : \'int [20]\' differs in levels of indirection from \'int\'
e:\\mycode\\update\\ttt\\ttt.cpp(48) : error C2440: \'=\' : cannot convert from \'int *\' to \'int\'
        There is no context in which this conversion is possible
e:\\mycode\\update\\ttt\\ttt.cpp(54) : error C2440: \'=\' : cannot convert from \'int\' to \'int [20]\'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
e:\\mycode\\update\\ttt\\ttt.cpp(64) : error C2446: \'!=\' : no conversion from \'int\' to \'int *\'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\\mycode\\update\\ttt\\ttt.cpp(64) : error C2040: \'!=\' : \'int [20]\' differs in levels of indirection from \'int\'
e:\\mycode\\update\\ttt\\ttt.cpp(64) : error C2446: \'<\' : no conversion from \'int\' to \'int *\'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\\mycode\\update\\ttt\\ttt.cpp(64) : error C2040: \'<\' : \'int [20]\' differs in levels of indirection from \'int\'
e:\\mycode\\update\\ttt\\ttt.cpp(115) : error C2440: \'=\' : cannot convert from \'int\' to \'int [20]\'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
Build log was saved at \"file://e:\\MyCode\\update\\ttt\\Debug\\BuildLog.htm\"
ttt - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我只是好奇这段代码在什么编译器下可以通过编译
回复

使用道具 举报

 楼主| 发表于 2009-5-21 15:23 | 显示全部楼层
耶...

在Scite下通过了哦......

我!@#!@#$!@#$!
回复

使用道具 举报

 楼主| 发表于 2009-5-21 15:27 | 显示全部楼层
呃...

bigInt[j] < 10 && bigInt[j]>=0

bigInt[][]是int 哦..不可以和数字比较吗?

sum = sum + bigInt[j];这也不行的哦???但是可以compile啊..晕了..
回复

使用道具 举报

     
发表于 2009-5-21 15:38 | 显示全部楼层
什么版本g++呀,即使不加-pedantic你那种情况也绝对会报错的
回复

使用道具 举报

发表于 2009-5-21 15:46 | 显示全部楼层
问题太多了

y++;

cout<<endl<<\"bigInt is \"<< bigInt[x][y] << endl << \"y is \" << y <<endl;
换过来;

int(ch)-int(\'0\') >=0 || int(ch)-int(\'0\')<10
改成 &&
回复

使用道具 举报

 楼主| 发表于 2009-5-21 15:48 | 显示全部楼层
真的没报哦..

对了对了...现在发现是bigInt[][]读不对数字哦..

我用int tempint = int(ch)-int(\'0\');读那个数字..cout<<tempint 是正确的..但是再用bigInt[x][y]=tempint输出又不对了..

难道int bigInt[][]不能读数????

应该怎么把数字放到array里啊?我记得以前这么做没错的哦..

另..真的..可以compile..不过pedantic是什么来的哦?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

发表于 2009-5-21 15:49 | 显示全部楼层
这个问题其实用 256 进制比 10 进制高效得多
回复

使用道具 举报

发表于 2009-5-21 15:55 | 显示全部楼层
void count(int bigInt[][MAX_NUM], int x)
{      
        //initial an array to store the addition.
        int result[MAX_NUM];
      
        //set an integer to store the addition of those digits which are in different
        //integer but in the same position.
        int sum = 0;
      
        //a loop that adds all the integers.
        for(int i = 0; i &lt; MAX_NUM; i++)
        {
                        //a loop that adds up the digits in the same position which
                        //are in different integers array.
                        for(int j = 0; j &lt; x; j++)
                        {
                                //gets the value of those digits in the same position.
                                sum += bigInt[j];
                        }
                        //add the exact value and the carry from the last position together.
                        //sight the value into the addition array.
                        result = sum % 10;
                        //puts the carry value into up and carry it to the next position.
                        sum /= 10;
      
        }
        if (sum &gt; 0) cout &lt;&lt; \\&quot;...\\&quot; &lt;&lt; endl;
        else for (int i = 0; i &lt; MAX_NUM; i++) cout &lt;&lt; result;
}

[ 本帖最后由 eph 于 2009-5-21 16:37 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2009-5-21 16:04 | 显示全部楼层
啊啊啊...楼上的老大...你先解决了我那个array的问题嘛...我..我晕着呢

为什么
int tempint=6;

bigIng[x][y] = tempint;这样的等式得出的数字不对呢?cout bigInt[x][y]不是6哦..
回复

使用道具 举报

发表于 2009-5-21 16:07 | 显示全部楼层

回复 15楼的 叮叮 的帖子

你把 y++ 和 bigInt[x][y] 的顺序换过来了没
回复

使用道具 举报

发表于 2009-5-21 16:12 | 显示全部楼层
另外你要习惯性地初始化 bigInt 数组为 0
回复

使用道具 举报

 楼主| 发表于 2009-5-21 16:14 | 显示全部楼层
//add the exact value and the carry from the last position together.
                        int temp = sum + up;


这个改的不对吧?如果得数大于10的话还是要继续向前进位的...所以sum要变成sum%10的吧?
回复

使用道具 举报

发表于 2009-5-21 16:16 | 显示全部楼层

回复 18楼的 叮叮 的帖子

你后面既然 temp % 10 了,就不需要 sum % 10 ,否则就丢失十位了。

重新看 14F ,temp 、sum 、up 用一个变量就够了。

[ 本帖最后由 eph 于 2009-5-21 16:18 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2009-5-21 16:17 | 显示全部楼层
原帖由 eph 于 2009-5-21 16:12 发表
另外你要习惯性地初始化 bigInt 数组为 0


初始为0哦......但是我已经用那个IF statement 来判断里面输出的数字啊..为什么还是不行呢...........

崩溃了要..

哦哦哦..我研究下啊...
回复

使用道具 举报

发表于 2009-5-21 16:19 | 显示全部楼层

回复 20楼的 叮叮 的帖子

你不初始化天知道里边是什么数字
回复

使用道具 举报

 楼主| 发表于 2009-5-21 16:34 | 显示全部楼层
不是啊...
我现在先这样了...
        for (int i = 0; i<=MAX; i++)
        {
                for (int j = 0; j<=MAX_NUM ; j++)
                        bigInt [j] = 0;
        }

但是        bigInt[x][y]= int(ch)-int(\'0\');还是读不进数哦..

现在结果是变成了
1
bigInt is 0
x is 0

...救命..
这段代码
bigInt[x][y]= int(ch)-int(\'0\');

我改成了

int tempint = int(ch)-int(\'0\');
然后我cout <<temp;看了一下..数字是正确的..出来是2..
但是用bigInt[x][y]=tempint;然后再cout..cout <<bigInt[x][y];出来还是0

[ 本帖最后由 叮叮 于 2009-5-21 16:37 编辑 ]
回复

使用道具 举报

发表于 2009-5-21 16:40 | 显示全部楼层
for (int i = 0; i < MAX; i++)
        for (int j = 0; j < MAX_NUM ; j++)
                bigInt[i][j] = 0;
...
bigInt[x][y] = ch-\'0\';
cout << bigInt[x][y];

再不行把整个 main 贴出来

[ 本帖最后由 eph 于 2009-5-21 16:42 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2009-5-21 16:44 | 显示全部楼层
...我编辑了两次..我打的有的...

但是不知道为什么..好像是被字体吃掉了=     =#####

有的...

这个是我的main..


int main()
{
        //initial an array to store all the numbers.
        int bigInt[MAX][MAX_NUM];
       
        for (int i = 0; i<MAX; i++)
        {
                for (int j = 0; j<MAX_NUM ; j++)
                        bigInt[ i ][j] = 0;
        }
       
        //a counter respects to numbers of integers
        int x = 0;  

       
        cout << \"It`s a program that counts the addition of integers.\"<<endl;
        cout << \"Please enter your big integer.\" << endl;
       
        //a loop that gets all the input integers.
        while (true)
        {
                cout << \"(it should be positive and less than 20 digits with no blank)\"<< endl;
               
                //a counter respects to numbers of digits.
                int y = 0;
                //set a charactor for cin.
                char ch;
               
                //a loop that gets digits for a integer.
                while (true)
                {
                        //cin digits as charator.
                        cin.get(ch);
                       
                        //test to see if he finish an integer input or the input is wrong.
                        if(ch  == \'\\n\' || ch == \'\\t\')
                                break;
                        else if (y >19)
                        {
                                cout << \"Sorry, you have more than 20 digits, it`s too much.\"<<endl;
                                break;
                        }
                       
                        //fits the charactor in integer and stores into the array.
                        bigInt[x][y]= int(ch)-int(\'0\');
                       
                        //increase the digits counter.
                        y++;
                        cout<<endl<<\"bigInt is \"<< bigInt[x][y] << endl << \"x is \"<< x << endl <<\" y is \" << y <<endl;
                }
               
                //a loop that moves digits in the array to the back.
                for(int i=MAX_NUM-1;i >= MAX_NUM-1-y;i--)
                {
                        bigInt[x]=bigInt[x][y];
                        y--;
                }
               
                //ask for if any following integers are need to be add.
                char ans;
                cout << \"Would you like to start to enter another big int?(y/n)\"<< endl;
                cin >> ans;
                cout << endl;
                //stops the loop when the user say no more integers.
                if (ans == \'n\' || ans == \'N\')
                {
                        cout<<\"OK\"<<endl;
                        break;
                }
                //stops the loop when there`re too many integers.
                else if (x >= MAX)
                {
                        cout << \"Sorry, you have enter too much integers!\"<<endl;
                        break;
                }
                //increase the integer counter.
                x++;
               
                cout <<endl;
                cout <<x<<endl<<y<<endl;
        }
      
    //call the count function.
    count(bigInt,x);
   
    return 0;
}

[ 本帖最后由 叮叮 于 2009-5-21 16:45 编辑 ]
回复

使用道具 举报

发表于 2009-5-21 16:44 | 显示全部楼层
哦,原来你的 [ i ] 被系统当成斜体了
放在 [ code ] 里边吧
回复

使用道具 举报

发表于 2009-5-21 16:46 | 显示全部楼层
//increase the digits counter.
y++;
cout&lt;&lt;endl&lt;&lt;\\&quot;bigInt is \\&quot;&lt;&lt; bigInt[x][y] &lt;&lt; endl &lt;&lt; \\&quot;x is \\&quot;&lt;&lt; x &lt;&lt; endl &lt;&lt;\\&quot; y is \\&quot; &lt;&lt; y &lt;&lt;endl;你这不是还没换过来么,y 变了,bigInt[x][y] 当然变了
回复

使用道具 举报

 楼主| 发表于 2009-5-21 16:51 | 显示全部楼层
啊-     -...噢..崩............


会被笑死么..
回复

使用道具 举报

     
发表于 2009-5-21 16:57 | 显示全部楼层
初始化用memset(bigInt, 0, sizeof(int) * MAX * MAX_NUM);if (ans == \'n\' || ans == \'N\')
                {
                        cout&lt;&lt;\\&quot;OK\\&quot;&lt;&lt;endl;
                        break;
                }
                //stops the loop when there`re too many integers.
                else if (x &gt;= MAX)
                {
                        cout &lt;&lt; \\&quot;Sorry, you have enter too much integers!\\&quot;&lt;&lt;endl;
                        break;
                }在后面加上 cin.get() 吸收回车,否则你读不进数

x++要写在判断x>=MAX前面,不然可能会溢出                for(int i=MAX_NUM-1;i &gt;= MAX_NUM-1-y;i--)
                {
                        bigInt[x]=bigInt[x][y];
                        y--;
                }这种错误我就不说了
我还真是蛋疼
回复

使用道具 举报

 楼主| 发表于 2009-5-21 17:01 | 显示全部楼层
memset(bigInt, 0, sizeof(int) * MAX * MAX_NUM);

看起来好帅...

哦哦哦..


你不说的错误是什么吖?

深夜2点..脑袋是浆糊做的.....麻烦
回复

使用道具 举报

 楼主| 发表于 2009-5-21 17:04 | 显示全部楼层
那个cin.get()+在哪里??????回车不是被前面那个CIN>>CH;吃了吗?
回复

使用道具 举报

发表于 2009-5-21 17:08 | 显示全部楼层
y 那个错误明显的
y参加判断循环的条件
你还y-- .......

不会死循环么
回复

使用道具 举报

     
发表于 2009-5-21 17:20 | 显示全部楼层
(y / n)那里输入y后面的回车没被吸收,子循环检测到回车直接退出,所以你读不进数
回复

使用道具 举报

 楼主| 发表于 2009-5-21 17:34 | 显示全部楼层
原来如此!!!

我X..为什么要手多按回车嘛....真是的...
回复

使用道具 举报

头像被屏蔽
     
发表于 2009-5-21 23:50 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

 楼主| 发表于 2009-5-22 07:11 | 显示全部楼层
好了好了…

其实早上起来…再看一遍…发现自己要多傻有多傻…果然深夜不时干这种事情的时机…
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|上海互联网违法和不良信息举报中心|网上有害信息举报专区|962110 反电信诈骗|举报电话 021-62035905|Stage1st ( 沪ICP备13020230号-1|沪公网安备 31010702007642号 )

GMT+8, 2025-9-20 05:58 , Processed in 0.305735 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表