写在开头的话:
今天难得一天没课。打算把之前学习过的课程再重新看一遍。写了好多代码,编译运行之后都删掉写下一个了。
久而久之不免觉得浪费,现在把它们都贴在下面把。就跟看着自己的孩子一样。说不定以后有利用的价值,没事儿还可以翻出来看看。
(不定时补充)
发现好像写了蛮多的,在1楼做一个目录吧。方便以后的查阅。
下面是代码的集合:
#include
using namespace std;
int main()
{
const int N=100;
char str[N];
int len=0,capital=0,smallletter=0,digit=0,others=0;
cout<<"Please input the sentence:"<='A'&&str[i]<='Z'){
capital++;
}
else if(str[i]>='a'&&str[i]<='z'){
smallletter++;
}
else if(str[i]>='0'&&str[i]<='9'){
digit++;
}
else{
others++;
}
i++;
}
cout<<"please wait..."<
#include
#include
using namespace std;
int main()
{
int N,i,j;
double a[100];
cout<<"Please input the number of tested numbers:"<>N;
cout<<"Please input the tested numbers:"<>a[i];
}
for(i=0;ia[j+1]){
int tmp;
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
cout<<"Please wait..."<
#include
#include
using namespace std;
int main()
{
int num1,num2,result;
char op;
char s1[30]="Error!Divided by 0!";
char s2[30]="Wrong Expression!";
cout<<"Please input the expression:"<>num1>>op>>num2;
switch(op)
{
case'+':
result=num1+num2;
cout<
#include
#include
using namespace std;
int main()
{
string text1("Heavy rains are pushing water levels beyoud the limit.");
string text2,text3;
int k;
text2="Sluice gates at Three Gorges Dam opened to discharge water.";
text3=text1+text2;
k=text3.find("Heavy");
text3.erase(k,sizeof("Heavy")-1);
text3.insert(k,"Strong");
cout<
#include
using namespace std;
int main()
{
int a,b,c,n;
cout<<"Please input a three-digit number:"<>n;
a=n%10;
b=(n/10)%10;
c=n/100;
cout<<"the inversion number is "<
#include
using namespace std;
int main()
{
char str[101];
char sub[101];
int len,k,l;
int i,j=0;
cout<<"Please input the sentence:"<>k>>l;
while(k!=0&&l!=0){
j=0;
for(i=k-1;i>k>>l;
}
return 0;
}
#include
#include
using namespace std;
struct WordList{
char word[20];
int freq;
};
int main()
{
WordList list[1000];
int N=0;
int i,j,k;
char tmp[20];
cout<<"Please input some English words,and input 'xyz' to end."<>tmp;
while(strcmp(tmp,"xyz")!=0){
for(i=0;i=N){
strcpy(list[i].word,tmp);
list[i].freq=1;
N++;
}
cin>>tmp;
}
for(i=0;i
#include
using namespace std;
double maxThree(double a,double b,double c)
{
double max;
max=a>b?a:b;
max=max>c?max:c;
return max;
}
int main()
{
double x1,x2,x3;
double max;
cout<<"Please input 3 numbers:"<>x1>>x2>>x3;
max=maxThree(x1,x2,x3);
cout<<"The biggest number is "<
//教材中将两个自定义void函数放在了main函数之后,但是由于个人习惯,我还是将main函数放在了最后
#include
using namespace std;
void hanoi(int n,int p1,int p2,int p3)
{
void move(int,char,char);
if(n==1){
move(n,p1,p3);
}
else{
hanoi(n-1,p1,p3,p2);
move(n,p1,p3);
hanoi(n-1,p2,p1,p3);
}
}
void move(int n,char from,char to)
{
cout<>n;
void hanoi(int,int,int,int);
hanoi(n,'A','B','C');
cout<
#include
using namespace std;
void swap(int *px,int *py)
{
int t=*px;
*px=*py;
*py=t;
}
int main()
{
int a,b,c;
cout<<"Please input 3 numbers:"<>a>>b>>c;
int *pa=&a;
int *pb=&b;
int *pc=&c;
if(*pa>*pb){
swap(pa,pb);
}
if(*pa>*pc){
swap(pa,pc);
}
if(*pb>*pc){
swap(pb,pc);
}
cout<<"The correct order is: "<
#include
using namespace std;
char *ltrim(char s[])
{
int k,j;
k=0;
while(s[k]==' '){
k++;
}
j=k;
while(s[j]!='\0'){
s[j-k]=s[j];
j++;
}
s[j-k]='\0';
return &s[0];
}
int main()
{
char str[100];
cout<<"请输入首部带一个或若干个空格的字符:"<
#include
#include
using namespace std;
class Clock
{
private:
int Hour;
int Minute;
int Second;
float Price;
public:
void Set(int h,int m,int s,float p);
void Run();
void Report_Time();
void Show_Time(){cout<
#include
#include
using namespace std;
class Date
{
private:
int year,month,day;
void SetSystemDate(); //取得系统日期
public:
void init(int,int,int);
void print_ymd();
void print_mdy();
int get_year(){return year;}
int get_month(){return month;} //上中下均为内联函数
int get_day(){return day;}
bool IsLeapYear(); //判断是否为闰年
};
void Date::SetSystemDate()
{
tm *gm; //tm是时间结构体
time_t t=time(NULL); //time_t是长整型
gm=gmtime(&t);
year=1900+gm->tm_year;
month=gm->tm_mon+1;
day=gm->tm_mday;
}
void Date::init(int yy,int mm,int dd) //有效性检验
{
if(yy>=1900&&yy<=9999)
year=yy;
else
{
SetSystemDate();
return;
}
if(mm>=1&&mm<=12)
month=mm;
else
{
SetSystemDate();
return;
}
if(dd>=1&&dd<=31)
day=dd;
else
{
SetSystemDate();
}
return;
}
void Date::print_ymd()
{
cout<
#include
#include
using namespace std;
class Fraction
{
protected:
int a; //分子定义成保护成员
int b; //分母因为不能为0,因此定义为私有成员
int divisor(int p,int q); // 求最大公约数
public:
void set(int aa,int bb);
void show(); //显示分数
Fraction add(Fraction u); //加一个分数
};
void Fraction::set(int aa,int bb)
{
a=aa;
if(bb!=0){
b=bb;
}
else{
a=0;
b=1;
}
}
void Fraction::show()
{
cout<>a>>b;
cin>>c>>d;
f1.set(a,b);
f2.set(c,d);
f1.show_real();
f2.show_real();
cout<
#include
#include
#include
#include
using namespace std;
class automobile
{
private:
char type[20];
char color[20];
float price;
int carry_weight;
int carry_customer;
public:
void set_data(char *t,char *c,float pri,int cw,int cc);
void movecar(int l,int k);
void horming(int num);
void downcar(int l);
void play_mp3(char *ps);
char *show_type(){return type;}
};
void automobile::set_data(char *t,char *c,float pri,int cw,int cc)
{
strcpy(type,t);
strcpy(color,c);
price=pri;
carry_weight=cw;
carry_customer=cc;
}
void automobile::movecar(int l,int k)
{
cout<<"\n"<>a;
}
int main()
{
automobile nison;
char tp[]="yyida";
char ys[]="white";
nison.set_data(tp,ys,200000,5,3);
nison.horming(5);
nison.movecar(10,3);
nison.downcar(8);
char mp[]="d:\\宋冬野 - 董小姐.flac";
nison.play_mp3(mp);
return 0;
}
#include
using namespace std;
class Point
{
private:
int x,y; //如不写private则默认为私有成员
public:
void InitPoint(int,int);
int GetX(){return x;}
int GetY(){return y;}
void print();
};
void Point::InitPoint(int a,int b)
{
x=a;
y=b;
}
void Point::print()
{
cout<<"["<0?r:0); //有效性检验
Center=p;
}
double Circle::GetRadius()
{
return radius;
}
Point Circle::GetCenter()
{
return Center;
}
double Circle::Area()
{
return 3.14159*radius*radius;
}
void Circle::Print()
{
cout<<"Center=";
Center.print();
cout<<"; Radius="<
#include
#include
using namespace std;
//设置信息函数Set_info
class Info
{
private: //我还是喜欢把private打出来
int Id;
char Name[20];
int Programming;
int Network;
int Database;
int Total;
public:
void Set_info(int id,char *name,int programming,int network,int database);
int Get_Pro();
int Get_Net();
int Get_Dat();
int Get_Tol();
void Show();
};
void Info::Set_info(int id,char *name,int programming,int network,int database)
{
Id=id;
strcpy(Name,name);
Programming=programming;
Network=network;
Database=database;
Total=programming+network+database;
}
int Info::Get_Pro()
{
return Programming;
}
int Info::Get_Net()
{
return Network;
}
int Info::Get_Dat()
{
return Database;
}
int Info::Get_Tol()
{
return Total;
}
//显示信息函数Show
void Info::Show()
{
cout<>id>>name>>programming>>network>>database;
student[i].Set_info(id,name,programming,network,database);
}
cout<<"学号 姓名 程序设计 计算机网络 数据库 总分"<
#include
#include
using namespace std;
//设置信息函数Set_info
class Info
{
private: //我还是喜欢把private打出来
int Id;
char Name[20];
int Programming;
int Network;
int Database;
int Total;
public:
void Set_info(int id,char *name,int programming,int network,int database);
int Get_Pro();
int Get_Net();
int Get_Dat();
int Get_Tol();
void Show();
};
void Info::Set_info(int id,char *name,int programming,int network,int database)
{
Id=id;
strcpy(Name,name);
Programming=programming;
Network=network;
Database=database;
Total=programming+network+database;
}
int Info::Get_Pro()
{
return Programming;
}
int Info::Get_Net()
{
return Network;
}
int Info::Get_Dat()
{
return Database;
}
int Info::Get_Tol()
{
return Total;
}
//显示信息函数Show
void Info::Show()
{
cout<>count;
while(count>MAX_COUNT){
cout<<"学生人数不能超过"<>count;
}
cout<<"请输入学生成绩的信息:"<>id>>name>>programming>>network>>database;
student[i].Set_info(id,name,programming,network,database);
}
cout<<"按总分高低排名如下:"<i;j=j-1){
if(student[j].Get_Tol()>student[j-1].Get_Tol()){
tmp=student[j];
student[j]=student[j-1];
student[j-1]=tmp;
}
}
}
cout<<"学号 姓名 程序设计 计算机网络 数据库 总分"<85&&student[i].Get_Net()>85&&student[i].Get_Dat()>85){
student[i].Show();
}
}
cout<
#include
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date(int y=1900,int m=1,int d=1) //名字相同,这就是构造函数
{
year=y;
month=m;
day=d;
}
void init(int,int,int);
void print_ymd();
void print_mdy();
};
void Date::init(int yy,int mm,int dd) //有效性检验
{
month=(mm>=1&&mm<=12)?mm:1;
year=(yy>=1900&yy<=9999)?yy:1900;
day=(dd>=1&&dd<=31)?dd:1;
}
void Date::print_ymd()
{
cout<
#include
#include
#include
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date():year(1900),month(1),day(1){}; //无参构造函数
Date(int yy,int mm=1,int dd=1); //默认参数构造函数
Date(Date &d):year(d.year),month(d.month),day(d.day){};
Date(char *ps);
void print_ymd();
};
Date::Date(int yy,int mm,int dd):year(1900),month(1),day(1)
{
if(yy>=1900&&yy<=9999){ //这段还是有效性检验
year=yy;
}
else{
return;
}
if(mm>=1&&mm<=12){
month=mm;
}
else{
year=1900;
return;
}
if(dd>=1&&dd<=31){
day=dd;
}
else{
year=1900;
month=1;
return;
}
}
Date::Date(char *ps):year(1900),month(1),day(1)
{
char py[5],pm[3],pd[3];
strncpy(py,ps,4);
ps=ps+5;
strncpy(pm,ps,2);
ps=ps+3;
strncpy(pd,ps,2);
int yy=atoi(py),mm=atoi(pm),dd=atoi(pd);
if(yy>=1900&yy<9999){
year=yy;
}
else{
return;
}
if(mm>=1&&mm<=12){
month=mm;
}
else{
year=1900;
return;
}
if(dd>=1&&dd<=31){
day=dd;
}
else{
year=1900;
month=1;
return;
}
}
void Date::print_ymd()
{
cout<
#include
#include
#include
using namespace std;
class Date
{
private:
int year;int month;int day;
public:
Date():year(1900),month(1),day(1){}
Date(int yy,int mm=1,int dd=1);
Date(Date &d):year(d.year),month(d.month),day(d.day){};
Date(char *ps); //字符串日期构造函数
void print_ymd();
~Date()
{
static int i=1;
cout<<"撤销日期对象"<=1900&&yy<=9999){ //这段还是有效性检验
year=yy;
}
else{
return;
}
if(mm>=1&&mm<=12){
month=mm;
}
else{
year=1900;
return;
}
if(dd>=1&&dd<=31){
day=dd;
}
else{
year=1900;
month=1;
return;
}
}
Date::Date(char *ps):year(1900),month(1),day(1)
{
char py[5],pm[3],pd[3];
strncpy(py,ps,4);
ps=ps+5;
strncpy(pm,ps,2);
ps=ps+3;
strncpy(pd,ps,2);
int yy=atoi(py),mm=atoi(pm),dd=atoi(pd);
if(yy>=1900&yy<9999){
year=yy;
}
else{
return;
}
if(mm>=1&&mm<=12){
month=mm;
}
else{
year=1900;
return;
}
if(dd>=1&&dd<=31){
day=dd;
}
else{
year=1900;
month=1;
return;
}
}
void Date::print_ymd()
{
cout<
#include
#include
using namespace std;
class robot
{
private:
char name[20];
char type[20];
public:
robot()
{
strcpy(name,"XXXXXX");
strcpy(type,"XXXXXX");
}
void set(char n[],char t[])
{
strcpy(name,n);
strcpy(type,t);
}
void out(int a);
void tran_int(int n);
~robot(){};
};
static char *num1[]=
{
" ","one ","two ","three ","four ","five ","six ","seven ",
"eight ","nine ","ten ","eleven ","twelve ","thirteen ",
"fourteen ","fifteen ","sixteen ","seventeen ","eighteen ",
"nineteen "
};
static char *num10[]=
{
" ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ",
"eighty ","ninety "
};
void robot::out(int a)
{
int b=a%100;
if(a/100!=0){
cout<1999999999){
cout<<"Dev C++ 平台无法处理大于1999999999位的数字!"<>n;
cout<
#include
#include
#include
using namespace std;
class robot
{
private:
char name[20];
char type[20];
int num;
char *ps;
public:
robot() //构造函数
{
strcpy(name,"XXXXXX");
strcpy(type,"XXXXXX");
num=0;
ps=new char[5];
strcpy(ps,"Zero");
}
void set(char n[],char t[],int m);
char *out(int a);
char *tran_int(int n);
void print_num();
~robot(){
cout<<"堆区中"<1999999999){
//cout<<"dev C++平台无法处理太大的数字!"<1999999999) else
p=new char[strlen(kk)+1];
strcpy(p,kk);
return p;
}
void robot::print_num()
{
cout<<"整数为:"<0){
char *tp=tran_int(num);
ps=new char[strlen(tp)+1];
strcpy(ps,tp);
delete[]tp;
}
else if(num==0){
ps=new char[5];
strcpy(ps,"Zero");
}
else{
ps=new char[13];
strcpy(ps,"负数不能翻译");
}
}
int main()
{
robot brown;
brown.print_num();
int n;
cout<<"请输入n:";
cin>>n;
brown.set("brown","800#",n);
brown.print_num();
cout<
#include
#include
using namespace std;
class Person
{
private:
char Name[9];
char Sex;
int Age;
char Pid[19];
char *Add;
public:
Person()
{
strcpy(Name,"XXX");
Age=0;
Sex=' ';
strcpy(Pid,"xxxxxxxxxxxxxxxxxx");
Add=NULL;
}
Person(char *N,int A,char S,char *P,char *Ad)
{
strcpy(Name,N);
Age=A;
Sex=S;
strcpy(Pid,P);
int L=strlen(Ad);
Add=new char[L+1];
strcpy(Add,Ad);
}
~Person()
{
cout<<"Now destroying Person: "<
#include
using namespace std;
class Test
{
private:
int x;
public:
Test(int=0);
void print();
};
Test::Test(int a)
{
x=a;
}
void Test::print()
{
int x=13;
cout<<" x="<x="<x<
#include
using namespace std;
class Date
{
private:
int day,month,year;
public:
Date(int y=1900,int m=1,int d=1); //构造函数设置默认值
void SetDate(int yy,int mm,int dd);
bool IsLeapYear();
void print_ymd();
void print_mdy();
};
Date::Date(int y,int m,int d)
{
SetDate(y,m,d);
}
void Date::SetDate(int yy,int mm,int dd)
{
month=(mm>=1&&mm<=12)?mm:1;
year=(yy>=1900&&yy<=2100)?yy:1;
switch(month)
{
case 4:
case 6:
case 9:
case 11:
day=(dd>=1&&dd<=30)?dd:1;break;
case 2:
if(IsLeapYear()){
day=(dd>=1&&dd<=29)?dd:1;
}
else{
day=(dd>=1&&dd<=28)?dd:1;
}
break;
default:day=(dd>=1&&dd<=31)?dd:1;
}
}
bool Date::IsLeapYear()
{
if(year%400==0||(year%100!=0&&year%4==0)){
return true;
}
else{
return false;
}
}
void Date::print_ymd()
{
cout<>a>>b>>c;
date.SetDate(a,b,c);
cout<<"两种输出格式如下:"<
#include
#include
#include
using namespace std;
class person
{
private:
char *name;
char sex;
char pid[19];
int weight;
int high;
public:
person(); //一个可爱的无参构造函数
person(char *n,char s,char *p,int w,int h);
void change_data(char *n,char s,char *p,int w,int h);
void walking(int k,int v);
void hearing(char *sentence);
void speaking(int n);
void writing();
void print();
void out(int a);
~person();
};
static char *num1[]=
{
" ","one ","two ","three ","four ","five ","six ","seven ",
"eight ","nine ","ten ","eleven ","twelve ","thirteen ",
"fourteen ","fifteen ","sixteen ","seventeen ","eighteen ",
"nineteen "
};
static char *num10[]=
{
" "," ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ",
"eighty ","ninety "
};
void person::print()
{
cout<'a'&&*p<='z'){
*p='A'+(*p-'a'+0); //删掉这个+0试试看
}
else if(*p>='A'&&*p<='Z'){
*p='a'+(*p-'A');
}
p++;
}
cout<1999999999){
cout<<"Dev C++平台无法处理太大的数字!"<
#include
#include
#include>
using namespace std;
class Date
{
protected:
int day,month,year;
public:
Date(){int year=1900,month=1,day=1;}
Date(int yy,int mm,int dd){init(yy,mm,dd);}
void init(int,int,int);
void print_ymd();
void print_mdy();
};
void Date::init(int yy,int mm,int dd)
{
month=(mm>=1&&mm<=12)?mm:1;
year=(yy>=1900&&yy<=2100)?yy:1;
day=(dd>=1&&dd<=31)?dd:1;
}
void Date::print_ymd(){cout<=0&&h<=24)?h:0;
minute=(m>=0&&m<=60)?m:0;
second=(s>=0&&s<=60)?s:0;
}
void Time::print_time(){cout<'a'&&*p<='z'){
*p='A'+(*p-'a'+0);
}
else if(*p>='A'&&*p<='Z'){
*p='a'+(*p-'A');
}
p++;
}
cout<1999999999){
cout<<"Dev C++平台无法处理太大的数字!"<
#include
#include
using namespace std;
class mobile
{
private:
char mynumber[12];
char m_type[40];
float price;
public:
mobile(){init("00000000000","None_type", 0);}
void init(char *number,char *pt,float pri);
void dial();
void answer(char othernumber[]);
void hangup();
void show();
};
void mobile::init(char *number,char *pt,float pri)
{
strcpy(mynumber,number);
strcpy(m_type,pt);
price=pri;
}
void mobile::dial()
{
cout<<"Dialing number is "<