How to Implement C ++ Arithmetic Coding

Source: Internet
Author: User

C ++ uses two basic parameters for arithmetic coding: the probability of a symbol and Its Encoding interval. The probability of a source symbol determines the compression encoding efficiency and the interval of the source symbol in the coding process, the encoding interval determines the output after the compressed symbol.

In C ++ arithmetic coding, the input is a symbol. The probability of each symbol also has a symbolic sequence to be encoded. The initial encoding interval can be calculated based on the probability. First, several variables are available at the end: high -- upper limit of the current encoding, Low -- lower limit of the current encoding, and high -- intermediate variable, used to calculate the upper limit of the current interval of the next encoding symbol, low -- intermediate variable, used to calculate the lower limit of the current interval of the next encoding symbol, d -- the distance between the current intervals.

The current interval of the 1st encoding symbols is its initial encoding interval, and the current interval of the I encoding symbol is [Low, High) after the I-1 ), the current interval algorithm for the I + 1 encoding symbol is as follows: high = Low + d * the upper limit corresponding to the I + 1 Initial encoding symbol, low = Low + d * lower limit corresponding to the I + 1 encoding symbol, and then the probability of High = high, Low = low, d = d * encoding symbol I.

The encoding procedure is as follows:

 
 
  1. #include <iostream.h> 
  2. #define M 100  
  3. #define N 4  
  4. class suanshu  
  5. {   
  6. int count,length;  
  7. char number[N],n;  
  8. long double chance[N],c;  
  9. char code[M];  
  10. long double High,Low,high,low,d;  
  11. public:  
  12. suanshu()  
  13.   {High=0;Low=0;}  
  14. void get_number();  
  15. void get_code();  
  16. void coding();  
  17. ~suanshu(){}  
  18. };  
  19.  
  20. void suanshu::get_number()  
  21. {  
  22. cout<<"please input the number and its chance."<<endl;  
  23. for(int i=0;i<N;i++)  
  24. {  
  25.   cin>>n>>c;  
  26.   number[i]=n;  
  27.   chance[i]=c;  
  28. }  
  29. if(i==20)  
  30.   cout<<"the number is full."<<endl;  
  31. count=i;  

This C ++ arithmetic code is run in VC6.0 and xp Professional Edition. This is my first time to write a complete program in c ++, but it is still somewhat unsatisfactory, for example, variable and function names are not very professional, so you will pay attention to them later.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.