Hello ..
i've created a small program (it was an HW ) and i wish to get some ideas about how to make my code more readble:
for example:
use whitespace.
.
.
...use meaningful variables.
this kind of ideas.
so here is what i coded (ignore the main() it ain't good enuth yet)
Code:/*************************************************************************** * Hw Assignment 1 * * Thu Apr 5 19:42:59 2007 * by Jabka Atu * ****************************************************************************/ /* As an hw assignment :(sorry if i write some thing that isn't understandble enugh) [qoute="Teacher"] create a program in cpp that will do substraction and addition on strings the length is unknown and the string could store extreamly big integers (both postive and negatice). also create a histogram,get size,print etc functions. you cannot use verybigintegers. you can use iostream,math,string,assert. [/Qoute] */ /* * 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 VeryLarge{ private: char *number; int size; int remove_zeroes(char *input_to_func); int check_if_postive(char *input_to_func); char* add_two_postive_numbers(const VeryLarge& second); void set_number_it(char *input_to_func); char is_bigger(VeryLarge &first,VeryLarge &second); VeryLarge* substruct_two_postive(VeryLarge &first,VeryLarge &second); bool sign;// True for postive public: VeryLarge(); VeryLarge(char *input_to_func); VeryLarge(const VeryLarge &); ~VeryLarge(); void set_number(char *input_to_func); char* get_number(); int get_size(); VeryLarge* addition ( VeryLarge &second); VeryLarge* substruct( VeryLarge &second); void histogram(); void print(); }; //default constructer VeryLarge::VeryLarge() { number=NULL; sign=false; size=0; } VeryLarge::VeryLarge(char *input_to_func) { number=NULL; set_number(input_to_func); } //Copy constructer VeryLarge::VeryLarge(const VeryLarge& sec) { number=new char[sec.size]; assert(number); strcpy(number,sec.number); size=sec.size; sign=sec.sign; } //Distructer VeryLarge::~VeryLarge() { if (number!=NULL) delete [] number; } int VeryLarge::check_if_postive(char *input_to_func) { if ((input_to_func[0]=='+') || ((input_to_func[0]>='0')&&(input_to_func[0]<='9'))) return 1; if (input_to_func[0]=='-') return 0; } int VeryLarge::remove_zeroes(char *input_to_func) { int size_of_input_to_func=strlen(input_to_func); int o_amount=0; for (int i=0;i<size_of_input_to_func;i++) { if (input_to_func[i]=='0') { o_amount++; } if (input_to_func[i]!='0') break; } return o_amount; } void VeryLarge::set_number(char *input_to_func) { if (number!=NULL) delete [] number; int o_amount=0; int postive_check=check_if_postive(input_to_func); if (postive_check) { sign=true; if ((input_to_func[0]>='0')&&(input_to_func[0]<='9')) { o_amount=remove_zeroes(input_to_func); } else { o_amount=remove_zeroes(input_to_func+1) + 1; } } if (!postive_check) { sign=false; o_amount=remove_zeroes(input_to_func+1) + 1; } size = strlen(input_to_func); size=size-o_amount; if (size!=0) { number=new char [size]; assert(number); strcpy(number,input_to_func + o_amount); } if (size==0) { //since i in all the class i use number as an array i use the same methom the same here number= new char[2]; assert(number); number[0]='0'; number[1]='\0'; } } char* VeryLarge::get_number() { return number; } int VeryLarge::get_size() { return size; } void VeryLarge::print() { if (!sign) cout<<'-'; cout<<number<<endl; } char* VeryLarge::add_two_postive_numbers(const VeryLarge& second) { int carry=0; char output=0; int second_size =0; int i; second_size=second.size; char *result=NULL; int length_of_result=0; int smallest_size=0; if (size > second_size) { length_of_result = size + 1; result = new char [length_of_result]; result[0]='0'; strcpy(result + 1 , number); smallest_size=second.size; } else{ length_of_result = second_size + 1; result = new char [length_of_result]; result[0]='0'; strcpy(result + 1 ,second.number ); smallest_size=size; } for (i=0;i<smallest_size;i++) { output=number[size-1-i]+ second.number[second.size - 1 - i] + carry - 48 * 2; carry=0; if (output>9) { result[length_of_result -1 - i] =output -10 + 48; carry =1; } else result[length_of_result - 1 - i] = output + 48; } if (carry!=0) result[length_of_result - 1 - i] = result[length_of_result - 1 -i ] + 1; return result; } VeryLarge* VeryLarge::addition(VeryLarge& second){ VeryLarge *obj; char *result=NULL; if (sign && second.sign) { result=add_two_postive_numbers(second); obj=new VeryLarge(result); } if (!sign && !second.sign) { result=add_two_postive_numbers(second); obj=new VeryLarge(result); (*obj).sign=false; } if (!sign &&second.sign) { (*this).sign = true; obj=substruct(second); (*obj).sign=!(*obj).sign; (*this).sign=false; } if (sign && !second.sign) { second.sign=true; obj=substruct(second); second.sign=false; } if (result!=NULL) delete [] result; return obj; } char VeryLarge::is_bigger(VeryLarge &first,VeryLarge &second){ /*############################################ This Routine is to find wich array is bigger (the value) ############################################ */ if (first.size > second.size) return 1; if (first.size < second.size) return 0; if (first.size == second.size) { for (int i=0;i<first.size;i++) { if (first.number[i]>second.number[i]) return 1; if (first.number[i]<second.number[i]) return 0; } return -1;//are equal } } VeryLarge* VeryLarge::substruct_two_postive(VeryLarge &first,VeryLarge &second){ VeryLarge *obj=NULL; //################# char *result; //################# /* At first i thout to use integer but then i thout why don't i use chars (only 1 byte instead of 4). and as the values are in -255 to int i=0; int borrow=0; int ans=0; int size_of_first=strlen(first_value); int size_of_second=strlen(second_value); int biggest_size=(size_of_first>size_of_second)?size_of_first:size_of_second; int smallest_size=((size_of_first<size_of_second)?size_of_first:size_of_second); */ char i=0; char ans=0; char borrow=0; char size_of_first=first.size; char size_of_second=second.size; char biggest_size=(size_of_first>size_of_second)?size_of_first:size_of_second; char smallest_size=((size_of_first<size_of_second)?size_of_first:size_of_second); result=new char[biggest_size + 1];//one more for '\0' result[biggest_size]='\0'; //################# char *first_value=first.number; char *second_value=second.number; //################# for (i=0;i<size_of_second;i++) { ans=first_value[size_of_first - i-1] - second_value[size_of_second-i-1]-borrow; borrow=0; if (ans<0) { borrow=1; result[biggest_size - i - 1]= ans + 10 + 48; } else { result[biggest_size - i - 1]=ans + 48; } }//end of for if (borrow) { for (i=0;i< (size_of_first - size_of_second);i++) { ans=first_value[size_of_first -size_of_second- i- 1 ] - borrow; borrow=0; if (ans<48) { borrow=1; result[biggest_size-size_of_second - i-1 ]= ans + 10; } else { result[biggest_size-size_of_second - i-1]=ans; } }//end of for }//end of if (exsitence of borrow) obj=new VeryLarge(result); return obj; } VeryLarge* VeryLarge::substruct( VeryLarge &second){ VeryLarge *result; //I will use return inside each of if sentence to make it work faster: if (sign && second.sign) { result=substruct_two_postive((*this),second); return result; } if (sign && !second.sign) { second.sign=true; result=addition(second); second.sign=false; return result; } if (!sign && second.sign) { this -> sign = true; second.sign = true; result=addition(second); (*result).sign=false; this -> sign = false; return result; } if (!sign && !second.sign){ second.sign=true; result=substruct_two_postive((*this),second); second.sign=true; (*result).sign=false; return result; } } void VeryLarge::histogram(){ char amount[11]={48,48,48,48,48,48,48,48,48,48,'\0'}; char max='0'; int i=0; int j=0; for (i=0;i<size;i++) { amount[number[i]-'0']++; if (amount[number[i]-'0']>max) max=amount[number[i]-'0']; } for (i=0;(i<(max - '0'));i++) { cout<<max-i-'0'<<":"; for (j=0;j<10;j++) { if (amount[j]!=(max-i)) cout<<" "; if (amount[j]==(max-i)) { cout<<"* "; amount[j]--; } } cout<<endl; } cout<<" "; for (i=0;i<10;i++) cout<<i<<" "; } void menu(){ cout<<"Welcome to SCS (String Calculation System)\n"; cout<<"\n\n"; cout<<"1. Enter first value \n"; cout<<"2. Enter second value \n"; cout<<"3. Add the strings (Arithmetic)\n"; cout<<"4. subtract the second string from the first\n"; cout<<"5. histogram of first value\n"; cout<<"6. histogram of seconf value n\n"; cout<<"7. extand the limit of input stram to n\n"; cout<<"q. to exit\n"; } bool check_input(char *buff){ int size=strlen(buff); int i=0; if (!((buff[0]=='+')||(buff[0]=='-')||((buff[0]>='0')&&(buff[0]<='9')))) return false; for (i=1;i<size;i++) if (!((buff[i]>='0')&&(buff[i]<='9'))) return false; return true; } void get_number(char *buff) { cout << "Enter number: "; cin>>buff; if (!(check_input(buff))) { cout<<"Dood im sorry but your input isn't ok"<<endl; get_number(buff); } } int main() { char ch='0'; int n=1024; char *buff=new char[n]; VeryLarge num1,num2,*obj; while ((ch!='q')&&(ch!='Q')) { menu(); cout<<"\nchoice:"; ch=getchar(); switch (ch) { case '1': { cout<<"\nThe First number :\n"; get_number(buff); num1.set_number(buff); break; } case '2': { cout<<"The Second number :\n"; get_number(buff); num2.set_number(buff); cout<<endl; break; } case '3': { obj=num1.addition(num2); cout <<"The Result is :"; (*obj).print(); delete obj; break; } case '4': { cout<<"The result is \n"; obj=num1.substruct(num2); (*obj).print(); delete obj; break; } case '5': { num1.histogram(); break; } case '6': { num2.histogram(); break; } case '7': { cout<<"Please enter an intger size"; cin>>n; delete [] buff; buff = new char[n]; assert(buff); break; } } } return 0; }