zl程序教程

您现在的位置是:首页 >  后端

当前栏目

(1)风色从零单排《C++ Primer》 一个简单的c++程序

C++程序 一个 简单 primer 单排
2023-09-11 14:20:46 时间


从零单排《C++ Primer》

——(1)一个简单的c++程序                                               


本次学习收获


0、写在前面

风色以前上过C++的课程,然而当时并没有认真去学,基本不能使用c++来作项目开发。

这次又一次学习c++,首先会阅读c++Prime英文版第五版,希望能够为以后的学习打下坚实的基础。



1、程序

一个简单的c++程序(P17),帮助我们高速了解c++的代码风格,并通过分析代码学习c++

程序意图:在终端上输入一组数字。输出不同数字输出的次数。同样的数字必须是连续的。

如果输入:

42 42 42 42 55 55 62 100 100 100

终端应该输出:

42 occurs 5 times
55 occurs 2 times
62 occurs 1 times
100 occurs 3 times

代码:

#include <iostream>

int main()
{
    //currVal is the number we are counting;we will read new value into val. currVal用来存放当前计算的数据,新读入的数据会放入val
    int currVal = 0, val = 0;   
    //read first number and ensure that we have data to process. 为确保有可操作的数据。先读入一个
    if(std::cin >> currVal){
        int cnt = 1; //store the count for the current value we're processing. cnt用来计数。当前存放的数据出现了多少次
	while (std::cin >> val){ // read the remaining numbers.读剩下的数据
	    if(val == currVal)   //if the values are the same.假设新读入的数据,和当前计算的数据是一样的
		++cnt;
	    else {
	   	std::cout<< currVal << " occurs " << cnt <<"times" << std::endl;
		currVal = val;   //remember the new value
                cnt = 1;
            }
	} //  while loop ends here. while循环结束
    } //  outermost if statement ends here . if结束
    return 0;
}



2、代码分析:


2.1函数:

每一个c++程序拥有至少一个函数,当中一个函数必须叫作main。他是操作系统执行c++程序时程序的入口。

在上面的程序中,我们也自定义了一个main函数。它是:

</pre><pre name="code" class="cpp">int main()
{
    //currVal is the number we are counting;we will read new value into val. currVal用来存放当前计算的数据,新读入的数据会放入val
    ......
    return 0;
}
一个函数由4个部分组成:

1)返回类型

在这个函数中,它的返回类型是int 型。表示整数。

2)函数名

在这个函数中为main

3)參数(能够为空)

參数填在main后面的()内。改函数的參数为空

4)函数主体

花括号及以内的部分

这个函数最后一条语句:return 。

这是一条能够用来结束函数的语句。在这个样例中,return能够返回一个值给函数的调用者。

返回的值必须喝函数的返回类型一致。在这个样例中,函数返回了一个整数型0。


2.2使用Library(函数库):

程序第一行:

#include<iostream>

在程序实现时,我们不仅会自定义一些函数。通常也会使用一些别人已经写好的函数来简化我们的编码。

使用别人的函数必须引入相应的函数库的头文件。

程序迪一行快速编译器我们要使用iostream libraary。<>里面的名称是我们要引入的头文件。

这里我们引入了iostream。以便实如今终端上的输入输出操作。<>表示引入的是标准文件,要引入自己的文件,使用""。


2.3凝视:

凝视掉的内容,编译器时不会运行的。凝视用来方便别人阅读代码。

c++有两种凝视方法

1)单行凝视 //

2)c凝视 /* */


2.4标准输入与输出:

这个样例中使用了iostream library来处理标准输入和输出。它包括了istream和ostream。分别代表input streams(输入流) 和 output streams(输出流)。Stream(流)表示从输入输出设备读取/写入一系列有顺序的字符。

std::cin >> currVal
从终端上读入一个数据。存放在变量currVal上。

currVal变量用来存放当前计算的数字。

std::cout<< currVal << " occurs " << cnt <<"times" << std::endl;
从终端上输出当前数字共出现了多少次。

变量cnt记录了currVal所存放的数字出现的次数。

endl表示换行。

输入输出能够这样连续使用:

int v1 =0, v2 = 0;
std::cin>>v1>>v2;
std::cout<<v1<<v2<<std::endl;

这是由于>>(<<)会返回它左边的操作数(operand)


2.5命名空间:

假设引入了不同库,而库里面假设有相同名称的函数,调用的时候这个函数的时候,编译器就会不知道调用哪个。为了解决这个冲突,c++使用了命名空间。注意到

std::cin >> currVal
里面的std::,表示使用标准库里面的cin函数。


2.6IF语句以及IF语句与输入语句的组合使用:

if(条件语句){
    //运行内容
}

首先运行在()内的语句条件语句,仅仅有条件语句为真时才运行{ } 内的内容。常见的使用方法有if( a == b) , 假设a等于b,则a==b为真,运行{}内容。否则a==b为false,不运行{}内.其实,条件语句非0即为真。

在这个样例中,有

if(std::cin >> currVal)
注意到,假设std::cin>>currVal成功读取到数据,则为真。


2.7WHILE语句以及IF语句与输入语句的组合使用:

while(条件语句){
    //运行内容
}
首先运行条件语句,假设条件为真,则会运行{}内的内行。接着继续运行条件语句。假设继续为真。则继续运行{}内容。如此循环,直到条件运行后为假。

结合if语句里所述。在不知道有多少数据要读取时,能够编写例如以下语句

while(std::cin>>val){
}

当istream无效时。std::cin>>currVal读取失败。条件为假。无效的情况有

1)当我们输入end-of-file(eof)时 (Dos/Windows:ctrl+z   Linux:ctrl+d)

2)输入了非法数据,如数据为空,或者类型不符



3、作业:

p9

/*
* Exercise 1.3: Write a program to print Hello,World on the standard output
* 写一个程序实如今标准输出中打印Hello,World
*/
#include<iostream>
int main()
{
    std::cout<<"Hello,World"<<std::endl;
    return 0; 
}

p11

/*
* Exercise 1.8:Indicate which,if any,of the following output statements are legal:
* 指出哪些输出语句是合法的 
*/
#include <iostream>
int main(){
    std::cout<<"/*";//合法输出为/*
//    std::cout<<"*/";//合法输出为*/
//    std::cout<</*"*/"*/;//非法 最后缺少一个"
//    std::cout<</*  "*/" /* "/*" */;//合法 输


p13
/*
* Exercise 1.9: Write a program that uses a while to sum the numbers from 50 to 100
* 用while计算50到100的累加值
*/
#include <iostream>
int main(){
  int sum = 0,val = 50;
  while(val<101){
     sum += val;
     ++val;
  } 
  std::cout<<"The sum of 50 to 100 inclusive is "<< sum <<std::endl; 
  return 0; 
}

/*
*Exercise 1.11 
*Write a program that prompts the user for two integers.Print each number in the range specified by thoe two intergers
*提示用户输入两个整数。打印着两个整数翻飞内的数。
*/

#include<iostream>
int main(){
    int val1 = 0, val2 = 0, temp = 0;
    std::cout<<"please input two intergers"<<std::endl;
    if(std::cin>>val1){
	if(std::cin>>temp){
	    if(temp>val1)
		val2 = temp;
            else{
	        val2 = val1;
                val1 = temp;   
	    }
            while(val1<=val2){
                std::cout<<val1<<std::endl;
                ++val1;
            }
	}
     }
    return 0;
}