24小时热门版块排行榜    

查看: 1558  |  回复: 8
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

zyj8119

木虫 (著名写手)

[交流] 【求助】为什么我编译了一个书本上的无错误的C++程序,却有这么多的错误?

CODE:
#include
#include
#include
using namespace std;
class bint
{
private:
        int n;
        double **a,*b,*x,eps;
        double **p,*r,*e;
public:
        bint(int nn)
        {
                int i;
                n=nn;
                a=new double*[n];
                for(i=0;i                 p=new double*[n];
                for(i=0;i                 r=new double[n];
                e=new double[n];
                b=new double[n];
                x=new double[n];
        }
        void input();
        void a_bint();
        void gauss();
        void output();
        bint()
        {
                int i;
                for(i=0;i                 delete[] a;
                for(i=0;i                 delete[] p;
                delete[] r,e,b,x;
        }
};

void bint::input()
{
        int i,j;
        char str1[20];
        cout<<"\n输入文件名:";
        cin>>str1;
        ifstream fin(str1);
        if(!fin)
        {cout<<"\n不能打开这个文件"<         for(i=0;i                 for(j=0;j>a[i][j];
                for(i=0;i>b[i];
                fin.close();
}

void bint::a_bint()
{
        int i,j,k;
        double q,qq;
        i=100;
        for(k=0;k<=n-1;k++)
                for(j=0;j<=n-1;j++)p[k][j]=a[k][j];
                for(k=0;k<=n-1;k++)r[k]=b[k];
                gauss();
                for(k=0;k                 q=1.0+eps;
                while(q>=eps)
                {
                        if(i==0)return;
                        i=i-1;
                        for(k=0;k                         {
                                e[k]=0.0;
                                for(j=0;j                                         e[k]=e[k]+a[k][j]*x[j];
                        }
                        for(k=0;k<=n-1;k++)r[k]=b[k]-e[k];
                                for(k=0;k<=n-1;k++)
                                        for(j=0;j<=n-1;j++)p[k][j]=a[k][j];
                                        gauss();
                                        q=0.0;
                                        for(k=0;k<=n-1;k++)
                                        {
                                                qq=fabs(r[k])/(1.0+fabs(x[k]+r[k]));
                                                if(qq>q)q=qq;
                                        }
                                        for(k=0;k<=n-1;k++)x[k]=x[k]+r[k];
                }
}
void bint::gauss()
{
        int *js,l,k,i,j,is;
        double d,t;
        js=new int[n];
        l=1;
        for(k=0;k<=n-2;k++)
        {
                d=0.0;
                for(i=k;i<=n-1;i++)
                        for(j=k;j<=n-1;j++)
                        {
                                t=fabs(p[i][j]);
                                if(t>d){d=t;js[k]=j;is=i;}
                        }
                        if(d+1.0==1.0)l=0;
                        else
                        {if(js[k]!=k)
                        for(i=0;i<=n-1;i++)
                        {
                                t=p[i][k];
                                p[i][k]=p[i][js[k]];
                                p[i][js[k]]=t;
                        }
                        if(is!=k)
                        {
                                for(j=k;j<=n-1;j++)
                                {
                                        t=p[k][j];
                                        p[k][j]=p[is][j];
                                        p[is][j]=t;
                                }
                                    t=r[k];r[k]=r[is];r[is]=t;
                        }
                }
                        if(l==0)
                        {
                                delete[] js;
                                cout<<"\n系数矩阵奇异!无解."<                                 return;
                        }
                        d=p[k][k];
                        for(j=k+1;j<=n-1;j++)
                                p[k][j]=p[k][j]/d;
                        r[k]=r[k]/d;
                        for(i=k+1;i<=n-1;i++)
                                p[i][j]=p[i][j]-p[i][k]*p[k][j];
                        r[i]=r[i]-p[i][k]*r[k];
        }
}
            d=p[n-1][n-1];
            if(fabs(d)+1.0==1.0)
                        {
                delete[] js;
                                cout<<"\n系数矩阵奇异!无解."<                             return;
                        }
            r[n-1]=r[n-1]/d;
            for(i=n-2;i>=0;i--)
                        {
                t=0.0;
                for(j=i+1;j<=n-1;j++)
                       t=t+p[i][j]*r[j];
                  r[i]=r[i]-t;
                        }
            js[n-1]=n-1;
          for(k=n-1;k>=0;k--)
          if(js[k]!=k)
                  {
               t=r[k];r[k]=r[js[k]];r[js[k]]=t;
                  }
       delete[] js;
    }

void bint::output()
{
        int i;
        char str2[20];
        cout<<"\n输出文件名:";
        cin>>str2;
        ofstream fout(str2);
        if(!fout)
        {cout<<"\n不能打开这个文件"<         fout<         for(i=0;i         {
                fout<                         cout<         }
        fout<         fout.close();
}

void main()
{
        bint c(4);
        c.input();
        c.a_bint();
        c.output();
}

[ Last edited by zyj8119 on 2010-10-12 at 01:57 ]
回复此楼

» 猜你喜欢

好好学习,天天向上。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zyj8119

木虫 (著名写手)

ajian04:谢谢参与交流~ 2010-10-12 10:08:47
引用回帖:
Originally posted by joy4210 at 2010-10-12 09:50:07:
是徐士良书上的吧
[code]
  //3BINT.CPP
  //求解病态方程组
  #include  
  #include  
  #include  
  using namespace std;
  class  bint
  {
  priv ...

是的,是徐士良的常用算法程序集(C++语言描述)
好好学习,天天向上。
7楼2010-10-12 09:51:53
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 9 个回答

zyj8119

木虫 (著名写手)

ajian04:欢迎交流~ 2010-10-12 08:16:06
错误是:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
bingtai.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(128) : error C2143: syntax error : missing ';' before 'return'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(139) : error C2501: 'd' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(139) : error C2065: 'p' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(139) : error C2065: 'n' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(139) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(139) : error C2109: subscript requires array or pointer type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(140) : error C2143: syntax error : missing ';' before 'if'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(141) : error C2143: syntax error : missing ';' before '{'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(141) : error C2447: missing function header (old-style formal list?)
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(146) : error C2057: expected constant expression
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(146) : error C2466: cannot allocate an array of constant size 0
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(146) : error C2501: 'r' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(146) : error C2440: 'initializing' : cannot convert from 'int' to 'int []'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2143: syntax error : missing ';' before 'for'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2143: syntax error : missing ')' before ';'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2143: syntax error : missing ';' before '>='
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2501: 'i' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2143: syntax error : missing ';' before '>='
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2143: syntax error : missing ';' before '--'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2501: 'i' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2086: 'i' : redefinition
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2143: syntax error : missing ';' before '--'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(147) : error C2059: syntax error : ')'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(148) : error C2143: syntax error : missing ';' before '{'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(148) : error C2447: missing function header (old-style formal list?)
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(154) : error C2057: expected constant expression
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(154) : error C2466: cannot allocate an array of constant size 0
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(154) : error C2501: 'js' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(154) : error C2440: 'initializing' : cannot convert from 'int' to 'int []'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2143: syntax error : missing ';' before 'for'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2143: syntax error : missing ')' before ';'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2143: syntax error : missing ';' before '>='
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2501: 'k' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2143: syntax error : missing ';' before '>='
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2143: syntax error : missing ';' before '--'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2501: 'k' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2086: 'k' : redefinition
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2143: syntax error : missing ';' before '--'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(155) : error C2059: syntax error : ')'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(157) : error C2143: syntax error : missing ';' before '{'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(157) : error C2447: missing function header (old-style formal list?)
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(160) : error C2143: syntax error : missing ';' before 'delete'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(161) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(161) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(161) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(164) : error C2143: syntax error : missing ';' before '{'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(164) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

bingtai.obj - 47 error(s), 0 warning(s)
好好学习,天天向上。
2楼2010-10-12 01:50:50
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zyj8119

木虫 (著名写手)

ajian04:加油~ 2010-10-12 08:16:14
引用回帖:
Originally posted by zyj8119 at 2010-10-12 01:50:50:
错误是:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
bingtai.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\1\bingtai.cpp(128) : error C2143: sy ...

用VC++6.0编译。
好好学习,天天向上。
3楼2010-10-12 01:55:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ajian04

禁虫 (正式写手)

zyj8119(金币+3):大括号的位置有要求吧? 2010-10-12 10:03:41
是你直接抄写的吗?遗漏了很多符号,还有可能是你的编译环境不对,好好看看书上介绍的运行环境吧
4楼2010-10-12 08:15:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 271材料工程求调剂 +8 .6lL 2026-03-18 8/400 2026-03-21 00:58 by JourneyLucky
[考研] 一志愿南昌大学,327分,材料与化工085600 +9 Ncdx123456 2026-03-19 9/450 2026-03-20 23:41 by lovewei0727
[考研] 294求调剂材料与化工专硕 +15 陌の森林 2026-03-18 15/750 2026-03-20 23:28 by JourneyLucky
[考研] 324求调剂 +5 lucky呀呀呀鸭 2026-03-20 5/250 2026-03-20 22:30 by 促天成
[考研] 药学383 求调剂 +3 药学chy 2026-03-15 5/250 2026-03-20 22:11 by 云游重阳
[考研] 中南大学化学学硕337求调剂 +3 niko- 2026-03-19 6/300 2026-03-20 21:58 by luoyongfeng
[考研] A区线材料学调剂 +5 周周无极 2026-03-20 5/250 2026-03-20 21:33 by laoshidan
[考研] 一志愿武理材料工程348求调剂 +3  ̄^ ̄゜汗 2026-03-19 4/200 2026-03-20 21:01 by zhukairuo
[考研] 一志愿北京化工大学0703化学318分,有科研经历,求调剂 +4 一瓶苯甲酸 2026-03-14 4/200 2026-03-20 20:36 by fen_rao
[考研] 260求调剂 +3 朱芷琳 2026-03-20 3/150 2026-03-20 20:35 by 学员8dgXkO
[考研] 一志愿西安交通大学 学硕 354求调剂 +3 我想要读研究生 2026-03-20 3/150 2026-03-20 20:13 by JourneyLucky
[考研] 一志愿吉林大学材料学硕321求调剂 +11 Ymlll 2026-03-18 15/750 2026-03-20 19:40 by 丁丁*
[考研] 工科材料085601 279求调剂 +7 困于星晨 2026-03-17 9/450 2026-03-20 17:38 by 无懈可击111
[考研] 08工学调剂 +5 用户573181 2026-03-20 5/250 2026-03-20 15:47 by xia_2003
[论文投稿] 申请回稿延期一个月,编辑同意了。但系统上的时间没变,给编辑又写邮件了,没回复 10+3 wangf9518 2026-03-17 4/200 2026-03-19 23:55 by babero
[考研] 085601专硕,总分342求调剂,地区不限 +5 share_joy 2026-03-16 5/250 2026-03-18 14:48 by haxia
[考研] 0854,计算机类招收调剂 +3 胡辣汤放糖 2026-03-15 6/300 2026-03-18 12:09 by 上岸上岸……..
[考研] 材料,纺织,生物(0856、0710),化学招生啦 +3 Eember. 2026-03-17 9/450 2026-03-18 10:28 by Eember.
[考研] 中科院材料273求调剂 +4 yzydy 2026-03-15 4/200 2026-03-16 15:59 by Gaodh_82
[考研] 本科南京大学一志愿川大药学327 +3 麦田耕者 2026-03-14 3/150 2026-03-14 20:04 by 外星文明
信息提示
请填处理意见