好学的C++ 第三章

程序的特点和优点:

程序语言无歧义,转换为机器码不会出现任何不确定的问题;

可反复执行;

计算机执行速度超快;

 

术语:

应用程序:用户视角,编译和测试之后的程序

代码:程序员视角的程序;另外还跟数据(供程序处理的被动性信息,以数值和单词为最基本)相对,表示完成操作的程序部分

  源代码:高级语言编写的程序

    语句(;结尾)和复合语句

  机器码:计算机的母语,每条计算机指令由0和1构成,通常用十六进制表示,例如08 A7 C3 9E 58。用机器码写程序,需                     要深入了解CPU体系结构。

编译器:即语言翻译器,将源代码转换为CPU能理解的机器码

 

C的优点和特点:

精炼,语法简明全面,写出来短小精悍;比其他程序语言限制要少。

 

C++相比C:

增加了面向对象的编程能力,适合解决复杂问题。

 

类与对象:

(it)类是扩展的数据结构,表明此类对象怎么初始化、怎么使用的共性,而对象是用类定义的实例,是真正干活的。

 

编写程序:

文本编辑器例如MS word或记事本,但必须保存为纯文本格式。

IDE例如VS(为了搞大工程,所以对简单初学者不友好)。

  工程:构成某个程序的全体文件。VS:file->new->console application->命名->ok。file->new等价于new project

  #include "stdafx.h"   vs里创建控制台应用程序(非windows应用程序)所必须。

  system("PAUSE");  窗口不会在屏幕上一闪而过。非MS-DOS或WINS,可能无效。

  建立:编译+链接,.cpp通过编译器变为.obj。编译会发现语法错误和程序结构错误。VS:F7

  运行:寻找程序逻辑错误。VS:Ctrl+F5(显示"press any key to continue",避免窗口在屏幕上一闪而过)。

 

cout对象,左向流操作符(<<)

C++语句以分号结尾,除了少数例外(?)

#include  :预处理指令(给编译器的活)

c语言与其标准库是分开的(可以不用标准库而用自己的运行库),标准库的库函数与库对象与用户自定义的无本质区别,因          而也需先声明再使用。头文件干这个活。

  <file>:引入c++库里的相关声明和定义。<iostream>是预编译格式的虚拟头文件,所以不带.h。

  ""

    #include <iostream>之后两种用法:(it:std是个对象,它被导入后,using它就可以直接使用它的数据和函数,

    否则得间接使用)

    using namespace std;

    cout << "whats up" << endl;

    或:

    std::cout << "awesome" <<std::endl; //endl是std里的预定义常数

 

main函数返回值:返回给操作系统或开发环境,一般0代表执行成功。

变量:存放数据的地方。int var; cin << var;

####规则:C++变量使用前必须定义####      //Basic对此不严格导致许多低级错误

三种基本数据格式:整数(int)、浮点数(double,8字节(pc都有8字节协处理器),c++运算都转double,存储默认double除非指定例如12.6F)、文本字符串(即字符串)。

c++库内部变量使用下划线开头,因此自编变量命名最好不用下划线开头。

 

整数和浮点数没有任何相似之处(150和150.0存储差别很大),没必要用double时用它是很低效的。

  整数:通常二进制补码形式

  浮点数:符号位+阶幂+尾数(用来表示超大整数可能会有舍入误差。C++0x大整数可用long long int)

 ####规则:计算机只能执行绝对清晰的指令####  //it:受限于cpu指令集

  AI:无数简单清晰的小判断组成复杂的程序

####c++里可用一条语句的地方都可使用符合语句(语句块)#### //语句块不需要以分号结束,可以是0条语句

c++里大部分表达式(包括赋值)都会生成一个值(void函数是例外),因而可以 x=y=z=0; 这样赋值。

C++编程风格:小写字母作为变量名。

bool类型:true(1)和false(0)是c++预定义常数。

自增自减:var++先传值再自增,++var先自增再传值。

表达式:使用c++个中国操作符将变量、常数、较小表达式连接在一起构成的值(通常会生成值)。

任何表达式加分号就是一条语句(3;也是)。一行可写多个语句但不推荐。

操作符:算术高于关系高于逻辑。||与&&是短路运算。位操作符(& | ! ^ ~)是操作二进制位,不使用短路逻辑。

#include <cmath>

  double x = sqrt(2); //ok,int会被自动转成double

  int y = sqrt(2); //warning, 小数会被舍弃

 

小结补漏:

凡是带小数点的值都会被识别为浮点数值。

语句块:花括号里的0条或多条语句。

 

 

for(初始化;条件;递增){...}  //it:初始化跟递增可以放任何语句包括语句块;条件需要可判断真假的值

//c++0x提供新版for关键字(依赖于数组和容器,后续介绍)

初始化语句里声明的变量,作用范围只在循环内,其值不影响循环体外的同名变量。如果想要在循环外,则将声明放在for之前即可。

for里的continue会执行递增语句后才执行下一次循环的条件语句。

 

温度计 test   p30

 

      

  

// Exercise 01.03.01
// Convert from Fahrenheit to Centigrade.

#include <iostream>
using namespace std;

int main() {
     double  ctemp, ftemp;

     cout << "Input a Fahrenheit temp and press ENTER: ";
     cin >> ftemp;
     ctemp = (ftemp - 32) / 1.8;
     cout << "Celsius temp is: " << ctemp;

     system("PAUSE");
     return 0;
}
 

// Exercise 01.03.02
// Convert from Fahrenheit to Centigrade, using only one variable.

#include <iostream>
using namespace std;

int main() {
     double  ftemp;

     cout << "Input a Fahrenheit temp and press ENTER: ";
     cin >> ftemp;
     cout << "Celsius temp is: " << (ftemp - 32) / 1.8;

     system("PAUSE"): 
     return 0;
}
 

// Exercise 01.03.03
// Calculate cube (n to the 3rd) for n, a number
//   entered from the keyboard.

#include <iostream>
using namespace std;

int main() {

// Declare n as a floating-pt variable.

    double  n;

// Prompt and input value of n.

    cout << "Input a number and press ENTER: ";
    cin >> n;

// Calculate and output the cube.

    cout << "The cube is: " << n * n * n;

    system("PAUSE");
    return 0;
}

 

// Exercise 01.03.04
// Output square of a number, using variable name "num."

#include <iostream>
using namespace std;

int main() {

// Declare num as a floating-pt variable.

    double  num;

// Prompt and input value of num.

    cout << "Input a number and press ENTER: ";
    cin >> num;

// Calculate and output the square.

    cout << "The square is: " << num * num;

    system("PAUSE");
    return 0;
}
 

 

 

#include <iostream>
#include <math.h>
using namespace std;

int main() {
    int  n;   // Number to test for prime-ness
    int  i;   // Loop counter
    int  is_prime;   // Boolean flag

    // Assume that a number is prime until proven otherwise

    is_prime = true;

    // Get a number from the keyboard. 

    cout << "Enter a number and press ENTER: ";
    cin >> n;
    
    // Test for prime-ness by checking for divisibility
    //  by all whole numbers from 2 to sqrt(n).

    i = 2;    
    while (i <= sqrt(static_cast<double>(n))) {
                               // While i is <= sqrt(n),
        if (n % i == 0)        //    If i divides evenly into n,
            is_prime = false;  //       n is not prime.
        i++;                   //    Add 1 to i.
    }

    // Print results

    if (is_prime)
        cout << "Number is prime.";
    else
        cout << "Number is not prime.";

    system("PAUSE");
    return 0;

 

 

 

#include <iostream>

using namespace std;

int main() {
    int total, n;
    
    cout << "Welcome to NIM. Pick a starting total: ";
    cin >> total;
    while (true) {

       // Pick best response and print results.

         if ((total % 3) == 2) {
              total = total - 2;
              cout << "I am subtracting 2." << endl;
         } else {
              total--;
              cout << "I am subtracting 1." << endl;            
         }
         cout << "New total is " << total << endl;
         if (total == 0) {
              cout << "I win!" << endl;
              break;
         }

       // Get user抯 response; must be 1 or 2.

         cout << "Enter number to subtract (1 or 2): ";
         cin >> n;
         while (n < 1 || n > 2) {
               cout << "Input must be 1 or 2." << endl;
               cout << "Re-enter: " << endl;
               cin >> n;
         }
         total = total - n;
         cout << "New total is " << total << endl;
         if (total == 0) {
              cout << "You win!" << endl;
              break;
         }
    }
    system("PAUSE");
    return 0;
}
 

第三章 一专多能的for语句

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值