




已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
沈陽理工大學課程設(shè)計專用紙 No26目 錄1 題目內(nèi)容及設(shè)計要求12 總體設(shè)計12.1 總體功能框圖12.2 類的設(shè)計說明12.3 主要算法流程圖13程序清單及注釋14運行結(jié)果與分析15總結(jié)26參考文獻2沈陽理工大學1 題目內(nèi)容及設(shè)計要求題目17 復數(shù)計算器內(nèi)容及要求:(1)所設(shè)計的復數(shù)計算器可以進行+ - * += -= *= + - = = (istream &is,CComplex &com ); /重載輸入friend ostream & operator(CComplex &com);int operator(CComplex &com);int operator!=(CComplex &com);int operator=(CComplex &com);2.3 主要算法流程圖開始聲明一個complex類,并定義double,real,image;聲明類的函數(shù),構(gòu)造函數(shù),加減乘除和取模運算c.real=real+c2.real;c.imag=imag+c2.imag;c.real=real-c2.real;c.imag=imag-c2.imag;c.real=(real*c2.real+imag*c2.imag)/a;c.imag=(imag*c2.real-real*c2.imag)/ac.real=real*c2.real-imag*c2.imag;c.imag=real*c2.imag+imag*c2.real;Sqrt(real*real=imag*imag);輸出c1,c2,c1+c2,c1-c2,c1*c2,c1/c2, c1的值終止。圖21 算法流程圖3 程序清單及注釋#include #include #include #include #include #include /#define EPS len-5 / 定義精度常數(shù)using namespace std;namespace NameCComplex / 定義命名空間 NameCComplex/*-|部分A:| 復數(shù)類 CComplex 的聲明和定義,以及結(jié)構(gòu)體類型 用戶 User 的定義| -*/*- | 復數(shù)類 CComplex 的聲明 -*/class CComplexprivate:double Real, Image; / 分別為復數(shù)的實部和虛部public:CComplex(double real=0, double image=0) / 構(gòu)造函數(shù)Real = real; Image = image;friend istream & operator (istream & is, CComplex & com); / 重載輸入friend ostream & operator (CComplex & com);int operator ”,比較模的大小int CComplex:operator (CComplex & com)if ( mod() com.mod() )return 1; / 若大,則返回 1elsereturn 0;/ 重載運算符“”,比較模的大小int CComplex:operator (CComplex & com)if ( mod() (istream & is, CComplex & com) cout s; / 用字符串的形式接收復數(shù)int len = strlen(s); / 求出字符串的長度int n = 0, sign = 1; / 其中的 n 值 為當前從字符串中提取出的數(shù)字,會在下面的 while 語句中得到確定的值 / sign 為狀態(tài)變量,表示數(shù)值的正負符號,以輔助辨認正負值com.Image = com.Real = 0;/ 判斷接收的字符串是否合法for(int k=0; klen; k+)if ( (sk9) & (sk!=+ & sk!=- & sk!=i) )cout error endl;return is; / 錯誤,輸出出錯信息并返回/ 順序識別字符串中各字符for(int k=0; klen;)if ( n!=0 & (sk=- | sk=+) ) / 當前字符是否為符號位com.Real = sign * n; / 是符號位,且 n!=0,即 n 已被賦值(通過下面的whlie語句),表明當前讀取的是虛部的符號n = 0; / 將原 n*sign 值(帶正負號的數(shù)值)賦給實部后,將 n 清零,準備下一次繼續(xù)接收并判斷是否為虛部的值if ( sk = -) / 當前字符若為負號sign = -1; k+; / 給符號標志變量 sign 賦值,表示為負數(shù)if ( sk = +) / 當前字符若為正號sign = 1; k+; / 給符號標志變量 sign 賦值,表示為正數(shù)if ( sk=i ) / 若當前字符為“i”if ( k!=len-1 ) / 判斷字符 i 是否為字符串中最后一個字符cout =0 & sk=9 ) / 當前字符若在 09 之間,則將數(shù)字字符轉(zhuǎn)換成數(shù)字數(shù)值n = n * 10 + sk - 0;k+;if ( slen-1!=i & n!=0 ) / 如果最后一個字符不是 i,表示復數(shù)對象內(nèi)只有實部,沒有虛部,如:-acom.Real = n * sign;return is;/ 重載復數(shù)的輸出ostream & operator (ostream & os, CComplex & com)if ( fabs(com.Image)=0 ) / 如果虛部為 0os com.Real; / 只輸出實部else if ( fabs(com.Real)=0 ) / 如果實部為 0os com.Image 0 )os com.Real + com.Image i; / 虛部為正elseos com.Real com.Image i; / 如 實部為 3,虛部為 -6i,就變?yōu)?3 - 6i,而不是 3 + -6ireturn os;/ 加法重載CComplex CComplex:operator + (CComplex & com)CComplex sum;sum.Real = Real + com.Real; / 實部相加sum.Image = Image + com.Image; / 虛部相加return sum;/ 乘法重載CComplex CComplex:operator * (CComplex & com)CComplex multi;multi.Real = Real * com.Real - Image * com.Image; / 乘積實部multi.Image = Real * com.Image + Image * com.Real; / 乘積虛部return multi;/ 減法重載CComplex CComplex:operator - (CComplex & com)CComplex sub;sub.Real = Real - com.Real; / 實部相減sub.Image = Image - com.Image; / 虛部相減return sub;/ 加法賦值重載CComplex CComplex:operator += (CComplex & com)Real = Real + com.Real; / 實部Image = Image + com.Image; / 虛部return *this;/ 減法賦值重載CComplex CComplex:operator -= (CComplex & com)Real = Real - com.Real; / 實部Image = Image - com.Image; / 虛部return *this;/ 乘法賦值重載CComplex CComplex:operator *= (CComplex & com)double nReal = Real * com.Real - Image * com.Image; / 乘積實部double nImage = Real * com.Image - Image * com.Real; / 乘積虛部Real = nReal;Image = nImage;return *this;/ 重載 = 運算符,分別比較兩個復數(shù)對象的實部和虛部int CComplex:operator = (CComplex & com)if ( Real=com.Real & Image=com.Image )return 1; / 實部與虛部部分相等,則返回 1elsereturn 0;/*-|部分B:|測試函數(shù) void Test(void)|實現(xiàn)復數(shù)的加法函數(shù) void Add()|實現(xiàn)復數(shù)的減法函數(shù) void Sub()|實現(xiàn)復數(shù)的乘法函數(shù) void Mul()|實現(xiàn)復數(shù)的自加函數(shù) void Add1()|比較兩個復數(shù)的大小函數(shù) void Compare()|輸出本次用戶使用計算器的情況記錄 void userprint()|當前用戶使用完計算器,保存或更新用戶資料函數(shù) void SaveFile()| -*/ 測試函數(shù),隨機出 10 道運算題,可以打分void Test(void)user.nTest+; / 用戶測試次數(shù)加 1cout 共10道題,作10以內(nèi)的加減運算,滿分 100分:n;double real1, real2, image1, image2, real3, real4, image3, image4; / 1 和 2 分別代表兩個待相加的復數(shù)的實部和虛部,3 和 4 則為相乘CComplex answer, temp;int score = 0;char op;for(int i=0; i=9; i+)real1 = rand()%200 - 100; / 產(chǎn)生的隨機數(shù)是兩位數(shù),可以是正數(shù)或負數(shù)image1 = rand()%200 - 100;real2 = rand()%200 - 100;image2 = rand()%200 - 100;CComplex a(real1, image1), b(real2, image2); / 用產(chǎn)生的隨機數(shù)對象分別初始化兩個復數(shù)對象real3 = rand()%20 - 10; / 產(chǎn)生的隨機數(shù)是一位數(shù),可以是正數(shù)或負數(shù)image3 = rand()%20 - 10;real4 = rand()%20 - 10;image4 = rand()%20 - 10;CComplex c(real3, image3), d(real4, image4);op = rand()%3; / 隨機產(chǎn)生 3 種運算符switch(op)case 0:answer = a + b;cout a 加上 b 等于;break;case 1:answer = a - b;cout a 減去 b 等于;break;case 2: / 乘法運算,用實部和虛部都是 1 位數(shù)的對象操作answer = c * d;cout c 乘以 d temp; / 接收用戶輸入的結(jié)果if ( answer=temp ) score+=10; / 正確則加 10分elsecout 此題做錯了n;cout 正確答案為: answer endl;cout 你的最后得分是: score endl;/ 計算最后 3次的平均分if ( user.nTest=3 ) / 若累計次數(shù)沒有超過 3次user.dlAve = 0;user.dlScoreuser.nTest-1 = score; / 將本次測試成績添加進記錄中for(int i=0; iuser.nTest; i+)user.dlAve += user.dlScorei; / 若以前有記錄,將其與本次記錄累計相加起來,用以計算平均分user.dlAve = user.dlAve / user.nTest; / 計算平均分,user.dlAve 從累計的分數(shù) 變成了平均分else / 如果累計測試超過 3次user.dlScore0 = user.dlScore1; / 最前面的一次記錄將被覆蓋,即:刪除user.dlScore1 = user.dlScore2;user.dlScore2 = score; / 將本次記錄添加進測試記錄的尾部user.dlAve=0;for(int i=0; i3; i+) / 計算最新 3次的平均分user.dlAve += user.dlScorei;user.dlAve = user.dlAve / 3;cout 按任意鍵繼續(xù)n;cout .flush();cin.get();cin.get();/ 實現(xiàn)復數(shù)的加法void Add()user.nAdd+;CComplex num1, num2, sum, Zero(0, 0);cout 加法計算n 最少輸入兩個復數(shù),輸入“0”結(jié)束n;cout num1; / 輸入第 1個復數(shù)cout num2; / 輸入第 2個復數(shù)sum = num1 + num2;cout num1; / 輸入第 3個復數(shù)int i = 4;while ( !(num1=Zero) )sum = sum + num1; / 實現(xiàn)復數(shù)相加cout 第 i num1; / 輸入第 i個復數(shù)i+;cout 結(jié)果是: sum endl;cout 按任意鍵繼續(xù)n;cout.flush();cin.get();cin.get();/ 實現(xiàn)復數(shù)的減法void Sub()user.nSub+;CComplex num1, num2, sub, Zero(0, 0);cout 減法計算n 最少輸入兩個復數(shù),輸入“0”結(jié)束n;cout num1; / 輸入第 1個復數(shù)cout num2; / 輸入第 2個復數(shù)sub = num1 - num2;cout num1; / 輸入第 3個復數(shù)int i = 4;while ( !(num1=Zero) )sub = sub - num1; / 實現(xiàn)復數(shù)減法cout 第 i num1; / 輸入第 i個復數(shù)i+;cout 結(jié)果是: sub endl;cout 按任意鍵繼續(xù)n;cin.get();cin.get();/ 實現(xiàn)復數(shù)的乘法void Mul()user.nMul+;CComplex num1, num2, mul, Zero(0, 0);cout 乘法計算n 最少輸入兩個復數(shù),輸入“0”結(jié)束n;cout num1; / 輸入第 1個復數(shù)cout num2; / 輸入第 2個復數(shù)mul = num1 + num2;cout num1; / 輸入第 3個復數(shù)int i = 4;while ( !(num1=Zero) )mul *= num1; / 實現(xiàn)復數(shù)的減法cout 第 i num1; / 輸入第 i個復數(shù)cout 結(jié)果是: mul endl;cout num1; / 這里 輸入的數(shù)可能是虛部為0的數(shù),原書代碼未作判斷num1+; / 實部與虛部分別加 1cout 自加結(jié)果為 num1 endl;cout num1;num1-;cout 自減結(jié)果為 num1 endl;cout 按任意鍵繼續(xù)n;cout.flush();cin.get();cin.get();/ 比較兩個復數(shù)的大小void Compare()CComplex num1, num2;cout 輸入兩個復數(shù)n;cout num1;cout num2;if ( num1=num2 )cout num2 )cout num1 的模大于 num2 的模n;else if ( num1num2 )cout num2 的模大于 num1 的模n;elsecout 這兩個復數(shù)的模相等n;cout 按任意鍵繼續(xù)n;cin.get();cin.get();/ 輸出本次用戶使用計算器的情況記錄void userprint()cout user.szName 使用的次數(shù)為: user.nTime 次 endl;cout 其中:t 加法次數(shù): user.nAdd 次n t 減法次數(shù): user.nSub 次n t 乘法次數(shù): user.nMul 次n t 測試次數(shù): user.nTest 次n t 平均成績: user.dlAve 次 endl;/ 用戶登陸,開始啟動計算器void Login()char szName20;cout 請輸入您的姓名:;cin.getline(szName, 20);ifstream infile;User user1;infile.open(user.dat, ios:binary|ios:in); / 打開用戶資料文件 (這個地方 若沒有文件,則不會創(chuàng)建新文件,不知何問題if ( !infile ) / 若沒有用戶資料文件cout 沒有原始記錄文件, 您是第 1位用戶!n;strcpy(user.szName, szName); / 為全局變量 user 中 szName 成員賦值user.nTime+;return; / 函數(shù)返回/ 讀取用戶資料文件(從該文件的第1個字節(jié)開始逐個讀取信息)/ 如果用戶資料中找到了當前姓名的用戶,則說明是老用戶,顯示一些信息,并作一些使用次數(shù)的記錄。infile.read( (char *)&user1, sizeof(User) ); while ( !infile.eof() ) / 只要沒到文件末尾(未遇文件結(jié)束符),則一直進行此循環(huán)if ( strcmp(user.szName, szName)=0 ) / 將用戶資料文件中的用戶名與讀取的用戶名進行比較user = user1; / 若該用戶以前使用計算器,將原資料賦值給全局變量 useruser.nTime+; / 用戶使用次數(shù)加 1cout 歡迎您再次使用復數(shù)計算器!;userprint(); / 輸出用戶資料中的信息cin.get();infile.close();return;infile.read( (char *)&user1, sizeof(User) );/ 如果用戶資料中沒有當前用戶,表明該用戶是第 1次使用計算器cout 歡迎您使用復數(shù)計算器!;strcpy(user.szName, szName); / 為全局變量 user 中 szName 成員賦值user.nTime+; /用戶使用次數(shù)加 1infile.close();return;/ 當前用戶使用完計算器后,保存或更新用戶資料void SaveFile()userprint(); / 輸出當前用戶使用計算器的詳細信息fstream file;User user1;file.open(user.dat, ios:binary|ios:in|ios:out); / 打開用戶資料if (!file)cout 文件打開錯誤,不能將記錄更新n;return;file.seekg(0, ios:beg); / 文件指針指向文件頭while( !file.eof() )file.read( (char *)&user1, sizeof(User) ); / 逐個讀取用戶資料文件中的用戶信息/ 將用戶資料文件中的用戶名依次與當前用戶名進行比較if ( strcmp(user1.szName, user.szName)=0 ) / 若在用戶資料文件中找到該用戶file.seekp(-(sizeof(User), ios:cur); / 文件指針退回到該用戶資料信息的首位置file.write( (char *)&user, sizeof(User) ); / 將全局變量 user 的內(nèi)容寫到用戶資料文件中,即更新該用戶的資料file.close();return; / 程序返回file.close();fstream outfile;/ 若在用戶資料文件中找不到當前用戶的資料,表明當前用戶是第 1次使用計算器outfile.open(user.dat, ios:binary|ios:app); / 以添加的方式打開用戶資料文件outfile.write( (char *)&user, sizeof(User) ); / 將當前用戶的資料添加在用戶資料文件中outfile.close();return;using namespace NameCComplex; / 使用標準命名空間 NameCComplex/*-| | 主函數(shù)部分| -*/int main(void)srand( time(NULL) ); / 初始化隨機數(shù)種子Login(); / 打開文件,登記用戶int Choice;dosystem(cls); / 系統(tǒng)執(zhí)行命令:cls 為清屏cout 這是一個簡單的復數(shù)計算器程序,可以實現(xiàn)以下功能,請按下對應(yīng)的鍵(1 7)進入nnn;cout t=主菜單=n;cout t 1:多復數(shù)加法n;cout t 2:多復數(shù)減法n;cout t 3:測試 100以內(nèi)的復數(shù)加減乘法運算,1次測試10道題n;cout t 4:多復數(shù)乘法n;cout t 5:復數(shù)自加n;cout t 6:復數(shù)自減n;cout t 7:復數(shù)比較n;cout t 0:退出計算器程序nn;cout Choice;/ 下面用 switch - case 語句實現(xiàn)多現(xiàn)選擇,當然也可以用 if - else 語句實現(xiàn)多項選擇switch(Choice)case 1:Add(); break;case 2:Sub(); break;case 3:Test(); break;case 4:Mul(); break;case 5:Add1(); break;case 6:Sub1(); break;case 7:Compare(); break;case 0:cout nt 歡迎下次繼續(xù)使用復數(shù)計算器!nn;break;default:cout nt 輸入錯誤,請按任意鍵后重新輸入!n;cin.get();cin.get();while(Choice); / 當 Choice 值為 0時, 結(jié)束循環(huán)SaveFile(); / 退出程序前,保存或更新當前用戶的使用情況system(pause);return 0;/* 書上的主函數(shù)寫法:int main(void)srand( time(NULL) ); / 初始化隨機數(shù)種子Login(); / 打開文件,登記用戶char strChoice20;dosystem(cls); / 系統(tǒng)執(zhí)行命令:cls 為清屏cout 這是一個簡單的復數(shù)計算器程序,可以實現(xiàn)以下功能,請按下對應(yīng)的鍵(17)進入nnn;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 村莊托管運營協(xié)議書
- 無償使用大棚協(xié)議書
- 湖北自考就業(yè)協(xié)議書
- 智障人員監(jiān)管協(xié)議書
- 村委離婚調(diào)解協(xié)議書
- 林木種子備案協(xié)議書
- 村級桑房征地協(xié)議書
- 演出合作承辦協(xié)議書
- 治安防火交通協(xié)議書
- 手機簽署開戶協(xié)議書
- 如何做好臨床科研
- 計算機wps一級excel操作題單選題100道及答案
- 2025年中國南水北調(diào)集團有限公司所屬水網(wǎng)發(fā)展研究有限公司招聘筆試參考題庫附帶答案詳解
- 《工程勘察設(shè)計收費標準》(2002年修訂本)
- 施工框架協(xié)議范本
- 【MOOC】用Python玩轉(zhuǎn)數(shù)據(jù)-南京大學 中國大學慕課MOOC答案
- 門診合作協(xié)議合同范本(2篇)
- 上市公司執(zhí)行企業(yè)會計準則案例解析
- 路燈安裝施工組織設(shè)計方案
- 蓋房四鄰簽字協(xié)議書范文
- 超聲考試題+參考答案
評論
0/150
提交評論