栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++

C++_Study_Day3 分支语句和逻辑表达式 if语句

逻辑表达式

字符函数库cctype

cctype头文件中的常用函数列表如下:

函数名称 返回值
isalnum() 如果参数是字母数字,即字母或者数字,函数返回true
isalpha() 如果参数是字母,函数返回true
iscntrl() 如果参数是控制字符,函数返回true
isdigit() 如果参数是数字(0-9),函数返回true
isgraph() 如果参数是除空格之外的打印字符,函数返回true
islower() 如果参数是小写字母,函数返回true
isprint() 如果参数是打印字符(包括空格),函数返回true
ispunct() 如果参数是标点符号,函数返回true
isspace() 如果参数是标准空白字符,如空格、换行符、水平或垂直制表符,函数返回true
isupper() 如果参数是大写字母,函数返回true
isxdigit() 如果参数是十六进制数字,即0-9、a-f、A-F,函数返回true

tolower() 如果参数是大写字符,返回其小写,否则返回该参数
toupper() 如果参数是小写字符,返回其大写,否则返回该参数

?:运算符(条件运算符)

常用于代替简单的if-else语句

switch语句

​ switch和if-else

switch语句中的每一个case标签都必须是一个单独的值,另外,这个值必须是整数(包括char),而且case标签值还必须是常量。

如果涉及取值范围、浮点测试或两个变量的比较,则应该用if-else语句。然而,如果所有的选项都可以使用整数常量来标识,则可以使用switch语句或if-else语句。就代码长度和执行速度而言,switch语句的效率更高

简单文件输入/输出 写入到文件中

对于文件输出,C++使用类似于cout的东西

  • 有关用cout用于控制台输出的基本原理

1.必须包含头文件iostream。

2.头文件iostream定义了一个用于处理输出的类ostream。

3.头文件iostream声明了一个名为cout的ostream对象。

4.必须指定名称空间std。

5.可以结合使用cout和运算符<<来显示各种数据。

  • 文件输出与此类似

1.必须包含头尾文件fstream。

2.头文件fstream定义了一个用于处理输出的ofstream类。

3.需要声明一个或多个ofstream变量(对象),并以自己喜欢的方式对其进行命名,条件是遵守常用的命名规则。

4.必须指明名称空间std。

5.需要将ofstream对象与文件关联起来,使用open()方法。

6.使用完文件后,应使用方法close()将其关闭。

7.可以结合ofstream对象和运算符<<来输出各种类型的数据。

使用文件输出的主要步骤如下。

  1. 包含头文件fstream。
  2. 创建一个ofstream对象。
  3. 将该ofstream对象同一个文件关联起来。
  4. 就像使用cout那样使用该ofstream对象。
#include 
#include
using namespace std;

int main()
{
    char automobile[50];
    int year;
    double a_price;
    double d_price;

    ofstream outFile;       //创建一个ofstream对象
    outFile.open("carinfo.txt");    //与文件关联起来,若没有该文件将在该目录下新建txt文件

    cout<<"Enter the make and model of automobile:";
    cin.getline(automobile,50);
    cout<<"Enter the mobile year:";
    cin>>year;
    cout<<"Enter the original asking price:";
    cin>>a_price;
    d_price = 0.193 * a_price;

    cout< 

outFile对象可以使用cout可使用的任何方法。不但可以使用运算符<<,还可以使用各种格式化方法,如setf()和precision()。

读取文本文件
// sumafile.cpp -- functions with an array argument
#include 
#include           // file I/O support
#include           // support for exit()
const int SIZE = 60;
int main()
{
    using namespace std;
    char filename[SIZE];
    ifstream inFile;        // 对象处理文件输入

    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    inFile.open(filename);  // associate inFile with a file
    if (!inFile.is_open())  // failed to open file
    {
        cout << "Could not open the file " << filename << endl;
        cout << "Program terminating.n";
        // cin.get();    // keep window open
        exit(EXIT_FAILURE);
    }
    double value;
    double sum = 0.0;
    int count = 0;          // number of items read

    inFile >> value;        // get first value
    while (inFile.good())   // while input good and not at EOF  
    {
        ++count;            // one more item read
        sum += value;       // calculate running total
        inFile >> value;    // get next value
    }
    if (inFile.eof())
        cout << "End of file reached.n";
    else if (inFile.fail())
        cout << "Input terminated by data mismatch.n";
    else
        cout << "Input terminated for unknown reason.n";
    if (count == 0)
        cout << "No data processed.n";
    else
    {
        cout << "Items read: " << count << endl;
        cout << "Sum: " << sum << endl;
        cout << "Average: " << sum / count << endl;
    }
    inFile.close();         // finished with the file
    // cin.get();
    return 0;
}
  • good()方法在没有发生任何错误时返回true;
  • eof()方法只能判断是否到达EOF;
  • fail()方法可以用于检测EOF和类型不匹配;

故,如果执行到else if测试便可排除EOF,因此,如果fail()返回true,便可断点循环终止的原因是类型不匹配。

函数------C++的编程模块 函数与数组
#include 
const int ArSize = 8;
int sum_arr(int arr[],int n);      //函数声明
using namespace std;

int main()
{
    int cookies[ArSize] = {1,2,4,8,16,32,64,128};

    int sum = sum_arr(cookies,ArSize);       //函数调用
    cout<<"Total cookies eaten:"< 

在C++中,当(且仅当)用于函数头或函数原型中,int *arr(指针表示法)和int arr[](数组表示法,提醒用户arr指向int数组的第一个元素)的含义是相同的。它们都意味着arr是一个int指针。

arr[i] == *(arr + i)

&arr[i] == arr + i

显示数组及用const保护数组

若只需要显示数组,为了防止函数无意中修改数组的内容,可在声明形参时使用关键字const

void show_array(const double arr[],int n);

注意,这并不是意味着原始数组必须是常量,而只是意味着不能在show_array()函数中使用arr来修改这些数据。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号