Click here to Skip to main content
15,879,490 members
Articles / Programming Languages / C++
Tip/Trick

Checksum Algorithm with Obj.Ori.Prog. the code

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
16 Mar 2016CPOL1 min read 9.9K   1   3
I have prepared a project for my lecture. In this project I decide two string value is same or not as checsum algorithm... I used 2 language to explain the codes.

Introduction

We use some algorithm to check the datas break down or not at computer networks. One of them is checksum algorithm. In this part we will take a string value from the user then create the checksum code as checksum algorithm. After that we will take another string value and create checksum code. If both of them are equal the console writed “the values are same!” or not equal the consol writed “the values are different!”

Turkish;

Bilgisayar ağlarında gönderilen verilerin yol boyunca bozulup bozulmadıklarını anlamak için çeşitli algoritmalar geliştirilmiştir. Bunlardan biri de checksum algoritmasıdır. Bu bölümde kullanıcıdan bir string değer alıp checksum algoritmasına göre checksum kodunu oluşturacağız daha sonra kullanıcıdan bir string değer daha alıp  bunun checsum kodunu oluşturacağız ve iki kodu karşılaştırıp verilerin birbiriyle aynı olup olmadığını kontrol edeceğiz. Eğer veriler farklıysa "veriler birbirinden farklı", aynı ise "veriler birbiriyle aynı" cümlelerini ekrana yazdıracağız.

Background

While creating checksum code we will write first two letters’ hexadecimal code together the other 2 letters under of other letters. Then we will sum them and create checksum code. I showed it on the picture.

Turkish;

Checksum kodu oluşturulurken ilk iki harfin kodları hex karşılığında yanyana yazılır diğer harflerde sırasıyla alta eklenir ve alt alta toplanarak checksum kodu bulunur.

For Example; Örneğin;

Data=Forouzan

checksum.png

veri= Forouzan

Using the code

#include <iostream>  

#include <cmath>

#include <conio.h>

#include <iomanip> 

#include <cstring>  //defining the libraries...

using namespace std;//bu noktaya kadar kütüphanelerimizi tanımladık...

class checksum{

    public:      //bir class oluşturduk ve bu classa ihtiyacımız olan fonksiyon ve değişkenleri tanımladık

        int k,l;//created a class and defined the variables and functions which we need.

        int n;

        long A[];

    int HexBul(string s);

    void uzunluk(string s);

    long sumbul(string s);    

}ob1,ob2;

void checksum:: uzunluk(string s)  //girilen verinin uzunluğunu bulmak için bir fonksiyon tanımladık
{
    n=s.length();            //created a function which will find the length of the data

}

int checksum::HexBul(string S)//her karakterin ASCII kodlarını ayrı ayrı arrayin elemanları olarak
{
    char a;                    //arrayin içine atadık 

    for(int i=0; i<n; i++)    //we assigned the all characters of the data one to one as member of
    {
        a=S.at(i);             //the array

        A[i]=a;
    }

return A[n];

}

long checksum:: sumbul(string s)
{

    k=0;

    for(int i=0; i<n ;i+=2)  //arrayin elemanlarının tekleri teklerle çiftleri çiftlerle topluyoruz
    {
        k=k+A[i];             // A[0]+A[2]+A[4]+...+A[n]

    }                         //we are summing the elements of array odds wiht odds evens with evens

    l=0;

    for(int j=1; j<n; j+=2)
    {

        l=l+A[j];

    }

    return k,l;           //toplamları k ve l değişkenlerine atadık. Assigned the sums k and l.

}

int main(int argc, char** argv) //programın ana kısmı.- Main part of the program
    {
    cout<<"Bir isim giriniz:"<<endl;

    string s1,s2;  //verileri tanımlıyoruz- define the datas

    getline(cin, s1);//kullanıcıdan ilk veriyi alıyoruz- taking the first data from user

    ob1.uzunluk(s1);//uzunluğunu hesaplıyor ob1 nesnenin n değerine atıyoruz

                    //calculate the lenght and assign to the n for ob1

    ob1.HexBul(s1); //karakterlerin kodlarını arraye atıyoruz (ob1 için)- assing the codes of chars for ob1

    ob1.sumbul(s1);//kve l değerlerini buluyoruz (ob1 nesnesi için) -find k and l values for ob1

    if(ob1.l>255)
    {

        ob1.k=ob1.k+l/256;// checksum algoritmasına göre altalta toplama işlemini

        ob1.l=ob1.l%256;  //make sum operation as checksum algorithm

        if(ob1.k>255)
       {

            ob1.l=ob1.l+k/256;

            ob1.k=ob1.k%256;

        }

    }

    else if(ob1.k>255)
    {  

        ob1.l=ob1.l+k/256;

        ob1.k=ob1.k%256;

    }

    cout<<hex<<"Checksum kodu: "<<ob1.k<<ob1.l<<endl;//ob1 için checksum kodunu yazdırıyoruz-write the checksum code for ob1

    cout<<"Baska bir isim giriniz"<<endl;//ikinci veriyi alıyoruz- taking the second data

    getline(cin, s2);  //Aynı işlemleri ob2 için tekrarlıyoruz

    ob2.uzunluk(s2);   //repeat the same operations for ob2

    ob2.HexBul(s2);

    ob2.sumbul(s2);

    if(ob2.l>255)
    {

        ob2.k=ob2.k+l/256;

        ob2.l=ob2.l%256;

        if(ob2.k>255)
        {

            ob2.l=ob2.l+k/256;

            ob2.k=ob2.k%256;

        }

    }else if(ob2.k>255)
    {

        ob2.l=ob2.l+k/256;

        ob2.k=ob2.k%256;

    }

    cout<<hex<<"Checksum kodu: "<<ob2.k<<ob2.l<<endl;

    if(ob1.k==ob2.k && ob1.l==ob2.l)//son olarak k ve l değerlerini birlikte karşılaştırıpverilerin aynı

        cout<<"iki isim birbiriyle ayni!";//veya farklı olduklarını konsoldan yazdırıyoruz

    else                                  //at the end looking k and l values and decide are they same or not 

        cout<<"isimler birbirinden farkli!";

    return 0;

 

I have written this codes on DEVC++

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBraces and readability Pin
ssiddall18-Mar-16 7:23
ssiddall18-Mar-16 7:23 
AnswerRe: Braces and readability Pin
Enes Yıldız20-Mar-16 1:46
Enes Yıldız20-Mar-16 1:46 
GeneralRe: Braces and readability Pin
ilkerKaran20-Mar-16 22:26
ilkerKaran20-Mar-16 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.