1 Star 0 Fork 0

Sjianzhao / C++学习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
基础学习.cpp 11.45 KB
一键复制 编辑 原始数据 按行查看 历史
Sjianzhao 提交于 2021-04-06 18:34 . update
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
#include<iostream> //类似于#include<stdio.h>的作用 i - o - stream 输入输出流
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define num 6
#define casse1 ((0x80>>(num))&1)
#define casse2 ((0x80>>(num))&2)
#define casse3 ((0x80>>(num))&4)
#define casse4 ((0x80>>(num))&8)
#define casse5 ((0x80>>(num))&16)
#define casse6 ((0x80>>(num))&32)
#define casse7 ((0x80>>(num))&64)
//#pragma _C_Plus_Plus
//#extern "C"
//{
//#endif
#if casse1
int main()
{
cout << "c++ program !\n"<< casse(num) <<endl; //cout :输出流对象 ; << 插入运算符
printf("the c++\n");
system("pause");
return 0;
}
#endif
// 2
/*
//求两个值的和
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a, b, sum;
cin >> a >> b; //scanf("%d,&d",&a,&b);
sum = a + b;
cout << "a + b = " << sum <<endl; //endl ---->\n
system("pause");
return 0;
}
*/
// 3
/*
//求两个值最大值
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int _max_value(int a, int b)
{
return a > b ? a : b;
}
int main(void)
{
int max;
int a, b;
cin >> a >> b;
max = _max_value(a, b);
cout << "max = " << max << endl;
system("pause");
return 0;
}
*/
// 4 类的使用
/*
#include<iostream>
#include<stdio.h>
#include <stdlib.h>
using namespace std;
class st //定义类,类的类型有三种,公有 私有 派生
{
private: //私有类,只能在类的内部使用
int num;
int score;
public: //公有类,可以被外部使用
void setdata() //类里面的函数可以当变量用
{
cin >> num;
cin >> score;
}
void display()
{
cout << "num =" << num << endl;
cout << "score =" << score << endl;
}
};
int main(void)
{
st str1, str2;
str1.setdata();
str2.setdata();
str1.display();
str2.display();
system("pause");
return 0;
}
*/
// 5
/*
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(void)
{
int i = 2;
cout << "sum =" << endl;
system("pause");
return 0;
}
*/
// 6, 不同类型一起输入,但是输出类型不能改变(输入是什么,输出是什么)
/*
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(void)
{
char table[10];
int age;
char ageage = 0;
cin >> table >> age;
ageage = (char)age; //不支持类型转换
cout << "name = " << table << endl << "age = " << age << endl<<"ageage = "<<ageage<<endl;
system("pause");
return 0;
}
*/
// 7 函数的重载,两个函数上函数名相同,但所带参数类型或者个数不同,就可以重名
/*
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int max(int a, int b,int c)
{
return (a > b ? a : b)>c ? (a > b ? a : b) : c;
}
int max(int a, int b)
{
return a > b ? a : b;
}
int max(float a,float b)
{
return (int)(a > b ? a : b);
}
char max(char a,char b)
{
return a > b ? a : b;
}
int main(void)
{
int a, b,q,isum2,isum3;
float c, d,fsum;
char A, B,csum;
cin >> a >> b >> q >> c >> d >> A >> B;
isum3 = max(a, b,q);
isum2 = max(a, b);
fsum = (float)max(c, d);
csum = max(A, B);
cout << "int sum = " << isum3 <<" " << isum2 << endl << "float sum = " << fsum << endl << "char sum = " << csum << endl;
system("pause");
return 0;
}
*/
/* 8
//函数模板 有点类似于隐含类型转换
#include <iostream>
#include<stdlib.h>
using namespace std;
template <typename SUN>
SUN max(SUN a, SUN b)
{
return a > b ? a : b;
}
int main(void)
{
int ia,ib,imax;
float fa, fb, fmax;
cin >> ia >> ib >> fa >> fb;
imax = max(ia, ib);
fmax = max(fa, fb);
cout << imax << endl << fmax << endl;
system("pause");
return 0;
}
*/
/* 9
//变量的引用最大的作用就是在形参上
//变量的引用最大的作用就是在形参上
//变量的引用 ,引用的变量类型要一样,引用就是取别名,a和b的地址实际上是同一个,引用就是取别名,引用就是取别名,引用就是取别名
//对同一个地址区两个名字,
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main(void)
{
int a = 10;
int &b = a;
a = 20;
cout << a << endl << b << endl;
a = 40;
cout << "a = " << a << endl << "b = " << b << endl;
system("pause");
return 0;
}
*/
/* 10
//C语言中值传递的方式
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return 0;
}
int main(void)
{
int a = 10,b = 20;
swap(&a, &b); //a , b 的值通过指针间接互换
cout << "a = " << a << endl << "b = " << b << endl;
system("pause");
return 0;
}
*/
//C++中利用引用实现值的传递,实现和指针一样的操作,形参中利用引用的方式,引用的变量就是完全等于传递的值
/* 11
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
return 0;
}
int main(void)
{
int a = 10, b = 20;
swap(a, b); //a , b 的值通过指针间接互换
cout << "a = " << a << endl << "b = " << b << endl;
system("pause");
return 0;
}
*/
/**
12
#define _CRT_SECURE_NO_WARNINGS
//内置函数 inline
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
inline int max(int &a, int &b)
{
return a > b ? a : b;
}
int main(void)
{
int a, b, m;
// scanf(" a = %d", &b); //cin >> "b = " >> b; 是错的
cin >> a >> b; //输入只能输入变量,不能直接输入字符串
m = max(a,b);
cout<< "a = "<<a << "b = " << b<<"max = "<<m<< endl;
system("pause");
return 0;
}
*/
/* 13
//作用域 :: string 字符串型变量
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int a = 10;
int main(void)
{
string str[10] = { "china of woeld,whis dd" };
string str1 = "afdgfdassad";
str[1] = str1;
int a = 23;
cout << "string 的大小" << sizeof(str) << endl ;
cout << ::a<< endl; //作用域
system("pause");
return 0;
}
*/
/*
//14
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
using namespace std;
int main(void)
{
string str1 = "abcd", str2 = "ABCD", str3 = "qwer", temp;
// cin >> str1 >> str2 >> str3;
temp = str1 + str2;
str1 = str2 + str3;
cout << "string 的大小" << sizeof(string) << endl;
cout << temp << " " << str1 << endl;
system("pause");
return 0;
}
*/
// 15
//动态开辟内存和释放内存
//new 和delete是运算符,不是函数
//new int; new char[2][];
//new float(3014159)
//int *p = new int ;
//不管是C还是C++,动态开辟的内存都是用指针进行访问的,不能对开辟的内存取别名
/*
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
typedef struct sta
{
char a[10];
int b;
char c;
}st;
int main(void)
{
st *p = new st; //开辟一块空间,是st类型的 ,并用结构体指针指向它,获取内存地址 // st *p = (st *)malloc(sizeof(st));
strcpy(p->a, "xiao ming");
p->b = 1001;
p->c = 'S';
cout << p->a << " " << p->b << " " << p->c << endl;
delete p; //释放开辟的地址,delete后必须是要释放的那块内存的地址
system("pause");
return 0;
}
*/
//-------------------------------------------------------------------
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
using namespace std;
int f(int a, int b, int c)
{
int m;
if (a > b)
m = b;
else
m = a;
if (c < m)
m = c;
return m;
}
/*
// ti mu 3
int main(void)
{
int a, b, c;
cin >> a >> b >> c;
c = f(a, b, c);
cout << c << endl;
system("pause");
return 0;
}
*/
/**
// xi ti 4
int main(void)
{
int a = 3, b = 5, c;
c = a + b;
cout << "a + b = " << a + b << endl;
system("pause");
return 0;
}
*/
/*
// xi ti 7
int max(int a = 8, int b = 10)
{
return a > b ? a : b ;
}
int main(void)
{
int a;
cin >> a;
int z;
z = max(); // z = max(a); ye ke yi
cout << "max = " << z << endl;
system("pause");
return 0;
}
*/
/*
// xi ti 8
int paixu(int &a, int &b)
{
int temp;
if (a > b)
{
return 0;
}
else
{
temp = a;
a = b;
b = temp;
return 0;
}
}
int main(void)
{
int a, b;
cin >> a>> b ;
paixu(a, b);
cout << a << ">"<< b << endl;
system("pause");
return 0;
}
*/
/*
// xi ti 9
int paixu(int &a, int &b, int &c)
{
int temp;
for (int i = 0; i < 2; i++)
{
if (c > b)
{
temp = c;
c = b;
b = temp;
}
if (a < b)
{
temp = b;
b = a;
a = temp;
}
}
return 0;
}
int main(void)
{
int x, y, z;
cin >> x >> y>> z;
paixu(x, y, z);
cout << x << ">" << y << ">" << z << endl;
system("pause");
return 0;
}
*/
/*
int main(void)
{
string str1 = "qzbfefgefdge", str2 = "qzcd", str3;
str3 = str1 + str2;
if(str2 > str1) //字符串大小比较是从左到右一个字符一个字符的比较,当有不同的字符时,哪个字符大,就输出对应的字符串
cout << str2 << endl;
else
cout << str1 << endl;
system("pause");
return 0;
}
*/
/*
int main(void)
{
string str1;
cin >> str1;
//获得反向枚举器,反向遍历
string::reverse_iterator iter = str1.rbegin();
cout << "输出字符串 : ";
while (iter != str1.rend())
{
cout << *iter;
iter++;
}
getchar();
system("pause");
return 0;
}
*/
/*
//对N个数进行排序,冒泡排序
template <typename SUN> //函数的重载方式
#define jz_type float
SUN paixu(SUN *p, int num)
{
int i, j;
jz_type temp = 0;
cout << endl;
for (i = 0; i <= num; i++)
{
for (j = i+1; j < num ; j++)
{
if (p[i] > p[j])
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
for (i = 0; i < num; i++)
{
cout << p[i] << " ";
}
return 0;
}
// 14函数模板实现排列
int main(void)
{
int num;
jz_type sum;
cout << "请输入数据个数 : ";
cin >> num;
jz_type table[30] = { 0 }; //数组做地址传递的时候必须指定内存大小
cout << endl << "shuru yuanshu";
for( int i = 0;i<num;i++)
cin >> table[i];
sum = paixu(table, num);
system("pause");
return 0;
}
*/
C++
1
https://gitee.com/Sjianzhao/Cplusplus.git
git@gitee.com:Sjianzhao/Cplusplus.git
Sjianzhao
Cplusplus
C++学习
master

搜索帮助