Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-City committed Apr 5, 2020
1 parent f97c156 commit a4d828b
Show file tree
Hide file tree
Showing 120 changed files with 4,413 additions and 0 deletions.
Binary file added practical_exercises/.README.md.un~
Binary file not shown.
24 changes: 24 additions & 0 deletions practical_exercises/10_day_practice/day1/打印练习.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int i,j,k,f;
for (i=1;i<=4;i++){
for (j=1;j<=30;j++)
cout<<" ";
for (k=1;k<=8-2*i;k++)
cout<<" ";
for (f=1;f<=2*i;f++)
cout<<'*';
cout<<endl;
}
for(i=1;i<=3;i++){
for (j=1;j<=30;j++)
cout<<" ";
for (f=1;f<=7-2*i;f++)
cout<<'*';
cout<<endl;
}
system("pause");
return 0;
}
20 changes: 20 additions & 0 deletions practical_exercises/10_day_practice/day1/是否闰年‘.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int year;
bool isLeapYear;
cout<<"Enter the year: ";
cin>>year;
isLeapYear = (((year%4==0)&&(year%100!=0))||(year%400==0));
if(isLeapYear)
{
cout<<year<<" is a leap year"<<endl;
}
else
{
cout<<year<<" is not a leap year"<<endl;
}
system("pause");
return 0;
}
16 changes: 16 additions & 0 deletions practical_exercises/10_day_practice/day1/注释.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<iostream>


一种条件编译指令注释


//另一种注释方法
#if 0
asd
#endif

//打开注释
//条件编译指令
#if 1
asData
#endif
18 changes: 18 additions & 0 deletions practical_exercises/10_day_practice/day1/联合体学习.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<iostream>
using namespace std;
//相同的内存地址
union myun
{
struct { int x; int y; int z; }u;
int k;
}a;
int main()
{
a.u.x =4;
a.u.y =5;
a.u.z =6;
a.k = 0; //覆盖掉第一个int空间值
printf("%d %d %d %d\n",a.u.x,a.u.y,a.u.z,a.k);
system("pause");
return 0;
}
1 change: 1 addition & 0 deletions practical_exercises/10_day_practice/day10/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# �ļ�����
18 changes: 18 additions & 0 deletions practical_exercises/10_day_practice/day10/文件例题/12-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//用cin输入字符串数据时,如果字符串中含有空白就不能完整输入。因为遇到空白字符时,cin就认为字符串结束了。
#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
char a[50];
cout<<"please input a string:";
cin>>a;
cout<<a<<endl;
system("pause");
return 0;
}
/*
若a的内容是:
this is a string!
就难以输入啦!
这样的数据应用输入流类的成员函数输入
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
char stu[5][10];
int i;
for(i=0;i<5;i++)
cin.getline(stu[i],10,',');
for(i=0;i<5;i++)
cout<<stu[i]<<endl;
system("pause");
return 0;
}
18 changes: 18 additions & 0 deletions practical_exercises/10_day_practice/day10/文件例题/12-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<iostream>
using namespace std;
//º¯ÊýÔ­ÐÍ
//put(char c)
//write(const char*c, int n)
int main(){
char c;
char a[50]="this is a string...";
cout<<"use get() input char:";
while((c=cin.get())!='\n'){
cout.put(c);
cout.put('\n');
cout.put('t').put('h').put('i').put('s').put('\n');
cout.write(a,12).put('\n');
cout<<"look"<<"\t here!"<<endl;
}
system("pause");
}
17 changes: 17 additions & 0 deletions practical_exercises/10_day_practice/day10/文件例题/12-5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//Eg12-5.cpp
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
char c[30]="this is string";
double d=-1234.8976;
cout<<setw(30)<<left<<setfill('*')<<c<<"----L1"<<endl;
cout<<setw(30)<<right<<setfill('*')<<c<<"----L2"<<endl;
//showbase显示数值的基数前缀
cout<<dec<<showbase<<showpoint<<setw(30)<<d<<"----L3"<<"\n";
//showpoint显示小数点
cout<<setw(30)<<showpoint<<setprecision(10)<<d<<"----L4"<<"\n";
//setbase(8)设置八进制
cout<<setw(30)<<setbase(16)<<100<<"----L5"<<"\n";
system("pause");
}
28 changes: 28 additions & 0 deletions practical_exercises/10_day_practice/day10/文件例题/12-6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//12-6.cpp
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char const *argv[])
{
fstream ioFile;
ioFile.open("./a.dat",ios::out);
ioFile<<"张三"<<" "<<76<<" "<<98<<" "<<67<<endl; //L3
ioFile<<"李四"<<" "<<89<<" "<<70<<" "<<60<<endl;
ioFile<<"王十"<<" "<<91<<" "<<88<<" "<<77<<endl;
ioFile<<"黄二"<<" "<<62<<" "<<81<<" "<<75<<endl;
ioFile<<"刘六"<<" "<<90<<" "<<78<<" "<<67<<endl;
ioFile.close();
ioFile.open("./a.dat",ios::in|ios::binary);
char name[10];
int chinese,math,computer;
cout<<"姓名\t"<<"英语\t"<<"数学\t"<<"计算机\t"<<"总分"<<endl;
ioFile>>name;
while(!ioFile.eof()) {
ioFile>>chinese>>math>>computer;
cout<<name<<"\t"<<chinese<<"\t"<<math<<"\t"<<computer<<"\t"<<chinese+math+computer<<endl;
ioFile>>name;
}
ioFile.close();
system("pause");
return 0;
}
21 changes: 21 additions & 0 deletions practical_exercises/10_day_practice/day10/文件例题/12-7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Eg12-7.cpp
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char ch;
ofstream out("/test.dat",ios::out|ios::binary); //L1
for(int i=0;i<90;i++){
if(i>0 && (i % 30)==0)
out.put('\n');
out.put(i);
out.put(' ');

}
out.close();
ifstream in("/test.dat",ios::in|ios::binary);
while(in.get(ch))
cout<<ch;
in.close();
system("pause");
}
41 changes: 41 additions & 0 deletions practical_exercises/10_day_practice/day10/文件例题/12-9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//Eg12-12.cpp
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
class Employee{
private:
int number ,age;
char name[20];
double sal;
public:
Employee(){}
Employee(int num,char* Name,int Age, double Salary){
number=num;
strcpy(name,Name);
age=Age;
sal=Salary;
}
void display(){
cout<<number<<"\t"<<name<<"\t"<<age<<"\t"<<sal<<endl;
}
};

int main(){
ofstream out("D:/Employee.dat",ios::out); //定义随机输出文件
Employee e1(1,"张三",23,2320);
Employee e2(2,"李四",32,3210);
Employee e3(3,"王五",34,2220);
Employee e4(4,"刘六",27,1220);
out.write((char*)&e1,sizeof(e1)); //按e1,e2,e3,e4顺序写入文件
out.write((char*)&e2,sizeof(e2));
out.write((char*)&e3,sizeof(e3));
out.write((char*)&e4,sizeof(e4));

//下面的代码将e3(即王五)的年龄改为40岁
Employee e5(3,"王五",40,2220);
out.seekp(3*sizeof(e1)); //指针定位到第3(起始为0)个数据块
out.write((char*)&e5,sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
out.close(); //关闭文件
system("pause");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//【例12-2】 用函数get和getline读取数据。
#include <iostream>
using namespace std;
int main()
{
char a,b,c,d;
cin.get(a);
cin.get(b);
c = cin.get();
d = cin.get();
cout<<int(a)<<','<<int(b)<<','<<int(c)<<','<<int(d)<<endl;
system("pause");
return 0;
}

/*
用法:a = cin.get() ?或者 ?cin.get(a)
结束条件:输入字符足够后回车
说明:这个是单字符的输入,用途是输入一个字符,把它的ASCALL码存入到a中
处理方法:与cin不同,cin.get()在缓冲区遇到[enter],[space],[tab]不会作为舍弃,而是继续留在缓冲区中
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

//【例12-2】 用函数get和getline读取数据。
#include <iostream>
using namespace std;
//cin.get(arrayname,size) 把字符输入到arrayname中,长度不超过size
int main()
{
//get()两个参数

//1.输入串长<size,输入串长>arraylength,会自动扩张arrayname大小,使能保存所有数据
// char a[10];
// cin.get(a,20);
// cout<<a<<endl;
// cout<<sizeof(a)<<endl;
//2.输入串长<size,输入串长<arraylength,把串全部输入,后面补‘\0’
// char b[10];
// cin.get(b,20);
// cout<<b<<endl;//12345,此时数组内数据为‘12345'\0’
// cout<<sizeof(b)<<endl;
//3.输入串长>size,先截取size个字符,若还是大于arraylength,则输入前arraylength-1个字符,最后补充‘\0’
// char c[5];
// cin.get(c,10);
// cout<<c<<endl;
// cout<<sizeof(c)<<endl;
//4.输入串长>size,先截取size个字符,若小于arraylength,则把截取串放入数组中,最后补充‘\0’
// char d[10];
// cin.get(d,5);
// cout<<d<<endl;
// cout<<sizeof(d)<<endl;

//get()三个参数
/*
用法:cin.get(arrayname,size,s) ?把数据输入到arrayname字符数组中,当到达长度size时结束或者遇到字符s时结束
注释:a必须是字符数组,即char a[]l类型,不可为string类型;size为最大的输入长度;s为控制,遇到s则当前输入结束缓存区里的s将被舍弃
*/
int i;
char e[10];
cin.get(e,8,',');
cout<<e;
system("pause");
return 0;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<iostream>
using namespace std;
/*
(1)cin.getline(arrayname,size)与cin.get(arrayname,size)的区别
cin.get(arrayname,size)当遇到[enter]时会结束目前输入,他不会删除缓冲区中的[enter]
cin.getline(arrayname,size)当遇到[enter]时会结束当前输入,但是会删除缓冲区中的[enter]
*/
int main()
{
/*
char a[10];
char b;
cin.get(a,10);
cin.get(b);
cout<<a<<endl<<int(b);//输入:12345[enter] 输出:12345 【换行】 10*/
/*char c[10];
char d;
cin.getline(c,10);
cin.get(d);
cout<<c<<endl<<int(d);//输入:12345[enter]a[enter] 输出:12345【换行】97*/
//cin.getline(arrayname,size,s)与cin.gei(arrayname,size,s)的区别
/*
cin.getline(arrayname,size,s)当遇到s时会结束输入,并把s从缓冲区中删除
cin.get(arrayname,size,s)当遇到s时会结束输入,但不会删除缓冲区中的s
*/
/*
char e[10];
char f;
cin.get(e,10,',');
cin.get(f);
cout<<e<<endl<<f;//输入:12345,[enter] 输出:12345【换行】,说明:cin,get不会删除缓冲区的,*/
char e1[10];
char f1;
cin.getline(e1,10,',');
cin.get(f1);
cout<<e1<<endl<<f1;//输入:asd,wqe 输出:asd【换行】w
system("pause");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<iostream>
using namespace std;

int main(int argc, char const *argv[])
{
char c[30]="this is string";
double d = -1231.232;
cout.width(30);
cout.fill('*');
cout.setf(ios::left);
cout<<c<<"----L1"<<endl;
cout.width(30);
cout.fill('-');
cout.setf(ios::right);
cout<<c<<"----L2"<<endl;
cout.setf(ios::dec|ios::showbase|ios::showpoint);
cout.width(30);
cout<<d<<"----L3"<<"\n";
cout.setf(ios::showpoint);
cout.precision(10);
cout.width(30);
cout<<d<<"----L4"<<"\n";
cout.width(30);
cout.setf(ios::oct,ios::basefield);
cout<<100<<"----L5"<<"\n";
system("pause");
return 0;
}
Loading

0 comments on commit a4d828b

Please sign in to comment.