[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

could you help with c++ ? (HW)



Hello ..
i know that it bad to ask for help in codding.
but if i help others maybe could help me.
im working on it for allmost 3 weeks and i really need help.
if you with to say to me that :
im bad codder - i know that this allready could you please write what exactly i did wrong. RTFM/STFW (My bible/koran/or what ever you call it is Deitl's C++ book) could you please write what chapter should i read.
I've got this hw asignmet:
build a program that will get two string (unkown size) and make the next math func between them :
addition.
substraction.

you cannot use math,bignumbers/bigintegers/changing the string onto integers/real numbers.
you can use complement.

if use a function you should write it.

Yours Truley
   Jabka Atu
      btw use _VIM_  not Visual @#$%^&

/***************************************************************************
 *            Hw Assignment 1
 *
 *  Thu Apr  5 19:42:59 2007
 *  ka Jabka Atu
 *  
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 


#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

class Very_Large
{
	private:
	char *local_value;
	
	int length_of_local_value;
	char* both_postive(char *);
	int truelangth(char *number);
	
	public:
		
	Very_Large();
	Very_Large(char *str);
	Very_Large(const Very_Large & objname);
	~Very_Large();

	
	void setnumber(char *);
	char* getnumber();
	void print();
	
	char* subtraction(char *);
	void histogram();
	char* complement(char *);
	int check_if_postive(char *);
	char* create_array_for_output(char *);
	char* addnumber(char *number);
	
	};

Very_Large::Very_Large()
{
 local_value = new char[2];
 assert(local_value);
 local_value[0]='0';
 length_of_local_value=2;
}

Very_Large::Very_Large(char *number)
{
 local_value = new char [strlen(number)+1];
 assert(local_value);
 strcpy(local_value,number);
 length_of_local_value=strlen(local_value);
}

Very_Large::Very_Large(const Very_Large& sec)
{
	strcpy(local_value,sec.local_value);
	assert(local_value);
	length_of_local_value=strlen(local_value);
}

Very_Large::~Very_Large(){
	delete [] local_value;
	
}

void Very_Large::print(){
	cout <<"The number is : "<<local_value<<endl;
}

void Very_Large::setnumber( char *number){
	local_value = new char[strlen(number) +1];
	assert(local_value);
	strcpy(local_value,number);
	length_of_local_value=strlen(local_value);	
}

char *Very_Large::getnumber(){
	return local_value;
}

int Very_Large::check_if_postive(char *number){
	if (strlen(number)>0)
	 {
		if (number[0]=='-')
			return 0;
		
		if ((number[0]>='0')&&(number[0]<='9'))
		{
			return 1;			
		}
		if (number[0]=='+')
		{
			number[0]='0';
			return 1;
		}
		
	 }
	 
	if (!strlen(number))
		cout<<"Dood your string is 0 length"<<endl;		
}

char* Very_Large::create_array_for_output(char *number){
		//I will create a new array with one more byte for hold the output.
	char *array_that_will_store_array;	
	int length_of_input_to_func=strlen(number);

	//if (local_value[0]=='9') aftre
	if (1)
		{
			if (length_of_local_value> length_of_input_to_func)
				{
					array_that_will_store_array = new char[length_of_local_value];
					assert(array_that_will_store_array);	
					strcpy(array_that_will_store_array+1,local_value);
				}
			else
				{
					array_that_will_store_array = new char[length_of_input_to_func];
					assert(array_that_will_store_array);
					strcpy(array_that_will_store_array+1,number);
				}
	
			array_that_will_store_array[0]='0';	
   		}
   if(1==0)
	   {
	    if (length_of_local_value> length_of_input_to_func)
			{
				array_that_will_store_array = new char[length_of_local_value];
				assert(array_that_will_store_array);	
				strcpy(array_that_will_store_array,local_value);
			}
		else
			{
				array_that_will_store_array = new char[length_of_input_to_func];
				assert(array_that_will_store_array);
				strcpy(array_that_will_store_array,number);
			}
	   }
return array_that_will_store_array;
}

char* Very_Large::both_postive(char *number){
	
		/*
		Used for subtraction of two postive numbers;
		*/
		char *result=NULL;
		int q=0,i=0;
		int length_of_input_to_func=strlen(number);
		char *temp;
		int length_of_temp=0;
		int delta=0;
		int biggest_size=(length_of_local_value>length_of_input_to_func)?length_of_local_value:
							length_of_input_to_func;

		delta=((biggest_size - length_of_local_value)==0)?length_of_local_value-length_of_input_to_func:
															length_of_input_to_func-length_of_local_value;

		temp=complement(number);
		
		result=addnumber(temp);
		
		if (result[0]=='0')
			{
				delete [] temp;
				temp=new char[strlen(result)-1];
				strcpy(temp,result +1 );
				delete [] result;
							
				result = temp;
			}
		
		return result;
}
int Very_Large::truelangth(char *number){
	int i=0;
	if (number[0]=='+' || (number[0] == '-'))
		return strlen(number) -1;
	else
		return strlen(number);
}

char* Very_Large::complement(char *number){
	//Mashlim
	char *result;
	char *temp;
	
	int i=0;
	int flag=0;
	int length_of_local_value	=strlen(local_value);
	int delta 				=0;
	int length_of_number  = strlen(number);
	int length_of_result=(length_of_local_value>length_of_number)
						 ?length_of_local_value : length_of_number ;
	int smallest_length=(length_of_local_value<length_of_number)
						 ?length_of_local_value : length_of_number ;
	/*
		20 - 5 = 
		=20 + 95 = 115
		=15
		20 - 30 = 20 + 70 = 90
		mashlim(90) = 10 add minus.
	*/
	
	//			 0   1	 2	 3	 4	 5 	 6 	 7 	 8 	 9	 
	char m[11]={'0','9','8','7','6','5','4','3','2','1','0'};
	
	result = new char[length_of_result];
	assert(result);
	
	//I need to enlarge the size of the number to local_value
	//number : input
	//local_value : the value inside the object
	for(int i=length_of_number-1;i>-1;i--)
		{
				result[i+length_of_local_value-length_of_number] = m[number[i]-'0'+flag];
				if (result[i+length_of_local_value-length_of_number]!='0')
					flag=1;
			
		}
	
	for (i=length_of_result-smallest_length-1;i>-1;i--)
		{
			result[i]='9';
		
		}
	
	return result;
}
void Very_Large::histogram(){}
char* Very_Large::addnumber(char *number){
	
	char *result= create_array_for_output(number);
	
	int i=0;
	int length_of_local_value=strlen(local_value); 
	int length_of_input_to_func=strlen(number);
	int length_of_result=strlen(result);
	int b=0;
	int carry=0;
	int q=0;
	int postivite_check_of_local_value=check_if_postive(local_value);
	int postivite_check_of_input_to_stream=check_if_postive(number);
	
	if (number[0]=='+')
		number[0]='0';
	if (local_value[0]=='+')
		local_value[0]='0';
	//if((check_if_postive(local_value) && check_if_postive(number)))//both postive
	if (postivite_check_of_local_value && postivite_check_of_input_to_stream)
		{
			if ((length_of_local_value - length_of_input_to_func)<=0)
				q=length_of_local_value;
     		else
				q=length_of_input_to_func;
		 
			for (i=0;i<q;i++)
				{
				b=local_value[length_of_local_value -1 - i] + number[length_of_input_to_func  -1 -i] + carry - 48 * 2;
		
				if ((local_value[length_of_local_value -1 -i]<48)&&(number[length_of_input_to_func  -1 -i]>=48))
					b= number[length_of_input_to_func  -1 -i] + carry -48;
				if ((number[length_of_input_to_func  -1 -i]<48)&&(local_value[length_of_local_value -1 -i]>=48))
					b=local_value[length_of_local_value  -1 -i] + carry -48;
		
				carry=0;
				if (b>9)
					{
						result[length_of_result -1 - i] =b -10 + 48;
						carry =1;
					}

				else
					result[length_of_result - 1 - i] = b  + 48;
		
				}//end of for
		
			if (carry!=0)
				result[length_of_result - 1 - i] = result[length_of_result - 1 -i ] + 1;
		}//end of first if (both are postive)
	
	
	if 	((!postivite_check_of_local_value) && (!postivite_check_of_input_to_stream))//both negative
	{
		char *tmp;
		tmp=new char[length_of_local_value-1];
		assert(tmp);
		strcpy(tmp,local_value + 1);
		delete [] local_value;
		local_value=tmp;
		
		tmp=NULL;
		
		tmp=new char [length_of_input_to_func];
		strcpy(tmp,number + 1);
		
		number=tmp;
		delete [] result;
		tmp = addnumber(number);
		result= new char[length_of_result];
		result[0]='-';
		strcpy(result + 1, tmp);
		delete tmp;
	}

	
	if (postivite_check_of_local_value && !postivite_check_of_input_to_stream)
		//Postive and Negtive
	{
		result=subtraction(number+1);
	}
	if (!postivite_check_of_local_value && postivite_check_of_input_to_stream)
	{
		char *temp;
		temp=local_value;
		local_value=number;
		number=temp;
		result = subtraction(number + 1);
		return result;	
	}
	
	return result;
}


char* Very_Large::subtraction(char *number){
	char *result=NULL;
	char postivite_check_of_input_to_stream=check_if_postive(number);
	char postivite_check_of_local_value=check_if_postive(local_value);
	
	/*
		There are 4 posible options :
		1.both postive 
		2.both negative the answar is using addnumber
		3.the local_value is negetive and the number is negtive
		4.the local_value is positive and the number is negative
	*/

	if (number=="0")
		return local_value;
	
	if (!postivite_check_of_input_to_stream && (!postivite_check_of_local_value))//both negative
	{
		local_value[0]='0';
		result=subtraction(number);
		assert(result);
		if (result[0]=='-')
			{
			result[0]='+';
			return result;
			}
		if (result[0]=='+')
			{
			result[0]='-';
			return result;
			}
		
		if (result[0]=='0')
			{
			result[0]='-';
			return result;
			}			
	}
	
	
	
	
	if (postivite_check_of_input_to_stream && postivite_check_of_local_value)//both postive
	{
		
		char *temp;
		result=both_postive(number);
		int length_of_local_value=strlen(local_value);
		int length_of_input_to_func=strlen(number);
		int length_of_result=strlen(result);	
		int biggest_size=(length_of_local_value>length_of_input_to_func)? 
							length_of_local_value:length_of_input_to_func;
		char  wich=(length_of_local_value>length_of_input_to_func)? local_value[0]:number[0];
		
				
		if ((length_of_result==biggest_size))
		{
			temp=result;
			result=NULL;
			result=complement(temp);
			delete [] temp;
			result[0]='-';
		}
		
		if ((result[0]>'0'))
			{
				
				char *temp;
				temp=new char[strlen(result)-1];
				
				strcpy(temp,result +1 );
				
				if (result!=NULL)
				{		
					//hazzerde of leak of memory				
					//if you are using POSIX system it may couse dobule delliting instead you can use 
					//result=NULL;
				
					delete [] result;
				
					//will make dobule free
				}
				
				result = temp;
			} 	
		
	}//endif
	
	if (postivite_check_of_local_value && !postivite_check_of_input_to_stream)//postive and negative
	{
			result=addnumber(number+1);
			return result;
	}
	
	if (!postivite_check_of_local_value && postivite_check_of_input_to_stream)
	{
		local_value[0]='+';
		result=addnumber(number);
		result[0]='-';
		local_value[0]='-';
		
	}
	
	
	return result;
}

void menu(){
cout<<"Welcome to SCS (String Calculation System)\n";
cout<<"\n\n";
cout<<"1. Enter first value \n";//(local_value)
cout<<"2. Enter second value \n";//(number)
cout<<"3. Add to strings (Arithmetic)\n";
cout<<"4. subtract the second string from the first\n";
cout<<"5. Print the value inside local_value\n";
cout<<"6. extand the limit of input stram to n\n";
cout<<"q. to exit\n";
}

int main(){
Very_Large a;
char *value_into_class=NULL;
char *number=NULL;
number=new char[256];
int n=256;
char *result=NULL;

	char choice='0';
	cout<<"please enter your choise\n";
	menu();
	while((choice!='q')){
		
	choice=getchar();
	switch(choice){
		case '1':
			{
			if (value_into_class!=NULL)
				delete [] value_into_class;
			
			value_into_class=new char[n];
			cout<<"the frase will be :";
			cin>>value_into_class;
			a.setnumber(value_into_class);
			break;	
			}
		case '2':
			{
			if (number!=NULL)	
				delete [] number;
			number=new char[n];
			cout<<"Please enter a phrase";
			cin>>number;
			break;
			}
		case '3':
			{
			cout<<number;
			result=a.addnumber(number);
			cout<<result<<endl;
			delete [] result;
			break;
			}
		case '4':
			{
			result=a.subtraction(number);
			cout<<result<<endl;
			delete [] result;
			break;
			}
		case '5':
			{
				a.print();
			}
		case '7':
			{
			a.histogram();
			}
		case '6':
			{
				cout<<"the limit will be (more other 256 byte):\n";
				cin>>n;
							
				break;
			}
		}
	
	}


}

Reply to: