musicbug 发表于 2003-2-10 19:30

很奇怪的错误,我怎么都看不出来,高手指点一把,不胜感激!!!

呵呵,改好了。。。。

//输入10个数字,冒泡排序。
#include<iostream.h>

void main()
{
        int temp=0;
        int n=0,m=0;
        int a,b;
                cout<<"bitte geben Sie 10 Zahlen!:"<<endl;
        for(int i=0;i<10;i++)
        {
                cin>>a;
               
        }

        for(int j=0;j<10;j++)
        {
                for(int i=0;i<9-j;i++)
                {
                        if(a>a)
                        {
                                temp=a;
                                a=a;
                                a=temp;
                        }
                }
        }
       
        for(i=0;i<j-n;i++)
        {
                if(a!=a)
                {
                        b=a;
                        m++;
                }
        }
        cout<<"sortiert: "<<endl;
        for(i=0;i<m;i++)
        {       
                cout<<i<<": ";
                cout<<b<<endl;
        }

/*
for(i=0;i<10;i++)
{
          if(a==a)
                  ;
          else
          {
                cout<<i<<"= ";
                cout<<a<<endl;
          }
}
*/

}

Mephis 发表于 2003-2-12 17:54

#include <stdio.h>
void main ()
{
        int a,i;
        printf("Geben Sie 10 Zahle ein\n");
    for (i=0;i<10;i++)
                scanf("%d",&a);
        int j,t;
    for (i=0;i<10-1;i++)
        {for (j=i+1;j<10;j++)
        {if (a<a)
        {t=a;
   a=a;
   a=t;
        }
        }
        }
        printf("die 10 Zahle in aufsteigender Reihefolge\n");
        for (i=0;i<10;i++)
                printf("%d\n",a);
}

Mephis 发表于 2003-2-12 17:55

oder

#include <stdio.h>
void sort(int a[],int n)
{
        int i,j,t;
    for (i=0;i<n-1;i++)
        {for (j=i+1;j<n;j++)
        {if (a<a)
        {t=a;
   a=a;
   a=t;
        }
        }
        }
}
void main ()
{
        int a,i;
        printf("Geben Sie 10 Zahle ein\n");
    for (i=0;i<10;i++)
                scanf("%d",&a);
    sort (a,10);
        printf("die 10 Zahle in aufsteigender Reihefolge\n");
        for (i=0;i<10;i++)
                printf("%d\n",a);
}

musicbug 发表于 2003-2-12 18:02

你的比我想的要简单一些。
再请教一下。
如果可以得话,如何不计重复的数字? 缩短数组?
谢谢

Mephis 发表于 2003-2-12 18:15

#include <stdio.h>
void main ()
{
int a,i;
printf("Geben Sie 10 Zahle ein\n");
for (i=0;i<10;i++)
scanf("%d",&a);

---------------------------

int j,t;
int x=0;
for (i=0;i<10-1;i++)
{
loop: for (j=i+1;j<10;j++)
{if (a==a)
   {
    j+=j;
    goto loop;
   }
if (a<a)
{t=a;
a=a;
a=t;
x++;
}
}
}
printf("die 10 Zahle in aufsteigender Reihefolge\n");
for (i=0;i<x+1;i++)
printf("%d\n",a);
}

Mephis 发表于 2003-2-12 18:16

guck mal, ob es richtig ist, ich weiss selber auch nicht genau...

Mephis 发表于 2003-2-12 18:20

loop: for (j=i+1;j<10;j++)
{if (a==a)
{
j+=j;               xxxxxxxxxxxxxxxxx
goto loop;
}


hier i+=1;oder?

musicbug 发表于 2003-2-12 18:21

有问题。。。。

Mephis 发表于 2003-2-12 18:24

nein, immer noch falsch

musicbug 发表于 2003-2-12 18:32

偶的想法是这样的。
数组元素a1,a2......a9.
如果a1=a2,那么a3的值赋给a2,依次类推,a9赋给a8,然后打印数组a1到a8。

实现的是出现了一个问题,没有办法解决,如果10个数字都相同,那么到最后一部,a1还是等于a2,,只打印1个a1,就容易错了。

请指教。


void main()
{
        int temp=0;
        int n=0;
        int a;
        for(int i=0;i<10;i++)
        {
                cin>>a;
        }

        for(int j=0;j<10;j++)
        {
                for(int i=0;i<9-j;i++)
                {
                        if(a>a)
                        {
                                temp=a;
                                a=a;
                                a=temp;
                        }
                }
        }

        for(j=0;j<10;j++)       
        for(i=0;i<j-n;i++)
        {
                if(a==a)
                {
                        int m=i;
                        cout<<"J: "<<j<<" i=";
                        cout<<i<<" ";
                        for(i=m;i<8-m;i++)
                        {
                                a=a;
                        }
                        n++;
                        cout<<"J: "<<j<<" i=";
                        cout<<i<<" ";
                        cout<<"n: "<<n<<endl;
                }
        }
        cout<<endl;

//这里没有办法,我只是打印出不重复的数字,而不是改变数组。
for(i=0;i<10;i++)
{
          if(a==a)
                  ;
          else
          {
                cout<<i<<"= ";
                cout<<a<<endl;
          }
}

}

Mephis 发表于 2003-2-12 18:38

Cpp1.exe - 9 error(s), 3 warning(s)

--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
d:\meine eigene dateien\temp\1\cpp1.cpp(8) : error C2065: 'cin' : undeclared identifier
d:\meine eigene dateien\temp\1\cpp1.cpp(8) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
d:\meine eigene dateien\temp\1\cpp1.cpp(30) : error C2065: 'cout' : undeclared identifier
d:\meine eigene dateien\temp\1\cpp1.cpp(30) : error C2297: '<<' : illegal, right operand has type 'char '
d:\meine eigene dateien\temp\1\cpp1.cpp(31) : error C2297: '<<' : illegal, right operand has type 'char '
d:\meine eigene dateien\temp\1\cpp1.cpp(37) : error C2297: '<<' : illegal, right operand has type 'char '
d:\meine eigene dateien\temp\1\cpp1.cpp(38) : error C2297: '<<' : illegal, right operand has type 'char '
d:\meine eigene dateien\temp\1\cpp1.cpp(39) : error C2297: '<<' : illegal, right operand has type 'char '
d:\meine eigene dateien\temp\1\cpp1.cpp(39) : error C2065: 'endl' : undeclared identifier
d:\meine eigene dateien\temp\1\cpp1.cpp(42) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
d:\meine eigene dateien\temp\1\cpp1.cpp(51) : error C2297: '<<' : illegal, right operand has type 'char '
d:\meine eigene dateien\temp\1\cpp1.cpp(52) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
Error executing cl.exe.

Cpp1.exe - 9 error(s), 3 warning(s)

Mephis 发表于 2003-2-12 18:39

让我再看看,再想想

musicbug 发表于 2003-2-12 18:39

呵呵。。
C++

#include<iostream.h>

Mephis 发表于 2003-2-12 18:47

axo,
不好意思没注意

Mephis 发表于 2003-2-12 18:57

不过用C++我解决了,通过试验成功

#include <stdio.h>
void main ()
{
int a,i;
printf("Geben Sie 10 Zahle ein\n");
for (i=0;i<10;i++)
scanf("%d",&a);

int j,t;
int b;
b=0;
int x=0;

for (j=1;j<10;j++)
{
if (a<a)
{t=a;
a=a;
a=t;
}
}

b=a;

for (i=1;i<10-1;i++)
{
for (j=i+1;j<10;j++)
{
if (a<a)
{t=a;
a=a;
a=t;
}
}

if (b!=a)
{
        b=a;
        x+=1;
}
}
printf("die Zahle in aufsteigender Reihefolge\n");
for (i=0;i<x+1;i++)
printf("%d\n",b);
}

musicbug 发表于 2003-2-12 18:59

有错。

Geben Sie 10 Zahle ein
1
1
1
2
2
2
3
3
3
4444
die Zahle in aufsteigende
1
2
3
Press any key to continue

Mephis 发表于 2003-2-12 19:00

先把第一轮下来的最小值a给b

然后每一轮得到的a和b,如果不同则b=a;

到最后就输出b

musicbug 发表于 2003-2-12 19:03

最大的数字总是丢失了。

Mephis 发表于 2003-2-12 19:04

唉呀,这个怪问题我也就不知道了,理论上来说该对的呀

我也请教一个问题
a=1
b=(++a)+(++a)+(++a)
b=?

Mephis 发表于 2003-2-12 19:07

最大的数字总是丢失了。

因为我定义了b的原因吧

但是我定义b[]在C++中不允许

musicbug 发表于 2003-2-12 19:19

b=(++a)+(++a)+(++a)=10


但是我定义b[]在C++中不允许

什么不允许?
i应该从0开始,不是从1。

Mephis 发表于 2003-2-12 19:24

b=(++a)+(++a)+(++a)=10
warum??

但是我定义b[]在C++中不允许
什么不允许?
Keine Ahnung, immer Fehler

i应该从0开始,不是从1。
nein, a wurde schon verglichen und hat schon den kleinste Wert gekriegt.

Mephis 发表于 2003-2-12 19:27

b=(++a)+(++a)+(++a)=10

我理论和实际从无法结合起来

musicbug 发表于 2003-2-12 19:36

sorry,我也有些晕。
算出来是9
可是编译器里一试的确是10,让我想想

Mephis 发表于 2003-2-12 19:43

算出来是9
可是编译器里一试的确是10

对啊,我问了Professor,他也说不知道。。。

如果(++a)+(++a)编译得到答案与计算一样,但是三个以上就不对了。。。

Mephis 发表于 2003-2-12 19:45

komisch

musicbug 发表于 2003-2-12 19:45

正在思索。


        #include<iostream.h>

        void main()
        {
                int i=1,a=0,b=0,c=0;
               
                a=++i;
                cout<<"a: "<<a<<endl;
                i=1;
                b=(++i)+(++i);//这里是6,这是关键。。。
                i=1;
                cout<<"b: "<<b<<endl;
                i=1;
                c=(++i)+(++i)+(++i);
                cout<<"c: "<<c<<endl;
               
        }

a: 2
b: 6
c: 10

Mephis 发表于 2003-2-12 19:48

兄台十么专业的?

musicbug 发表于 2003-2-12 19:58

惭愧。。。。info。。。。

Mephis 发表于 2003-2-12 19:59

为使么惭愧??

我是telekom的
页: [1] 2
查看完整版本: 很奇怪的错误,我怎么都看不出来,高手指点一把,不胜感激!!!