我不能将字符串更改为int。 [英] I cant do change string to int.

查看:82
本文介绍了我不能将字符串更改为int。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char poli [50];

int p;

scanf("%s",poli);

if(poli) [i] ==''X''&& poli [i + 1] ==''^'')

{

toplam = i + 3 ;

p = poli [i + 2];


p是整数的ASCII码,但我需要整数

,例如if poli [i + 2] =''2''它放p 50(ascii代码为''2'')

但我需要p = 2整数形式,我该怎么办,请帮忙

char poli[50];
int p;
scanf("%s", poli);
if(poli[i]==''X'' && poli[i+1]==''^'')
{
toplam=i+3;
p=poli[i+2];

p is a ASCII code of integer but I need integer
for example if poli[i+2]=''2'' it puts p 50 (ascii code of ''2'')
but I need p=2 integer form, how can I do, please help

推荐答案

文章< 11 ******************** *@o80g2000hse.googlegroups。 com>,emre

esirik(hacettepe计算机科学与工程)

< em ******** @ gmail.comwrote于2007年11月11日星期日9:下午16点:
In article <11*********************@o80g2000hse.googlegroups. com>, emre
esirik(hacettepe computer science and engineering)
<em********@gmail.comwrote on Sunday 11 Nov 2007 9:16 pm:

char poli [50];

int p;

scanf("%s" ;,poli);

if(poli [i] ==''X''&& poli [i + 1] ==''^'')

{

toplam = i + 3;

p = poli [i + 2];


p是ASCII代码整数,但我需要整数

例如,如果poli [i + 2] =''2''它将p 50(ascii代码为''2'')

但我需要p = 2整数形式,我该怎么办,请帮助
char poli[50];
int p;
scanf("%s", poli);
if(poli[i]==''X'' && poli[i+1]==''^'')
{
toplam=i+3;
p=poli[i+2];

p is a ASCII code of integer but I need integer
for example if poli[i+2]=''2'' it puts p 50 (ascii code of ''2'')
but I need p=2 integer form, how can I do, please help



做:


p = poli [i +2] - ''0'';


这仅适用于''0''...'''9''。

Do:

p = poli[i+2] - ''0'';

This only works for ''0''... ''9''.


#include< stdio.h>

#include< stdlib.h>

int main()

{

char poli [50],tanpon [6] = {''\ 0''};

int i,d ,l,toplam,polinom [6];

int p;

int z = 0;

scanf("%s", poli);

for(i = 0;我< 50; i ++)

{

if(poli [i] ==''X''&& poli [i + 1] ==''^'')

{

toplam = i + 3;

p = poli [i + 2] - ''0'';

if(z == 0)

{

for(d = 0; d< i; d ++)

{

tanpon [d] = poli [d];

}

polinom [p] = atoi(tanpon);

} else {

tanpon [6] =''\ 0'';

for(l = toplam + 1; l< i; l ++)

{

printf(" toplam%d,l ---->%d",toplam,l);

tanpon [l] = poli [l];

}

polinom [p] = atoi(tanpon);

}

z = 1;

}

}

for(i = 0; i< 6; i ++){

printf("%d",polinom [i]);

}

返回0;

}


这是我的代码,我想输入像这样的3X ^ 1 + 32X ^ 4 + 12X ^ 0但它的

只做部分如果z == 0但为什么它没有别的部分????

#include <stdio.h>
#include<stdlib.h>
int main()
{
char poli[50],tanpon[6]={''\0''};
int i,d,l,toplam,polinom[6];
int p;
int z=0;
scanf("%s", poli);
for(i=0 ; i<50 ; i++)
{
if(poli[i]==''X'' && poli[i+1]==''^'')
{
toplam=i+3;
p = poli[i+2] - ''0'';
if(z==0)
{
for(d=0 ; d<i ; d++)
{
tanpon[d]=poli[d];
}
polinom[p]=atoi(tanpon);
}else {
tanpon[6]=''\0'';
for(l=toplam+1; l<i ; l++)
{
printf("toplam%d,l---->%d",toplam,l);
tanpon[l]=poli[l];
}
polinom[p]=atoi(tanpon);
}
z=1;
}
}
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom[i]);
}
return 0;
}

this is my code, I want to enter like this 3X^1+32X^4+12X^0 but its
only do if part which if z==0 but why it doesnt do ''else'' part????


在艺术中icle< 11 ********************** @ 19g2000hsx.googlegroups。 com>,emre

esirik(hacettepe计算机科学与工程)

< em ******** @ gmail.comwrote于2007年11月11日星期日11: 04 pm:


请引用您所关注文章的相关部分

回复。
In article <11**********************@19g2000hsx.googlegroups. com>, emre
esirik(hacettepe computer science and engineering)
<em********@gmail.comwrote on Sunday 11 Nov 2007 11:04 pm:

Please quote the relevant portions of the article to which you are
replying.

#include< stdio.h>

#include< stdlib.h>

int main()

{

char poli [50],tanpon [6] = {''\ 0''};

int i,d,l,toplam,polinom [6];
#include <stdio.h>
#include<stdlib.h>
int main()
{
char poli[50],tanpon[6]={''\0''};
int i,d,l,toplam,polinom[6];



良好的缩进和明智地使用空格可以增强代码对其他人的可读性。

Good indentation and judicious use of whitespace can enhance the
readability of your code for others.


int p;

int z = 0;

scanf("%s",poli);
int p;
int z=0;
scanf("%s", poli);



scanf()与's''说明符的行为很像gets(),是一种获得输入的危险方式。如果输入包含超过50个
个字符,则scanf()超出''poli''的范围并覆盖它所拥有的内存,即b $ b不拥有,调用未定义的行为。 br />

要读取一行输入,请使用fgets()。


fgets(poli,sizeof poli,stdin);


注意''sizeof poli''只能在函数中按预期工作

,其中''poli''被定义。在其他任何地方你必须明确地将'/ b'大小的''poli''传递给需要它的代码。

scanf() with the ''s'' specifier behaves much like gets() and is a
dangerous way to get input. If the input consists of over 50
characters, scanf() exceed the bounds of ''poli'' and overwrite memory it
does not own, invoking undefined behaviour.

To read a line of input use fgets().

fgets(poli, sizeof poli, stdin);

Note that ''sizeof poli'' only works as expected within the function
where ''poli'' is defined. Anywhere else you must explicitly pass the
size of ''poli'' to code that needs it.


for(i = 0; i< 50; i ++)

{

if(poli [i] ==''X''&& poli [i + 1] =='' ^'')

{

toplam = i + 3;

p = poli [i + 2] - ''0'';

if(z == 0)

{

for(d = 0; d< i; d ++)

{

tanpon [d] = poli [d];

}

polinom [p] = atoi(tanpon);

} else {

tanpon [6] =''\ 0'';

for(l = toplam + 1; l< i; l ++ )

{

printf(" toplam%d,l ---->%d",toplam,l);

tanpon [l] = poli [l];

}

polinom [p] = atoi(tanpon);

}

z = 1;

}

}

for(i = 0; i< 6; i ++){

printf("%d",polinom [i]);

}

返回0;

}
for(i=0 ; i<50 ; i++)
{
if(poli[i]==''X'' && poli[i+1]==''^'')
{
toplam=i+3;
p = poli[i+2] - ''0'';
if(z==0)
{
for(d=0 ; d<i ; d++)
{
tanpon[d]=poli[d];
}
polinom[p]=atoi(tanpon);
}else {
tanpon[6]=''\0'';
for(l=toplam+1; l<i ; l++)
{
printf("toplam%d,l---->%d",toplam,l);
tanpon[l]=poli[l];
}
polinom[p]=atoi(tanpon);
}
z=1;
}
}
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom[i]);
}
return 0;
}



没有缩进你的代码难以辨认。我确定必须有一个更简单的方法来解决你想要做的事情,如果你能拼出来的那么明显。

Without indentation your code is illegible. I''m sure there must be a
much simpler way for what you are trying to do, if you can spell it out
clearly.


这是我的代码,我想输入像这样的3X ^ 1 + 32X ^ 4 + 12X ^ 0但是它的

只做部分如果z = = 0但为什么它不做''其他''部分????
this is my code, I want to enter like this 3X^1+32X^4+12X^0 but its
only do if part which if z==0 but why it doesnt do ''else'' part????



因为''z''始终为零。您将其初始化为零但没有其他代码

更改它,因此始终执行if / else语句的if部分。

Because ''z'' is always zero. You initialise it to zero but no other code
changes it, so the if part of the if/else statement is always executed.


这篇关于我不能将字符串更改为int。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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