//C++第一次作業,老師試探C語言基礎
//輸入不多于20是個字符,遇到回車結束輸入,并判斷個數,字符類型,并反序輸出。
//
#include <stdio.h>
?? int XiaoXie=0,ShuZi=0,DaXie=0,Other=0;
main(){
?? int i,n,m;
?? char a[100];
??? printf("最多輸入20個你想到的字符:\n");
??? for(i=0;i<20;i++){
???? a[i]=getchar();
???? if (a[i]=='\n')
?????? break;
?????? m=i+1;???????????? //回車前字符個數
???? }
?? printf("你輸入的字符:\n");
?? for(n=0;n<i;n++)
???? putchar(a[n]);
???? putchar('\n');
???? printf("反序:");
?? for(n=i-1;n>=0;n--){? //回車時i已經計數,所以'n=i-1'
???? putchar(a[n]);
//判斷字符類型,個數:
???? if (a[n]>='0'&&a[n]<='9')
???????? ShuZi=ShuZi+1;
???? else if(a[n]>='a'&&a[n]<='z')
???????? XiaoXie=XiaoXie+1;
???? else if (a[n]>='A'&&a[n]<='Z')
???????? DaXie=DaXie+1;
???? else Other=Other+1;
???? }
???? printf("\n你輸入了%d個字符",m);
???? printf("其中:\n");
?????? printf("所輸入的小寫字母個數為:%d\n",XiaoXie);
?????? printf("所輸入的數字個數為:%d\n",ShuZi);
?????? printf("所輸入的大寫字母個數為:%d\n",DaXie);
?????? printf("其他字符的個數為:%d\n",Other);
}
實驗2? 類與對象
1.實驗目的
(1)學習類與對象的定義。
(2)掌握類與對象的使用方法。
(3)學習類和對象的聲明。
(4)學習數據成員與成員函數的訪問方式。
(5)理解構造函數和析構函數的定義與執行過程。
(6)學會構造函數的重載方法。
2.實驗基本要求
(1)可以定義出一個類,并且在程序中創建一個該類的對象。
(2)實現類的定義,建立一個新的類。
(3)體現出構造函數和析構函數的調用。
(4)重載構造函數。
(5)為了提高對相關知識的綜合應用能力,編寫一個較為復雜的類和對象的應用程序。
3.實驗基本步驟
(1) 建立一個源程序文件。
(2)在此文件中建立一個新的類,將新建的類命名為Rect。
class Rect
{
public:
? int Area_int();
? double Area_double();
? Rect(double l,double w);
? Rect(int l,int w);
? virtual ~Rect();
? int nLength;
?? int nWidth;
? double dLength;
? double dWidth;
};
(3)向Rect類中添加數據成員及成員函數,并完善成員函數的功能。如設計一個Area_int()函數,計算邊長為整型的長方形的面積;設計一個Area_double()函數,計算邊長為double型的長方形的面積。
(4)重載構造函數。一種構造函數用整型變量記錄長方形的長和寬,另一種構造函數用double型記錄。
(5)體現對象的構造和析構過程。例如,在構造函數中用cout<<”I am the constructor!”<<endl;在析構函數中輸出cout<<”I am the destructor”<<endl。
(6)在main()函數中定義兩個Rect類的對象,一個對象用實例實現(就像定義普通的變量一樣),另一個對象用指針實現(利用關鍵字new,給指針分配內存空間)。并用不同的參數,以調用不同的構造函數體現構造函數的重載。
(7)下面提供一個較為復雜的類與對象的應用,請根據掌握情況進行實驗。定義一個類student,管理一個學生的基本信息,包括學生姓名、學號、英語成績、數學成績、程序設計語言成績、平均成績等數據成員,以及對這些數據成員的操作。
class student
{
private:
? char stu_name[10];
? charstu_no[10];
? float english_score;
? float math_score;
? float program_score;
? float avg_score;
public:
? student();
? void setname(char'newname);
? void setno(charG number);
? char getname();
? char getno();
? void setenglish(float e);
? void setmath(float m);
? void setprogram(float p);
? float getavg();
};
(8)在main()函數中,根據要求動態分配一組學生的基本信息。從鍵盤輸入得到學生的基本信息,并輸出到顯示器上。
(9)設置一靜態成員記錄學生人數。
// (10)設計一友元函數,輸出學生名單。
//第二次作業1
///////////////////////////////////
////計算機C041 袁建華 044599///////
//////////////////////////////////
#include <iostream.h>
class Rect {
public:
? int Area_int();
? double Area_double();
? Rect();
? Rect (int l,int w);
? Rect (double l,double w);
? virtual ~Rect();
int nLength;
int nWidth;
double dLength;
double dWidth;
};
//計算面積
int Rect::Area_int(){
return nLength*nWidth;
}
double Rect::Area_double(){
return dLength*dWidth;
}
//定義構造函數
Rect::Rect(int l,int w){
?? nLength=l;
?? nWidth=w;
?? cout<<"I am the constructor!"<<endl;
}
Rect::Rect(double l,double w){
?? dLength=l;
?? dWidth=w;
?? cout<<"I am the constructor!"<<endl;
}
Rect::~Rect(){
cout<<"I am the destructor"<<endl;
}
void main(){
?Rect A1(123,321);
?cout<<"the int Area:"<<A1.Area_int()<<endl;
?Rect *P1=new Rect(0.123,0.321);?????? //動態創建對象
?cout<<"the double Area:"<<P1->Area_double()<<endl;
}
/////第二次作業2
?//////////////////////////////
?//////////student.cpp/////////
?//////////////////////////////
//1.在main()函數中,根據要求動態分配一組學生的基本信息。從鍵盤輸入得到學生的基本信息,并輸出到顯示器上。
//2.設置一靜態成員記錄學生人數。
//3.設計一友元函數,輸出學生名單。
#include "iostream"
#include "string"
using namespace std;
class student{
private:
? int i;
? string stu_name;
? string stu_no;
? float english_score;
? float math_score;
? float program_score;
? float avg_score;
public:
student(){
??? }
? void setname();
? void setno();
? string getname();
? string getno();
? void setenglish();
? void setmath();
? void setprogram();
? float getavg();
// void friend list_stu(student &);????????? //聲明友元函數
};
//定義友元函數
/*
void list_stu(student &Stu[i]){
? cout>>"Name:">>Stu[i]->stu_name;
? cout>>"Number:">>Stu[i]-stu_no;
}*/
void student::setname(){? //通過一個數組接受學號
?string name;
?cout<<"Name:";
?cin>>name;
?stu_name=name;
}
void student::setno(){??? //通過一個數組接受一個學號
?string number;
?cout<<"Number:";
?cin>>number;
?stu_no=number;
}
string student::getname(){?? //返回一個名字
?return stu_name;
}
string student::getno(){???? //返回學號
?return stu_no;
}
void student::setenglish(){? //輸入英語成績
?float e;
?cout<<"English_score:";
?cin>>e;
?english_score=e;
}
void student::setmath(){?????? //輸入數學成績
?float m;
?cout<<"Math_score:";
?cin>>m;
?math_score=m;
}
void student::setprogram(){??? //輸入程序設計程序
?float p;
?cout<<"Program_score:";
?cin>>p;
?cout<<endl;
?program_score=p;
}
float student::getavg(){?????? //計算三科平均成績
?avg_score=(english_score+math_score+program_score)/3;
?return avg_score;
}
?
void main(){
?//?? int i;
?student *stu=new student();??? //動態創建一組
??? stu->setname();
??? stu->setno();
??? stu->setenglish();
??? stu->setmath();
??? stu->setprogram();
?/* student Stu[10];
for(i=0;i<4;i++){
??? cout<<"第"<<i+1<<"個"<<endl;
?Stu[i].setname();
??? Stu[i].setno();
??? Stu[i].setenglish();
??? Stu[i].setmath();
??? Stu[i].setprogram();
??? }
?*/
/*for (i=0;i<10;i++){
?? cout<<"第"<<i+1<<"個"<<endl;
?cout<<"Name:"<<Stu[i].getname()<<endl;
??? cout<<"Number:"<<Stu[i].getno()<<endl;
?? // cout<<Stu[i].getenglish();
?? // cout<<Stu[i].getmath();
?? // cout<<Stu[i].getprogram();
?*/
}
//list_stu(Stu[1]);
?
posted on 2006-04-03 13:02
華劍緣 閱讀(413)
評論(0) 編輯 收藏 引用