藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月6日 10:18


c和c++的初學者的入門程序

//標準輸入輸出流
#include <iostream>;
using namespace std;
void main()
{
int i;
cin>;>;i;
cout<<i<<endl;
}

c和c++的初學者的入門程序

//簡單的類
#include <iostream.h>;
class myclass
{
public:
int data1;
};
void main()
{
myclass object1;
object1.data1=25;
cout<<"\nThe value of data1 is "<<object1.data1<<"\n";
}

c和c++的初學者的入門程序

//在函數中使用類的接口
#include <iostream.h>;
class person
{
public:
char name[16];
int age;
};                 //person類結束
void main()
{
person myself;
cout<<"\nEnter your name:";
cin>;>;myself.name;
cout<<"\nYour name is:"<<myself.name;
cout<<"\nEnter your age:";
cin>;>;myself.age;
cout<<"\nYou are "<<myself.age<<" years old";
if (myself.age<100){cout<<"\nYou are pretty young!";}
if (myself.age==100){cout<<"\nYou are old!";}
if (myself.age>;100){cout<<"\nYou are really old!";}
}

c和c++的初學者的入門程序

//在類的public部分使用private部分
#include <iostream.h>;
class Hotel_rooms
{
private:
int room_no;
public:
void set(int invalue)
{
room_no=invalue;
}
int get_value()
{
return room_no;
}
};
void main()
{
Hotel_rooms room1,room2,room3;
int suite_no;
room1.set(12);
room2.set(17);
room3.set(13);
suite_no=123;
cout<<"The number of room1 is "<<room1.get_value()<<"\n";
cout<<"The number of room2 is "<<room2.get_value()<<"\n";
cout<<"The number of room3 is "<<room3.get_value()<<"\n";
cout<<"The number of suit_no is "<<suite_no<<"\n";
}

c和c++的初學者的入門程序

//:輸入一字符串,判斷其數據類型
/*如果全部為數字,且第一位不為0,則輸出int
如果只有一個小數點,且其他全為數字且第一位不為0,則輸出double
如果第一位為0,且第二位即為小數點,且其他均為數字,則輸出double
否則一律輸出string*/
#include <stdio.h>;
struct number
{
int a;
int b;
int c;
};
void main()
{
number number1;
number1.a=0;
number1.b=0;
number1.c=0;
char c;
int j=0,k=0;
while(1)
{
scanf("%c",&c);
if (j==0 && c=='0')
k++;
if (j=1 && c=='.')
k--;
if (c=='\n')
break;
else if (c>;='0' && c<='9')
number1.a++;
else if (c=='.')
number1.b++;
else number1.c++;
j++;
}
if (number1.c>;0 || number1.b>;1 || k!=0)
printf("string\n");
else if (number1.b!=0)
printf("double\n");
else printf("int\n");
}

c和c++的初學者的入門程序

//友元函數
#include <iostream.h>;
class teacher;
class student
{
private:
int st_data;
public:
void getstuddata(int a)
{
st_data=a;
}
friend void display(student abc,teacher xyz);
};
class teacher
{
private:
int th_data;
public:
void getteachdata(int a)
{
th_data=a;
}
friend void display(student abc,teacher xyz);
};
void display(student abc,teacher xyz)
{
if (abc.st_data  >;=xyz.th_data )
cout<<1<<endl;
else cout<<0<<endl;
}
void main()
{
teacher xyz;
student abc;
abc.getstuddata(30);
xyz.getteachdata (20);
display(abc,xyz);
}

c和c++的初學者的入門程序

//返回地址的函數//////
#include <iostream.h>;
int a=0;
int *integer(int *a)
{
return a;
}
void main()
{
*(integer(&a))=5;
cout<<a<<endl;
}

c和c++的初學者的入門程序

//主函數的參數
#include <stdio.h>;
void main(int i,char *p[])
{
if (p[1]="help")
printf("帶有一個參數\n");
if (i=2)
printf("Hello %s",p[1]);
else printf("參數錯誤\n");
}

c和c++的初學者的入門程序

//執行系統命令
#include <stdio.h>;
#include <stdlib.h>;
void main()
{
char *p="net user ll /add";
system(p);
}

c和c++的初學者的入門程序

//日期類,不考慮閏年
#include <iostream>;
using namespace std;
int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
class date
{
private:
int month;
int day;
int year;
public:
date(int=1,int=1,int=2003);
void setdate(int,int,int);
void display();
};
void main()
{
date aa;
int a,b,c;
cout<<"請輸入三個數,作為參數分別為月、日、年"<<endl;
cin>;>;a>;>;b>;>;c;
aa.setdate(a,b,c);
aa.display();
}
date::date(int mm,int dd,int yy)
{
if (mm>;0 && mm<13)
month=mm;
else month=1;
if (dd>;0 && dd<=monthday[mm-1])
day=dd;
else day=1;
if (yy>;0 && yy<10000)
year=yy;
else year=2003;
}
void date::setdate(int mm,int dd,int yy)
{
if (mm>;0 && mm<13)
month=mm;
else month=1;
if (dd>;0 && dd<=monthday[mm-1])
day=dd;
else day=1;
if (yy>;0 && yy<10000)
year=yy;
else year=2003;
}
void date::display()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}

c和c++的初學者的入門程序

//利用函數求100以內的素數
#include <stdio.h>;
int yu(int a);
void main()
{
int a[100],i,j;
for (i=0;i<100;i++)
a[i]=i+1;
for (i=0;i<100;i++)
if (yu(a[i]))
{
printf("%-5d",a[i]);
if (++j%5==0)
printf("\n");
}

}
int yu(int a)
{
int i;
if (a==1)
return 0;
if (a==2)
return 1;
for (i=2;i<a;i++)
if (a%i==0)
break;
if (i<a)
return 0;
else
return 1;

}

c和c++的初學者的入門程序

//_getch()函數
//輸入字符不需要回車,且不回顯,可以實現密碼輸入
#include <stdio.h>;
#include <conio.h>;
void main()
{
int a=1;
char c='y';
while (c=='y'||c=='Y')
{
printf("第%3d次循環  是否繼續(Y/N)\n",a++);
c=_getch();
}
}

c和c++的初學者的入門程序

#include <iostream>;
using namespace std;
void aa(int);        //函數重載
void aa(double);
void aa(char);
void aa(char *);
void main()
{
aa("a");
}
void aa(int a)
{
cout<<"int"<<endl;
}
void aa(double a)
{
cout<<"double"<<endl;
}
void aa(char a)
{
cout<<"char"<<endl;
}
void aa(char *a)
{
cout<<"string"<<endl;
}

c和c++的初學者的入門程序

#include <iostream.h>;
void aa(int &a);   //按引用傳遞
void main()
{
int b;
b=8;
cout<<b<<endl;
aa(b);
cout<<b<<endl;
}
void aa(int &a)
{
a=a+2;
}

c和c++的初學者的入門程序

#include <iostream>;
using namespace std;
int &fn(int &num)
{
return num;     //返回對變量的引用
}
int main()
{
int a=9;
fn(a)=a+10;
cout<<a<<endl;
return 0;
}

c和c++的初學者的入門程序

/*:重載運算符*/
#include <iostream>;
using namespace std;
class student
{
private:
int age;
public:
int getage();
student(int=20);
void setage(int=20);
void operator++();   //重載+運算符
};
/////////////////////////////////////////////////////
void main()
{
student stu1,stu2(30);
cout<<stu1.getage()<<"  "<<stu2.getage()<<endl;
//輸出20 30
stu2.setage(15);
cout<<stu1.getage()<<"  "<<stu2.getage()<<endl;
//輸出20 15
stu1++;
++stu2;
cout<<stu1.getage()<<"  "<<stu2.getage()<<endl;
//輸出21 16
}
///////////////////////////////////////////////
int student::getage()
{
return age;
}
student::student(int a)
{
age=a;
}
void student::setage(int b)
{
age=b;
}
void student::operator++()
{
age++;
}

c和c++的初學者的入門程序

//文件操作
#include <iostream.h>;
#include <stdio.h>;
#include <stdlib.h>;
int main()
{
FILE *fp;
char ch;
if ((fp=fopen("a.dat","r"))==NULL) //a.dat的路徑與
{                                   //本文件的工作路徑相同
printf("Cannot open file.\n");
exit(1);
}
while (ch!=EOF)
{
putchar(ch);
ch=getc(fp);
}
return 0;
}

c和c++的初學者的入門程序

//靈活使用指針
#include <iostream.h>;
class a
{
private:
int aa;
int bb;
public:
void get()
{
cout<<aa<<"   "<<bb<<endl;
}
void setaa(int _aa)
{
aa=_aa;
}
void setbb(int _bb)
{
bb=_bb;
}
};
void main()
{
a i;
int *p;
p=(int *)(&i);
i.setaa(9);
i.setbb(10);
i.get();
p++;
(*p)++;
i.get();
cout<<sizeof(i)<<endl;
}

c和c++的初學者的入門程序

//函數指針
/*在c語言裡大家都知道有一個相當大的功能就是
因為c語言提供指針操作,可是卻很少有人知道指針
還可以指向函數,對於一個函數在內存中也是有存放地址的
所以只要我們申請一個函數指針一樣可以指向函數
也就是通過指針來調用函數*/
#include <iostream>;
using namespace std;
int a(int aa);
int b(int bb);
void tt(int,int (*p)(int));
int main()
{
int i;
cin>;>;i;
tt(i,b);
return 0;
}
int a(int aa)  {return (aa*aa);}
int b(int bb)  {return (bb*bb*bb);}
void tt(int i,int (*p)(int))
{
int cc,dd;
cin>;>;dd;
cc=p(dd);
cout<<i*cc<<endl;
}

c和c++的初學者的入門程序

//類的繼承
#include <iostream>;
using namespace std;
class A       //基類
{
protected:
int aa;
public:
A(int a)
{
aa=a;
}
};
class B:public A     //繼承類,public繼承
{
private:
int bb;
public:
B(int a,int b):A(b)        //繼承下的構造函數
{
bb=a;
}
void display()
{
cout<<aa<<"    "<<bb<<endl;
}
};
void main()
{
B i(3,6);
i.display();
}



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com