姓名:張立斐 ? 學號:19020700001? ?學院:電子工程學院?
轉自:https://blog.csdn.net/lvliang2017232003/article/details/85645040
【嵌牛導讀】C++學習
【嵌牛鼻子】C++
【嵌牛提問】如何學習C++?
【嵌牛正文】
1验靡、strcmp( ):比較兩個字符串,相同返回0;第一個字符串按字母順序排在第二個字母之后將返回負值;
2.類型別名:
#define BYTE char? //使用預處理器样悟,用char替代所有的BYTE咏瑟,BYTE是char的別名
typedef char byte? //使用關鍵字typedef ,byte是char的別名。typedef不會創(chuàng)建新類型
3. C++11中基于范圍的for循環(huán)归榕,對數(shù)組(或vector,array)的每個元素執(zhí)行相同的操作:
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;? } // 要修改數(shù)組元素時,循環(huán)變量要取地址吱涉,改為&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.邏輯運算符||? 邏輯運算符&&
逞敷!的優(yōu)先級大于邏輯運算符和關系運算符(> < =), &&優(yōu)先級大于|| 狂秦,關系運算符優(yōu)先級大于 &&,||
8.定義二維字符串數(shù)組方式:
(1)char指針數(shù)組: char * cities[3]={"Beijing", "TianJin", "ShangHai"};
(2)char數(shù)組的數(shù)組 char? cities[3][10]={"Beijing", "TianJin", "ShangHai"};
(3)string對象數(shù)組 string? cities[3]={"Beijing", "TianJin", "ShangHai"};
希望字符串可以修改的情況下推捐,string類可以自動調節(jié)大小更方便裂问。
9.switch語句,while語句中,將int值與枚舉量標簽進行比較時玖姑,枚舉量將提升為int愕秫。
switch語句中每一個case的標簽必須是int或char,還必須是常量
10.continue :跳出循環(huán)剩余部分,執(zhí)行新的一輪循環(huán)
? ? break :跳出循環(huán)剩余部分焰络,不在執(zhí)行循環(huán)語句
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])){? //當輸入不是字符時戴甩,執(zhí)行下面循環(huán)
cin.clear(); // 如果輸入的是字符,不是數(shù)字時闪彼,應利用cin.clear() 重置cin以接受新的輸入甜孤。
while (cin.get() != '\n')? continue;? //while中用cin.get() 讀取行尾之前的輸入,從而刪除這行錯誤輸入
cout<<"Pleae enter a number:? ";
}? }
12.寫入文本文件文件輸出:必須包含頭文件 #include<fstream>畏腕,需要聲明一個或多個ofstream對象缴川,將ofstream對象與文件關聯(lián)用open()方法。使用完文件用close()關閉描馅“芽洌可以使用ofstream對象和運算符<<來輸出各種類型的數(shù)據(jù)。(和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; //創(chuàng)建一個輸出的ofstream對象铭污,可以像使用cout一樣使用outfile
outfile.open("carinfo.txt"); //關聯(lián)的文件,創(chuàng)建一個名為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); //顯示浮點數(shù)小數(shù)點后的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對象與文件關聯(lián)用open()方法磅网。使用完文件用close()關閉谈截。可以使用ifstream對象和運算符>>來讀取各種類型的數(shù)據(jù)。
可以使用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.\n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
infile >> value; //在循環(huán)外讀取第一個值
while (infile.good()) //讀取輸入操作成功且未到達EOF
{
++count;
sum += value;
infile >> value;? //讀取下一個值
}
if (infile.eof()) //判讀是否達到EOF
cout << "End of file read.\n";
else if (infile.fail()) //可以檢測EOF或類型不匹配
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated by unkonwn reason.\n";
if (count == 0)
cout << "No data processed.\n";
else
{
cout << "Item read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
infile.close();
return 0;
}
infile >> value; //在循環(huán)外需進行一次讀取
? ? while (infile.good()) //
? ? {? .......
? ? ? ? ? infile >> value;? //讀取下一個值
? ? }
以上這一部分等效于
while (infile>>value)? //讀取并測試是否成功
{ ...... }
————————————————
版權聲明:本文為CSDN博主「lvliang2229」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權協(xié)議诽表,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/lvliang2017232003/article/details/85645040