c++Day1

c-c高级-数据结构-高级编程-c++ --c#

c c++ java

一、1、linux下安装

在用户目录中单独创建一个文件夹存放下载后的3个安装文件,然后进入到目录中执行下面命令
//cd 到放的目录下  例:cd  ~/Desktop
sudo dpkg -i *.deb        命令安装
sudo apt-get install g++  在线安装
sudo apt-get remove g++	  卸载之前的版本

2、c语言面向过程:

    大象装冰箱 总共分3步  每步都亲自去做  
    
    
    c++面向对象:
    关注点不再是放大象的过程 而是冰箱 
    
    冰箱模型
    生产冰箱
    
    冰箱.开门
    冰箱.放物品
    冰箱.关门
    
	
 c与c++区别总结:
	1) 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,
		使用的时候一个一个依次调用就可以了。 
	2) 面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,
		而是为了描叙某个事物在整个解决问题的步骤中的行为。

3、环境

	linux下
	1.c++编译器 g++ 
	2.编辑器   vi 
	3、简介	
	头文件:
		#include<iostream>	 没有.h 
	4.C++兼容C语言中的头文件   
        strcpy  #include<string.h> 改成:#include<cstring>

	5.源文件  
	.cpp    后缀名 

二、第一个程序

第一个程序
例子1:
#include//input output stream 输入输出头文件

int main()
{
    return 0;//省略
}

编译:g++ demo1.cpp 
执行:./a.out 


例子2:
#include<iostream>//input output stream  输入输出头文件
using namespace std;//标准库的名字空间std

int main()
{
    cout<<"hello world"<<endl<<"comeon baby!"<<" day day up!";
    //cout<<endl;
    //cout 输出  <<数据  endl 换行
}

练习:
打印心形
 * * * *
*   *   *
 *     * 
  *   *	
	*

例子3:cout自动识别类型
#include<iostream>//input output stream  输入输出头文件
using namespace std;
int main()
{
    int a=90;
    char ch='x';
    cout<<a<<endl;
    cout<<"ch:"<<ch<<endl;//识别类型
    return 0;//省略
}

例子4:了解
在输出数据时,为简便起见,往往不指定输出的格式,由系统根据数据的类型采取默认的格式,但有时希望数据按指定的格式输出,如要求以十六进制或八进制形式 输出一个 整数,对输出的小数只保留两位小数等。
有两种方法可以达到此目的。

使用控制符的方法 

#include<iostream>//相当于stdio.h  input output stream 输入输出头文件
#include<iomanip>  //注意:添加头文件
using namespace std;
int main()
{
    //整数输出
    int a=10;
    cout<<"dec:"<<dec<<a<<endl;//10 十进制
    cout<<"hex:"<<hex<<a<<endl;//a 十六进制
    cout<<"Oct:"<<setbase(8)<<a<<endl;//12 八进制

    //输出宽度
    const char *p="good";
    const char *q="china";
    cout<<setw(7)<<p<<endl;//指定输出宽度为7 不足7位 默认空格补齐
    cout<<setfill('*')<<setw(8)<<p<<endl;  //输出宽度8位 不足8位 用* 补齐
    //注意:只对其后边的第一个输出项有效
    

    //指数形式输出
    double pi=22.0/7.0;
   
    cout<<setiosflags(ios::scientific)<<setprecision(8)<<pi<<endl;//指数形式输出 小数点后保留8位
    

}

例子5 四舍五入
#include
#include
using namespace std;
int main()
{

    double pi=3.1415926;
    cout<<setprecision(4)<<pi<<endl;//3.142 有效数字保留4位
    cout<<setiosflags(ios::fixed)<<setprecision(4)<<pi<<endl;//3.1416 小数点后保留4位


}

三、cin输入

1.输入流 : 输入流对象可以自动识别基本类型的输出类型
实例:
#include//input output stream 输入输出头文件
using namespace std;
int main()
{
int a;
int b;
cout<<“请输入整数:”<<endl;
cin>>a>>b;//输入不加回车 遇到空格或回车结束输入
cout<<“a:”<<a<<endl;
cout<<“b:”<<b<<endl;
}

练习:键盘输入一个字母 如果小写转为大写 如果大写 则转为小写
#include//input output stream 输入输出头文件
using namespace std;
int main()
{
char ch;
cout<<“input:”<<endl;
cin>>ch;

    if(ch>='A'&&ch<='Z')
    {
        ch += 32;
    }
    else if(ch>='a'&&ch<='z')
    {
        ch -=32;
    }

    cout<<ch<<endl;
}

2.getline
cin同scanf 空格结束输入 ,
cin.getline()函数是处理数组字符串的,
原型:cin.getline(char * , N,结束符)
第一个参数为一个char指针,第二个参数为数组字符串长度 fgets(buf,size fp)

以指定的地址为存放第一个读取的字符的位置,依次向后存放读取的字符,直到读满N-1个,或者遇到指定的结束符为止
若不指定结束符,则默认结束符为’\n’
cin.getline()当输入超长时,会引起cin函数的错误,后面的cin操作将不再执行

实例:
#include//input output stream 输入输出头文件
using namespace std;
int main()
{
char str[20];
//cout<<“输入:”<<endl;
//cin>>str;
//cout<<str<<endl;

char buf[20];
cout<<"输入:"<<endl;
cin.getline(buf,sizeof(buf));//默认结束符为'\n'
cout<<buf<<endl;

}

四、c++对c增强:实用性增强

使用for循环输出10个整数
#include
using namespace std;

int main()
{

for(int i=1;i<=10;i++)//使用的位置定义  注意:循环内部生效
{
	cout<<i;
}
cout<<i<<endl;

}

2.名字空间

namespace :名字空间也叫命名空间 是指标识符的各种范围

(1)、使用命名空间的目的:避免名字冲突 造成空间污染

(2)如何使用名字空间
c++标准库中 所有标识符都被定义在std中
1)标准库的标识符都放到std中 使用时 加上using namespace std;	

例子1:
#include//input output stream 输入输出头文件
using namespace std;//标准库的名字空间std

int main()
{
    cout<<"hello world"<<endl<<"comeon baby!"<<" day day up!";
    //cout<<endl;
    //cout 输出  <<数据  endl 换行
}

	
 2)使用std里的名字时 添加std::
#include<iostream>//input output stream  输入输出头文件
int main()
{
    std::cout<<"hello"<<std::endl;//cout是std空间的名字 endl是std空间的名字  ::作用域限定符

}


 3)上边添加使用到的名字using std::cout;
#include<iostream>//input output stream  输入输出头文件

using std::cout;
using std::endl;
int main()
{
    cout<<"hello"<<endl;//cout是std空间的名字

}

(3)自定义名字空间 名称(name)可以是符号常量、变量、宏、函数、结构体、枚举、类和对象等等

1)自定义名字空间teacher 花括号中的标识符 都属于名字空间teacher
实例:
#include<iostream>//input output stream  输入输出头文件
#include<cstring>
using namespace std;
namespace teacher
{
    char name[20]="小明";
    int age;
}

using namespace teacher;
int main()
{
    strcpy(name,"小");
    cout<<name<<endl;
}


2)不同的名字空间的标识符可以同名 不会发生冲突
    #include<iostream>//input output stream  输入输出头文件
    #include<cstring>
    using namespace std;
    namespace teacher
    {
        char name[20]="小明";
        int age;
    }

    namespace stu 
    {
        char name[20]="xiaohuihui";
    }
    using namespace teacher;
    using namespace stu;
    int main()
    {
        cout<<stu::name<<endl;//避免歧义
    }


    名字空间内可以定义函数
    #include<iostream>//input output stream  输入输出头文件
    #include<cstring>
    using namespace std;
    namespace teacher
    {
        char name[20]="小明";
        int age;

        void show()
        {
            cout<<name<<endl;
        }
    }

    using namespace teacher;
    int main()
    {
        cout<<name<<endl;
        show();
    }


练习:分别在teacher和student命名空间中添加函数showName()来输出name;
	   main函数中,调用函数showName();

            
        #include<iostream>
        using namespace std;
        namespace teacher 
        {
            char name[20]="小丽";
            void showName()
            {
                cout<<name<<endl;
            }
        }

        namespace student 
        {
            char name[20]="小红";
            void showName()
            {
                cout<<name<<endl;
            }
        }

        int main()
        {
            teacher::showName();
            student::showName();
        }



3)	如果有局部同名 局部优先(内部优先)
 #include<iostream>//input output stream  输入输出头文件
    #include<cstring>
    using namespace std;
    namespace teacher
    {
        char name[20]="小明";
    }

    namespace stu 
    {
        char name[20]="xiaohuihui";
    }

    using namespace teacher;
    using namespace stu;
    int main()
    {
		char name[20]="大明";
        cout<<name<<endl;//大明
    }


4)全局变量 会存入匿名空间 也就是默认的名字空间  输出匿名空间的名字用::
 #include<iostream>//input output stream  输入输出头文件
    #include<cstring>
    using namespace std;

	char name[20]="张三";//匿名空间

    namespace teacher
    {
        char name[20]="小明";
    }

    namespace stu 
    {
        char name[20]="xiaohuihui";
    }

    
    int main()
    {
		char name[20]="大明";
        cout<<name<<endl;
		cout<<teacher::name<<endl;//输出teacher名字空间名字
		cout<<::name<<endl;//输出匿名空间名字
    }            
            

 5)名字空间可以嵌套的
#include<iostream>//input output stream  输入输出头文件
using namespace std;
namespace s_A
{
    namespace s_B 
    {
        struct stu 
        {
            char name[20];
            int age;
        };
    }
}
int main()
{
    s_A::s_B::stu  student1;
    student1.age=18;
    cout<<student1.age<<endl;
}

3、c++对c增强(三)—bool

bool类型
(1)、是c++对c的增强 是c++中基本数据类型
(2)、专门表示逻辑真假
(3)、bool类型只有真 true(非0) false假(0)
例子1:bool类型只有两种结果 1 0 如果是整数或负数 会自动转为1

例子2:bool类型大小 当前平台是1个字节 面试:至少一个字节
#include<iostream>//input output stream  输入输出头文件
using namespace std;
int main()
{
    bool x;
    x=100;
    x=-100;
    x=true;
    cout<<x<<endl;//1  真

    x=0;
    x=false;
    cout<<x<<endl;//0 

    cout<<"size:"<<sizeof(x)<<endl;//1
}


例子3:应用
#include<iostream>//input output stream  输入输出头文件
using namespace std;
int main()
{
    bool gender=1;
    cout<<(gender?"男生":"美女")<<endl;

}

4 c++对c增强(四)–三目运算符

C语言返回变量的值 C++语言是返回变量本身

实例:
#include<iostream>
using namespace std;
int main()
{
    int a=8,b=9;
    a<b?a:b=100;//不报错

}

5.类型转换检测增强(五)

char *p=malloc(sizeof(int));//error  必须强换

五、c++字符串类型

1.c++中字符串与c中字符串区别

c风格:char str[200]="hello" char *p="world"  strcat  

2.如何使用string类型

(1)必须加#include<iostream>
(2)必须加using namespace std;
(3)必须加#include<string>

3.基本使用

示例1:创建string对象及初始化
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s1;//定义

    //初始化
    string s2="good good study";
    cout<<"s2:"<<s2<<endl;

    string s3("day day up");
    cout<<"s3:"<<s3<<endl;

    string s4(s3);
    cout<<"s4:"<<s4<<endl;
}
    
示例2:赋值 =      string对象可以通过赋值符号 直接将值赋予string对象
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s1;//定义
    s1="hello";//strcpy 

    string s2;
    s2=s1;
    cout<<"s1:"<<s1<<endl;
    cout<<"s2:"<<s2<<endl;
}


示例3:拼接
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s1;//定义
    s1="hello";//strcpy 

    string s2="   world";
    
    string s3=s1+s2;//strcat
    cout<<"s1+s2:"<<s3<<endl;
}


示例4:比较 == != < > <= >=  真假 
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s1;//定义
    s1="hello";//strcpy 

    string s2="   world";

    if(s1>s2)
    {
        cout<<">"<<endl;
    }
    else 
    {
        cout<<"<="<<endl;
    }
}


示例5:string类型的成员函数 empty() 和 size()  
empty() 如果字符串是空 则返回真 否则返回假
size() 返回字符串大小 有效字符个数

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s1;//定义

    cout<<s1.empty()<<endl;//1 空

    s1="wake up";
    cout<<s1.empty()<<endl;//0 非空

    cout<<"size:"<<s1.size()<<endl;//7

}     

示例8:获取字符s1[i];
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s1;//定义

    cout<<s1.empty()<<endl;//1 空

    s1="wake up";
    cout<<s1.empty()<<endl;//0 非空

    cout<<"size:"<<s1.size()<<endl;//7

    //循环打印字符
    for(int i=0;i<s1.size();i++)
    {
        cout<<s1[i]<<" ";
    }
    cout<<endl;

}

示例9:getline(cin,str) str是string类型 可以读入一行字符串 包括空格 getline 不是成员函数
#include//input output stream 输入输出头文件
#include
using namespace std;
int main()
{
cout<<“input string:”<<endl;
string str;
getline(cin,str);
cout<<str<<endl;
}

练习:getline获取一个字符串,将其中的大写字母转换为小写字母

#include
#include
using namespace std;

int main()
{
string s;
cout<<“input a string:”<<endl;
getline(cin,s);
for(int i=0;i<s.size();i++)
{
if(s[i]>=‘A’&&s[i]<=‘Z’)
{
s[i]+=32;
}
}

cout<<s<<endl;

}

复习:

c++结构体:
struct stu
{
string name;
int age;
};

(1)定义变量
stu s1;

(2)赋值
s1.name=“xiaoming”

(3)输入 输出
cout<<s1.name

(4)指针操作
stu *p=&s1;

.和->的区别
结构体变量.成员
结构体指针变量->成员

####练习:定义一个变量,保存N本书的信息(name,author,price),然后打印输出。
#include//input output stream 输入输出头文件
#include
using namespace std;
#define N 2
struct Book
{
string name;
string author;
int price;
};
int main()
{
Book arr[N];
cout<<“请输入:书名 作者 价格”<<endl;
for(int i=0;i<N;i++)
{

	getline(cin,arr[i].name);//遇到回车结束输入
	getline(cin,arr[i].author);
	cin>>arr[i].price;//10
	getchar();//取回车
}

for(int i=0;i<N;i++)
{
	cout<<"书名:"<<arr[i].name<<endl;
	cout<<"作者:"<<arr[i].author<<endl;
	cout<<"价格:"<<arr[i].price<<endl;
}

}

练习1:定义结构体 结构体成员 string name int age
编写函数 实现输入N个学生信息
键盘输入某个学生姓名 如果该学生存在 则输出其基本信息 如果不存在 则输出"not found"
#include
#include
#include
using namespace std;

#define N 3

struct student
{
string name;
int age;
};

void inputStu(student *p)
{
cout<<“请输入”<<N<<“个人信息: 人名 年龄”<<endl;
for(int i=0;i<N;i++)
{
getline(cin,p[i].name);//小明\0 gets
cin>>p[i].age;//1\n
getchar();//取年龄后的回车

}

}
void findStu(student *p,string findName)
{
for(int i=0;i<N;i++)
{
if(p[i].name==findName)
{
cout<<“name:”<<p[i].name<<“age:”<<p[i].age<<endl;
return;
}
}
cout<<“not found”<<endl;
}
int main()
{
student arr[N];//student
string findName;
inputStu(arr);

cout<<"请输入你要查找的人名:"<<endl;
getline(cin,findName);//gets()
findStu(arr,findName);

}

作业:
随机点名 定义名字数组string names[10]={“rose”,“jack”,“tom”}; 字符串数组
随机抽取一个数组中的人员

	头文件:#include<cstdlib>
			#include<ctime>
	方法:srand(time(0));初始化随机种子
			rand()%10;生成随机数
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

encounter♌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值