注册 登录
编程论坛 C++教室

一个关于语法的问题!谢谢

独孤小梦 发布于 2008-08-31 12:56, 1587 次点击
先谢谢大家能看本人的帖,问题如下:
先看个程序:
#include<iostream>
using namespace std;
main(void)
{
    int xuhuan,xuhuan1,xuhuan2;
    char shuruwenzi[30],bh[30],zimu[]={"0ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    for(xuhuan=0;xuhuan<27;xuhuan++)
        bh[xuhuan]=0;
    cin>>shuruwenzi;
    for(xuhuan=0;xuhuan!='\0';xuhuan++)
        for(xuhuan1=1;xuhuan1<27;xuhuan1++)
            if(shuruwenzi[xuhuan]==zimu[xuhuan1])
                bh[xuhuan]=zimu[26-xuhuan1+1];
    cout<<bh;
}

我这里本来想把输入的字母转为第(26-i+1)个字母,可是这里算法我改了很多次,就是不知道哪里错了!得不出自己想要的结果!请大家给我看看,谢谢了!
12 回复
#2
raulxxyuer2008-08-31 15:10
比较这句.
    for(xuhuan=0;shuruwenzi[xuhuan]!='\0';xuhuan++)

我想说:变量命名,最好用匈牙利命名法.这样的程序看得别扭.
还有你的程序有内存越界的问题.如果输入的字符超过你定义的数组shuruwenzi[30]的范围,就会崩溃.
#3
独孤小梦2008-09-01 08:12
先谢谢LS的解答!问题如下:
恩,我现在在考虑字母怎么转换的问题!但是改了那个循环里的表达式还是得不出自己想要的结果!
程序虽然能编译,但是编译的时候有俩个警告!
--------------------Configuration: min - Win32 Debug--------------------
Compiling...
min.cpp
C:\Documents and Settings\Administrator\桌面\c\min\min.cpp(15) : warning C4508: 'main' : function should return a value; 'void' return type assumed
C:\Documents and Settings\Administrator\桌面\c\min\min.cpp(5) : warning C4101: 'xuhuan2' : unreferenced local variable
Linking...

min.exe - 0 error(s), 2 warning(s)

请问怎么回事?大家帮帮忙啊!谢谢了!关于命名法,谢谢了啊,我新手,一开始还不会……呵呵!
#4
ting47632008-09-01 09:47
第一个警告是说main函数应该有个返回值,一般情况下都是返回void类型的,
void main(){
}

第二个警告是说“xuhuan2”这个变量定义了,但是没有用到,你可以把它去掉
#5
独孤小梦2008-09-01 09:52
谢谢大家的帮助,问题依然没有解决!
我想了很久!也不知道要怎么转换这个字母……
#6
lkj7892008-09-01 09:52
有我鸟事!!!














" border="0" />
[url=http://www.]盘龙[/url]
#7
独孤小梦2008-09-02 14:55
大家帮帮忙啊!谢谢了!
#8
ml2325282008-09-02 19:32
main()函数应该这样写

int _tmain(int argc, _TCHAR* argv[])

或者

int main()
#9
ml2325282008-09-02 19:35
for(xuhuan=0;xuhuan!='\0';xuhuan++)肯定不对
#10
独孤小梦2008-09-06 16:32
请问那要怎么改呢?
#11
守鹤2008-09-06 20:54
问题如下:
1、main的声明    应为  void  main()


2、数据交换的问题
  显示的是26-i+1的字符,我认为应这样写:
//输入的数据为英文字符
shuruwenzi=0;
while(shuruwenzi[xuhuan]!='\0')
      for(xuhuan1=1;xuhuan1<27;xuhuan1++)
            if(shuruwenzi[xuhuan]==zimu[xuhuan1])
             {   bh[xuhuan]=zimu[26-xuhuan1+1];
                xuhuan++;
                break;
              }
#12
独孤小梦2008-09-08 09:13
谢谢LS的朋友!你这个程序我试过!虽然还没过,但是得出来一些结论!
#include<iostream>
using namespace std;
void main()
{
    int xuhuan,xuhuan1;
    char shuruwenzi[30],bh[30],zimu[]={"0ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    for(xuhuan=0;xuhuan<27;xuhuan++)
        bh[xuhuan]=0;
    for(xuhuan=0;xuhuan<30;xuhuan++)
        shuruwenzi[xuhuan]=0;
    xuhuan=0;
    cin>>shuruwenzi;
    while(shuruwenzi[xuhuan]!='\0')
      for(xuhuan1=1;xuhuan1<27;xuhuan1++)
            if(shuruwenzi[xuhuan]==zimu[xuhuan1])
             {   bh[xuhuan]=zimu[26-xuhuan1+1];
                xuhuan++;
              }
    cout<<bh<<"\n";
    cout<<shuruwenzi<<"\n";
    cout<<zimu<<"\n";
}
我考虑到了输入时大小写的问题!还有xuhuan,shuruwenzi这两个变量的值的问题!
但是问题依然存在,谢谢回复帖的朋友们!我继续在考虑下别的可能!
#13
lonmaor2008-09-08 10:04
个人看法:
1.判断输入合法性
 i)输入字母->处理函数
 ii)输入非法字符->结束 or 重新输入
2.输入字符以char型读取为佳:如字母'c',在26个子母中的排序为 i='c'-'a'+1
代入所要求的表达式 26-i+1 既得所求char。

ps: int main()与 void main(),去baidu一下,有结果的。
1