函数
#include
在最终编译之前,用iostream文件替换替换该编译指令
#include
using namespace std;
double C2F(double C);
int main(){
double C;
cout<<"Please enter a Celsius value:";
cin>>C;
double F=C2F(C);
cout<<C<<" degrees Celsius is "<<F<<" degrees Fahrenheit";
}
double C2F(double C){
double F=1.8*C+32;
return F ;
}
#include
void format_time(int hours,int minutes)
{
std::cout<<"Time: "<<hours<<":"<<minutes<<std::endl;
}
int main()
{
int hours,minutes;
std::cout<<"Enter the number of hours: ";
std::cin >> hours;
std::cout<<"Enter the number of minutes: ";
std::cin >> minutes;
format_time(hours,minutes);
return 0;
}
std::cin>>val1>>val2; 可以输入多个数据。
#include
int main()
{
int hours,minutes;
std::cout<<"Enter the number of hours and minutes: \n";
std::cin >> hours >> minutes;
std::cout<<hours<<":"<<minutes<<std::endl;
return 0;
}
在 C++ 中,函数和变量一样都需要先定义后使用;
std::cout 可以输出多个变量,std::cin 可以输入多个变量;
C++ 的标准输入输出被定义在 iostream 中,需要先包含头文件才可以输入输出;
cin 和 cout 位于 std 命名空间下。
#include
using namespace std;
int main(){
int input_number;
cout<<hex<<"the number input is ";
cin>>input_number;
cout<<"output number is "<< input_number<<endl;
return 0;
}
#include
int main(){
using namespace std;
//cout.setf(ios_base::fixed,ios_base::floatfield)
long long world_population ,us_population;
cout<<"Enter the world population:";
cin>>world_population;
cout<<"Enter the population of the US:";
cin>>us_population;
double rate=(double)us_population/world_population*100;
cout<<"The population of the US is "<<rate<<"% of the world population.";
return 0;
cout.setf(ios_base::fixed,ios_base::floatfield)
强制类型转换的优先级为 2,高于乘除运算;使用 cin 读取数据产生错误,如果不进行特殊处理,则之后调用 cin 也无法正常读取缓冲区或触发用户输入;cin.clear() 可以清除错误。
这里字符串长度要为定义的+1,即char[3]只能定义两个字符
#include
using namespace std;
int main(){
char str[3]="ab";
char str1[3]="abcdef";
//char str[3]="a";
cout<<str1<<endl;
cout<<str<<endl;
return 0 ;
}
若是超出字符,会提示出错,编译的化也会报错
若是短与定义会加入/0
#include
using namespace std;
int main(){
char str[3]="ab";
char str1[3]="a";
//char str[3]="a";
cout<<str<<endl;
cout<<str1<<endl;
return 0 ;
}
#include
#include
using namespace std;
int main(){
char name[20],name1[20];
strcpy(name,"ancsaca");
strcpy(name1,name);
cout<<"name value: "<<name<<endl;
cout<<"name1 value: "<<name1<<endl;
return 0;
}
char actor[30]
short betsie[100]
float chuck[13]
long double dipsea[64]
array<char ,30> actor
array<short,100> betsie
array<float,13>chuck
array<long double ,64> dipsea
cout<<ideas[1];
cout<<*(ideas+1)
double* teddy=&ted;
cout<<*teddy;
#include
using namespace std;
int main(){
const int len_name=15;
char first_name[len_name];
char last_name[len_name];
char grade;
int age;
cout<<"what is your first name? ";
cin.get(first_name,len_name).get();
cout<<"what is your last name? ";
cin>>last_name;
cout<<"what letter grade do you deserve?";
cin>>grade;
cout<<"what is your age?";
cin>>age;
cout<<"Name: "<<last_name<<","<<first_name<<endl;
cout<<"Grade: ";
cout.put(grade+1);
cout<<endl;
cout<<"Age: "<<age<<endl;
}
#include
#include
int main()
{
using namespace std;
string name;
string dessert;
cout << "Enter your name\n";
getline(cin,name);
cout << "Enter your favorite dessert\n";
getline(cin,dessert);
cout <<"I have some delicious "<< dessert;
cout << " for you, "<<name<<".\n";
return 0;
}
#include
#include
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calory;
};
int main()
{
CandyBar* snack=new CandyBar[3] {
{"brand 1",11,111},
{"brand 2",22,222},
{"brand 3",33,333}
};
for(int i=0;i<3;i++)
{
cout<<"brand:"<<snack[i].brand<<endl;
cout<<"weight:"<<snack[i].weight<<endl;
cout<<"calorie:"<<snack[i].calory<<endl;
}
delete []snack;
return 0;
}
4.8.4
0369
12
k=8
#include
using namespace std;
int main(){
int i=1
for(i;i<=64;i*=2)
cout<<i<<endl
}
5.11表达式和语句
#include
using namespace std;
int main(){
int first,second;
short sum=0;
cin>>first>>second;
cout <<endl;
for (int i=first;i<=second;i++){
sum+=i;
}
cout<<"the sum of number is "<<sum<<endl;
return 0;
}
参考内容5.1.14C语言风格字符比较
4.2.4getline 和get
#include
#include
using namespace std;
int main(){
char word[10];
int num=0;
char ch;
cout<<"Enter words (to stop ,type the word done) "<<endl;
do{
num++;
cin>>word;
cin.get(ch);
}while(strcmp(word,"done")!=0);
cout<<" for sure"<<endl;
cout<<"you entered a total of "<<num-1<<" words."<<endl;
cout<<"ch is "<<ch<<endl;
return 0;
}
#include
#include
using namespace std;
int main(){
char ghost[15]="abcdefg";
//char * str="abcdefg";
const char * str="abcdefg";
int n1 =strlen(ghost);
int n2 =strlen(str);
int n3=strlen("abcdefg");
cout<<"n1= "<<n1<<endl;
cout<<"n2= "<<n2<<endl;
cout<<"n3= "<<n3<<endl;
return 0;
}
函数将字符串输入为char类型,这里之所以要用const char
等号两边的变量类型不一样,那么编译器会进行一种叫做 implicit conversion 的操作来使得变量可以被赋值。
在上面的表达式中等号右边的"abc"是一个不变常量,在c++中叫做string literal,type是const char *,而p则是一个char指针。如果强行赋值会发生什么呢?没错,就是将右边的常量强制类型转换成一个指针,结果就是我们在修改一个const常量。编译运行的结果会因编译器和操作系统共同决定,有的编译器会通过,有的会抛异常,就算过了也可能因为操作系统的敏感性而被杀掉。
1定义函数
2提供原型
3调用函数
void set_array(int begin ,int end ,int value){
for(int *pt=begin;pt!=end;pt++)
pt*=value;
}
#include
using namespace std;
void count();
int main(){
for(int j=0;j<10;j++)
count();
return 0;
}
void count(){
static int i=0 ;//定义初始化
//i=0;//赋值
for(int a=0;a<10;a++)
i+=a ;
cout<<"out ="<<i<<endl;
}
最小申明区域只有有局部变量不管在前在后都会被覆盖
具体内容再9-3中
#include
namespace Jill{
double fetch=2;
}
using namespace std;
int main(){
double fetch=3;
using namespace Jill;
cout<<"fetch="<<fetch<<endl;
cout<<"Jill_fetch= "<<Jill::fetch<<endl;
}
即使后使用using namespacee fetch依然是局部变量
1
4,1,2
2
2
4,1,2
2
// filename 9-1.cpp
#include
#include"golf.h"
const int Men=5;
int main()
{
golf golfer[Men];
int i;
for( i=0;i<Men;i++)
{
if(setgolf(golfer[i])==0)
break;
}
for( int j=0;j<i;j++)
showgolf(golfer[j]);
golf ann;
setgolf(ann,"Ann Birdfree",24);
showgolf(ann);
handicap(ann,4);
showgolf(ann);
return 0;
}
golf.cpp
// filename golf.cpp
#include
#include
#include"golf.h"
using namespace std;
int setgolf(golf&g)
{
cout<<"Enter the golfer's name:\n";
cin.get(g.fullname,Len);
if(g.fullname[0]=='\0')
return 0;
cout<<"Enter the handicap for "<<g.fullname<<endl;
while(!(cin>>g.handicap))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"Please enter an integer.\n";
}
cin.get();
return 1;
}
void setgolf(golf&g,const char*name,int hc)
{
strncpy(g.fullname,name,Len);
g.handicap=hc;
}
void handicap(golf&g,int hc)
{
g.handicap=hc;
}
void showgolf(const golf&g)
{
cout<<"Golfer:"<<g.fullname<<"\n";
cout<<"Handicap:"<<g.handicap<<"\n\n";
}
golf.h
// filename golf.h
const int Len=40;
struct golf
{
char fullname[Len];
int handicap;
};
void setgolf(golf&g,const char*name,int hc);
int setgolf(golf&g);
void handicap(golf&g,int hc);
void showgolf(const golf&g);
6.7中有知识点
#include
using namespace std;
int main(){
cout<<"enter a number "<<endl;
int num;
for (int i=0;i<5;i++){
while(!(cin>>num)){
cin.clear();
while(cin.get()!='\n')
continue;
cout<<i<<" times "<< "enter a number"<<endl;
}
cout<<i<<" number is "<<num<<endl;
}
return 0;
}
cin 要有一个回车结束,这里面如果出现非整数,会直接全部清空,到换行符’\n’
### 9.3.2 string 和char的区别
知识点在4-10
// static.cpp -- using a static local variable
#include
// constants
const int ArSize = 10;
// function prototype
void strcount(const char * str);
int main()
{
using namespace std;
char input[ArSize];
char next;
cout << "Enter a line:\n";
cin.get(input, ArSize);
while (cin)
{
cin.get(next);
while (next != '\n') // string didn't fit!
cin.get(next); // dispose of remainder
strcount(input);
cout << "Enter next line (empty line to quit):\n";
cin.get(input, ArSize);
}
cout << "Bye\n";
// code to keep window open for MSVC++
/*
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
*/
return 0;
}
void strcount(const char * str)
{
using namespace std;
static int total = 0; // static local variable
int count = 0; // automatic local variable
cout << "\"" << str <<"\" contains ";
while (*str++) // go to end of string
count++;
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
编码
#include
#include
using namespace std;
void strcount(const string str);
int main(){
string input;
cout << "Enter a line:\n";
getline(cin,input);//4-10 知识点
while (input!="")
{
strcount(input);
cout << "Enter next line (empty line to quit):\n";
getline(cin,input);//4-10 知识点
}
cout << "Bye\n";
// code to keep window open for MSVC++
/*
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
*/
return 0;
}
void strcount(const string str){
static int total = 0; // static local variable
int count = 0;
cout << "\"" << str <<"\" contains ";
while (str[count]) // go to end of string
count++;
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
9-4.cpp
// filename 9-4.cpp
#include
#include"sales.h"
using namespace std;
int main()
{
using namespace SALES;
Sales salesBook;
double salesList[]={12.2,11.16,10.61,16.24,11.53};
setSales(salesBook,salesList,sizeof(salesList)/sizeof(salesList[0]));
showSales(salesBook);
Sales salesPen;
setSales(salesPen);
showSales(salesPen);
return 0;
}
sales.h
namespace SALES
{
const int QUARTERS=4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
//copiesthelesserof4ornitemsfromthearrayar
//tothesalesmemberofsandcomputesandstoresthe
//average,maximum,andminimumvaluesoftheentereditems;
//remainingelementsofsales,ifany,setto0
void setSales(Sales&s,const double ar[],int n);
//gatherssalesfor4quartersinteractively,storesthem
//inthesalesmemberofsandcomputesandstoresthe
//average,maximum,andminimumvalues
void setSales(Sales&s);
//displayallinformationinstructures
void showSales(const Sales&s);
}
sales.cpp
// filename sales.cpp
#include
#include"sales.h"
namespace SALES
{
using std::cout;
using std::cin;
using std::endl;
static double calaverage(double arr[],unsigned arrSize)
{
double sum=0;
for( int i=0;i<arrSize;i++)
sum+=arr[i];
return sum/arrSize;
}
static double calmax(double arr[],unsigned arrSize)
{
double max=arr[0];
for( int i=1;i<arrSize;i++)
{
if(max<arr[i])
max=arr[i];
}
return max;
}
static double calmin(double arr[],unsigned arrSize)
{
double min=arr[0];
for( int i=1;i<arrSize;i++)
{
if(min>arr[i])
min=arr[i];
}
return min;
}
void setSales(Sales&s,const double ar[],int n)
{
unsigned times=n<QUARTERS?(unsigned)n:QUARTERS;
for( int i=0;i<times;i++)
s.sales[i]=ar[i];
for( int i=times;i<QUARTERS;i++)
s.sales[i]=0;
s.average=calaverage(s.sales,times);
s.max=calmax(s.sales,times);
s.min=calmin(s.sales,times);
}
void setSales(Sales&s)
{
cout<<"Enter4sales:\n";
for( int i=0;i<QUARTERS;i++)
{
cout<<"sales"<<i+1<<":";
cin>>s.sales[i];
}
s.average=calaverage(s.sales,QUARTERS);
s.max=calmax(s.sales,QUARTERS);
s.min=calmin(s.sales,QUARTERS);
}
void showSales(const Sales&s)
{
cout<<"sales:";
for( int i=0;i<QUARTERS;i++)
cout<<s.sales[i]<<" ";
cout<<endl;
cout<<"average:"<<s.average<<endl;
cout<<"max:"<<s.max<<endl;
cout<<"min:"<<s.min<<endl;
}
}
类是现实中物体的抽象,类声明定义了数据如何储存,同时指定了用来访问和操作这些数据的方法。
this指针是类方向可以使用的指针,它指向类调用方法的对象。*this是对象本身