Skip to content

Commit a4d828b

Browse files
committed
update
1 parent f97c156 commit a4d828b

File tree

120 files changed

+4413
-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.

120 files changed

+4413
-0
lines changed

practical_exercises/.README.md.un~

28.3 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<iostream>
2+
using namespace std;
3+
int main(int argc, char const *argv[])
4+
{
5+
int i,j,k,f;
6+
for (i=1;i<=4;i++){
7+
for (j=1;j<=30;j++)
8+
cout<<" ";
9+
for (k=1;k<=8-2*i;k++)
10+
cout<<" ";
11+
for (f=1;f<=2*i;f++)
12+
cout<<'*';
13+
cout<<endl;
14+
}
15+
for(i=1;i<=3;i++){
16+
for (j=1;j<=30;j++)
17+
cout<<" ";
18+
for (f=1;f<=7-2*i;f++)
19+
cout<<'*';
20+
cout<<endl;
21+
}
22+
system("pause");
23+
return 0;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
using namespace std;
3+
int main(int argc, char const *argv[])
4+
{
5+
int year;
6+
bool isLeapYear;
7+
cout<<"Enter the year: ";
8+
cin>>year;
9+
isLeapYear = (((year%4==0)&&(year%100!=0))||(year%400==0));
10+
if(isLeapYear)
11+
{
12+
cout<<year<<" is a leap year"<<endl;
13+
}
14+
else
15+
{
16+
cout<<year<<" is not a leap year"<<endl;
17+
}
18+
system("pause");
19+
return 0;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include<iostream>
2+
3+
4+
一种条件编译指令注释
5+
6+
7+
//另一种注释方法
8+
#if 0
9+
asd
10+
#endif
11+
12+
//打开注释
13+
//条件编译指令
14+
#if 1
15+
asData
16+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<iostream>
2+
using namespace std;
3+
//相同的内存地址
4+
union myun
5+
{
6+
struct { int x; int y; int z; }u;
7+
int k;
8+
}a;
9+
int main()
10+
{
11+
a.u.x =4;
12+
a.u.y =5;
13+
a.u.z =6;
14+
a.k = 0; //覆盖掉第一个int空间值
15+
printf("%d %d %d %d\n",a.u.x,a.u.y,a.u.z,a.k);
16+
system("pause");
17+
return 0;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# �ļ�����
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//用cin输入字符串数据时,如果字符串中含有空白就不能完整输入。因为遇到空白字符时,cin就认为字符串结束了。
2+
#include<iostream>
3+
using namespace std;
4+
int main(int argc, char const *argv[])
5+
{
6+
char a[50];
7+
cout<<"please input a string:";
8+
cin>>a;
9+
cout<<a<<endl;
10+
system("pause");
11+
return 0;
12+
}
13+
/*
14+
若a的内容是:
15+
this is a string!
16+
就难以输入啦!
17+
这样的数据应用输入流类的成员函数输入
18+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include<iostream>
2+
using namespace std;
3+
int main(int argc, char const *argv[])
4+
{
5+
char stu[5][10];
6+
int i;
7+
for(i=0;i<5;i++)
8+
cin.getline(stu[i],10,',');
9+
for(i=0;i<5;i++)
10+
cout<<stu[i]<<endl;
11+
system("pause");
12+
return 0;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<iostream>
2+
using namespace std;
3+
//º¯ÊýÔ­ÐÍ
4+
//put(char c)
5+
//write(const char*c, int n)
6+
int main(){
7+
char c;
8+
char a[50]="this is a string...";
9+
cout<<"use get() input char:";
10+
while((c=cin.get())!='\n'){
11+
cout.put(c);
12+
cout.put('\n');
13+
cout.put('t').put('h').put('i').put('s').put('\n');
14+
cout.write(a,12).put('\n');
15+
cout<<"look"<<"\t here!"<<endl;
16+
}
17+
system("pause");
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Eg12-5.cpp
2+
#include<iostream>
3+
#include<iomanip>
4+
using namespace std;
5+
int main(){
6+
char c[30]="this is string";
7+
double d=-1234.8976;
8+
cout<<setw(30)<<left<<setfill('*')<<c<<"----L1"<<endl;
9+
cout<<setw(30)<<right<<setfill('*')<<c<<"----L2"<<endl;
10+
//showbase显示数值的基数前缀
11+
cout<<dec<<showbase<<showpoint<<setw(30)<<d<<"----L3"<<"\n";
12+
//showpoint显示小数点
13+
cout<<setw(30)<<showpoint<<setprecision(10)<<d<<"----L4"<<"\n";
14+
//setbase(8)设置八进制
15+
cout<<setw(30)<<setbase(16)<<100<<"----L5"<<"\n";
16+
system("pause");
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//12-6.cpp
2+
#include<iostream>
3+
#include<fstream>
4+
using namespace std;
5+
int main(int argc, char const *argv[])
6+
{
7+
fstream ioFile;
8+
ioFile.open("./a.dat",ios::out);
9+
ioFile<<"张三"<<" "<<76<<" "<<98<<" "<<67<<endl; //L3
10+
ioFile<<"李四"<<" "<<89<<" "<<70<<" "<<60<<endl;
11+
ioFile<<"王十"<<" "<<91<<" "<<88<<" "<<77<<endl;
12+
ioFile<<"黄二"<<" "<<62<<" "<<81<<" "<<75<<endl;
13+
ioFile<<"刘六"<<" "<<90<<" "<<78<<" "<<67<<endl;
14+
ioFile.close();
15+
ioFile.open("./a.dat",ios::in|ios::binary);
16+
char name[10];
17+
int chinese,math,computer;
18+
cout<<"姓名\t"<<"英语\t"<<"数学\t"<<"计算机\t"<<"总分"<<endl;
19+
ioFile>>name;
20+
while(!ioFile.eof()) {
21+
ioFile>>chinese>>math>>computer;
22+
cout<<name<<"\t"<<chinese<<"\t"<<math<<"\t"<<computer<<"\t"<<chinese+math+computer<<endl;
23+
ioFile>>name;
24+
}
25+
ioFile.close();
26+
system("pause");
27+
return 0;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//Eg12-7.cpp
2+
#include <iostream>
3+
#include <fstream>
4+
using namespace std;
5+
int main(){
6+
char ch;
7+
ofstream out("/test.dat",ios::out|ios::binary); //L1
8+
for(int i=0;i<90;i++){
9+
if(i>0 && (i % 30)==0)
10+
out.put('\n');
11+
out.put(i);
12+
out.put(' ');
13+
14+
}
15+
out.close();
16+
ifstream in("/test.dat",ios::in|ios::binary);
17+
while(in.get(ch))
18+
cout<<ch;
19+
in.close();
20+
system("pause");
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//Eg12-12.cpp
2+
#include <iostream>
3+
#include <cstring>
4+
#include <fstream>
5+
using namespace std;
6+
class Employee{
7+
private:
8+
int number ,age;
9+
char name[20];
10+
double sal;
11+
public:
12+
Employee(){}
13+
Employee(int num,char* Name,int Age, double Salary){
14+
number=num;
15+
strcpy(name,Name);
16+
age=Age;
17+
sal=Salary;
18+
}
19+
void display(){
20+
cout<<number<<"\t"<<name<<"\t"<<age<<"\t"<<sal<<endl;
21+
}
22+
};
23+
24+
int main(){
25+
ofstream out("D:/Employee.dat",ios::out); //定义随机输出文件
26+
Employee e1(1,"张三",23,2320);
27+
Employee e2(2,"李四",32,3210);
28+
Employee e3(3,"王五",34,2220);
29+
Employee e4(4,"刘六",27,1220);
30+
out.write((char*)&e1,sizeof(e1)); //按e1,e2,e3,e4顺序写入文件
31+
out.write((char*)&e2,sizeof(e2));
32+
out.write((char*)&e3,sizeof(e3));
33+
out.write((char*)&e4,sizeof(e4));
34+
35+
//下面的代码将e3(即王五)的年龄改为40岁
36+
Employee e5(3,"王五",40,2220);
37+
out.seekp(3*sizeof(e1)); //指针定位到第3(起始为0)个数据块
38+
out.write((char*)&e5,sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
39+
out.close(); //关闭文件
40+
system("pause");
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//【例12-2】 用函数get和getline读取数据。
2+
#include <iostream>
3+
using namespace std;
4+
int main()
5+
{
6+
char a,b,c,d;
7+
cin.get(a);
8+
cin.get(b);
9+
c = cin.get();
10+
d = cin.get();
11+
cout<<int(a)<<','<<int(b)<<','<<int(c)<<','<<int(d)<<endl;
12+
system("pause");
13+
return 0;
14+
}
15+
16+
/*
17+
用法:a = cin.get() ?或者 ?cin.get(a)
18+
结束条件:输入字符足够后回车
19+
说明:这个是单字符的输入,用途是输入一个字符,把它的ASCALL码存入到a中
20+
处理方法:与cin不同,cin.get()在缓冲区遇到[enter],[space],[tab]不会作为舍弃,而是继续留在缓冲区中
21+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
//【例12-2】 用函数get和getline读取数据。
3+
#include <iostream>
4+
using namespace std;
5+
//cin.get(arrayname,size) 把字符输入到arrayname中,长度不超过size
6+
int main()
7+
{
8+
//get()两个参数
9+
10+
//1.输入串长<size,输入串长>arraylength,会自动扩张arrayname大小,使能保存所有数据
11+
// char a[10];
12+
// cin.get(a,20);
13+
// cout<<a<<endl;
14+
// cout<<sizeof(a)<<endl;
15+
//2.输入串长<size,输入串长<arraylength,把串全部输入,后面补‘\0’
16+
// char b[10];
17+
// cin.get(b,20);
18+
// cout<<b<<endl;//12345,此时数组内数据为‘12345'\0’
19+
// cout<<sizeof(b)<<endl;
20+
//3.输入串长>size,先截取size个字符,若还是大于arraylength,则输入前arraylength-1个字符,最后补充‘\0’
21+
// char c[5];
22+
// cin.get(c,10);
23+
// cout<<c<<endl;
24+
// cout<<sizeof(c)<<endl;
25+
//4.输入串长>size,先截取size个字符,若小于arraylength,则把截取串放入数组中,最后补充‘\0’
26+
// char d[10];
27+
// cin.get(d,5);
28+
// cout<<d<<endl;
29+
// cout<<sizeof(d)<<endl;
30+
31+
//get()三个参数
32+
/*
33+
用法:cin.get(arrayname,size,s) ?把数据输入到arrayname字符数组中,当到达长度size时结束或者遇到字符s时结束
34+
注释:a必须是字符数组,即char a[]l类型,不可为string类型;size为最大的输入长度;s为控制,遇到s则当前输入结束缓存区里的s将被舍弃
35+
36+
*/
37+
int i;
38+
char e[10];
39+
cin.get(e,8,',');
40+
cout<<e;
41+
system("pause");
42+
return 0;
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<iostream>
2+
using namespace std;
3+
/*
4+
(1)cin.getline(arrayname,size)与cin.get(arrayname,size)的区别
5+
cin.get(arrayname,size)当遇到[enter]时会结束目前输入,他不会删除缓冲区中的[enter]
6+
cin.getline(arrayname,size)当遇到[enter]时会结束当前输入,但是会删除缓冲区中的[enter]
7+
*/
8+
int main()
9+
{
10+
/*
11+
char a[10];
12+
char b;
13+
cin.get(a,10);
14+
cin.get(b);
15+
cout<<a<<endl<<int(b);//输入:12345[enter] 输出:12345 【换行】 10*/
16+
/*char c[10];
17+
char d;
18+
cin.getline(c,10);
19+
cin.get(d);
20+
cout<<c<<endl<<int(d);//输入:12345[enter]a[enter] 输出:12345【换行】97*/
21+
//cin.getline(arrayname,size,s)与cin.gei(arrayname,size,s)的区别
22+
/*
23+
cin.getline(arrayname,size,s)当遇到s时会结束输入,并把s从缓冲区中删除
24+
cin.get(arrayname,size,s)当遇到s时会结束输入,但不会删除缓冲区中的s
25+
*/
26+
/*
27+
char e[10];
28+
char f;
29+
cin.get(e,10,',');
30+
cin.get(f);
31+
cout<<e<<endl<<f;//输入:12345,[enter] 输出:12345【换行】,说明:cin,get不会删除缓冲区的,*/
32+
char e1[10];
33+
char f1;
34+
cin.getline(e1,10,',');
35+
cin.get(f1);
36+
cout<<e1<<endl<<f1;//输入:asd,wqe 输出:asd【换行】w
37+
system("pause");
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main(int argc, char const *argv[])
5+
{
6+
char c[30]="this is string";
7+
double d = -1231.232;
8+
cout.width(30);
9+
cout.fill('*');
10+
cout.setf(ios::left);
11+
cout<<c<<"----L1"<<endl;
12+
cout.width(30);
13+
cout.fill('-');
14+
cout.setf(ios::right);
15+
cout<<c<<"----L2"<<endl;
16+
cout.setf(ios::dec|ios::showbase|ios::showpoint);
17+
cout.width(30);
18+
cout<<d<<"----L3"<<"\n";
19+
cout.setf(ios::showpoint);
20+
cout.precision(10);
21+
cout.width(30);
22+
cout<<d<<"----L4"<<"\n";
23+
cout.width(30);
24+
cout.setf(ios::oct,ios::basefield);
25+
cout<<100<<"----L5"<<"\n";
26+
system("pause");
27+
return 0;
28+
}

0 commit comments

Comments
 (0)