70,023
社区成员




//问题描述:
//当独立执行此段代码时输出:Glmre
//当和其他代码并列执行时输出:口Glmr
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("hw 3.6\n");
printf("请输入五位数密码:\n");
char c[5]; //这里c[5],下标为c[0]---c[4]
int i;
for(i=0;i<5;i++) //for(i=0;i<6;i++)
{
c[i]=getchar();
}
for(i=0;i<5;i++) //for(i=0;i<6;i++)
{
c[i]=c[i]+4;
//c[5]='\0'; //结束 总共5位的数组,c[5]下标越界了,没有放'\0'结束符的位置了,这是字符数组,
//没必要有结束标志。
putchar(c[i]);
}
putchar('\n');
return 0;
}
//hw 3.6
//请输入五位数密码:
//China (5位)
//Glmre (5位)
//请按任意键继续. . .
#include <math.h>
#include <stdio.h>
int main(void)
{
//以下为并列的代码,不是主要问题
{
printf("hw 3.2\n");
float way,temp;
printf("请输入金额:\n");
scanf("%f",&temp);
printf("请输入存款方式(1-5):\n");
scanf("%f",&way);
if(way==1)
{
temp*=(1+5*0.03);
printf("result= %f \n\n",temp);
}
else if(way==2)
{
temp*=(1+2*0.021);
temp*=(1+3*0.0275);
printf("result= %f \n\n",temp);
}
else if(way==3)
{
temp*=(1+3*0.0275);
temp*=(1+2*0.021);
printf("result= %f \n\n",temp);
}
else if(way==4)
{
temp*=pow(1.015,5);
printf("result= %f \n\n",temp);
}
else
{
temp*=pow(1+0.0035/4,20);
printf("result=%f \n\n",temp);
}
}
getchar();//这里加一句,吸收前面输入过程中留在缓冲区多余的字符。
//以下为提问的代码
{
printf("hw 3.6\n");
printf("请输入五位数密码:\n");
char c[5];
int i;
for(i=0;i<5;i++) //for循环输入多个字符
{
c[i]=getchar(); //将输入的字符赋给变量
}
for(i=0;i<5;i++)
{
c[i]=c[i]+4; //将输入的字符换成后面第四个
//c[5]='\0';
putchar(c[i]);
}
putchar('\n');
}
system("pause");
return 0;
}
//hw 3.2
//请输入金额:
//3.4
//请输入存款方式(1-5):
//5
//result=3.459997
//hw 3.6
//请输入五位数密码:
//China
//Glmre
//请按任意键继续. . .
飞禽走兽卍一2b 等级 #include <math.h> #include <stdio.h> int main(void) { //以下为并列的代码,不是主要问题 { printf("hw 3.2\n"); float way,temp; printf("请输入金额:\n"); scanf("%f",&temp); printf("请输入存款方式(1-5):\n"); scanf("%f",&way); if(way==1) { temp*=(1+5*0.03); printf("result= %f \n\n",temp); } else if(way==2) { temp*=(1+2*0.021); temp*=(1+3*0.0275); printf("result= %f \n\n",temp); } else if(way==3) { temp*=(1+3*0.0275); temp*=(1+2*0.021); printf("result= %f \n\n",temp); } else if(way==4) { temp*=pow(1.015,5); printf("result= %f \n\n",temp); } else { temp*=pow(1+0.0035/4,20); printf("result=%f \n\n",temp); } } //以下为提问的代码 { printf("hw 3.6\n"); printf("请输入五位数密码:\n"); char c[5]; int i; for(i=0;i<5;i++) //for循环输入多个字符 { c[i]=getchar(); //将输入的字符赋给变量 } for(i=0;i<5;i++) { c[i]=c[i]+4; //将输入的字符换成后面第四个 c[5]='\0'; //这里多了对c[5]数组的操作,而且下标为5,下标越界。 putchar(c[i]); } putchar('\n'); } }
当和其他代码并列执行?把代码补全贴上来看看。这里对c[5]数组的操作,下标0-4,或者你不能按字符串的形式输出。
供参考://问题描述: //当独立执行此段代码时输出:Glmre //当和其他代码并列执行时输出:口Glmr #include <math.h> #include <stdio.h> int main(void) { printf("hw 3.6\n"); printf("请输入五位数密码:\n"); char c[5]; //这里c[5],下标为c[0]---c[4] int i; for(i=0;i<5;i++) //for(i=0;i<6;i++) { c[i]=getchar(); } for(i=0;i<5;i++) //for(i=0;i<6;i++) { c[i]=c[i]+4; //c[5]='\0'; //结束 总共5位的数组,c[5]下标越界了,没有放'\0'结束符的位置了,这是字符数组, //没必要有结束标志。 putchar(c[i]); } putchar('\n'); return 0; } //hw 3.6 //请输入五位数密码: //China (5位) //Glmre (5位) //请按任意键继续. . .
供参考://问题描述: //当独立执行此段代码时输出:Glmre //当和其他代码并列执行时输出:口Glmr #include <math.h> #include <stdio.h> int main(void) { printf("hw 3.6\n"); printf("请输入五位数密码:\n"); char c[5]; //这里c[5],下标为c[0]---c[4] int i; for(i=0;i<5;i++) //for(i=0;i<6;i++) { c[i]=getchar(); } for(i=0;i<5;i++) //for(i=0;i<6;i++) { c[i]=c[i]+4; //c[5]='\0'; //结束 总共5位的数组,c[5]下标越界了,没有放'\0'结束符的位置了,这是字符数组, //没必要有结束标志。 putchar(c[i]); } putchar('\n'); return 0; } //hw 3.6 //请输入五位数密码: //China (5位) //Glmre (5位) //请按任意键继续. . .
代码里有输入金额、存款方式等,象我们敲完3.4,接下去就敲一个回车,这个回车其实也被读入的,这就是多余的字符。你可以百度下scanf()和getchar()等几个函数的详细用法,了解下输入输出时的一些细节的东西。