1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A C++ problem!

Discussion in 'C++' started by h994422, Dec 25, 2010.

  1. h994422

    h994422 New Member

    Joined:
    Sep 13, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Hi everybody. I need help with the problem below. I studied C and I want to learn C++. Actually this is a problem of the first chapter of the book. It really made me crazy..!
    Thanks for your help.
    ***************************************************************
    Write a program that will act as an interactive calculator capable of handling very large (larger than the largest long integer) nonnegative integers. This calculator need perform only the operations of addition and multiplication.
    Needs:
    *A data structure to represent large numbers:for example an array of digits in a number.
    *A function to read in numbers.
    *A function to write numbers.
    *A function to add two numbers.
    *A function to multiply two numbers.
    *Check for overflow (numbers with more than MAX_SIZE digits) when reading, adding, and multiplying numbers.
    *Allow signed integers (negative as well as positive integers) and write a function for substraction.
     
  2. wafa

    wafa New Member

    Joined:
    Dec 24, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    السلام عليكم ...
    brother>>

    what u want ‘‘‘ use in this programme ??

    when u use just function i can help u *ـ*
    but **array idont have alot information abut it ??

    good luke
     
  3. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    your code so far?
     
  4. h994422

    h994422 New Member

    Joined:
    Sep 13, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    I made it.Variables defined in program are in Turkish. Any suggestion for shortening the Line of Codes?
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    string sayi_1,sayi_2;															
    void sayiOku ()																	
    {
    	cout << "\nBirinci sayiyi giriniz : ";
    			cin >> sayi_1;
    	cout << "Ikinci sayiyi giriniz  : ";
    		cin >> sayi_2;
    }
    void sayiYaz()	                             									
    {			
    	cout << '\n' << "Girilen ilk sayi       : " << sayi_1 << endl;
    	cout << "Girilen ikinci sayi    : " << sayi_2 << endl;
    }
    void toplama (int p_dizi_sayi_1[],int p_dizi_sayi_2[],int u)					
    {
    	int sonuc[u];
    	for(int j=0;j<u;++j)														
    		sonuc[j]=0;
    	int i,j,s,elde=0;															
    	for(s=u-1,i=u-1,j=u-1;i>=0,s>=0,j>=0;--i,--s,--j)							
    	{
    		sonuc[s]=p_dizi_sayi_1[i]+p_dizi_sayi_2[j]+elde;
    		if(sonuc[s]>9)
    		{
    			sonuc[s]=sonuc[s]%10;
    			elde=1;
    		}
    		else
    		elde=0;	
    	}
    	cout << "\n" << "SONUC = "	;												
    	for(int i=0;i<u;++i)									
    	cout << sonuc[i];
    	cout << endl << '\n';
    }																				
    void carpma (int p_sayi_1[],int p_sayi_2[],int p_uzunluk,int p_kisa,int p_bayrak)
    {
    	int sayac,elde,carpim,toplam,gecici[p_uzunluk],sonuc[p_uzunluk],j=0,t=0;
    	for(int i=0;i<p_uzunluk;++i)												
    	{
    		gecici[i]=0;
    		sonuc[i]=0;	
    	}
    	for (int k=0;k<p_kisa;k++)													
    	{
    		elde=0;																	
    		for (sayac=0;sayac<p_uzunluk/2;sayac++)
    		{
    			switch(p_bayrak)
    			{
    				case 1:carpim=p_sayi_2[p_uzunluk/2-1-t]*p_sayi_1[p_uzunluk/2-1-sayac];break;
    				case 2:carpim=p_sayi_1[p_uzunluk/2-1-t]*p_sayi_2[p_uzunluk/2-1-sayac];break;
    			}
    		gecici[p_uzunluk-1-sayac-j]=(carpim+elde)%10;
    		elde=(carpim+elde)/10;
    		}	
    		if ((sayac==(p_uzunluk/2)) && (elde!=0)) 
    			gecici[p_uzunluk-1-sayac-j]=elde;
    		elde=0;
    		for(int i=0;i<p_uzunluk;++i)
    		{
    			toplam=sonuc[p_uzunluk-1-i]+gecici[p_uzunluk-1-i]+elde;
    			elde=toplam/10;
    			sonuc[p_uzunluk-1-i]=toplam%10;
    		}
    		++t;
    		++j;
    		for(int i=0;i<p_uzunluk;++i)
    		gecici[i]=0;
    	}
    	cout << "\n" << "SONUC = "	;												
    	for(int i=0;i<p_uzunluk;++i)									
    		cout << sonuc[i] ;
    	cout << endl;	
    }																				
    int main ()
    {	
    	int uzunluk_sayi_1,uzunluk_sayi_2,uzunluk_toplama,							
    	uzunluk_carpma,bayrak,kisa,dizi_uzun;
    	char cevap,devammi;								 							
    	do																			
    	{
    		sayiOku();																
    		sayiYaz();									 							
    		uzunluk_sayi_1=sayi_1.size();											
    		uzunluk_sayi_2=sayi_2.size();
    		if(uzunluk_sayi_1>uzunluk_sayi_2){
    			dizi_uzun=uzunluk_sayi_1;
    			uzunluk_toplama=uzunluk_sayi_1+1;
    			uzunluk_carpma=uzunluk_sayi_1*2;
    			bayrak=1;
    			kisa=uzunluk_sayi_2;}
    		else
    			{dizi_uzun=uzunluk_sayi_2;
    			uzunluk_toplama=uzunluk_sayi_2+1; 
    			uzunluk_carpma=uzunluk_sayi_2*2;
    			bayrak=2;
    			kisa=uzunluk_sayi_1;}
    		int dizi_sayi_1[dizi_uzun],dizi_sayi_2[dizi_uzun];	   					
     	 	for (int i=0 ; i<uzunluk_toplama ; ++i ){								
     			dizi_sayi_1[i]=0;
    		 	dizi_sayi_2[i]=0;}
    	 	for (int i=0 ; i<uzunluk_sayi_1 ; ++i )									
    		dizi_sayi_1[dizi_uzun-1-i]=sayi_1[uzunluk_sayi_1-1-i]%48;					
     		for (int i=0 ; i<uzunluk_sayi_2 ; ++i )																			
    	 	dizi_sayi_2[dizi_uzun-1-i]=sayi_2[uzunluk_sayi_2-1-i]%48;					
    	 	do																		
    		{
    			cout << "\nWhich one? ( * / + ) : ";			
    				cin >> cevap;
    			if(cevap=='+'||cevap=='*')
    				break;		
    		}
    		while (cevap!='+'||cevap!='*');
    		switch (cevap)															
    		{
    			case '+':toplama(dizi_sayi_1,dizi_sayi_2,uzunluk_toplama);			
    			break;
    			case '*':carpma(dizi_sayi_1,dizi_sayi_2,uzunluk_carpma,kisa,bayrak);
    			break;
    			default :cout << "Wrong choice..!";
    		}
    		cout << "Do you wanna continue (Y / y) : " ;
    			cin >> devammi;
    	}
    	while (devammi=='e'||devammi=='E');
    }
     
    Last edited by a moderator: Dec 28, 2010
  5. h994422

    h994422 New Member

    Joined:
    Sep 13, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    There is sth wrong in the addition function.This is the last version.

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    string sayi_1,sayi_2;															
    void sayiOku ()																	
    {
    	cout << "\nFirst number : ";
    			cin >> sayi_1;
    	cout << "Second number  : ";
    		cin >> sayi_2;
    }
    void sayiYaz()	                             									
    {			
    	cout << '\n' << "first number       : " << sayi_1 << endl;
    	cout << "second number    : " << sayi_2 << endl;
    }
    void toplama (int p_dizi_sayi_1[],int p_dizi_sayi_2[],int u)					
    {
    	int sonuc[u];
    	for(int j=0;j<u;++j)														
    		sonuc[j]=0;
    	int i,j,s,elde=0;															
    	for(s=u-1,i=u-1,j=u-1;i>0,s>0,j>0;--i,--s,--j)								
    	{
    		sonuc[s]=p_dizi_sayi_1[i-1]+p_dizi_sayi_2[j-1]+elde;
    		if(sonuc[s]>9)
    		{
    			sonuc[s]=sonuc[s]%10;
    			elde=1;
    		}
    		else
    		elde=0;	
    	}
    	cout << "\n" << "SONUC = "	;												
    	for(int i=0;i<u;++i)									
    	cout << sonuc[i];
    	cout << endl << '\n';
    }																				
    void carpma(int p_sayi_1[],int p_sayi_2[],int p_uzunluk,int p_kisa,int p_bayrak)
    {
    	int sayac,elde,carpim,toplam,gecici[p_uzunluk],sonuc[p_uzunluk],j=0,t=0;
    	for(int i=0;i<p_uzunluk;++i)												
    	{
    		gecici[i]=0;
    		sonuc[i]=0;	
    	}
    	for (int k=0;k<p_kisa;k++)													
    	{
    		elde=0;																	
    		for (sayac=0;sayac<p_uzunluk/2;sayac++)
    		{
    			switch(p_bayrak)													
    			{
    				case 1:carpim=p_sayi_2[p_uzunluk/2-1-t]*p_sayi_1[p_uzunluk/2-1-sayac];break;
    				case 2:carpim=p_sayi_1[p_uzunluk/2-1-t]*p_sayi_2[p_uzunluk/2-1-sayac];break;
    			}
    		gecici[p_uzunluk-1-sayac-j]=(carpim+elde)%10;
    		elde=(carpim+elde)/10;
    		}	
    		if ((sayac==(p_uzunluk/2)) && (elde!=0)) 
    			gecici[p_uzunluk-1-sayac-j]=elde;
    		elde=0;
    		for(int i=0;i<p_uzunluk;++i)
    		{
    			toplam=sonuc[p_uzunluk-1-i]+gecici[p_uzunluk-1-i]+elde;
    			elde=toplam/10;
    			sonuc[p_uzunluk-1-i]=toplam%10;
    		}
    		++t;
    		++j;
    		for(int i=0;i<p_uzunluk;++i)
    			gecici[i]=0;
    	}
    	cout << "\n" << "SONUC = "	;												
    	for(int i=0;i<p_uzunluk;++i)									
    		cout << sonuc[i] ;
    	cout << endl;	
    }																				
    int main ()
    {	
    	int uzunluk_sayi_1,uzunluk_sayi_2,uzunluk_toplama,							
    	uzunluk_carpma,bayrak,kisa,dizi_uzun;
    	char cevap,devammi;								 							
    	do																			
    	{
    		sayiOku();																
    		sayiYaz();									 							
    		uzunluk_sayi_1=sayi_1.size();											
    		uzunluk_sayi_2=sayi_2.size();
    		if(uzunluk_sayi_1>uzunluk_sayi_2){
    			dizi_uzun=uzunluk_sayi_1;
    			uzunluk_toplama=uzunluk_sayi_1+1;
    			uzunluk_carpma=uzunluk_sayi_1*2;
    			bayrak=1;
    			kisa=uzunluk_sayi_2;}
    		else
    			{dizi_uzun=uzunluk_sayi_2;
    			uzunluk_toplama=uzunluk_sayi_2+1; 
    			uzunluk_carpma=uzunluk_sayi_2*2;
    			bayrak=2;
    			kisa=uzunluk_sayi_1;}
    		int dizi_sayi_1[dizi_uzun],dizi_sayi_2[dizi_uzun];	   					
     	 	for (int i=0 ; i<uzunluk_toplama ; ++i ){								
     			dizi_sayi_1[i]=0;
    		 	dizi_sayi_2[i]=0;}
    	 	for (int i=0 ; i<uzunluk_sayi_1 ; ++i )									
    		dizi_sayi_1[dizi_uzun-1-i]=sayi_1[uzunluk_sayi_1-1-i]%48;					
     		for (int i=0 ; i<uzunluk_sayi_2 ; ++i )																			
    	 	dizi_sayi_2[dizi_uzun-1-i]=sayi_2[uzunluk_sayi_2-1-i]%48;					
    	 	do																		
    		{
    			cout << "\nWhich one? ( * / + ) : ";			
    				cin >> cevap;
    			if(cevap=='+'||cevap=='*')
    				break;		
    		}
    		while (cevap!='+'||cevap!='*');
    		switch (cevap)															
    		{
    			case '+':toplama(dizi_sayi_1,dizi_sayi_2,uzunluk_toplama);			
    			break;
    			case '*':carpma(dizi_sayi_1,dizi_sayi_2,uzunluk_carpma,kisa,bayrak);
    			break;
    			default :cout << "Wrong choice..!";
    		}
    		cout << "Continue? (Y / y) : " ;						
    			cin >> devammi;
    	}
    	while (devammi=='e'||devammi=='E');
    }
     
  6. h994422

    h994422 New Member

    Joined:
    Sep 13, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    There was ..
     
  7. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    change this

    Code:
    .............
            if(sonuc[s]>9){
                sonuc[s]=sonuc[s]%10;
                elde=1;
            }else
                elde=0;
        }
        [COLOR=Red]sonuc[s]=elde;[/COLOR]//add this line
        cout << [COLOR=Red]endl[/COLOR]<< "SONUC = " ;
    ........
    
    also change this

    Code:
    ...........
        cin >> devammi;
    }while (devammi[COLOR="Red"]!=[/COLOR]'e' [COLOR="Red"]&&[/COLOR] devammi[COLOR="Red"]!=[/COLOR]'E');
    ........
    
     
    Last edited: Dec 27, 2010
  8. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    Are you sure your compiler is not complaining??...

    Code:
    void toplama (int p_dizi_sayi_1[],int p_dizi_sayi_2[],int u)					
    {
    	int sonuc[u]; // HOW IS THIS POSSIBLE??????
    
     
  9. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    Code:
    // HOW IS THIS POSSIBLE??????
    void carpma(int p_sayi_1[],int p_sayi_2[],int p_uzunluk,int p_kisa,int p_bayrak)
    {
    	int sayac,elde,carpim,toplam,gecici[p_uzunluk],sonuc[p_uzunluk],j=0,t=0;
    
     
  10. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0

    in dev c++ there is no problem to compile this.What's your compiler?
     
  11. h994422

    h994422 New Member

    Joined:
    Sep 13, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your advices.
    Also there is no problem now. I made a mistake while i was writing the message (There is / There was). I don't use English in my daily life. Sorry!?
    and micsom i don't understand your problem?
     

Share This Page