作者DiLegend (JOU)
看板C_and_CPP
标题[问题]不知为何 会出现乱码
时间Wed Feb 25 00:36:26 2009
我要弄一个依据输入的xyz值做出运算所得到的M值
在让输入的字母进行调换
如 M=3
则 A--->D
程式码如下 但不知为何输出会出现乱码
例如我输入 1 1 8(enter)
f fd
出现 g ge聂x
请按任意键继续
有请高手们进行指导
刚又试了一下
该不会印出字那边改成
cout<<str;
就可以了(我试这样OK)
所以说是没有字的部分出问题是吗
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(void)
{
int x,y,z,i;
float temp;
float M;
char str[50];
cin>>x>>y>>z; //输入x y z 值
cin.get(); //由於按下的一次enter会被读取,若少这行cin.getline会读取
// enter的动作而继续进行後面的程式码
cin.getline(str,50,'\n'); // 输入原始字串
temp=pow(x,1.75)-pow(y,1.75); //先算出决定字母要移几个位的M值的积分部分
M=(float)4/7*temp+(float)8/z; //算出移几个位的M值
for(i=0;i<50;i++)
{if(str[i]>=65&&str[i]<=90) //使用十进位制以判断出是大写的才进行
{str[i]=str[i]+(int)M; // 移位
while(str[i]>=91)
{ str[i]=str[i]-26; //若大於A~Z所被给予的数字范围,则
//不断减少26使其回至范围内
}
while(str[i]<=64) //若小於A~Z所被给予的数字范围,则不
//断增加26使其回至范围内
{ str[i]=str[i]+26;
}
}
else if(str[i]>=97&&str[i]<=122) //使用十进位制以判断出是小写的才进行
{str[i]=str[i]+(int)M; // 移位
while(str[i]>=123)
{str[i]=str[i]-26; //若大於a~Z所被给予的数字范围,则不
// 断减少26使其回至范围内
}
while(str[i]<=96) //若小於a~Z所被给予的数字范围,则不
// 断增加26使其回至范围内
{str[i]=str[i]+26;
}
}
else
continue; //若输入的并非是字母,即非所需做改变
//的intput,便跳回至for回圈处理下一个
}
for(i=0;i<50;i++) //以回圈印出output
{
cout<<str[i];
}
system("pause");
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.45.66.185
※ 编辑: DiLegend 来自: 114.45.66.185 (02/25 00:47)
1F:推 LPH66:你印字的回圈只要印到字串长度即可 02/25 01:03
2F:→ LPH66:<cstring>里有个strlen可以用 02/25 01:04