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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <string.h>
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
//#include <iostream.h>
#include <stdlib.h>
//#include <time.h>
#include <conio.h>
#include <math.h>
/*And if digit is .39, the value will be, ... -3?
I didn't understand why .36 is minus 1?
To get tail of digit: use ceil, like
c=a/c;
digit=c-ceil(c)
and extract two digit decimal:
twodecimal=ceil(digit*100);*/
using namespace std;
int main();
void primechecker();
void Starthash();
void Prime();
void folding();
void GetResultsa();
float numtohash,choosehash,anshash,digplaces,input,fop,rfop,hashfunc;
int primediv;
void
Starthash ()
{
cout<<"Choose Hashing Technique: ";
cout<<"(Choose number)\n";
cout<<"[1]Prime Number Division Remainder\n";
cout<<"[2]folding\n";
cout<<"\n";
cin>>hashfunc;
if(hashfunc==1)
{Prime();}
else if(hashfunc==2)
{folding();}
else
{cout<<"Wrong number Input!";Starthash ();}
}
void
Prime()
{
cout<<"Prime Number Division Remainder\n";
cout<<"Enter number to hash: ";
cin>>numtohash;
cout<<"Enter prime divisor: ";
cin>>primediv;
primechecker();
anshash=numtohash/primediv;
digplaces=anshash;
int op=(int)digplaces;
fop=digplaces-ceil(op);
rfop=fop*100;
int srfop=(int)rfop;
cout<<"Ans: ";
cout<<ceil(srfop)-1;
cout<<"\n\n";
main();
//{getch();}
}
void
folding()
{
char *buffer=new char[80];
char *buffin=new char[80];
char *buff;
char *pos=new char[80];
char handle;
char i,ncpy=0;
int dummy=0;
char next=0;
cout<<"\n\nHashing-folding (eg:123+45+78)"<<"\n";
//init hash
cin.get(buffer,80);
//search + sign
pos[0]=0;//+ count
pos[1]=0;
i=0;
do {
handle=buffer[i++];//get char list
if(handle=='+')
{
pos[0]++;//count + sign
pos[pos[0]+1]=i; //save position at next pointer
}
}
while(handle); //until zoro string
pos[0]++;
pos[pos[0]+1]=i;//save max string to last pos
//end search
cout<<"Now input again, then number will hash (eg: 1234578 just copy above without the plus ("<<"+"<<") symbol\n";
cin.read(buffin,1);
cin.get(buffin,80);
//compare hash
if(strlen(buffin)-(i-pos[0])){
cout <<"Hash not match!"<<"\n";
delete pos;
delete buffin;
delete buffer;
{getch();}
}
//end compare
//eval list
next=0;
dummy=0;
for(i=0;i<pos[0];i++)
{
ncpy=pos[i+2]-pos[i+1]-1;
buff=new char[ncpy+1];
strcpy (buff,"\0");
strncat(buff,buffin+next,ncpy);
cout<<"Push buffer ";
cout.setf(cout.right);
cout.width(6);
cout <<buff<<"\n";
next+=pos[i+2]-pos[i+1]-1;
dummy+=atoi(buff);//eval buffer
delete buff;
}
//end eval list
cout<<"Sum buffer return value = "<<dummy
<<"\nAnswer "<<dummy%100<<"\n";
delete pos;
delete buffin;
delete buffer;
//return 0;
{getch();}
cout<<"\n\n";
//Starthash();
}
void
primechecker()
{
if(primediv==1)
{
cout << "invalid or not a prime divisor" <<"\n";
Prime();
}
for(int i=2; i<=primediv/2; i++)
{
if(primediv%i==0)
{
cout << "invalid or not a prime divisor" << "\n";
Prime();
}
}
//{getch();}
}
int
main()
{
Starthash();
cout<<"\n";
//{getch();}
}
| |