65.9K
CodeProject 正在变化。 阅读更多。
Home

面向对象编程的校验和算法代码

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2投票s)

2016年3月17日

CPOL

1分钟阅读

viewsIcon

10564

我为我的课程准备了一个项目。在这个项目中,我决定使用校验和算法来判断两个字符串值是否相同... 我使用了两种语言来解释代码。

引言

我们在计算机网络中使用一些算法来检查数据在传输过程中是否损坏。其中之一就是校验和算法。 在这部分中,我们将从用户那里获取一个字符串值,然后根据校验和算法创建校验和代码。 之后,我们将获取另一个字符串值并创建校验和代码。 如果两者相等,控制台将输出“值相同!”;如果不相等,控制台将输出“值不同!”

土耳其语:

为了了解在计算机网络中发送的数据在传输过程中是否损坏,已经开发了各种算法。 其中之一是校验和算法。 在本节中,我们将从用户那里获取一个字符串值,并根据校验和算法生成校验和代码,然后再次从用户那里获取一个字符串值并生成其校验和代码,然后比较这两个代码以检查数据是否相同。 如果数据不同,则在屏幕上打印“数据不同”;如果相同,则打印“数据相同”。

背景

在创建校验和代码时,我们将前两个字母的十六进制代码写在一起,并将其他两个字母写在其他字母的下方。 然后我们将它们相加并创建校验和代码。 我在图片中展示了它。

土耳其语:

在创建校验和代码时,将前两个字符的十六进制代码并排书写,其他字符依次添加到下方,然后将上下相加得到校验和代码。

例如:

数据=Forouzan

checksum.png

数据= Forouzan

使用代码

#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;


 

我用DEVC++编写了这些代码

面向对象编程的校验和算法代码 - CodeProject - 代码之家
© . All rights reserved.