CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

初学C++的代码,请大家指点里面的不足之处,谢谢了 !

楼主racewind()2006-07-03 11:46:02 在 C/C++ / C++ 语言 提问

#include   <iostream>  
  #include   <string>  
   
   
  using   namespace   std;  
   
  int   i=1;  
  double   d   =   11.12;  
  float   f   =   123.1;  
  char   c   =   'a';  
  string   s   =   "leelnxxiang";  
  //long   l   =   123.2;  
  //short   sh   =   123123.123;  
  bool   boo   =   1;  
   
  void   for_dir()  
  {  
            for(int   i=0;i<100;i++)  
            {  
                            cout   <<   i;  
                            cout   <<   endl;  
            }  
  }  
   
  void   arrays()  
  {  
            int   id[100];  
            for(int   i=0;i<100;i++)  
            {  
                            id[i]   =   i;  
                            cout   <<   "id["   <<   id[i]   <<   "]";  
                            cout   <<   endl;  
            }  
  }  
   
  void   arrays_char()  
  {  
            cout   <<   "请输入您的名字................................";    
            cout   <<   endl;  
            char   ch[11];  
            string   str   =   ""   ;  
             
            for(int   i=0;i<11;i++)  
            {  
                            cin   >>   ch[i];  
                            str   +=   ch[i];  
            }  
            cout   <<"您输入的是:   "   <<     str;  
  }  
   
  void   while_dir()  
  {  
            int   x=1;  
            while(true)  
            {  
                            cout   <<   endl;  
                            cout     <<   "while     ing   ............"   <<   x;  
                            x++;  
                            if(x   ==   33)  
                            {  
                                      return;  
                            }  
            }  
  }    
   
  void   do_dir()  
  {  
            int   x   =   1;  
            do  
            {  
                            cout   <<   endl;  
                            cout     <<   "do   while     ing   ............"   <<   x;  
                            x++;  
                            if(x   ==   33)  
                            {  
                                      return;  
                            }  
            }  
            while(true);  
  }  
  void   exit_exit()  
  {  
          cout   <<   "退出程序请输入   'exit'   ...........";  
          cout   <<   endl;  
          string   s   =   "";  
          cin   >>   s;  
          if(s   ==   "exit")  
          {  
                  cout   <<   "保存退出程序成功!";    
                  exit(0);  
          }    
          else  
          {  
                  cout   <<   "输入错误,程序将不保存运行";  
          }  
  }  
   
   
  void   display()  
  {  
            cout   <<   i;cout   <<   endl;  
            cout   <<   d;cout   <<   endl;  
            cout   <<   f;cout   <<   endl;  
            cout   <<   c;cout   <<   endl;  
            cout   <<   s;cout   <<   endl;  
            //cout   <<   l;cout   <<   endl;  
            //cout   <<   sh;cout   <<   endl;  
            cout   <<   boo;cout   <<   endl;  
  }  
   
   
  void   Cf()  
  {  
            for(int   i=1;i<10;i++)  
            {  
                            for(int   j=1;i>=j;j++)  
                            {  
                                            cout   <<   i   <<   "*"   <<   j   <<   "="   <<   i*j   <<   "     ";  
                            }  
                            cout   <<   endl;  
            }  
  }  
  int   main()  
  {  
          cout   <<   "hello   world";  
          cout   <<   endl;  
           
          cout   <<   "for   .....................";  
          cout   <<endl;  
          for_dir();  
           
          cout   <<   endl;  
          cout   <<   "while   ...................";  
          while_dir();  
           
          cout   <<   endl;  
          cout   <<   "do   ..................................";  
          do_dir();  
           
          cout   <<   endl;  
          printf("显示乘法口诀..................");  
          cout   <<   endl;  
          Cf();    
          printf("显示数据类型..................");  
          cout   <<   endl;  
          display();  
           
          cout   <<   endl;  
          arrays();  
           
          arrays_char();  
           
          cout   <<   endl;  
          exit_exit();  
  }  
  问题点数:20、回复次数:9Top

1 楼racewind()回复于 2006-07-03 11:46:51 得分 0

对了,C++里有没有long和short的类型?为什么我用++vdev-c++编译器有时候会编译出long和short的错误那?Top

2 楼rollor_phoe(柔枫)回复于 2006-07-03 12:04:53 得分 0

void   arrays_char()  
  {  
            cout   <<   "请输入您的名字................................";    
            cout   <<   endl;  
            char   ch[11];  
            string   str   =   ""   ;  
             
            for(int   i=0;i<11;i++)  
            {  
                            cin   >>   ch[i];  
                            str   +=   ch[i];  
            }  
            cout   <<"您输入的是:   "   <<     str;  
  --------------------------------------------  
  这个函数改成:  
  void   arrays_char()  
  {  
            cout   <<   "请输入您的名字................................";    
            cout   <<   endl;  
             
            string   str   =   ""   ;  
            getline(cin,str);//这样可以输入空格,它是接改一行数据,以换行符分隔  
             
            cout   <<"您输入的是:   "   <<     str;  
  ---------------------------------------------------  
  C++中有long和short类型。  
  Top

3 楼Veiz(理论上存在)回复于 2006-07-03 12:25:36 得分 0

不要用   float   这种数据类型,   就当它不存在行了。Top

4 楼Veiz(理论上存在)回复于 2006-07-03 12:47:40 得分 0

string   s   =   "leelnxxiang";   改为   const   string   s   =   "leelnxxiang";   其他类似  
  能const   就尽量     const  
  少用全局变量。注意缩小变量作用域。  
  没有必要的话不要用printf,   用cout  
  int   id[100]   =   {0};     //要初始化id  
  char   ch[11]   =   {0};   //     这里也要初始化  
  一行中不宜出现两个以上的分号,   这是程序维护的需要  
  多加空格是好的。  
  endl会清空流缓冲区,没有必要的话用   "\n"   行了  
  写++i,   尽量不写i++  
  按照单一职责原则,一个函数应只完成一项功能。Top

5 楼Veiz(理论上存在)回复于 2006-07-03 12:53:36 得分 0

dev   c++   不是编译器,  
  你可以输出   sizeof(short)   和sizeof(long)   ,看看它们的长度Top

6 楼racewind()回复于 2006-07-03 13:16:46 得分 0

谢谢各位大侠!我现在脑子里总是有java的印象而且很深刻,如果不是要开发基于PPC2003的东西我可能就不学c++了,呵呵Top

7 楼racewind()回复于 2006-07-03 13:30:05 得分 0

受教,修改如下:#include   <iostream>  
  #include   <string>  
  #include   <math.h>  
   
  using   namespace   std;  
   
  int   i=1;  
  double   d   =   11.12;  
  float   f   =   123.1;  
  char   c   =   'a';  
  const   string   s   =   "leelnxxiang";  
  //long   l   =   123.2;  
  //short   sh   =   123123.123;  
  bool   boo   =   1;  
   
  static   const   string   static_string   =   "javasoft";  
   
  void   for_dir()  
  {  
            for(int   i=0;i<100;i++)  
            {  
                            cout   <<   i;  
                            cout   <<   "n";  
            }  
  }  
   
  void   arrays()  
  {  
            int   id[100]   =   {0};  
            for(int   i=0;i<100;++i)  
            {  
                            id[i]   =   i;  
                            cout   <<   "id["   <<   id[i]   <<   "]";  
                            cout   <<   "\n";  
            }  
  }  
   
  void   arrays_char()  
  {  
            cout   <<   "请输入您的名字................................";    
            cout   <<   "\n";  
            char   ch[11];  
            string   str   =   ""   ;  
             
            getline(cin,str);  
            cout   <<"您输入的是:   "   <<     str;  
  }  
   
  void   while_dir()  
  {  
            int   x=1;  
            while(true)  
            {  
                            cout   <<   "\n";  
                            cout     <<   "while     ing   ............"   <<   x;  
                            x++;  
                            if(x   ==   33)  
                            {  
                                      return;  
                            }  
            }  
  }    
   
  void   do_dir()  
  {  
            int   x   =   1;  
            do  
            {  
                            cout   <<   "\n";  
                            cout     <<   "do   while     ing   ............"   <<   x;  
                            x++;  
                            if(x   ==   33)  
                            {  
                                      return;  
                            }  
            }  
            while(true);  
  }  
  void   exit_exit()  
  {  
          cout   <<   "退出程序请输入   'exit'   ...........";  
          cout   <<   "\n";  
          string   s   =   "";  
          cin   >>   s;  
          if(s   ==   "exit")  
          {  
                  cout   <<   "保存退出程序成功!";    
                  exit(0);  
          }    
          else  
          {  
                  cout   <<   "输入错误,程序将不保存运行";  
          }  
  }  
   
   
  void   display()  
  {  
            cout   <<   i;cout   <<   "\n";  
            cout   <<   d;cout   <<   "\n";  
            cout   <<   f;cout   <<   "\n";  
            cout   <<   c;cout   <<   "\n";  
            cout   <<   s;cout   <<   "\n";  
            //cout   <<   l;cout   <<   "\n";  
            //cout   <<   sh;cout   <<   "\n";  
            cout   <<   boo;cout   <<   "\n";  
  }  
   
   
  void   Cf()  
  {  
            for(int   i=1;i<10;++i)  
            {  
                            for(int   j=1;i>=j;j++)  
                            {  
                                            cout   <<   i   <<   "*"   <<   j   <<   "="   <<   i*j   <<   "     ";  
                            }  
                            cout   <<   "\n";  
            }  
  }  
  int   main()  
  {  
          cout   <<   "hello   world";  
          cout   <<   "\n";  
           
          cout   <<   "for   .....................";  
          cout   <<endl;  
          for_dir();  
           
          cout   <<   "\n";  
          cout   <<   "while   ...................";  
          while_dir();  
           
          cout   <<   "\n";  
          cout   <<   "do   ..................................";  
          do_dir();  
           
          cout   <<   "\n";  
          printf("显示乘法口诀..................");  
          cout   <<   "\n";  
          Cf();    
          printf("显示数据类型..................");  
          cout   <<   "\n";  
          display();  
           
          cout   <<   "\n";  
          arrays();  
           
          arrays_char();  
           
          cout   <<   "\n";  
   
          cout   <<   static_string;  
           
          cout   <<   "\n";  
          exit_exit();  
  }  
  Top

8 楼Veiz(理论上存在)回复于 2006-07-03 17:07:57 得分 0

cout     <<   "abcdefg"   <<   endl;      
  cout     <<   "abcdefg\n"   ;  
  可以连在一起写的。Top

9 楼racewind()回复于 2006-07-03 17:15:52 得分 0

哦,知道了,语法和java差好多来,呵呵Top

相关问题

关键词

得分解答快速导航

  • 帖主:racewind

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo