哎.....又来求助了...C++.....
/////////////////////////////////////////////////////////////////
// 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[], int x)
{
//initial an array to store the addition.
int result;
//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<10 && bigInt>=0)
{
//gets the value of those digits in the same position.
sum = sum + bigInt;
}
}
//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;
//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 = 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=bigInt;
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;
}
就是要读取大的整数并且做加法...
我怎么感觉没错呢= =# cin.get(ch);
....
bigInt = int(ch)-int(\'0\');
这样的表述方式不对吗??
我输入了2..想把它读成\'2\'这个char..然后再用bigInt = int(ch) - int(\'0\')来把它换成整数2存到array里..错了吗?错了吗?? 没错,写ch-‘0’就可以了
另外,lz你这代码满篇都是int* 到int的赋值错误 楼上..举个例子啊..
= =####
另外..那个bigInt真的没吃到正确的数字哦...
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 = int(ch)-int(\'0\');
//increase the digits counter.
y++;
cout<<endl<<\"bigInt is \"<< bigInt << 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 原帖由 PabloPicasso 于 2009-5-21 14:53 发表 http://bbs.saraba1st.com/images/common/back.gif
unsigned int的最大取值范围不超过10位的2^32
你这超过20位才报警
数据类型应该是long long吧
不许用long..这个烂program就是要求做一些array来实现char 和int 的转换做加减法..好像很弱智..但是我还是做砸了...恼火中 ------ 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 \' 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 \'
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 \' 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 \' differs in levels of indirection from \'int\'
e:\\mycode\\update\\ttt\\ttt.cpp(115) : error C2440: \'=\' : cannot convert from \'int\' to \'int \'
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 ==========
我只是好奇这段代码在什么编译器下可以通过编译 耶...
在Scite下通过了哦......
我!@#!@#$!@#$! 呃...
bigInt < 10 && bigInt>=0
bigInt[][]是int 哦..不可以和数字比较吗?
sum = sum + bigInt;这也不行的哦???但是可以compile啊..晕了.. 什么版本g++呀,即使不加-pedantic你那种情况也绝对会报错的 问题太多了
y++;
和
cout<<endl<<\"bigInt is \"<< bigInt << endl << \"y is \" << y <<endl;
换过来;
int(ch)-int(\'0\') >=0 || int(ch)-int(\'0\')<10
改成 && 真的没报哦..
对了对了...现在发现是bigInt[][]读不对数字哦..
我用int tempint = int(ch)-int(\'0\');读那个数字..cout<<tempint 是正确的..但是再用bigInt=tempint输出又不对了..
难道int bigInt[][]不能读数????
应该怎么把数字放到array里啊?我记得以前这么做没错的哦..
另..真的..可以compile..不过pedantic是什么来的哦? 这个问题其实用 256 进制比 10 进制高效得多 void count(int bigInt[], int x)
{
//initial an array to store the addition.
int result;
//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 < 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 < x; j++)
{
//gets the value of those digits in the same position.
sum += bigInt;
}
//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 > 0) cout << \\"...\\" << endl;
else for (int i = 0; i < MAX_NUM; i++) cout << result;
}
[ 本帖最后由 eph 于 2009-5-21 16:37 编辑 ] 啊啊啊...楼上的老大...你先解决了我那个array的问题嘛...我..我晕着呢
为什么
int tempint=6;
bigIng = tempint;这样的等式得出的数字不对呢?cout bigInt不是6哦..
回复 15楼的 叮叮 的帖子
你把 y++ 和 bigInt 的顺序换过来了没 另外你要习惯性地初始化 bigInt 数组为 0 //add the exact value and the carry from the last position together.int temp = sum + up;
这个改的不对吧?如果得数大于10的话还是要继续向前进位的...所以sum要变成sum%10的吧?
回复 18楼的 叮叮 的帖子
你后面既然 temp % 10 了,就不需要 sum % 10 ,否则就丢失十位了。重新看 14F ,temp 、sum 、up 用一个变量就够了。
[ 本帖最后由 eph 于 2009-5-21 16:18 编辑 ] 原帖由 eph 于 2009-5-21 16:12 发表 http://bbs.saraba1st.com/images/common/back.gif
另外你要习惯性地初始化 bigInt 数组为 0
初始为0哦......但是我已经用那个IF statement 来判断里面输出的数字啊..为什么还是不行呢...........
崩溃了要..
哦哦哦..我研究下啊...
回复 20楼的 叮叮 的帖子
你不初始化天知道里边是什么数字 不是啊...我现在先这样了...
for (int i = 0; i<=MAX; i++)
{
for (int j = 0; j<=MAX_NUM ; j++)
bigInt = 0;
}
但是 bigInt= int(ch)-int(\'0\');还是读不进数哦..
现在结果是变成了
1
bigInt is 0
x is 0
...救命..
这段代码
bigInt= int(ch)-int(\'0\');
我改成了
int tempint = int(ch)-int(\'0\');
然后我cout <<temp;看了一下..数字是正确的..出来是2..
但是用bigInt=tempint;然后再cout..cout <<bigInt;出来还是0
[ 本帖最后由 叮叮 于 2009-5-21 16:37 编辑 ] for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX_NUM ; j++)
bigInt[i] = 0;
...
bigInt = ch-\'0\';
cout << bigInt;
再不行把整个 main 贴出来
[ 本帖最后由 eph 于 2009-5-21 16:42 编辑 ] ...我编辑了两次..我打的有的...
但是不知道为什么..好像是被字体吃掉了= =#####
有的...
这个是我的main..
int main()
{
//initial an array to store all the numbers.
int bigInt;
for (int i = 0; i<MAX; i++)
{
for (int j = 0; j<MAX_NUM ; j++)
bigInt[ i ] = 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= int(ch)-int(\'0\');
//increase the digits counter.
y++;
cout<<endl<<\"bigInt is \"<< bigInt << 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=bigInt;
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 编辑 ] 哦,原来你的 [ i ] 被系统当成斜体了
放在 [ code ] 里边吧 //increase the digits counter.
y++;
cout<<endl<<\\"bigInt is \\"<< bigInt << endl << \\"x is \\"<< x << endl <<\\" y is \\" << y <<endl;你这不是还没换过来么,y 变了,bigInt 当然变了 啊- -...噢..崩............
会被笑死么..
初始化用memset(bigInt, 0, sizeof(int) * MAX * MAX_NUM);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;
}在后面加上 cin.get() 吸收回车,否则你读不进数
x++要写在判断x>=MAX前面,不然可能会溢出 for(int i=MAX_NUM-1;i >= MAX_NUM-1-y;i--)
{
bigInt=bigInt;
y--;
}这种错误我就不说了
我还真是蛋疼 memset(bigInt, 0, sizeof(int) * MAX * MAX_NUM);
看起来好帅...
哦哦哦..
你不说的错误是什么吖?
深夜2点..脑袋是浆糊做的.....麻烦 那个cin.get()+在哪里??????回车不是被前面那个CIN>>CH;吃了吗? y 那个错误明显的
y参加判断循环的条件
你还y-- .......
不会死循环么 (y / n)那里输入y后面的回车没被吸收,子循环检测到回车直接退出,所以你读不进数 原来如此!!!
我X..为什么要手多按回车嘛....真是的... 好了好了…
其实早上起来…再看一遍…发现自己要多傻有多傻…果然深夜不时干这种事情的时机…
页:
[1]