有人可以告诉我这个程序有什么问题吗? [英] Can some one tell me what is wrong with this program ?

查看:59
本文介绍了有人可以告诉我这个程序有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< stdlib.h>

struct point


{


双x,y,z;


};


typedef struct point point;


结构三角形

{


点x,y,z;


};


typedef结构三角形;


int main(无效)


{


三角形* T;


T = malloc(sizeof(三角形));

printf(输入三角形的顶点\ n);

printf(" First vertex:\ n");

scanf(" %lf%lf%lf",&(T-> xx),&(T-> xy),&(T-> xz));

printf(" ;第二个顶点:\ n");

scanf("%lf%lf%lf",&(T-> yx),&(T-> yy), &(T-> yz));

printf(" Third vertex:\ n");

scanf("%lf%lf%lf" ;,&(T-> zx),&(T-> z .y),&(T-> zz));

printf(" Vertex 1:%lf \ t%lf \ t%lf \ n,T-> ; xx,T-> xy,T-> xz);

printf(" Vertex 2:%lf \ t%lf \ t%lf \ n,T- > yx,T-> yy,T-> yz);

printf(" Vertex 3:%lf \ t%lf \ t%lf \ n,T - > zx,T-> zy,T-> zz);


返回0;


}


我在输出中得到一些垃圾值。



解决方案

broli说:


[主题:有人可以告诉我这个程序有什么问题吗?]


< snipSo很好。


int main(无效)


{


triangle * T;


T = malloc(sizeof(triangle));



检查是否成功。如果没有,T将是一个空指针。


if(T!= NULL)

{


(并且,如果它/是一个空指针,你的''else''至少应该报告缺少内存。


printf("输入三角形的顶点\ n);

printf(" First vertex:\ n");

scanf(%lf%lf%lf,&(T-> xx),&(T-> xy),&(T-> xz));



检查scanf是否返回3(因为你要求转换三个字段

)。如果没有,则输入数据出错。


printf(" Second vertex:\ n");

scanf (%lf%lf%lf,&(T-> yx),&(T-> yy),&(T-> yz));

printf(Third vertex:\ n);

scanf("%lf%lf%lf"&(T-> zx),&(T-> zy),&(T-> zz));



同样适用于这些。


printf(" Vertex 1:%lf \ t% lf \t%lf \ n,T-> xx,T-> xy,T-> xz);



与scanf不同,printf在格式说明符中不需要''l''。 %f是足够双打的
。事实上,除非你有幸拥有一个

C99编译器,否则%lf实际上是不正确的。使用%f代替,因为它在C90和C99中都是正确的。


我在输出中得到一些垃圾值。



除非你的内存不足(在这种情况下不太可能)或

提供不良输入,你唯一的理由应该是由于%lf的原因,得到破碎的输出是

。即便如此,一些实现(例如我用来测试你的代码的那个
)非常高兴地接受%lf并且做你想要的b $ b期望 - 即打印双倍。尽管如此,将每个printf中的每个%lf更改为
到%f(但将scanf单独留下)并重新测试。


告诉我们如何你继续。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


3月12日晚上8:21,Richard Heathfield< r ... @ see.sig.invalidwrote:


broli说:


[主题:有人可以告诉我这个程序有什么问题吗?]


< snipSo far,很好。


int main(void)


{


triangle * T;


T = malloc(sizeof(triangle));



检查一下是否成功。如果没有,T将是一个空指针。


if(T!= NULL)

{


(并且,如果它/是一个空指针,你的''else''至少应该报告缺少内存。


printf("输入三角形的顶点\ n);

printf(" First vertex:\ n");

scanf(%lf%lf%lf,&(T-> xx),&(T-> xy),&(T-> xz));



检查scanf是否返回3(因为你要求转换三个字段

)。如果没有,则输入数据出错。


printf(" Second vertex:\ n");

scanf (%lf%lf%lf,&(T-> yx),&(T-> yy),&(T-> yz));

printf(Third vertex:\ n);

scanf("%lf%lf%lf"&(T-> zx),&(T-> zy),&(T-> zz));



同样适用于这些。


printf(" Vertex 1:%lf \ t% lf \t%lf \ n,T-> xx,T-> xy,T-> xz);



与scanf不同,printf在格式说明符中不需要''l''。 %f是足够双打的
。事实上,除非你有幸拥有一个

C99编译器,否则%lf实际上是不正确的。使用%f代替,因为它在C90和C99中都是正确的。


我在输出中得到一些垃圾值。



除非你的内存不足(在这种情况下不太可能)或

提供不良输入,你唯一的理由是由于%lf的原因,得到破碎的输出是

。即便如此,一些实现(例如我用来测试你的代码的那个
)非常高兴地接受%lf并且做你想要的b $ b期望 - 即打印双倍。尽管如此,将每个printf中的每个%lf更改为
到%f(但将scanf单独留下)并重新测试。


告诉我们如何你继续。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日



奇怪的是,我仍然没有得到它。这是我使用的输入 -


第一顶点:

23 34 44

第二顶点:

33 55 66

第三顶点:

-77 88 99


/ *更新* /


#include< stdio.h>

#include< stdlib.h>

struct point


{


双x,y,z;


};


typedef struct point point;


struct triangle


{


point x,y ,z;


};


typedef结构三角形三角形;


int main(void )


{


三角形* T;

T = malloc(sizeof(三角形));

if(T == NULL)

printf(低内存\ n);

其他

{

printf("输入三角形的顶点\ n);;

printf(" First vertext:\ n");

if(scanf("%lf%lf%lf&q uot;,&(T-> xx),&(T-> xy),&(T-> xz))!= 3)

printf(" error \ n");

printf(" Second vertex:\ n");

if(scanf("%lf%lf%lf",&) ;(T-> yx),&(T-> yy),&(T-> yz))!= 3)

printf(" error \ n" );

printf(" Third vertex:\ n");

if(scanf("%lf%lf%lf",&(T-) > zx),&(T-> zy),&(T-> zz))!= 3);

printf(" error \ n");

printf(Vertex 1:%f \ t%f \ t%f \ n,T-> xx,T-> xy,T-> xz);

printf(Vertex 2:%f \ t%f \ t%f \ n,T-> yx,T-> yy,T-> yz) ;

printf(Vertex 3:%f \ t%f \ t%f \ n,T-> zx,T-> zy,T-> zz );


}


返回0;


}

#include<stdio.h>
#include<stdlib.h>
struct point

{

double x, y, z;

};

typedef struct point point;

struct triangle

{

point x,y, z;

};

typedef struct triangle triangle;

int main(void)

{

triangle *T;

T = malloc(sizeof(triangle));
printf("Enter the vertices of the triangle\n");
printf("First vertex:\n");
scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z));
printf("Second vertex:\n");
scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z));
printf("Third vertex:\n");
scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z));
printf("Vertex 1: %lf\t%lf\t%lf\n", T->x.x, T->x.y, T->x.z);
printf("Vertex 2: %lf\t%lf\t%lf\n", T->y.x, T->y.y, T->y.z);
printf("Vertex 3: %lf\t%lf\t%lf\n", T->z.x, T->z.y, T->z.z);

return 0;

}

I''m getting some garbage values in the output.



解决方案

broli said:

[Subject: Can some one tell me what is wrong with this program ?]

<snipSo far, so good.

int main(void)

{

triangle *T;

T = malloc(sizeof(triangle));

Check that it succeeded. If not, T will be a null pointer.

if(T != NULL)
{

(and, if it /is/ a null pointer, your ''else'' should, at the very least,
report the lack of memory.)

printf("Enter the vertices of the triangle\n");
printf("First vertex:\n");
scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z));

Check that scanf returns 3 (because you are asking for three fields to be
converted). If not, there was an error in the input data.

printf("Second vertex:\n");
scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z));
printf("Third vertex:\n");
scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z));

Same applies to these.

printf("Vertex 1: %lf\t%lf\t%lf\n", T->x.x, T->x.y, T->x.z);

Unlike scanf, printf does not require an ''l'' in the format specifier. %f is
sufficient for doubles. In fact, unless you are fortunate enough to have a
C99 compiler, %lf is actually incorrect. Use %f instead, since it is
correct in both C90 and C99.

I''m getting some garbage values in the output.

Unless you''re running out of memory (deeply unlikely in this case) or
providing bad input, the only reason you should get broken output is
because of the %lf thing. Even then, some implementations (such as the one
I used for testing your code) accept %lf quite happily and do what you
expect - i.e. print a double. Nevertheless, change each %lf in each printf
to %f (but leave the scanf ones alone), and re-test.

Let us know how you get on.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Mar 12, 8:21 pm, Richard Heathfield <r...@see.sig.invalidwrote:

broli said:

[Subject: Can some one tell me what is wrong with this program ?]

<snipSo far, so good.

int main(void)

{

triangle *T;

T = malloc(sizeof(triangle));


Check that it succeeded. If not, T will be a null pointer.

if(T != NULL)
{

(and, if it /is/ a null pointer, your ''else'' should, at the very least,
report the lack of memory.)

printf("Enter the vertices of the triangle\n");
printf("First vertex:\n");
scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z));


Check that scanf returns 3 (because you are asking for three fields to be
converted). If not, there was an error in the input data.

printf("Second vertex:\n");
scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z));
printf("Third vertex:\n");
scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z));


Same applies to these.

printf("Vertex 1: %lf\t%lf\t%lf\n", T->x.x, T->x.y, T->x.z);


Unlike scanf, printf does not require an ''l'' in the format specifier. %f is
sufficient for doubles. In fact, unless you are fortunate enough to have a
C99 compiler, %lf is actually incorrect. Use %f instead, since it is
correct in both C90 and C99.

I''m getting some garbage values in the output.


Unless you''re running out of memory (deeply unlikely in this case) or
providing bad input, the only reason you should get broken output is
because of the %lf thing. Even then, some implementations (such as the one
I used for testing your code) accept %lf quite happily and do what you
expect - i.e. print a double. Nevertheless, change each %lf in each printf
to %f (but leave the scanf ones alone), and re-test.

Let us know how you get on.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Strangely, Im still not getting it. Here''s the input I used -

First Vertex:
23 34 44
Second Vertex:
33 55 66
Third Vertex:
-77 88 99


/* UPDATED */

#include<stdio.h>
#include<stdlib.h>
struct point

{

double x, y, z;

};

typedef struct point point;

struct triangle

{

point x,y, z;

};

typedef struct triangle triangle;

int main(void)

{

triangle *T;
T = malloc(sizeof(triangle));
if(T==NULL)
printf("Low memory\n");
else
{
printf("Enter the vertices of the triangle\n");
printf("First vertext:\n");
if(scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z))!=3)
printf("error\n");
printf("Second vertex:\n");
if(scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z))!=3)
printf("error\n");
printf("Third vertex:\n");
if(scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z))!=3);
printf("error\n");
printf("Vertex 1: %f\t%f\t%f\n", T->x.x, T->x.y, T->x.z);
printf("Vertex 2: %f\t%f\t%f\n", T->y.x, T->y.y, T->y.z);
printf("Vertex 3: %f\t%f\t%f\n", T->z.x, T->z.y, T->z.z);

}

return 0;

}


这篇关于有人可以告诉我这个程序有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆