行业资讯 2025年06月7日
0 收藏 0 点赞 464 浏览 4042 个字
摘要 :

1、strcmp( ):比较两个字符串,相同返回0;第一个字符串按字母顺序排在第二个字母之后将返回负值; 2.类型别名: #define BYTE char  //使用预处理器,用char替代……

1、strcmp( ):比较两个字符串,相同返回0;第一个字符串按字母顺序排在第二个字母之后将返回负值;

2.类型别名:

#define BYTE char  //使用预处理器,用char替代所有的BYTE,BYTE是char的别名

typedef char byte  //使用关键字typedef ,byte是char的别名。typedef不会创建新类型

3. C++11中基于范围的for循环,对数组(或vector,array)的每个元素执行相同的操作:

double prices[5]= {4.99,5.6,2.13,2.69,7.34};

for (double x : prices)   {cout<<x<<std::end;}

for (double &y : prices)   { y=y*0.8;  } // 要修改数组元素时,循环变量要取地址,改为&y

4、cin() 忽略空格和换行符,cin.get()中包含空格、制表符、换行符。

  cin.get(name,size).get();

5、检测文件尾(EOF) :检测到EOF,cin.eof() 或cin.fail() 将返回bool值true,否则返回bool值 false; 

6.字符输入做法:

char ch; cin.get(ch);  while (cin.fail()==false) { ….  cin.get(ch); }  //或者while (!cin.fail()) 或while (cin) 

char ch;  while (cin.get(ch))  { … }

int ch; ch=cin.get();  while ( ch!=EOF )  { …  ch=cin.get(); } 

int ch;   while((ch = cin.get() ) != EOF) {     } 

更常用,拼接字符串:cin.get(ch1).get(ch2);

7.逻辑运算符||  逻辑运算符&&

!的优先级大于逻辑运算符和关系运算符(> < =), &&优先级大于|| ,关系运算符优先级大于 &&,||

8.定义二维字符串数组方式:

(1)char指针数组: char * cities[3]={"Beijing", "TianJin", "ShangHai"};

(2)char数组的数组 char  cities[3][10]={"Beijing", "TianJin", "ShangHai"};

(3)string对象数组 string  cities[3]={"Beijing", "TianJin", "ShangHai"};

希望字符串可以修改的情况下,string类可以自动调节大小更方便。

9.switch语句,while语句中,将int值与枚举量标签进行比较时,枚举量将提升为int。

switch语句中每一个case的标签必须是int或char,还必须是常量

10.continue :跳出循环剩余部分,执行新的一轮循环

     break :跳出循环剩余部分,不在执行循环语句

switch一般与break组合使用

int chice[3]={0,1,2};

switch (choice){case 0: … break;  case 1: … break; case 2: … break;  }

11.int n[6];  for(i=0;i<6;i++){

 cout<<"round "<<i+1<<" : ";

while(!(cin>>n[i])){   //当输入不是字符时,执行下面循环

cin.clear(); // 如果输入的是字符,不是数字时,应利用cin.clear() 重置cin以接受新的输入。

while (cin.get() != '
')   continue;  //while中用cin.get() 读取行尾之前的输入,从而删除这行错误输入

cout<<"Pleae enter a number:  ";

}  }

12.写入文本文件文件输出:必须包含头文件 #include<fstream>,需要声明一个或多个ofstream对象,将ofstream对象与文件关联用open()方法。使用完文件用close()关闭。可以使用ofstream对象和运算符<<来输出各种类型的数据。(和cout类似)

   

#include "stdafx.h"

#include<iostream>

#include<fstream>

 

int _tmain()

using namespace std;

char automobile[50];

int year;

double a_price,d_price;

 

ofstream outfile; //创建一个输出的ofstream对象,可以像使用cout一样使用outfile

outfile.open("carinfo.txt"); //关联的文件,创建一个名为carinfo.txt的文件

cout << "Enter the make and model of automobile: ";

cin.getline(automobile, 50);

cout << "Enter the model year: ";

cin >> year;

cout << "Enter the original asking price: ";

cin >> a_price;

d_price = 0.913*a_price;

cout << fixed;

cout.precision(2);  //设置显示精确度为2位并返回上一次的设置。

cout.setf(ios_base::showpoint); //显示浮点数小数点后的0;

cout << "Make and Model: " << automobile << endl;

cout << "Year: " << year << endl;

cout << "Was asking $" << a_price << endl;

cout << "Now asking $" << d_price << endl;

 

outfile << fixed;  //可以像使用cout一样使用outfile

outfile.precision(4);  ////设置输出精确度为4位并返回上一次的设置。

outfile.setf(ios_base::showpoint);  

outfile << "Make and Model: " << automobile << endl;

outfile << "Year: " << year << endl;

outfile << "Was asking $" << a_price << endl;

outfile << "Now asking $" << d_price << endl;

 

outfile.close();  //关闭文件

return 0;

}

13.读取文本文件:必须包含头文件 #include<fstream>,需要声明一个或多个ifstream对象,将ifstream对象与文件关联用open()方法。使用完文件用close()关闭。可以使用ifstream对象和运算符>>来读取各种类型的数据。

可以使用ifstream对象和get()方法来读取一个字符。使用ifstream对象和getline()方法来读取一行字符。

使用ifstream对象和eof() 、fail()等方法来判断输入是否成功

ifstream对象本身作为测试条件时,如果最后一个读取操作成功,它将会被转化为bool值true.(和cin类似)

检查文件是否被成功打开用方法is_open();

#include "stdafx.h"

#include<iostream>

#include<fstream>

#include<cstdlib> //支持exit()

const int SIZE = 60;

 

int _tmain()

using namespace std;

char filename[SIZE];

ifstream infile;

cout << "Enter name of data file: ";

cin.getline(filename, SIZE);  //输入要读取的文件名

infile.open(filename);

if (!infile.is_open()) //检查文件是否被成功打开

{

cout << "Could not open the file" << endl;

cout << "Program terminating.
";

exit(EXIT_FAILURE);

}

double value;

double sum = 0.0;

int count = 0;

infile >> value; //在循环外读取第一个值

while (infile.good()) //读取输入操作成功且未到达EOF

{

++count;

sum += value;

infile >> value;  //读取下一个值

}

if (infile.eof()) //判读是否达到EOF

cout << "End of file read.
";

else if (infile.fail()) //可以检测EOF或类型不匹配

cout << "Input terminated by data mismatch.
";

else

cout << "Input terminated by unkonwn reason.
";

if (count == 0)

cout << "No data processed.
";

else

{

cout << "Item read: " << count << endl;

cout << "Sum: " << sum << endl;

cout << "Average: " << sum / count << endl;

}

infile.close();

return 0;

}

infile >> value; //在循环外需进行一次读取

    while (infile.good()) //

    {  …….

          infile >> value;  //读取下一个值

    } 

以上这一部分等效于

while (infile>>value)  //读取并测试是否成功

{ …… } 

————————————————

原文链接:https://blog.csdn.net/lvliang2017232003/java/article/details/85645040

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/4102.html

管理员

相关推荐
2025-06-07

在数字化阅读与实体书籍并存的时代,图书租赁管理系统正成为图书馆、书店及共享书吧提升运营效率的…

955
2025-06-07

在当今快节奏的校园生活中,外卖已成为学生和教职工日常生活中不可或缺的一部分。随着外卖需求的不…

998
2025-06-07

“金融市场瞬息万变,能否用Python实现同花顺自动化交易?” 这个问题,正是当下许多投资者和技术开发…

779
2025-06-07

你是否想过用中文编写一款高效、稳定的多用户聊天软件? 对于中小型团队或个人开发者而言,*易语言*…

1,019
2025-06-07

一、系统架构设计 分层架构: 前端层:用户端(H5/小程序/APP)+ 管理后台(Web) 服务层:抽奖核心…

675
2025-06-07

在数字化时代,二手交易市场正以前所未有的速度蓬勃发展。无论是闲置物品的流通,还是环保意识的提…

470
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号