Skip to content

Commit 29cf9a0

Browse files
committed
C++学习代码
0 parents  commit 29cf9a0

File tree

83 files changed

+2896
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2896
-0
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C++学习代码
2+
## 学习资料源自:C++面向对象程序设计(第二版)
3+
## 学习资料作者:谭浩强
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void swap(int a,int b)
5+
{
6+
int temp;
7+
temp=a;
8+
a=b;
9+
b=temp;
10+
}
11+
12+
int main()
13+
{
14+
int i=3,j=5;
15+
swap(i,j);
16+
cout<<i<<","<<j<<endl;
17+
return 0;
18+
}
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void swap(int *p1,int *p2)
5+
{
6+
int temp;
7+
temp=*p1;
8+
*p1=*p2;
9+
*p2=temp;
10+
}
11+
12+
int main()
13+
{
14+
int i=3,j=5;
15+
swap(&i,&j);
16+
cout<<i<<","<<j<<endl;
17+
return 0;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void swap(int &a,int &b)
5+
{
6+
int temp;
7+
temp=a;
8+
a=b;
9+
b=temp;
10+
}
11+
12+
int main()
13+
{
14+
int i=3,j=5;
15+
swap(i,j);
16+
cout<<"i="<<i<<" "<<"j="<<j<<endl;
17+
return 0;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
inline int max(int a,int b,int c)
5+
{
6+
if(b>a) a=b;
7+
if(c>a) a=c;
8+
return a;
9+
}
10+
11+
int main()
12+
{
13+
int i=7,j=10,k=25,m;
14+
m=max(i,j,k);
15+
cout<<"max="<<m<<endl;
16+
return 0;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
float a=13.5;
5+
6+
int main()
7+
{
8+
int a=5;
9+
cout<<a<<endl;
10+
return 0;
11+
}
12+
13+
/*
14+
int main()
15+
{
16+
int a=5;
17+
cout<<a<<endl; //输出局部变量a的值
18+
cout<<::a<<endl;//输出全局变量a的值
19+
}
20+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
string string1,string2,string3,temp;
8+
cout<<"Please input three strings:";
9+
cin>>string1>>string2>>string3;
10+
if(string2>string3) {temp=string2;string2=string3;string3=temp;}
11+
//使串2<=串3
12+
if(string1<=string2) cout<<string1<<" "<<string2<<" "<<string3<<endl;
13+
//如果串1<=串2,则串1<=串2<=串3
14+
else if(string1<=string3) cout<<string2<<" "<<string1<<" "<<string3<<endl;
15+
//如果串1>串2,且串1<=串3,则串2<串1<=串3
16+
else cout<<string2<<" "<<string3<<" "<<string1<<endl;
17+
//如果串1>串2,且串1>串3,则串2<=串3<=串1
18+
return 0;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
struct Student
6+
{
7+
char name[10];
8+
int num;
9+
char sex;
10+
};
11+
12+
int main()
13+
{
14+
Student *p;
15+
p=new Student;
16+
strcpy(p->name,"Wang Yun");
17+
p->num=10123;
18+
p->sex='M';
19+
cout<<p->name<<" "<<p->num<<" "<<p->sex<<endl;
20+
delete p;
21+
return 0;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
cout<<"This is C++ program.\n";
7+
return 0;
8+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int a,b,sum;
7+
cin>>a>>b;
8+
sum=a+b;
9+
cout<<"a+b="<<sum<<endl;
10+
return 0;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int max(int x,int y);
7+
int a,b,c;
8+
cin>>a>>b;
9+
c=max(a,b);
10+
cout<<"max="<<c<<endl;
11+
return 0;
12+
}
13+
14+
int max(int x,int y)
15+
{
16+
int z;
17+
if(x>y) z=x;
18+
else z=y;
19+
return (z);
20+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Student
5+
{
6+
private:
7+
int num;
8+
int score;
9+
public:
10+
void setdate()
11+
{
12+
cin>>num;
13+
cin>>score;
14+
}
15+
void display()
16+
{
17+
cout<<"num="<<num<<endl;
18+
cout<<"score="<<score<<endl;
19+
}
20+
};
21+
Student stud1,stud2;
22+
23+
int main()
24+
{
25+
stud1.setdate();
26+
stud2.setdate();
27+
stud1.display();
28+
stud2.display();
29+
return 0;
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
cout<<"please enter your name and age:"<<endl;
7+
char name[10];
8+
int age;
9+
cin>>name;
10+
cin>>age;
11+
cout<<"your name is "<<name<<endl;
12+
cout<<"your age is "<<age<<endl;
13+
return 0;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int max(int a,int b,int c)
5+
{
6+
if(b>a) a=b;
7+
if(c>a) a=c;
8+
return a;
9+
}
10+
11+
float max(float a,float b,float c)
12+
{
13+
if(b>a) a=b;
14+
if(c>a) a=c;
15+
return a;
16+
}
17+
18+
long max(long a,long b,long c)
19+
{
20+
if(b>a) a=b;
21+
if(c>a) a=c;
22+
return a;
23+
}
24+
25+
int main()
26+
{
27+
int a,b,c;
28+
float d,e,f;
29+
long g,h,i;
30+
cin>>a>>b>>c;
31+
cin>>d>>e>>f;
32+
cin>>g>>h>>i;
33+
int m;
34+
m=max(a,b,c);
35+
cout<<"max_i="<<m<<endl;
36+
float n;
37+
n=max(d,e,f);
38+
cout<<"max_f="<<n<<endl;
39+
long int p;
40+
p=max(g,h,i);
41+
cout<<"max_l="<<p<<endl;
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int max(int a,int b,int c)
5+
{
6+
if(b>a) a=b;
7+
if(c>a) a=c;
8+
return a;
9+
}
10+
11+
int max(int a,int b)
12+
{
13+
if(a>b) return a;
14+
else return b;
15+
}
16+
17+
int main()
18+
{
19+
int a=7,b=-4,c=9;
20+
cout<<"max_3="<<max(a,b,c)<<endl;
21+
cout<<"max_2="<<max(a,b)<<endl;
22+
}

Diff for: 第1章 C++的初步知识/1.8_函数模板.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
template <typename T>
5+
T max(T a,T b,T c)
6+
{
7+
if(b>a) a=b;
8+
if(c>a) a=c;
9+
return a;
10+
}
11+
12+
int main()
13+
{
14+
int i1=8,i2=5,i3=6,i;
15+
double d1=56.9,d2=90.765,d3=43.1,d;
16+
long g1=67843,g2=-456,g3=78123,g;
17+
i=max(i1,i2,i3);
18+
d=max(d1,d2,d3);
19+
g=max(g1,g2,g3);
20+
cout<<"i_max="<<i<<endl;
21+
cout<<"f_max="<<d<<endl;
22+
cout<<"g_max="<<g<<endl;
23+
return 0;
24+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int a=10;
7+
int &b=a;
8+
a=a*a;
9+
cout<<a<<" "<<b<<endl;
10+
b=b/5;
11+
cout<<b<<" "<<a<<endl;
12+
return 0;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Time
5+
{
6+
public:
7+
int hour;
8+
int minute;
9+
int sec;
10+
};
11+
12+
int main()
13+
{
14+
Time t1;
15+
cin>>t1.hour;
16+
cin>>t1.minute;
17+
cin>>t1.sec;
18+
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
19+
return 0;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Time
5+
{
6+
public:
7+
int hour;
8+
int minute;
9+
int sec;
10+
};
11+
12+
int main()
13+
{
14+
Time t1;
15+
cin>>t1.hour;
16+
cin>>t1.minute;
17+
cin>>t1.sec;
18+
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
19+
Time t2;
20+
cin>>t2.hour;
21+
cin>>t2.minute;
22+
cin>>t2.sec;
23+
cout<<t2.hour<<":"<<t2.minute<<":"<<t2.sec<<endl;
24+
return 0;
25+
}

0 commit comments

Comments
 (0)