2011-07-24 24 views
0

我想寫一個程序,輸入用戶的兩個數字作爲字符串....如果第一個數字大於第二個數字它們相乘,結果返回...我轉換輸入數字入字符數組,然後用ASCII碼的charcters轉化爲實際的數字...然後我上的數字進行計算......這裏是我使用的功能...數組函數

double greater1(char a[],char b[],int size1,int size2)//the arrays here are those containing the two numbers 
{ double first; 
double second; 
for(int i=0;i<size1;i++) 
first=first+(pow(10.0,(double)(size1-i-1))*(a[i]-48)); 
for(int i=0;i<size2;i++) 
second=second+(pow(10.0,(double)(size2-i-1))*(b[i]-48)); 
return(first>second?first:second); 
} 

double smaller1(char a[],char b[],int size1,int size2) 
{ double first; 
double second; 
for(int i=0;i<size1;i++) 
first=first+(pow(10.0,(double)(size1-i-1))*(a[i]-48)); 
for(int i=0;i<size2;i++) 
second=second+(pow(10.0,(double)(size2-i-1))*(b[i]-48)); 
return(first<second?first:second); 
}  

double multiply(char a[], char b[],int size1,int size2) 
{double first=greater1(a,b,size1,size2); 
double second=smaller1(a,b,size1,size2); 
//cout<<second;....(a) 
//cout<<smaller1(a,b,size1,size2);....(b) 
//cout<<smaller1(a,b,size1,size2);....(c) 
//cout<<smaller1(a,b,size1,size2);....(d) 
double mult=first*second; 
return mult; 
} 

現在來測試這些函數我輸入43和10 ...但返回的產品是860 ...所以要找到錯誤我插入行(a),(b),(c),(d)....(a )給出了輸出20,(b)給出了30,之後所有其他行(c),(d)...都輸出爲10 ...當我插入

cout<<smaller1(arr1,arr2,ln1,ln2);//these parameters were the ones also passed to the above three functions    
在main()

,每次我得到了輸出10 ....所以必須有一些問題與使用乘法FUNC smaller1()... PLZ任何人都可以指出問題

+1

[標籤:c]不是[標籤:C++] –

+5

你的空間條被破壞了嗎? –

+0

你在'greater1'和'smaller1'中都有未初始化的變量'first'和'second',所以你的結果將會是垃圾... –

回答

1

實際上很簡單:你沒有初始化第一個和第二個小1和更大1。局部變量不會被隱式初始化。