Love and Friendship calculator is a small C or C++ application which implements a school-time logic. When we are in school time we play with our names and calculate some percentage. I also implemented one of them logic to calculate Love and friendship.
The Logic which i have implemented is as follows.
- Two names n1 and n2 are taken from the user. And one choice for friendship or love calculation.
- If user choose friendship then we make a string n1+ friend + n2.
- If user choose love then we make a string n1 + love + n2.
- Then we start manipulation over the concatenated string from left to right.
- We count occurrence of each character (except space) until all characters are traversed and keep that count of each character in an array.
- Now we have an array containing count of each character. now we perform operation on this array.
- we start adding first element of array with last element of array, second element with second last element of the array and so on.
- Keep continue the above step until we get array with only two elements.
- Now we have percentage. if percentage is higher then 100% we manipulate it until we get percentage less than or equal to 100%.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
/* Author - Nisar Ahmed. Description - It takes two names and then calculate Love and Friendship percentage of these names. */ #include< conio.h> #include< iostream.h> #include< string.h> #include< stdio.h> #include< stdlib.h> int calcPercentage(int l,int h,int ar[]); struct nameModel { char name[100]; int isCounted[100]; }nameObject; int main(){ textmode(C40); textbackground(GREEN); textcolor(WHITE); do{ x: clrscr(); int count,a=0,ar[20],i,j; char name1[40],ch,name2[40],choice; cout<<"\n"; cout<<"------- 799 Web Nixar Creation ---------"; cout<<"----------------------------------------"; cout<<"| * Welcome To Friendship * |"; cout<<"| * & Love Calculator * |"; cout<<"----------------------------------------"; cout<<" 1.Friendship "; cout<<" 2.Love "; cout<<" 3.Exit "; cout<<" Enter Your Choice "; gotoxy(28,10); cin>>choice; switch(choice) { case '1': cout<<" "; cout<<" ---------------------- "; cout<<" Enter Your Name "; gotoxy(17,14); gets(name1); cout<<" ---------------------- "; cout<<" Enter Your Friend's Name "; gotoxy(17,17); gets(name2); cout<<" ---------------------- "; break; case '2': cout<<" "; cout<<" ---------------------- "; cout<<" Enter Your Name "; gotoxy(17,14); gets(name1); cout<<" ---------------------- "; cout<<" Enter Your Lover's Name "; gotoxy(17,17); gets(name2); cout<<" ---------------------- "; break; case '3': exit(1); break; default: cout<<" Wrong Choice !! "; goto x; } strlwr(name1); strlwr(name2); if(choice=='1') strcat(name1,"friend"); else strcat(name1,"love"); strcat(name1,name2); strcpy(nameObject.name,name1); for(i=0;i< strlen(name1);i++){ nameObject.isCounted[i]=0; } for(i=0;i< strlen(nameObject.name);i++){ if(nameObject.isCounted[i]==0){ ch=nameObject.name[i]; if(ch==' ') continue; count=1; nameObject.isCounted[i]=1; for(j=i+1;j< strlen(nameObject.name);j++){ if(ch==nameObject.name[j]){ count+=1; nameObject.isCounted[j]=1; } } ar[a++]=count; } } gotoxy(18,19); cout<< calcPercentage(0,(a-1),ar) << "%\n\n\n"; cout<< "########################################"; getch(); }while(1); return 0; } int calcPercentage(int l,int h,int ar[]) { int i,r=0,j,sum; if((l+h)%2 == 0){ for(i=l,j=h;i < j;i++,j--) ar[r++]=ar[i]+ar[j]; ar[r++]=ar[i]; } else{ for(i=l,j=h;i < j;i++,j--) ar[r++]=ar[i]+ar[j]; } h=r-1; if(l+h > 1) calcPercentage(l,h,ar); else{ sum=0; for(i=0;i < 2;i++){ if(i==0) sum+=ar[i]*10; else sum+=ar[i]; } while(sum > 100){ sum = (sum%10)*10 +(sum/10); } return sum; } } |