| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 518 人关注过本帖
标题:求助:为什么这个用C语言写的程序在C++编译时老是出错?
取消只看楼主 加入收藏
skim65
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-19
结帖率:0
收藏
 问题点数:0 回复次数:0 
求助:为什么这个用C语言写的程序在C++编译时老是出错?
下面是一段C语言的程序,为什么用C++编译后老有错误?要怎样修改?求大侠们指点一下

#include <iostream>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <ctime>


#define MAX_RANDOM LONG_MAX    /* Maximum value of random() */
#define RATE 0.5               /* Coding rate = 4/8 */
#define INIT_SNR 7.5           /* Initial value of Eb/N0 */
#define FINAL_SNR 8.0          /* Final value of Eb/N0 */
#define SNR_INCREMENT 0.5      /* Increment in Eb/N0 */
#define NUMSIM 50000000         /* Number of simulations (one per 4 bits) */

int wh[16] = { 0, 1, 1, 2, 1, 2, 2, 3,       /* Hamming weight function: */
               1, 2, 2, 3, 2, 3, 3, 4 };     /* wh[i] = weight of i      */
int n = 8;
int k = 4;
int G[4][8] = { 1,1,1,1,1,1,1,1,
                0,1,0,1,0,1,0,1,
                0,0,1,1,0,0,1,1,
                0,0,0,0,1,1,1,1 };

float sim, block_error;
float ber;
float amp;
double seed;
int error;
int data[4], codeword[8];
int data_int;
float snr;
float transmited[8];
float received[8];
int estiword[8], estidata[4];

void initialize(void);
void awgn(void);
void encode(void);
void HD_decode(void);
int green_machine(void);

int main()
{
  int i,j;

  snr = INIT_SNR;
  while ( snr < (FINAL_SNR+0.001) )
    {
      initialize();
      while (sim < NUMSIM)
      {
        for (i=0; i<k; i++)
          data[i] = (random()>>10) & 0x01;
        /* convert data[] to integer for error computation purposes */
        data_int = 0;
        for (i=0; i<k; i++)
          data_int = (data_int << 1) ^ data[i];

        encode();

        for (i=0; i<n; i++)
          if (codeword[i]) transmited[i] = -1.0;
          else transmited[i] = 1.0;

        awgn();

        /* HD_decode(); /* */

        i = green_machine();

        error = i ^ data_int;
        /* if (error) block_error+=1.0;    /* block error rate */

        ber += (float) wh[error];

        sim+=1.0;
      }
    printf("%f %13.8e\n", snr, (ber/(sim*4.0))); /* 1 sim = 4 bits! */
    fflush(stdout);
    snr += SNR_INCREMENT;
  }
return 0;
}

void encode()
{
  int i,j;
  for (j=0; j<n; j++)
    {
    codeword[j] = 0;
    for (i=0; i<k; i++)
      codeword[j] ^= ( data[i] * G[i][j] ) & 0x01;
    }
}

void HD_decode()
{

}

int green_machine()
{
  float r1[8], r2[8], r3[8];
  int i, est_data;
  float max;

  /* First stage */
  r1[0] = received[0] + received[1];
  r1[1] = received[0] - received[1];
  r1[2] = received[2] + received[3];
  r1[3] = received[2] - received[3];
  r1[4] = received[4] + received[5];
  r1[5] = received[4] - received[5];
  r1[6] = received[6] + received[7];
  r1[7] = received[6] - received[7];

  /* Second stage */
  r2[0] = r1[0] + r1[2];
  r2[1] = r1[0] - r1[2];
  r2[2] = r1[1] + r1[3];
  r2[3] = r1[1] - r1[3];
  r2[4] = r1[4] + r1[6];
  r2[5] = r1[4] - r1[6];
  r2[6] = r1[5] + r1[7];
  r2[7] = r1[5] - r1[7];

  /* Third stage */
  r3[0] = r2[0] + r2[4];
  r3[1] = r2[0] - r2[4];
  r3[2] = r2[1] + r2[5];
  r3[3] = r2[1] - r2[5];
  r3[4] = r2[2] + r2[6];
  r3[5] = r2[2] - r2[6];
  r3[6] = r2[3] + r2[7];
  r3[7] = r2[3] - r2[7];

  /* Find coordinate with largest magnitude */
  max = 0.0;
  for (i=0; i<n; i++)
    if (fabs(r3[i]) > max)
      {
      est_data = i;
      max = fabs(r3[i]);
      }

  /* Decode the sign bit */
  if (r3[est_data] < 0.0) est_data += 8;

  return(est_data);
}

void awgn()
{
  double u1,u2,s,noise,randmum;
  int i;
  for (i=0; i<n; i++)
    {
      do {
        randmum = (double)(random())/MAX_RANDOM;
        u1 = randmum*2.0 - 1.0;
        randmum = (double)(random())/MAX_RANDOM;
        u2 = randmum*2.0 - 1.0;
        s = u1*u1 + u2*u2;
        } while( s >= 1);
      noise = u1 * sqrt( (-2.0*log(s))/s );
      received[i] = transmited[i] + noise/amp;
#ifdef NO_NOISE
      received[i] = transmited[i];
#endif
    }
}

void initialize()
{
  time(&seed);
  srand(seed);
  amp = sqrt(2.0*RATE*pow(10.0,(snr/10.0)));
  block_error = 0.0;
  ber = 0.0;
  sim = 0.0;
}
搜索更多相关主题的帖子: 编译 C语言 
2010-03-19 09:47
快速回复:求助:为什么这个用C语言写的程序在C++编译时老是出错?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025517 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved