C++学习笔记
- 参考书籍:《C++面向对象程序设计(第3版)》谭浩强 著
- 编程环境:Visual Studio 2022
- 学习内容:第一章 C++的初步知识
目录
- C++学习笔记
- 1.1 输出“Hello World.This is a C++ program.”
- 1.2 求a和b两个数之和
- 1.3 从键盘输入a和b,求两数中的大者。
- 1.4 包含类的C++程序
- 1.5用cin和cout 实现数据的输入和输出
- 1.6 求3个数中最大的数(分别考虑整数、实数、长整数的情况)
- 1.7用一个函数求2个整数或3个整数中的最大者
- 1.8 将1.6程序改为通过函数模板来实现
- 1.9 了解引用和变量的关系
- 1.10 无法实现两个变量的值呼唤的程序
- 1.11 使用指针变量作为形参,实现两个变量的值互换
- 1.12 利用“引用形参”实现两个变量的值互换
- 1.13 将函数指定为内置函数
- 1.14 局部变量和全局变量同名
- 1.15 输入3个字符串,要求按字母由小到大顺序输出
- 1.16 开辟空间以存放一个结构体变量
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World.This is a C++ program.\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a, b, sum;
cin >> a >> b;
sum = a + b;
cout << "a+b=" << sum << endl;
return 0;
}
#include<iostream>
using namespace std;
int main() {
int max(int x, int y);
int a, b, c;
cin >> a >> b;
c = max(a, b);
cout << "max=" << c << endl;
}
int max(int x, int y)
{
int max;
if (x > y) {
max = x;
}
else {
max = y;
}
return max;
}
#include<iostream>
using namespace std;
class Student
{
private:
int num;
int score;
public:
void setdata()
{
cin >> num;
cin >> score;
}
void display()
{
cout << "num = " << num << endl;
cout << "score = " << score << endl;
}
};
Student stu1, stu2;
int main()
{
stu1.setdata();
stu2.setdata();
stu1.display();
stu2.display();
return 0;
}
1 2
3 4
num = 1
score = 2
num = 3
score = 4
#include<iostream>
using namespace std;
int main()
{
cout << "please enter your name and age:" << endl;
char name[10];
int age;
cin >> name;
cin >> age;
cout << "your name is:" << name << endl;
cout << "your age is:" << age << endl;
return 0;
}
please enter your name and age:
shen
18
your name is:shen
your age is:18
#include<iostream>
using namespace std;
int max(int a, int b, int c) {
if (b > a)a = b;
if (c > a)a = c;
return a;
}
float max(float a, float b, float c) {
if (b > a)a = b;
if (c > a)a = c;
return a;
}
long max(long a, long b, long c) {
if (b > a)a = b;
if (c > a)a = c;
return a;
}
int main() {
int a, b, c;
float d, e, f;
long g, h, i;
cin >> a >> b >> c;
cin >> d >> e >> f;
cin >> g >> h >> i;
int m;
m = max(a, b, c);
cout << "max_i=" << m << endl;
float n;
n = max(d, e, f);
cout << "max_f=" << n << endl;
long int p;
p = max(g, h, i);
cout << "max_l=" << p << endl;
}
8 5 6
56.9 90.765 43.1
67543 -567 78123
max_i=8
max_f=90.765
max_l=78123
#include<iostream>
using namespace std;
int max(int a, int b, int c)
{
if (b > a)a = b;
if (c > a)a = c;
return a;
}
int max(int a, int b)
{
if (b > a)return b;
if (b < a)return a;
}
int main() {
int a = 7, b = -4, c = 9;
cout << "max_3=" << max(a, b, c) << endl;
cout << "max_2=" << max(a, b) << endl;
}
max_3=9
max_2=7
#include<iostream>
using namespace std;
template<typename T>
T max(T a, T b, T c)
{
if (b > a)a = b;
if (c > a)a = c;
return a;
}
int main() {
int a, b, c, m;
float d, e, f, n;
long g, h, i, p;
cin >> a >> b >> c;
cin >> d >> e >> f;
cin >> g >> h >> i;
//int m;
m = max(a, b, c);
cout << "max_i=" << m << endl;
//float n;
n = max(d, e, f);
cout << "max_f=" << n << endl;
//long int p;
p = max(g, h, i);
cout << "max_l=" << p << endl;
}
8 5 6
56.9 90.765 43.1
67843 -456 78123
max_i=8
max_f=90.765
max_l=78123
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int& b = a;
a = a * a;
cout << a << " " << b << endl;
b = b / 5;
cout << b << " " << a << endl;
return 0;
}
100 100
20 20
#include<iostream>
using namespace std;
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int i = 3, j = 5;
swap(i, j);
cout << i << "," << j << endl;
return 0;
}
3,5
#include<iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int i = 3, j = 5;
swap(i, j);
cout << i << "," << j << endl;
return 0;
}
5,3
#include<iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int i = 3, j = 5;
swap(i, j);
cout << i << "," << j << endl;
return 0;
}
5,3
#include<iostream>
using namespace std;
inline int max(int a, int b, int c)
{
if (b > a)a = b;
if (c > a)a = c;
return a;
}
int main() {
int i = 7, j = 10, k = 25, m;
m = max(i, j, k);
cout << "max=" << m << endl;
return 0;
}
max=25
#include<iostream>
using namespace std;
float a = 2.3;
int main() {
int a = 666;
cout << "a=" << a << endl;
cout << "::a=" << ::a << endl;
return 0;
}
a=666
::a=2.3
#include<iostream>
#include<string>
using namespace std;
int main()
{
string string1, string2, string3, temp;
cout << "请输入三个字符串:";
cin >> string1 >> string2 >> string3;
if (string2 > string3)
{
temp = string2;
string2 = string3;
string3 = temp;
}
if (string1 <= string2) {
cout << string1 << " " << string2 << " " << string3 << endl;
}
else if (string1 <= string3) {
cout << string2 << " " << string1 << " " << string3 << endl;
}
else if (string1 > string3) {
cout << string2 << " " << string3 << " " << string1 << endl;
}
return 0;
}
请输入三个字符串:China U.S.A Germany
China Germany U.S.A
#include<iostream>
#include<string.h>
using namespace std;
struct Student
{
char name[10];
int num;
char sex;
};
int main()
{
Student* p;
p = new Student;
strcpy(p->name, "Wang Yun");
p->num = 10123;
p->sex = 'M';
cout << (p->name) << " " << (p->num) << " " << (p->sex) << endl;
delete p;
return 0;
}
这个有错误,不知道为什么