一個矩陣類Matrix 類,在構造函數中根據參數創建數據成員:一個二維數組,類中實際定義的二維數組的數據成員是一個指針 ( 二級指針 ) , int **pMatrix 。 在構造函數中根據傳送的參數為這個二維數組分配空間: pMatrix = newint[x][y]
~~~~~~~~
?int **pMatrix;
pMatrix = new int [x][y] ;
~~~~~~~~
*pMatrix = new int[x][y]
這樣她提示什么
?error C2440: '=' : cannot convert from 'int (*)[1]' to 'int *'
編譯器說new int [x][y]是int (*)[1]型的
?
如果改成:
int **pMatrix;
*pMatrix= new int [x];
倒可以通過編譯
??????
//忘了從什么地方看到某人這么說:
動態數組不能用在二維……
動態數組實際上是一個指針,為其分配空間
如果想設置二維數組的話,就必須設置一個指針類型的動態數組,數組中的每個項都是另一個二維數組
舉例:
int **a = new int* [m];
for(int i=0; i<m; i++)
??a[i]= new int[n];
釋放內存時要先
for(i=0; i<m; i++)
??delete[] a[i];
然后再
delete[]??a;
呵呵;
posted @
2006-04-09 22:54 華劍緣 閱讀(591) |
評論 (2) |
編輯 收藏
作用域:
??
標識符是否有效的范圍
??1.? 函數原形作用域
? 2.塊作用域
? 3.類作用域
??4.文件作用域
程序運行在內存中分配空間:代碼區,全局數據區,堆區 ,棧區/
具有文件作用域的變量放在全局數據區。
具有其它的作用域的變量放在堆區,棧區。??
可見性?:
?? 標識符是否能夠被引用的問題。
?? 1聲明在先,引用在后。
?? 2不能在同作用域中重名。
?? 3標識符在外層中聲明,在內層同名標識符中沒有聲明,則在該內層中可見。
?? 4對于兩個嵌套的作用域,內層聲明的標識符跟外層的標識符同名則外層的標識符在內層中不可見。
posted @
2006-04-05 08:43 華劍緣 閱讀(234) |
評論 (0) |
編輯 收藏
Lucy上了初中,她很喜歡數學,經常做數學奧林匹克的題目,可是今天她遇到了難題,于是就向她在南開大學上學的哥哥Feagle請教,聰明的哥哥不一會功夫就編程解決了妹妹的問題(^_^,南開大學的學生就是優秀)! 妹妹的題目是這樣的:對給定的f(n) 當 n>=50025002 的時候,f(n)=n-5;當 n<50025002 的時候,f(n)=f(f(n+2005))。現在請您試試編程解決Lucy的難題!?
輸入?
?? 輸入只有一個整數n,-2147483647<n<2147483647 。? ? 輸出 ?? 輸出只有一個整數,f(n) 的值。 ? 樣例輸入 樣例輸出 50025002 50024997 ?? ? 時間限制 ?? 對每個輸入數據,程序應在5秒內給出結果。 ?
|
? |
? |
輸入 |
? |
輸入只有一個整數n,-2147483647<n<2147483647 。 |
? |
輸出 |
? |
輸出只有一個整數,f(n) 的值。 |
? |
樣例輸入 |
樣例輸出 |
50025002 |
50024997 |
|
? |
? |
時間限制 |
? |
對每個輸入數據,程序應在5秒內給出結果。 |
posted @
2006-04-04 22:13 華劍緣 閱讀(137) |
評論 (1) |
編輯 收藏
試驗
一
目的:
用遞推近似計算定積分
內容:
對
? n = 0,1,2,…,20?
計算定積分
????
?
?=?
?
代碼:
算法一:
#include
<
stdio.h
>
#include
<
math.h
>
main()


{

???
int
?n;

???
double
?y[
21
];

y[
0
]
=
log(
6
)
-
log(
5
);

for
(n
=
1
;n
<=
20
;n
++
)

?y[n]
=
(
1.0
/
n)
-
5
*
y[n
-
1
];

printf(
"
Data?is:\n
"
);

??????
for
(n
=
1
;n
<=
20
;n
++
)


??????
{

??????printf(
"
y[
""
%d
""
]:??
"
,n);

??????printf(
"
%f
"
,y[n]);

??????printf(
"
\n
"
);

??????printf(
"
\n
"
);

????}
}
Data is:
y[1]:? 0.088392
y[2]:? 0.058039
y[3]:? 0.043139
y[4]:? 0.034306
y[5]:? 0.028468
y[6]:? 0.024325
y[7]:? 0.021233
y[8]:? 0.018837
y[9]:? 0.016926
y[10]:? 0.015368
y[11]:? 0.014071
y[12]:? 0.012977
y[13]:? 0.012040
y[14]:? 0.011229
y[15]:? 0.010522
y[16]:? 0.009890
y[17]:? 0.009372
y[18]:? 0.008696
y[19]:? 0.009151
y[20]:? 0.004243
算法二:
#include
<
stdio.h
>
#include
<
math.h
>
main()


{

???
int
?n;

???
double
?y[
21
];

y[
20
]
=
(
1
/
2
)
*
(
1
/
105
+
1
/
126
);

for
(n
=
20
;n
>=
0
;n
--
)

?y[n
-
1
]
=
((
1.0
/
n)
-
y[n])
/
5.0
;

printf(
"
Data?is:\n
"
);

??????
for
(n
=
1
;n
<=
20
;n
++
)


??????
{

??????printf(
"
y[%d]:??
"
,n);

??????printf(
"
%f
"
,y[n]);

??????printf(
"
\n
"
);

??????printf(
"
\n
"
);

????}
}
Data is:
y[1]:? 0.088392
y[2]:? 0.058039
y[3]:? 0.043139
y[4]:? 0.034306
y[5]:? 0.028468
y[6]:? 0.024325
y[7]:? 0.021233
y[8]:? 0.018837
y[9]:? 0.016926
y[10]:? 0.015368
y[11]:? 0.014071
y[12]:? 0.012977
y[13]:? 0.012040
y[14]:? 0.011229
y[15]:? 0.010523
y[16]:? 0.009884
y[17]:? 0.009406
y[18]:? 0.008526
y[19]:? 0.010000
y[20]:? 0.000000
分析對算法的認識:
在
Y[20]
之前兩種算法所產生的結果相差不大。但是由于誤差傳播是逐步擴大的。因而不可直接就此斷言那種算法好;隨著次數的增大其后果是可怕的,代價是巨大的
。經驗在下親自驗證到
Y[50]
時,會出現相當可怕的結果!!
算法一:
?
算法二:
?
進行誤差分析:
由遞推公式布難求出:算法一的誤差是
5^n
級別!!
???????????????????
算法二
的誤差級別則為
(1/5)^n
級別的!!
綜上所述:在數值計算中
如不注意誤差分析,就會出現“差之毫厘
失之千里”
的錯誤結果。我們應重視計算過程中的誤差分析,算法分析。
?
posted @
2006-04-04 11:43 華劍緣 閱讀(161) |
評論 (0) |
編輯 收藏
- 類就是一種自己定義的類型,如同int ,double ,bool ,char~~~
對象就相當于變量,如int a;double b;char c;~~~
用起來又是一樣的,呵呵,現在才明白真是好糊涂呀。
拷貝構造函數:
-
-
//調用之前一定要定義構造函數
-
class 類名{
-
類名? (類名&?? 對象名);
-
}
-
-
類名::類名(類名&?? 類名){
-
函數體;
-
}
-
-
-
-
(1):當用類的一個對象去初始化里為一個對象時系統自動調用拷貝構造函數來實現拷貝賦值。
- void main(void)
- {
- ?? Point ?A(1,2);?? //
- ???Point? B(A);?????? //拷貝構造函數被調用
- ???? cout<<B.GetX()<<endl;
- }
?????????? (2):若函數的形式函數為類對象,調用函數時,實參賦值給形參,系統自動調用拷貝構造函數。
????
void fund1(Point A)
{
??? cout<<p.GetX()<<endl;
}
void main(void)
{
Point A(1,2);
fund1(A):????????? //調用拷貝構造函數
}
(3)若函數的返回值是類對象,調用函數時,系統自動調用拷貝構造函數。
posted @
2006-04-03 21:59 華劍緣 閱讀(188) |
評論 (0) |
編輯 收藏
終于發現什么叫:
紙上得來終覺淺,絕知此事要躬行。
以后要多多練習才是呀 。
郁悶了,看那么多書。想得方法到很豐富,可是自己能力不足實現不了。
東西記得模模糊糊的。用的時候才知道。
posted @
2006-04-03 21:08 華劍緣 閱讀(92) |
評論 (0) |
編輯 收藏
摘要: 實驗
3
對象數組與對象指針
1
.實驗目的
(1)
掌握數組與指...
閱讀全文
posted @
2006-04-03 16:21 華劍緣 閱讀(1367) |
評論 (0) |
編輯 收藏
//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 @
2006-04-03 13:02 華劍緣 閱讀(413) |
評論 (0) |
編輯 收藏
一本C++ primer,什么時候能看完呢,其中要夾雜一些練習。
堅持寫讀書筆記。
這就開始,呵呵。
posted @
2006-04-02 20:08 華劍緣 閱讀(98) |
評論 (0) |
編輯 收藏