有时候,当我孤独地坐着等待生命大门关闭时,一种与世隔绝的感觉就会像冷雾一样笼罩着我。远处有光明、音乐和友谊,但我进不去,命运之神无情地挡住了大门。我真想义正词严地提出抗议,因为我的心仍然充满了热情。但是那些酸楚而无益的话语流溢在唇边,欲言又止,犹如泪水往肚里流,沉默浸透了我的灵魂。然后,希望之神微笑着走来对我轻轻耳语说:“忘我就是快乐。”因而我要把别人眼睛所看见的光明当作我的太阳,别人耳朵所听见的音乐当作我的乐曲,别人嘴角的微笑当作我的快乐。

发布新日志

  • c++程序学习7

    2009-07-16 11:13:59

     

    (a) int ival = 1.01; 是否合法?合法,把double 型子面值腹给int 型变量,发生隐性类型变幻,ival 得到的值为1

    习题 2.15 下面两个定义是否同?有何不同?

         int month = 9,day = 7;

              int month = 09,day = 07;

    如果上述定义有错的话,那么应该怎么更正呢?

    第一个定义了2个10进制的数,分别是9 和 7;

    第二个定义了一个07,是八进制的数,还有一个是09 ,这个在8进制中是不存在的。

    修改成011 ,这个数字转换成十进制的时候,为1 * 80 = 1  1 * 81 =8 ,加起来就是9

     

    八进制就是逢8进1。

    八进制数采用 0~7这八数来表达一个数。

    八进制数第0位的权值为8的0次方,第1位权值为8的1次方,第2位权值为8的2次方……

    所以,设有一个八进制数:1507,转换为十进制为:

    用竖式表示:

     

    1507换算成十进制。

     

    第0位 7 * 80 = 7

    第1位 0 * 81 = 0

    第2位 5 * 82 = 320

    第3位 1 * 83 = 512   +

    --------------------------

                  839

    同样,我们也可以用横式直接计算:

    7 * 80 + 0 * 81 + 5 * 82 + 1 * 83 = 839

    结果是,八进制数 1507 转换成十进制数为 839

     

    习题 2.16 假设calc是一个返回double对象的函数。下面哪些是非法定义?改正所有的非法定义。

    (a) int car = 1024,auto = 2048;

    (b) int ival = ival;

    (c) std::cin >> int input_value;

    (d) double salary = wage = 999.99;

    (e) double calc = calc();

     

    a 非法。auto 是关键字,不可以使用。

    c 》 运算符后不能进行变量定义

    d 同一语句中不同的变量初始化应该分开定义:

    double salary = 999.99

    wage = 999.99;

    b 没有语法错误,但是ival 还是没有数值。

     

    习题 2.17 下列变量的初始值(如果有)是什么?

    std::string global_str;

    int global_int;

    int main()

    {

        int local_int;

        std::string local_str;

        // …

        return 0;

    }

     

    global_str 和 local_str 是空字符串,global_int 是0

    local_int 没有初始值。

     

    习题 2.18 解释下列例子中name的意义:

    extern std::string name;

    std::string name(“exercise 3.5a”);

    extern std::string name(“exercise 3.5a”);

     

    声明std::string 变量name在其他地方定义;

    定义std::string 变量name ,并初始化为exercise 3.5a;

    定义std::string 变量name ,并初始化为exercise 3.5a,name 是一个全局变量;

     

    习题 2.19 下列程序中j的值是多少?

    int i = 42;

    int main()

    {

        int i = 100;

        int j = i;

        //…

    }

    解答:局部变量i=100屏蔽了全局变量i=42,j的值是100

     

    习题 2.20 下列程序段将会输出什么?

    int i = 100,sum = 0;

    for(int i =0; i!= 10; ++i)

        sum += i;

    std::cout << i << " " << sum << std::endl;

    解答:for语句中定义的变量i在退出for循环后就不可见,最后输出的I 值仍然是100For循环改变了sum的值,输出从09的和45。所以输出结果为100 45

     

    习题 2.21 下列程序合法吗?

    int sum = 0;

    for(int i =0; i!= 10; ++i)

        sum += i;

    std::cout << "Sum from 0 to " << i << " is " << sum << std::endl;

    因为i的作用域在语句中,输出i不合法。

     

    习题 2.22 下列程序虽然合法,但是风格很糟糕。有什么问题呢?怎样改善?

    for(int i = 0; i < 100; ++i )

        // process i

    问题是程序的可读性差,100的具体含义不清楚;程序的可维护性差。无法修改

    因为出现魔数,修改为const int a=100

    在i< 100 中修改为 i < a ;

     

    习题 2.23 下列哪些语句合法?对于那些不合法的,请解释为什么不合法?

    (a) const int buf;

    (b) int cnt = 0;

    const int sz = cnt;

    (c) cnt ++; sz ++;

     

    a 定义const 变量(常量)时必须进行初始化,而buf 没有;

    b 合法

    c sz 是常量,无法修改数值。

     

     

    习题 2.24 下列哪些定义是非法的?为什么?如何改正?

    (a) int ival = 1.01;     (b) int &rval1 = 1.01;

    (c) int &rval2 = ival;   (d) const int &rval3 = 1;

     

    b 引用必须用与该引用同类型的对象初始化。int &rval1 =ival;

     

    习题 2.25 在上题给出的定义下,下列哪些赋值是非法的?如果赋值合法,解释赋值的作用。

    (a) rval2 = 3.14159;     (b) rval2 = rval3;

    (c) ival = rval3;        (d) rval3 = ival;

     

    d  rval3是一个const 引用,不能进行赋值。

     

     

     

    习题 2.26 (a)中的定义和(b)中的赋值存在哪些不同?哪些是非法的?

    (a) <

  • c++程序学习记录6

    2009-07-08 10:42:17

     

    问题1 如何计算求模?

    100000超过了16位的unsigned short类型的表示范围,编译器对其二进制表示截取低16位,相当于对65536求余(求模,%),得34464。

    问题2

    (a) "Who goes with F\145rgus?\012" 个人认为非法,此处的问号是啥意思?

    习题2.1 int long 和short 类型之间有什么差别?

    都表示整形值,存储空间不同。short 定义半个机器字,int 和long 定义一个机器字长,也就是4个字节,32位。

    习题2.2 整形数包含带符号的signed 和 无符号的unsigned。前者可以定义正数,负数和O ,后者可以定义0和正数。

    习题2.3

    如果在某机器上short类型占16位,那么可以赋给short类型的最大数是什么?unsigned short类型的最大数又是什么?

    short 默认是signed 类型,为 215-1=32767 ,也就是范围从-32767 ~32768

    unsigned 类型只能是正数,最大数为216-1=65535 。也就是范围从0~65535

    习题2.4

    当给16位的unsigned short对象赋值100000时,赋的值是什么?

    16位的unsigned 类型是范围是0~65535 ,赋值为100000时,产生溢出。

    100000超过了16位的unsigned short类型的表示范围,编译器对其二进制表示截取低16位,相当于对65536求余(求模,%),得34464。

    习题2.5

    float类型和double类型有什么差别?

    对于float类型来说,存储位数不同,f存储32位, double 存储64位。

    所以取值范围不同,精度不同。f 表示6位有效数字,而 d 至少表示10位有效数字。

    习题2.7

    解释下列字面值常量的不同之处。

    (a) ’a’,L’a’,"a",L"a"

    (b) 10,10u,10L,10uL,012,0xC

    (c) 3.14,3.14f,3.14L

    ’a’为char型字面值,L’a’为wchar_t型字面值,"a"为字符串字面值,L"a"为宽字符串字面值。

    10为int型字面值,10u为unsigned型字面值,10L为long型字面值,10uL为unsigned long型字面值,012为八进制表示的int型字面值,0xC为十六进制表示的int型字面值。

    3.14为double型字面值,3.14f为float型字面值,3.14L为long double型字面值。

    习题2.8

    确定下列字面值常量的类型:

    (a) ?10 (b) -10u (c) -10. (d) -10e-2

    【解答】

    (a) int型

    (b) unsigned int型

    (c) double型

    (d) double型

    习题2.9

    下列哪些(如果有)是非法的?

    (a) "Who goes with F\145rgus?\012" 个人认为非法

    (b) 3.14e1L                      (c) "two" L"some"

    (d) 1024f                        (e) 3.14UL

    (f) "multiple line

    comment"

    【解答】

    (c) 非法。因为字符串字面值与宽字符串字面值的连接是未定义的。

    (d) 非法。因为整数1024后面不能带后缀f。

    (e) 非法。因为浮点字面值不能带后缀U。

    (f) 非法。因为分两行书写的字符串字面值必须在第一行的末尾加上反斜线。

    习题2.10

    使用转义字符编写一段程序,输出2M,然后换行。修改程序,输出2,跟着一个制表符,然后是M,最后是换行符。

    【解答】

    输出2M、然后换行的程序段:

    // 输出"2M"和换行字符

    std::cout << "2M" << ’\n’;

    修改后的程序段:

    // 输出’2’, ’\t’, ’M’和换行字符

    std::cout << ’2’ << ’\t’ << ’M’ << ’\n’;

    习题2.11

    编写程序,要求用户输入两个数??底数(base)和指数(exponent),输出底数的指数次方的结果。

    程序调试出现问题:

    0x004048c8指令引用的“0x0046f03c"内存。该内存不能为“written”。

    access violation 访问冲突;

     //之前这个句子写错了,删除然后添加下一句话后有结果std::cin>>"please input base and exponent" ;


    #include "stdafx.h"
    #include <iostream>

    int main ()
    {
     int bas,exp;
     long result=1;

     //之前这个句子写错了,删除然后添加下一句话后有结果std::cin>>"please input base and exponent" ;

    std::cout<<"please input base and exponent" <<std::endl;
     std::cin>>bas>>exp;
     for( int cnt=0;cnt<exp;cnt++)
      result*=bas;
     std::cout<<bas
       <<"raised to the power of "
       <<exp
       <<":\t"
       <<result<<std::endl;

        return 0;
    }
       

    please input base and exponent
    2 10
    2raised to the power of 10:     1024
    Press any key to continue

    习题2.12

    区分左值和右值,并举例说明。

    左值(lvalue)就是变量的地址,或者是一个代表“对象在内存中的位置”的表达式。

    右值(rvalue)就是变量的值,见2.3.1节。

    变量名出现在赋值运算符的左边,就是一个左值;而出现在赋值运算符右边的变量名或字面常量就是一个右值。

    例如:

    val1=val2/8

    这里的val1是个左值,而val2和8都是右值。

    习题2.14

    下面哪些(如果有)名字是非法的?更正每个非法的标识符名字。

    (a) int double = 3.14159;  (b) char _;

    (c) bool catch-22;          (d) char 1_or_2 =’1’;

    (e) float Float = 3.14f;

    【解答】

    (a) double是C++语言中的关键字,不能用作用户标识符,所以非法。此语句可改为:double dval = 3.14159;。

    (c) 名字catch-22中包含在字母、数字和下划线之外的字符“-”,所以非法。可将其改为:catch_22;。

    (d) 名字1_or_2非法,因为标识符必须以字母或下划线开头,不能以数字开头。可将其改为:one_or_two;。


     

  • C++ unexpected end of file while 的问题

    2009-06-25 11:34:26

    如何解决:"fatal error C1010:VC++6.0中常出现的"unexpected end of file while looking for precompiled header directive"的问题?

    我想大家在VC6.0中经常回遇到这样的问题,如何解决呢?

    1、看看是否缺少“;”,“}”  
      隐藏得深的是宏、.h文件的问题就要费点心思了

    2、一定是你在类的部分定义被删除了,M$在每个类中定义一些特殊的常量,是成对的,如下:  
      .h:  
      #if   !defined(AFX_CHILDFRM_H__54CA89DD_BA94_11D4_94D7_0010B503C2EA__INCLUDED_)  
      #define   AFX_CHILDFRM_H__54CA89DD_BA94_11D4_94D7_0010B503C2EA__INCLUDED_  
      .......  
      //{{AFX_INSERT_LOCATION}}  
      //   Microsoft   Visual   C++   will   insert   additional   declarations   immediately   before   the   previous   line.  
       
      #endif   //   !defined(AFX_MAINFRM_H__54CA89DB_BA94_11D4_94D7_0010B503C2EA__INCLUDED_)  
      你可以新建一个类,然后把这些拷贝过去或补上就可以了。   
    3、在头部加入   #include   "stdafx.h"

    4、在CPP文件第一行加上#include   "stdafx.h"。  
      或者Rebuild   All.  

    5、

    (1). [Project] - [Settings] - [C/C++] - [Category]
    (2). 选择 [Precomplied Headers]
    (3). 单选 [Not Using Precomplied Headers]
    (4). [OK]

     

    拷贝文件:在工程中的header files 中加入文件:Sales_item.h (注意文件放在工程dami1目录下。)

    出现调试问题:unexpected end of 这个也是文件路径错误导致的,也就是作者说明的第一点 “.h文件的问题”

    习题1.21 类Sales_item 提供了操作对象,下面从标准输入读取数据,使用该数据建立一个Sales_item对象,并将该Sales_item对象写到标准输出:#include  "stdafx.h"
    #include <iostream>
    #include "Sales_item.h"
    int main () {

     Sales_item book;

     std::cin>> book;

     std::cout<< book <<std::endl;

        return 0;
    }

    编辑结果:

    0-201-77353-x 4 24.99
    0-201-77353-x   4       99.96   24.99
    Press any key to continue

    习题1.22 编写程序,读入2个具有相同ISBN的Sales_item对象并产生它们的和。

    #include "stdafx.h"
    #include <iostream>
    #include "Sales_item.h"
    int main () {

     Sales_item book1,book2;

     std::cin>> book1 >> book2;

     std::cout<< book1+book2 <<std::endl;

        return 0;
    }

    编辑结果:

    0-201-78345-x 3 20
    0-201-78345-x 5 35
    0-201-78345-x   8       235     29.375
    Press any key to continue

    习题1.24 编写 程序,读入几笔不同的交易,对于每笔新读入的交易,要确定它的ISBN是否和之前的交易ISBN一样,并且记下每一个ISBN交易的总数。通过给定多比不同的交易来测试程序。这些交易必须代表多个不同的ISBN,但是每个ISBN的记录应分在同一组。

    #include "stdafx.h"
    #include <iostream>
    #include "Sales_item.h"
    int main ()
    {

     Sales_item item1,item2;
     int amount=1 ;
     std::cout<<"please input first item:";
     std::cin>>item1;
     while (std::cin>>item2)
      if (item1.same_isbn(item2))
       amount++;
      else {
       std::cout<<" the previous isbn number is :"<< amount <<std::endl;
                item1=item2;
       amount=1;     
       }
       std::cout<<" the last isbn number is :"<< amount <<std::endl;


        return 0;
    }

    please input first item:
    0-201-78345-x 3 20.00
    0-201-78345-x 2 25.00
    0-201-78342-x 2 25.00
     the previous isbn number is :2

    0-201-78342-x 4 25.00
    0-201-78345-x 4 25.00
     the previous isbn number is :2
    a


    s
     the last isbn number is :1
    Press any key to continue

    这个程序问题在于:在最后的0-201-78345-x 数据不能和之前的合并,而是自行列出。

    总结:判断和之前的交易isbn是否相同,采用了循环比较的方法。先输入一个数字item1,后来又输入另一个数字item2。比较后面输入的item和前面的是否相同。如果相同,就把数值amount递加;如果不同,输出上一个相同ISBN的数值,同时把amount置1 。把item1=item2;
    依次循环比较即可。

    习题1.26 在书店程序中,使用了加法操作符而不是复合赋值操作符将trans 加到total 中?

    原因是在1.51中的操作中可执行的操作中没有复合赋值操作符。

  • c程序学习记录5

    2009-06-25 10:35:15

    习题 1.17 编写程序,要求用户输入一组数,输出信息说明其中有多少负数。

    #include "stdafx.h"
    #include <iostream>
    int main()
    {

    int amount=0,value;
    while(std::cin>>value)
    if (value<=0)
    ++amount;
    std::cout<<"the number of negative value  is "<< amount <<std::endl;

    return 0;
    }

    2
    3
    4
    -2
    s
    the number of negative value  is 1
    Press any key to continue

    习题1.18 编写程序,提示用户输入两个数,并将这两个数范围内的每个数写到标准输出。发现第一次写的程序没有考虑到数字大小颠倒的情况。#include "stdafx.h"
    #include <iostream>
    int main()
    {
    std::cout<<"Please enter two numbers:"<<std::endl;

    int v1,v2;

    std::cin>>v1>>v2;
    int lower,upper;
    if (v1 <= v2){
     lower=v1;
     upper=v2;
    } else {
     lower=v2;
     upper=v1;
    }
    std::cout<<" the  number between "<< lower<<" and "<< upper<<" : "<<std::endl;
    for (int i=lower; i<=upper; i++) {
     std::cout<< i <<std::endl;
    }

    return 0;
    }

     

    调试显示:

    Please enter two numbers:
    6 2
     the  number between 2 and 6 :
    2
    3
    4
    5
    6
    Press any key to continue

    习题1.19 如果上题给定数1000 和 2000,程序将产生什么结果?修改程序,使每一行输出不超过10个数。

    #include "stdafx.h"
    #include <iostream>
    int main()
    {
    std::cout<<"Please enter two numbers:"<<std::endl;

    int v1,v2;

    std::cin>>v1>>v2;
    int lower,upper;
    if (v1 <= v2){
     lower=v1;
     upper=v2;
    } else {
     lower=v2;
     upper=v1;
    }
    std::cout<<" the  number between "<< lower<<" and "<< upper<<" : "<<std::endl;
    for (int i=lower,j=0; i<=upper; ++i) {
    std::cout<< i <<" ";
     ++j;
     if (j==10) {
      j=0;
      std::cout<<std::endl;
     }

    }

    return 0;
    }

    Please enter two numbers:
    1000 2000
     the  number between 1000 and 2000 :
    1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

    ............
    1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
    2000 Press any key to continue

    总结:可以在for 循环中添加输出行;

    达到换行目的:可以采用计数,每10个可以输出std::cout<<std::endl;

    同时输出一个数字后,需要输出“ ”以便与另一个数字隔开。

    开始学习的时候,想到程序不知道用什么实现,觉得蛮神秘的,但是后来发现答案全部是已经掌握的知识,而不是需要新知识。也增加了对于概念的理解。

    习题 1.20 编写程序,求用户指定范围内的数的和,省略设置上界和下界的if测试。假定输入数是7和3,按照这个顺序,预测程序运行结果。然后按照给定的数是7和3运行程序,看结果是否与你预测的相符。如果不相符,反复研究关于for和while循环的讨论直到弄清楚其中的原因。
    #include "stdafx.h"
    #include <iostream>
    int main()
    {
    std::cout<<"Please enter two numbers:"<<std::endl;
    int v1,v2;
    std::cin>>v1>>v2;
    int sum=0;
    for (int i=v1; i<=v2; i++) { 
     sum+=i;
    }
    std::cout<<"the sum between "<< v1 << " to "<< v2 << " is:"<< sum <<std::endl;
    return 0;
    }

    Please enter two numbers:
    7 3
    the sum between 7 to 3 is:0
    Press any key to continue

    后来执行完毕结果为0 的原因是 :

    对于for循环,i的初始值为 7 ,判断i小于3为假 ,for语句不执行,结果为0 。
     

  • c程序学习记录4

    2009-06-18 11:36:47

    #include <iostream>
    int main()
    {
    int sum=0,value;

    std::cout<<"Please enter your number:"<<std::endl;

    while(std::cin >> value){
     sum += value;
     std::cout<<" sum is: "<< sum <<std::endl;
    }
    std::cout<<" sum is: "<< sum <<std::endl;
    return 0;
    }

    问题:在输入了合法数字后,红色字体的行不会执行,循环没有跳出。

    Please enter your number:
    2 3 4 5
     sum is: 2
     sum is: 5
     sum is: 9
     sum is: 14

    而在输入不合法的数字,使循环跳出,红色字体执行了,而且显示了Press any key to continuePlease

    enter your number:
    2 3.1
     sum is: 2
     sum is: 5
     sum is: 5
    Press any key to continue

    但是3.1也被修改成了3

  • vc问题:Compiling... ,Error spawning cl.exe 修改办法

    2009-06-18 11:15:59

    可能很多人在安装VC 6.0后有过点击“Compile”或者“Build”后被出现的
    “Compiling... ,Error spawning cl.exe”错误提示给郁闷过。很多人的
    选择是重装,实际上这个问题很多情况下是由于路径设置的问题引起的,
    “CL.exe”是VC使用真正的编译器(编译程序),其路径在“VC根目录\VC98\Bin”下面,
    你可以到相应的路径下找到这个应用程序。

    因此问题可以按照以下方法解决:打开vc界面 点击VC“TOOLS(工具)”—>“Option(选择)”
    —>“Directories(目录)”重新设置“Excutable Fils、Include Files、
    Library Files、Source Files”的路径。很多情况可能就一个盘符的不同
    (例如你的VC装在C,但是这些路径全部在D),改过来就OK了。


    如果你是按照初始路径安装vc6.0的,路径应为:
    executatble files:
    C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin
    C:\Program Files\Microsoft Visual Studio\VC98\BIN
    C:\Program Files\Microsoft Visual Studio\Common\TOOLS
    C:\Program Files\Microsoft Visual Studio\Common\TOOLS\WINNT

    include files:
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE
    C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
    C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE

    library files:
    C:\Program Files\Microsoft Visual Studio\VC98\LIB
    C:\Program Files\Microsoft Visual Studio\VC98\MFC\LIB

    source files:
    C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC
    C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
    C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE
    C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC

    如果你装在其他盘里,则仿照其路径变通就行(我就是装在D盘)。
    关键是microsoft visual studio\ 后面的东西要相同。
    本人深受其害,重装多次不管用,上面中文部分为高手借鉴,

    cannot open Debug/mousePosition.exe for writingcannot open Debug/mousePosition.exe for writing

    出现这个的原因是终端的输入屏没有关闭,而且好像没有出现

    Press any key to continue导致的这个问题,关闭后再调试就没有这个错误了

  • c程序学习记录3

    2009-06-18 10:49:59

    #include "stdafx.h"
    #include <iostream>
    int main()
    {
    int sum=0,value;

    std::cout<<"Please enter your number:"<<std::endl;

    while(std::cin >> value){
     sum += value;

    }
    std::cout<<" sum is: "<< sum <<std::endl;
    return 0;
    }

    生成的结果:

    在程序中输入数字,最后一个是小数,这样就生成了总和:

    Please enter your number:
    2 3 4.1
     
     sum is: 9
    Press any key to continue

     

    当输入奇怪之处:

    在上面的程序中,修改

  • c程序学习记录2

    2009-06-16 17:25:39

     

    容易出现错误的地方:

    please enter two numbers:
    3 7
    3and7
    the bigeer number is7
    Press any key to continue

    修改办法:在双引号扩住的两边都增加空格(and和the bigeer number is) :

    std::cout << v1 <<" and "<< v2 <<std::endl;
    std::cout <<" the bigeer number is "<< upper <<std::endl;

    习题1.16

    #include "stdafx.h"
    #include <iostream>
    int main()
    {
     std::cout<<"please enter two numbers:"<<std::endl;

     int v1,v2;

     std::cin >> v1 >> v2 ;

     

     int lower,upper;

     if(v1<=v2){
        lower=v1;
        upper=v2;
     }else {
        lower=v2;
        upper=v1;
      }
        std::cout << v1 <<" and "<< v2 <<std::endl;
        std::cout <<" the bigeer number is "<< upper <<std::endl;
     //keep executing the while until val is greater than 10
    /* int sum=0;
     for(int val=lower;val<=upper;val++)
      sum+=val;
     std::cout <<"sum of "<<lower<< " to "<< upper<< " is "
       <<sum<<std::endl;
     */  
     
     return 0;
    }

     

  • c程序学习记录

    2009-06-16 16:07:34

    程序执行后,val的值变为11:

    #include <iostream>
    int main()
    {
     int sum=0,val=1;
     //keep executing the while until val is greater than 10
     while (val<=10){
      sum+=val;
      val++;
     }
      std::cout <<val<<std::endl;
      std::cout <<"sum of 1 to 10 inclusive is "
          <<sum<<std::endl;
     
     return 0;
    }

    11
    sum of 1 to 10 inclusive is 55
    Press any key to continue

     

     

    注意:修改以下程序++的位置在val++ 或者++val ,程序结果相同:#include "stdafx.h"
    #include <iostream>
    int main()
    {
     int sum=0,val;
     //keep executing the while until val is greater than 10
     for (val=1;val<=10;val++){

      std::cout <<val<<std::endl;

      sum+=val;
      
     }
      std::cout <<val<<std::endl;
      std::cout <<"sum of 1 to 10 inclusive is "
          <<sum<<std::endl;
     
     return 0;
    }

     

    在val++ 中在for循环内外分别输出数值,显示为内为10 ,外为11,结果显示为55(也就是1 到10的总和) ,重新修改为++val,结果不变:

    #include "stdafx.h"
    #include <iostream>
    int main()
    {
     int sum=0,val;
     //keep executing the while until val is greater than 10
     for (val=1;val<=10;val++){
      std::cout <<val<<std::endl;
      sum+=val;
      
     }
      //std::cout <<val<<std::endl;
      std::cout <<"sum of 1 to 10 inclusive is "
          <<sum<<std::endl;
     
     return 0;
    }

    习题:1.10 1.11

    #include "stdafx.h"
    #include <iostream>
    int main()
    {
     /*int sum=0,val;
     //keep executing the while until val is greater than 10
     for(val=50;val<=100;val++)
      sum+=val;
      */
     int sum=0,val=50;
     while(val<=100){
      sum+=val;
      ++val;
     }
      std::cout <<"sum of 50 to 100 inclusive is "
          <<sum<<std::endl;
     
     return 0;
    }

    #include "stdafx.h"
    #include <iostream>
    int main()
    {
     /*int sum=0,val;
     //keep executing the while until val is greater than 10
     for(val=50;val<=100;val++)
      sum+=val;
      */
     int sum=0,val=50;
     while(val<=100){
      sum+=val;
      ++val;
     }
      std::cout <<"sum of 50 to 100 inclusive is "
          <<sum<<std::endl;
     
     return 0;
    }
    #include "stdafx.h"
    #include <iostream>
    int main()
    {
     /*int sum=0,val;
     //keep executing the while until val is greater than 10
     for(val=50;val<=100;val++)
      sum+=val;
      */
     int sum=0,val=50;
     while(val<=100){
      sum+=val;
      ++val;
     }
      std::cout <<"sum of 50 to 100 inclusive is "
          <<sum<<std::endl;
     
     return 0;
    }

    while循环的条件判断跟在while后面,判断条件比较灵活,可以是各种判断。
    for循环的条件也是跟在FOR后面,不过for循环多了初始条件设置,而且,for循环的条件比较适合用于参数梯级变化的情况。 

     

    在执行c++程序的时候,经常是在原程序上做修改:

    不要着急,心静自然会发现问题:

    最容易出现的问题是:

    1 程序的输入输出部分;这个输出之前都没有把lower 和upper 当作变量来看,导致终端输入两个数子没有反映;

     std::cout <<"sum of "<<lower<< " to "<< upper<< " is "
       <<sum<<std::endl;

    2 程序中变量的定义部分,增加了变量,那么变量是否定义呢?

    3 在循环中的变化;

    第一次写成了val=upper ,所以程序不能执行下去。

    for(int val=lower;val<=upper;val++)

     

    习题1.14

    #include "stdafx.h"
    #include <iostream>
    int main()
    {
     std::cout<<"please enter two numbers:"<<std::endl;

     int v1,v2;

     std::cin >> v1 >> v2 ;

     

     int lower,upper;

     if(v1<=v2){
        lower=v1;
        upper=v2;
     }else {
        lower=v2;
        upper=v1;
      }
        std::cout << lower <<"and"<< upper <<std::endl;
     //keep executing the while until val is greater than 10
     int sum=0;
     for(int val=lower;val<=upper;val++)
      sum+=val;
     std::cout <<"sum of "<<lower<< " to "<< upper<< " is "
       <<sum<<std::endl;
       
     
     return 0;
    }

    终端显示:

    please enter two numbers:
    3 7 //用户输入的数字
    3and7
    sum of 3 to 7 is 25
    Press any key to continue



     

  • c程序刚刚调通

    2009-06-16 11:42:36

    第一个c语言程序:

    出现问题:

    Enter two numbers:
    1 2
    the sum of 1 and 2is3

    出现问题原因是缺少空格:

    #include "stdafx.h"

    #include <iostream>
    int main(int argc, char* argv[])
    {
     std::cout <<"Enter two numbers:"<<std::endl;
     int v1,v2;
     std::cin>>v1>>v2;
     std::cout<< "the sum of "<< v1 <<" and "<< v2 << "is"<<v1+v2<<std::endl;
     return 0;
    }

    std::cout<< "the sum of "<< v1 <<" and "<< v2 << " is "<<v1+v2<<std::endl;

    再次调试,终端显示正确;

    Enter two numbers:
    1 2
    the sum of 1 and 2 is 3

  • 如何学好c++,

    2009-06-15 11:23:06

    如何学好c++:

    一、c++程序设计这门课程与你们以往所学的数学等不同,有他特有的思考和解决问题方法,所以一定要掌握这种方法。也许这样说太空,具体说:(1)一定要化时间,这是前提。你看很多“电脑高手”,都是花很多时间在那儿琢磨,他们很痴迷,所以不觉的。你现在还未找到编程的乐趣,太可惜。(2)掌握学习的方法,首先将概念搞懂,看书,听老师讲,与同学讨论,如听不懂老师所讲,就必须预习。还可以找参考书看,因为每一本书都有不足,和他的长处。

    第二,对一些典型的例子,一定要搞懂,记住,不是叫你去死记,例如运算符重载中complex的例子,将概念与例子结合,就容易理解概念,同时当你自己编程时,你就会想这与那个例子类似,然后先模仿,慢慢你就能熟能生巧了

    第三,大量练习,即使有答案,先别看,自己做,不行再看,同时想,是那儿不会,再去补那方面的知识,这样才能进步。有条件要去上机,是否正确,上机运行就知,而且程序最终总要运行的。

    开始是非常痛苦的,一定要坚持!不懂,赶快加大时间去搞懂,否则会越来越多,然后整个放弃,就完了!千万别指望补,它需要更多的精力,否则很难通过!

  • mysql内容要点记录:

    2009-06-12 20:43:55

       数据库的安装分为2部分:mysql-server 5.0 和SQL-front 两部分。后者是前者的前台界面。通过mysql数据库修改的内容可以在SQL-front 中看到。

    在使用数据库之前关注几个提示符的含义:

    mysql>  表示准备好接受新命令;

         >  表示命令没有输入完毕,可以继续输入;出现这个有两种可能:1 缺少分号作为结束符;2 命令行还需要多行中的下一行

         ‘> 表示输入的命令没有完毕,原因是缺少另一部分的';

         “> 表示输入的命令没有完毕,原因是缺少另一部分的“;

      实际出现这个问题,不能输入另一半的分号或者引号,这个时候输入\c来返回到mysql> 准备好接受新命令,重新输入命令行即可;

    mysql> select * from pet where name="fluffy;
        "> /c
        "> "
        -> \c

    1 查看版本和当前时间:

    mysql> select version(),current_date;
    +---------------------+--------------+
    | version()           | current_date |
    +---------------------+--------------+
    | 5.0.22-community-nt | 2009-06-12   |
    +---------------------+--------------+
    1 row in set (0.02 sec)

    2 显示数据库:

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | pet                |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)

    3 使用数据库:

    mysql> use pet;
    Database changed

    4 在数据库下面创建表格:

    mysql> create table telephone (name varchar(20),number int(10));
    Query OK, 0 rows affected (0.09 sec)

    5 把文件加载到数据库中;

    mysql> load data local infile "d:/07c/pet/telephone.txt" into table te
    mber;
    Query OK, 5 rows affected, 6 warnings (0.03 sec)
    Records: 5  Deleted: 0  Skipped: 0  Warnings: 6

    6在数据库中查找相关数据;

    mysql> select * from telephone_number;
    +---------+------------+
    | name    | number     |
    +---------+------------+
    | sister  | 2147483647 |
    | bobo    | 2147483647 |
    | father  | 2147483647 |
    | puling  | 2147483647 |
    | haifeng | 2147483647 |
    +---------+------------+
    5 rows in set (0.00 sec)

     

     

     

  • mysql 学习注意事项:

    2009-06-11 11:20:03

    在学习过程中,出现2个问题,和教程中的并不相同,说明如下:

    1 把本地文件通过命令加载到数据库中,出现错误。需要写出文件的路径即可:

    mysql> load data local infile "pet.txt" into TABLE pet;
    ERROR 2 (HY000): File 'pet.txt' not found (Errcode: 2)

    错误原因是没有找到文件:修改方法是增加文件的路径:

    mysql> load data local infile "d:/07c/pet/pet.txt" into table event;
    Query OK, 8 rows affected, 16 warnings (0.02 sec)
    Records: 8  Deleted: 0  Skipped: 0  Warnings: 16

    2 加载文件正确。但是查找时候的显示格式不正确,而在SQL-FROUNT上显示的格式正确。不用理会啦!

    mysql> select * from event;
    +----------+------------+----------+--------------------------------+
    | name     | date       | type     | remark                         |
    +----------+------------+----------+--------------------------------+
     | luffy   | 1995-05-15 | litter   | 4 kittens, 3 female, 1 male
     | uffy    | 1993-06-23 | litter   | 5 puppies, 2 female, 3 male
             | | 1994-06-19 | litter   | 3 puppies, 3 female
        | py   | 1999-03-21 | vet      | needed beak straightened
                      | -03 | vet      | broken rib

    而在以文件名称为luffy查找相关信息,没有信息;

    mysql> select * from pet where name="luffy";
    Empty set (0.02 sec)

    而在以文件名称为fluffy查找相关信息,有信息,说明是显示问题,导入本身没有问题;

    mysql> select * from pet where name="fluffy";
    +--------+--------+----------+------+------------+------------+
    | name   | owner  | specials | sex  | birth      | death      |
    +--------+--------+----------+------+------------+------------+
    | Fluffy | Harold | cat      | f    | 1993-02-04 | 0000-00-00 |
    +--------+--------+----------+------+------------+------------+
    1 row in set (0.00 sec)
                

Open Toolbar