九九热最新网址,777奇米四色米奇影院在线播放,国产精品18久久久久久久久久,中文有码视频,亚洲一区在线免费观看,国产91精品在线,婷婷丁香六月天

歡迎來(lái)到裝配圖網(wǎng)! | 幫助中心 裝配圖網(wǎng)zhuangpeitu.com!
裝配圖網(wǎng)
ImageVerifierCode 換一換
首頁(yè) 裝配圖網(wǎng) > 資源分類(lèi) > DOC文檔下載  

復(fù)數(shù)計(jì)算器課程設(shè)計(jì)

  • 資源ID:23766460       資源大?。?span id="24d9guoke414" class="font-tahoma">299.91KB        全文頁(yè)數(shù):26頁(yè)
  • 資源格式: DOC        下載積分:9.9積分
快捷下載 游客一鍵下載
會(huì)員登錄下載
微信登錄下載
三方登錄下載: 微信開(kāi)放平臺(tái)登錄 支付寶登錄   QQ登錄   微博登錄  
二維碼
微信掃一掃登錄
下載資源需要9.9積分
郵箱/手機(jī):
溫馨提示:
用戶(hù)名和密碼都是您填寫(xiě)的郵箱或者手機(jī)號(hào),方便查詢(xún)和重復(fù)下載(系統(tǒng)自動(dòng)生成)
支付方式: 支付寶    微信支付   
驗(yàn)證碼:   換一換

 
賬號(hào):
密碼:
驗(yàn)證碼:   換一換
  忘記密碼?
    
友情提示
2、PDF文件下載后,可能會(huì)被瀏覽器默認(rèn)打開(kāi),此種情況可以點(diǎn)擊瀏覽器菜單,保存網(wǎng)頁(yè)到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請(qǐng)使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無(wú)水印,預(yù)覽文檔經(jīng)過(guò)壓縮,下載后原文更清晰。
5、試題試卷類(lèi)文檔,如果標(biāo)題沒(méi)有明確說(shuō)明有答案則都視為沒(méi)有答案,請(qǐng)知曉。

復(fù)數(shù)計(jì)算器課程設(shè)計(jì)

目 錄1 題目?jī)?nèi)容及設(shè)計(jì)要求12 總體設(shè)計(jì)12.1 總體功能框圖12.2 類(lèi)的設(shè)計(jì)說(shuō)明12.3 主要算法流程圖13程序清單及注釋14運(yùn)行結(jié)果與分析15總結(jié)26參考文獻(xiàn)21 題目?jī)?nèi)容及設(shè)計(jì)要求題目17 復(fù)數(shù)計(jì)算器內(nèi)容及要求:(1)所設(shè)計(jì)的復(fù)數(shù)計(jì)算器可以進(jìn)行+ - * += -= *= + - >= <= = !=運(yùn)算符,其中>= <=是針對(duì)復(fù)數(shù)的模進(jìn)行運(yùn)算。(2)設(shè)計(jì)輸入重載函數(shù),要求能接收從鍵盤(pán)輸入a+bi形式的復(fù)數(shù),在程序中可以識(shí)別出實(shí)部虛部并正確賦值。(3) 設(shè)計(jì)計(jì)算器測(cè)試程序,對(duì)加減法進(jìn)行測(cè)試,要求在兩位數(shù)以?xún)?nèi)進(jìn)行,對(duì)乘法進(jìn)行測(cè)試,乘法要求為一位數(shù)的運(yùn)算。(4) 設(shè)計(jì)記錄功能,可以記錄下不同用戶(hù)使用計(jì)算器的情況,記錄以文件的形式保存在計(jì)算機(jī)內(nèi),開(kāi)機(jī)時(shí)要求用戶(hù)輸入用戶(hù)名,顯示該名戶(hù)以前的記錄。用戶(hù)記錄用包括:做了多少次加減法、乘法、進(jìn)行測(cè)試的次數(shù),后3次的測(cè)試平均分等,在退出計(jì)算器程序時(shí)用心的用戶(hù)記錄代替原有的記錄。2 總體設(shè)計(jì)2.1 總體功能框圖根據(jù)題中任務(wù)的功能,首先要搞清復(fù)數(shù)的運(yùn)算法則,并使用c+語(yǔ)言表達(dá)。復(fù)數(shù)包含實(shí)部與虛部,如何將這兩部分表達(dá)出來(lái)也使關(guān)鍵定義一個(gè)復(fù)數(shù)類(lèi)complex。聲明所需成員函數(shù)和友元函數(shù)對(duì)各運(yùn)算符進(jìn)行重載。定義各成員函數(shù)。定義主函數(shù),實(shí)現(xiàn)對(duì)整個(gè)程序的控制。編譯、運(yùn)行并調(diào)試2.2 類(lèi)的設(shè)計(jì)說(shuō)明class CComplex /定義命名空間NameCComplexprivate: double Real,Image;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<<(ostream &os,CComplex &com); /重載輸出CComplex operator+(CComplex &com); /加法重載CComplex operator-(CComplex &com); /減法重載CComplex operator*(CComplex &com); /乘法重載CComplex operator+=(CComplex &com); /加法賦值重載CComplex operator-=(CComplex &com); /減法賦值重載CComplex operator*=(CComplex &com); /乘法賦值重載CComplex operator+(); /自加CComplex operator-(); /自減double mod (void); /求復(fù)數(shù)的模int operator>(CComplex &com);int operator<(CComplex &com);int operator!=(CComplex &com);int operator=(CComplex &com);2.3 主要算法流程圖開(kāi)始聲明一個(gè)complex類(lèi),并定義double,real,image;聲明類(lèi)的函數(shù),構(gòu)造函數(shù),加減乘除和取模運(yùn)算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 <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <fstream>#include <ctime>/#define EPS len-5 / 定義精度常數(shù)using namespace std;namespace NameCComplex / 定義命名空間 NameCComplex/*-|部分A:| 復(fù)數(shù)類(lèi) CComplex 的聲明和定義,以及結(jié)構(gòu)體類(lèi)型 用戶(hù) User 的定義| -*/*- | 復(fù)數(shù)類(lèi) CComplex 的聲明 -*/class CComplexprivate:double Real, Image; / 分別為復(fù)數(shù)的實(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 << (ostream & os, CComplex & com); / 重載輸出CComplex operator + (CComplex & com); / 加法重載CComplex operator - (CComplex & com); / 減法重載CComplex operator * (CComplex & com); / 乘法重載CComplex operator += (CComplex & com); / 加法賦值重載CComplex operator -= (CComplex & com); / 減法賦值重載CComplex operator *= (CComplex & com); / 乘法賦值重載CComplex operator + (); / 自加CComplex operator - (); / 自減double mod(void);int operator > (CComplex & com);int operator < (CComplex & com);int operator != (CComplex & com);int operator = (CComplex & com);/*- | 結(jié)構(gòu)體類(lèi)型 用戶(hù) User 的定義 -*/struct Userchar szName20; / 用戶(hù)名int nTime; / 使用次數(shù)int nTest; / 測(cè)試次數(shù)double dlAve; / 平均成績(jī)int nAdd; / 加法次數(shù)int nSub; / 減法次數(shù)int nMul; / 乘法次數(shù)double dlScore3; / 3次測(cè)試得分 user;/*- | 復(fù)數(shù)類(lèi) CComplex 的類(lèi)外定義部分 -*/ 重載運(yùn)算符“+”,實(shí)部與虛部均加 1CComplex CComplex:operator + ()Real+;Image+;return *this;/ 重載運(yùn)算符“-”,實(shí)部與虛部均減 1CComplex CComplex:operator - ()Real-;Image-;return *this;/ 求復(fù)數(shù)的模,返回 實(shí)部2 + 虛部2double CComplex:mod()return Real * Real + Image * Image;/ 重載運(yùn)算符“>”,比較模的大小int CComplex:operator > (CComplex & com)if ( mod() > com.mod() )return 1; / 若大,則返回 1elsereturn 0;/ 重載運(yùn)算符“<”,比較模的大小int CComplex:operator < (CComplex & com)if ( mod() < com.mod() )return 1; / 若小,則返回 1elsereturn 0;/ 重載運(yùn)算符“!=”,分別判斷復(fù)數(shù)的實(shí)部與虛部int CComplex:operator != (CComplex & com)if ( *this=com )return 0;else return 1;/ 重載復(fù)數(shù)的輸入, a+bi 的形式istream & operator >> (istream & is, CComplex & com) cout << "輸入復(fù)數(shù):"char s80;is >> s; / 用字符串的形式接收復(fù)數(shù)int len = strlen(s); / 求出字符串的長(zhǎng)度int n = 0, sign = 1; / 其中的 n 值 為當(dāng)前從字符串中提取出的數(shù)字,會(huì)在下面的 while 語(yǔ)句中得到確定的值 / sign 為狀態(tài)變量,表示數(shù)值的正負(fù)符號(hào),以輔助辨認(rèn)正負(fù)值com.Image = com.Real = 0;/ 判斷接收的字符串是否合法for(int k=0; k<len; k+)if ( (sk<0 | sk>9) && (sk!=+ && sk!=- && sk!=i) )cout << "error" << endl;return is; / 錯(cuò)誤,輸出出錯(cuò)信息并返回/ 順序識(shí)別字符串中各字符for(int k=0; k<len;)if ( n!=0 && (sk=- | sk=+) ) / 當(dāng)前字符是否為符號(hào)位com.Real = sign * n; / 是符號(hào)位,且 n!=0,即 n 已被賦值(通過(guò)下面的whlie語(yǔ)句),表明當(dāng)前讀取的是虛部的符號(hào)n = 0; / 將原 n*sign 值(帶正負(fù)號(hào)的數(shù)值)賦給實(shí)部后,將 n 清零,準(zhǔn)備下一次繼續(xù)接收并判斷是否為虛部的值if ( sk = -) / 當(dāng)前字符若為負(fù)號(hào)sign = -1; k+; / 給符號(hào)標(biāo)志變量 sign 賦值,表示為負(fù)數(shù)if ( sk = +) / 當(dāng)前字符若為正號(hào)sign = 1; k+; / 給符號(hào)標(biāo)志變量 sign 賦值,表示為正數(shù)if ( sk=i ) / 若當(dāng)前字符為“i”if ( k!=len-1 ) / 判斷字符 i 是否為字符串中最后一個(gè)字符cout << "errorn" / 如果不是,說(shuō)明復(fù)數(shù)數(shù)據(jù)格式錯(cuò)誤elsecom.Image = sign * n; / 是最后一個(gè)字符,復(fù)數(shù)對(duì)象已接收完,用 sign*n 為虛部賦值break;while ( sk>=0 && sk<=9 ) / 當(dāng)前字符若在 09 之間,則將數(shù)字字符轉(zhuǎn)換成數(shù)字?jǐn)?shù)值n = n * 10 + sk - 0;k+;if ( slen-1!=i && n!=0 ) / 如果最后一個(gè)字符不是 i,表示復(fù)數(shù)對(duì)象內(nèi)只有實(shí)部,沒(méi)有虛部,如:-acom.Real = n * sign;return is;/ 重載復(fù)數(shù)的輸出ostream & operator << (ostream & os, CComplex & com)if ( fabs(com.Image)=0 ) / 如果虛部為 0os << com.Real; / 只輸出實(shí)部else if ( fabs(com.Real)=0 ) / 如果實(shí)部為 0os << com.Image << "i" / 只輸出虛部else if ( com.Image>0 )os << com.Real << "+" << com.Image << "i" / 虛部為正elseos << com.Real << com.Image << "i" / 如 實(shí)部為 3,虛部為 -6i,就變?yōu)?3 - 6i,而不是 3 + -6ireturn os;/ 加法重載CComplex CComplex:operator + (CComplex & com)CComplex sum;sum.Real = Real + com.Real; / 實(shí)部相加sum.Image = Image + com.Image; / 虛部相加return sum;/ 乘法重載CComplex CComplex:operator * (CComplex & com)CComplex multi;multi.Real = Real * com.Real - Image * com.Image; / 乘積實(shí)部multi.Image = Real * com.Image + Image * com.Real; / 乘積虛部return multi;/ 減法重載CComplex CComplex:operator - (CComplex & com)CComplex sub;sub.Real = Real - com.Real; / 實(shí)部相減sub.Image = Image - com.Image; / 虛部相減return sub;/ 加法賦值重載CComplex CComplex:operator += (CComplex & com)Real = Real + com.Real; / 實(shí)部Image = Image + com.Image; / 虛部return *this;/ 減法賦值重載CComplex CComplex:operator -= (CComplex & com)Real = Real - com.Real; / 實(shí)部Image = Image - com.Image; / 虛部return *this;/ 乘法賦值重載CComplex CComplex:operator *= (CComplex & com)double nReal = Real * com.Real - Image * com.Image; / 乘積實(shí)部double nImage = Real * com.Image - Image * com.Real; / 乘積虛部Real = nReal;Image = nImage;return *this;/ 重載 = 運(yùn)算符,分別比較兩個(gè)復(fù)數(shù)對(duì)象的實(shí)部和虛部int CComplex:operator = (CComplex & com)if ( Real=com.Real && Image=com.Image )return 1; / 實(shí)部與虛部部分相等,則返回 1elsereturn 0;/*-|部分B:|測(cè)試函數(shù) void Test(void)|實(shí)現(xiàn)復(fù)數(shù)的加法函數(shù) void Add()|實(shí)現(xiàn)復(fù)數(shù)的減法函數(shù) void Sub()|實(shí)現(xiàn)復(fù)數(shù)的乘法函數(shù) void Mul()|實(shí)現(xiàn)復(fù)數(shù)的自加函數(shù) void Add1()|比較兩個(gè)復(fù)數(shù)的大小函數(shù) void Compare()|輸出本次用戶(hù)使用計(jì)算器的情況記錄 void userprint()|當(dāng)前用戶(hù)使用完計(jì)算器,保存或更新用戶(hù)資料函數(shù) void SaveFile()| -*/ 測(cè)試函數(shù),隨機(jī)出 10 道運(yùn)算題,可以打分void Test(void)user.nTest+; / 用戶(hù)測(cè)試次數(shù)加 1cout << "共10道題,作10以?xún)?nèi)的加減運(yùn)算,滿(mǎn)分 100分:n"double real1, real2, image1, image2, real3, real4, image3, image4; / 1 和 2 分別代表兩個(gè)待相加的復(fù)數(shù)的實(shí)部和虛部,3 和 4 則為相乘CComplex answer, temp;int score = 0;char op;for(int i=0; i<=9; i+)real1 = rand()%200 - 100; / 產(chǎn)生的隨機(jī)數(shù)是兩位數(shù),可以是正數(shù)或負(fù)數(shù)image1 = rand()%200 - 100;real2 = rand()%200 - 100;image2 = rand()%200 - 100;CComplex a(real1, image1), b(real2, image2); / 用產(chǎn)生的隨機(jī)數(shù)對(duì)象分別初始化兩個(gè)復(fù)數(shù)對(duì)象real3 = rand()%20 - 10; / 產(chǎn)生的隨機(jī)數(shù)是一位數(shù),可以是正數(shù)或負(fù)數(shù)image3 = rand()%20 - 10;real4 = rand()%20 - 10;image4 = rand()%20 - 10;CComplex c(real3, image3), d(real4, image4);op = rand()%3; / 隨機(jī)產(chǎn)生 3 種運(yùn)算符switch(op)case 0:answer = a + b;cout << a << "加上" << b << "等于"break;case 1:answer = a - b;cout << a << "減去" << b << "等于"break;case 2: / 乘法運(yùn)算,用實(shí)部和虛部都是 1 位數(shù)的對(duì)象操作answer = c * d;cout << c << "乘以" << d << "等于"break;cin >> temp; / 接收用戶(hù)輸入的結(jié)果if ( answer=temp ) score+=10; / 正確則加 10分elsecout << "此題做錯(cuò)了n"cout << "正確答案為:" << answer << endl;cout << "你的最后得分是:" << score << endl;/ 計(jì)算最后 3次的平均分if ( user.nTest<=3 ) / 若累計(jì)次數(shù)沒(méi)有超過(guò) 3次user.dlAve = 0;user.dlScoreuser.nTest-1 = score; / 將本次測(cè)試成績(jī)添加進(jìn)記錄中for(int i=0; i<user.nTest; i+)user.dlAve += user.dlScorei; / 若以前有記錄,將其與本次記錄累計(jì)相加起來(lái),用以計(jì)算平均分user.dlAve = user.dlAve / user.nTest; / 計(jì)算平均分,user.dlAve 從累計(jì)的分?jǐn)?shù) 變成了平均分else / 如果累計(jì)測(cè)試超過(guò) 3次user.dlScore0 = user.dlScore1; / 最前面的一次記錄將被覆蓋,即:刪除user.dlScore1 = user.dlScore2;user.dlScore2 = score; / 將本次記錄添加進(jìn)測(cè)試記錄的尾部user.dlAve=0;for(int i=0; i<3; i+) / 計(jì)算最新 3次的平均分user.dlAve += user.dlScorei;user.dlAve = user.dlAve / 3;cout << "按任意鍵繼續(xù)n"cout .flush();cin.get();cin.get();/ 實(shí)現(xiàn)復(fù)數(shù)的加法void Add()user.nAdd+;CComplex num1, num2, sum, Zero(0, 0);cout << "加法計(jì)算n" << "最少輸入兩個(gè)復(fù)數(shù),輸入“0”結(jié)束n"cout << "第1個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 1個(gè)復(fù)數(shù)cout << "第2個(gè)復(fù)數(shù):"cin >> num2; / 輸入第 2個(gè)復(fù)數(shù)sum = num1 + num2;cout << "第3個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 3個(gè)復(fù)數(shù)int i = 4;while ( !(num1=Zero) )sum = sum + num1; / 實(shí)現(xiàn)復(fù)數(shù)相加cout << "第" << i << "個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 i個(gè)復(fù)數(shù)i+;cout << "結(jié)果是:" << sum << endl;cout << "按任意鍵繼續(xù)n"cout.flush();cin.get();cin.get();/ 實(shí)現(xiàn)復(fù)數(shù)的減法void Sub()user.nSub+;CComplex num1, num2, sub, Zero(0, 0);cout << "減法計(jì)算n" << "最少輸入兩個(gè)復(fù)數(shù),輸入“0”結(jié)束n"cout << "第1個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 1個(gè)復(fù)數(shù)cout << "第2個(gè)復(fù)數(shù):"cin >> num2; / 輸入第 2個(gè)復(fù)數(shù)sub = num1 - num2;cout << "第3個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 3個(gè)復(fù)數(shù)int i = 4;while ( !(num1=Zero) )sub = sub - num1; / 實(shí)現(xiàn)復(fù)數(shù)減法cout << "第" << i << "個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 i個(gè)復(fù)數(shù)i+;cout << "結(jié)果是:" << sub << endl;cout << "按任意鍵繼續(xù)n"cin.get();cin.get();/ 實(shí)現(xiàn)復(fù)數(shù)的乘法void Mul()user.nMul+;CComplex num1, num2, mul, Zero(0, 0);cout << "乘法計(jì)算n" << "最少輸入兩個(gè)復(fù)數(shù),輸入“0”結(jié)束n"cout << "第1個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 1個(gè)復(fù)數(shù)cout << "第2個(gè)復(fù)數(shù):"cin >> num2; / 輸入第 2個(gè)復(fù)數(shù)mul = num1 + num2;cout << "第3個(gè)復(fù)數(shù):"cin >> num1; / 輸入第 3個(gè)復(fù)數(shù)int i = 4;while ( !(num1=Zero) )mul *= num1; / 實(shí)現(xiàn)復(fù)數(shù)的減法cout << "第" << i << "個(gè)復(fù)數(shù):"i+; cin >> num1; / 輸入第 i個(gè)復(fù)數(shù)cout << "結(jié)果是:" << mul << endl;cout << "按任意鍵繼續(xù)n"cin.get();cin.get();/ 實(shí)現(xiàn)復(fù)數(shù)的自加,實(shí)部與虛部均自加 1void Add1()user.nAdd+; / 用戶(hù)加法記錄次數(shù)加 1CComplex num1;cin >> num1; / 這里 輸入的數(shù)可能是虛部為0的數(shù),原書(shū)代碼未作判斷num1+; / 實(shí)部與虛部分別加 1cout << "自加結(jié)果為" << num1 << endl;cout << "按任意鍵繼續(xù)n"cout.flush();cin.get();cin.get();/ 實(shí)現(xiàn)復(fù)數(shù)的自減void Sub1()user.nSub+; / 用戶(hù)減法記錄次數(shù)加 1CComplex num1;cin >> num1;num1-;cout << "自減結(jié)果為" << num1 << endl;cout << "按任意鍵繼續(xù)n"cout.flush();cin.get();cin.get();/ 比較兩個(gè)復(fù)數(shù)的大小void Compare()CComplex num1, num2;cout << "輸入兩個(gè)復(fù)數(shù)n"cout << "第1個(gè)復(fù)數(shù):"cin >> num1;cout << "第2個(gè)復(fù)數(shù):"cin >> num2;if ( num1=num2 )cout << " 這兩個(gè)復(fù)數(shù)相等n"else if ( num1>num2 )cout << num1 << "的模大于" << num2 << "的模n"else if ( num1<num2 )cout << num2 << "的模大于" << num1 << "的模n"elsecout << "這兩個(gè)復(fù)數(shù)的模相等n"cout << "按任意鍵繼續(xù)n"cin.get();cin.get();/ 輸出本次用戶(hù)使用計(jì)算器的情況記錄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 測(cè)試次數(shù):" << user.nTest << "次n" << "t 平均成績(jī):" << user.dlAve << "次" << endl;/ 用戶(hù)登陸,開(kāi)始啟動(dòng)計(jì)算器void Login()char szName20;cout << "請(qǐng)輸入您的姓名:"cin.getline(szName, 20);ifstream infile;User user1;infile.open("user.dat", ios:binary|ios:in); / 打開(kāi)用戶(hù)資料文件 (這個(gè)地方 若沒(méi)有文件,則不會(huì)創(chuàng)建新文件,不知何問(wèn)題if ( !infile ) / 若沒(méi)有用戶(hù)資料文件cout << "沒(méi)有原始記錄文件, 您是第 1位用戶(hù)!n"strcpy(user.szName, szName); / 為全局變量 user 中 szName 成員賦值user.nTime+;return; / 函數(shù)返回/ 讀取用戶(hù)資料文件(從該文件的第1個(gè)字節(jié)開(kāi)始逐個(gè)讀取信息)/ 如果用戶(hù)資料中找到了當(dāng)前姓名的用戶(hù),則說(shuō)明是老用戶(hù),顯示一些信息,并作一些使用次數(shù)的記錄。infile.read( (char *)&user1, sizeof(User) ); while ( !infile.eof() ) / 只要沒(méi)到文件末尾(未遇文件結(jié)束符),則一直進(jìn)行此循環(huán)if ( strcmp(user.szName, szName)=0 ) / 將用戶(hù)資料文件中的用戶(hù)名與讀取的用戶(hù)名進(jìn)行比較user = user1; / 若該用戶(hù)以前使用計(jì)算器,將原資料賦值給全局變量 useruser.nTime+; / 用戶(hù)使用次數(shù)加 1cout << "歡迎您再次使用復(fù)數(shù)計(jì)算器!"userprint(); / 輸出用戶(hù)資料中的信息cin.get();infile.close();return;infile.read( (char *)&user1, sizeof(User) );/ 如果用戶(hù)資料中沒(méi)有當(dāng)前用戶(hù),表明該用戶(hù)是第 1次使用計(jì)算器cout << "歡迎您使用復(fù)數(shù)計(jì)算器!"strcpy(user.szName, szName); / 為全局變量 user 中 szName 成員賦值user.nTime+; /用戶(hù)使用次數(shù)加 1infile.close();return;/ 當(dāng)前用戶(hù)使用完計(jì)算器后,保存或更新用戶(hù)資料void SaveFile()userprint(); / 輸出當(dāng)前用戶(hù)使用計(jì)算器的詳細(xì)信息fstream file;User user1;file.open("user.dat", ios:binary|ios:in|ios:out); / 打開(kāi)用戶(hù)資料if (!file)cout << "文件打開(kāi)錯(cuò)誤,不能將記錄更新n"return;file.seekg(0, ios:beg); / 文件指針指向文件頭while( !file.eof() )file.read( (char *)&user1, sizeof(User) ); / 逐個(gè)讀取用戶(hù)資料文件中的用戶(hù)信息/ 將用戶(hù)資料文件中的用戶(hù)名依次與當(dāng)前用戶(hù)名進(jìn)行比較if ( strcmp(user1.szName, user.szName)=0 ) / 若在用戶(hù)資料文件中找到該用戶(hù)file.seekp(-(sizeof(User), ios:cur); / 文件指針退回到該用戶(hù)資料信息的首位置file.write( (char *)&user, sizeof(User) ); / 將全局變量 user 的內(nèi)容寫(xiě)到用戶(hù)資料文件中,即更新該用戶(hù)的資料file.close();return; / 程序返回file.close();fstream outfile;/ 若在用戶(hù)資料文件中找不到當(dāng)前用戶(hù)的資料,表明當(dāng)前用戶(hù)是第 1次使用計(jì)算器outfile.open("user.dat", ios:binary|ios:app); / 以添加的方式打開(kāi)用戶(hù)資料文件outfile.write( (char *)&user, sizeof(User) ); / 將當(dāng)前用戶(hù)的資料添加在用戶(hù)資料文件中outfile.close();return;using namespace NameCComplex; / 使用標(biāo)準(zhǔn)命名空間 NameCComplex/*-| | 主函數(shù)部分| -*/int main(void)srand( time(NULL) ); / 初始化隨機(jī)數(shù)種子Login(); / 打開(kāi)文件,登記用戶(hù)int Choice;dosystem("cls"); / 系統(tǒng)執(zhí)行命令:cls 為清屏cout << " 這是一個(gè)簡(jiǎn)單的復(fù)數(shù)計(jì)算器程序,可以實(shí)現(xiàn)以下功能,請(qǐng)按下對(duì)應(yīng)的鍵(1 7)進(jìn)入nnn"cout << "t=主菜單=n"cout << "t 1:多復(fù)數(shù)加法n"cout << "t 2:多復(fù)數(shù)減法n"cout << "t 3:測(cè)試 100以?xún)?nèi)的復(fù)數(shù)加減乘法運(yùn)算,1次測(cè)試10道題n"cout << "t 4:多復(fù)數(shù)乘法n"cout << "t 5:復(fù)數(shù)自加n"cout << "t 6:復(fù)數(shù)自減n"cout << "t 7:復(fù)數(shù)比較n"cout << "t 0:退出計(jì)算器程序nn"cout << "t 請(qǐng)輸入您的選擇:"cin >> Choice;/ 下面用 switch - case 語(yǔ)句實(shí)現(xiàn)多現(xiàn)選擇,當(dāng)然也可以用 if - else 語(yǔ)句實(shí)現(xiàn)多項(xiàng)選擇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ù)使用復(fù)數(shù)計(jì)算器!nn"break;default:cout << "nt 輸入錯(cuò)誤,請(qǐng)按任意鍵后重新輸入!n"cin.get();cin.get();while(Choice); / 當(dāng) Choice 值為 0時(shí), 結(jié)束循環(huán)SaveFile(); / 退出程序前,保存或更新當(dāng)前用戶(hù)的使用情況system("pause");return 0;/* 書(shū)上的主函數(shù)寫(xiě)法:int main(void)srand( time(NULL) ); / 初始化隨機(jī)數(shù)種子Login(); / 打開(kāi)文件,登記用戶(hù)char strChoice20;dosystem("cls"); / 系統(tǒng)執(zhí)行命令:cls 為清屏cout << " 這是一個(gè)簡(jiǎn)單的復(fù)數(shù)計(jì)算器程序,可以實(shí)現(xiàn)以下功能,請(qǐng)按下對(duì)應(yīng)的鍵(17)進(jìn)入nnn"cout << "t=主菜單=n"cout << "t 1:多復(fù)數(shù)加法n"cout << "t 2:多復(fù)數(shù)減法n"cout << "t 3:測(cè)試 100以?xún)?nèi)的復(fù)數(shù)加減乘法運(yùn)算,1次測(cè)試10道題n"cout << "t 4:多復(fù)數(shù)乘法n"cout << "t 5:復(fù)數(shù)自加n"cout << "t 6:復(fù)數(shù)自減n"cout << "t 7:復(fù)數(shù)比較n"cout << "t 0:退出計(jì)算器程序nn"cout << "t 請(qǐng)輸入您的選擇:"cin >> strChoice;/ 下面用 if - else 語(yǔ)句實(shí)現(xiàn)多現(xiàn)選擇,當(dāng)然也可以用 switch - case 語(yǔ)句實(shí)現(xiàn)多項(xiàng)選擇if ( strcmp(strChoice, "1")=0 )Add();else if ( strcmp(strChoice, "2")=0 )Sub();else if ( strcmp(strChoice, "3")=0 )Test();else if ( strcmp(strChoice, "4")=0 )Mul();else if ( strcmp(strChoice, "5")=0 )Add1();else if ( strcmp(strChoice, "6")=0 )Sub1();else if ( strcmp(strChoice, "7")=0 )Compare();else if ( strcmp(strChoice, "0")=0 )cout << "nt 歡迎下次繼續(xù)使用復(fù)數(shù)計(jì)算器!nn"break;elsecout << "nt 輸入錯(cuò)誤,請(qǐng)按任意鍵后重新輸入!n"cin.get();cin.get();while( (strcmp(strChoice, "0") );SaveFile(); / 退出程序前,保存或更新當(dāng)前用戶(hù)的使用情況system("pause");return 0;*/4 運(yùn)行結(jié)果與分析5 總結(jié)1. 明確實(shí)驗(yàn)操作對(duì)象和目的。2. 針對(duì)目的和對(duì)象進(jìn)行總體設(shè)計(jì)。3. 細(xì)化流程:書(shū)寫(xiě)程序,編譯、運(yùn)行并調(diào)試。4. 一開(kāi)始錯(cuò)誤的將復(fù)數(shù)的兩個(gè)部分一起聲明,在后來(lái)的編程過(guò)程中遇到了計(jì)算上的錯(cuò)誤,于是將復(fù)數(shù)的實(shí)部和虛部分開(kāi)聲明,問(wèn)題得到了解決5. 計(jì)算器還可擴(kuò)充用戶(hù)輸入錯(cuò)誤算式時(shí)提示用戶(hù)的功能6. 計(jì)算器還可以擴(kuò)充帶有括號(hào)的功能(課程設(shè)計(jì)過(guò)程中出現(xiàn)的問(wèn)題及其解決方案,可擴(kuò)充的功能及設(shè)計(jì)等。)6 參考文獻(xiàn)1李?lèi)?ài)華,程磊著. 面向?qū)ο蟪绦蛟O(shè)計(jì)(C+語(yǔ)言) .北京:清華大學(xué)出版社,20102劉振安,劉燕君著. C+程序設(shè)計(jì)課程設(shè)計(jì). 北京: 機(jī)械工業(yè)出版社,20043譚浩強(qiáng)著. C+程序設(shè)計(jì)實(shí)踐指導(dǎo). 北京:清華大學(xué)出版社,2005

注意事項(xiàng)

本文(復(fù)數(shù)計(jì)算器課程設(shè)計(jì))為本站會(huì)員(jun****875)主動(dòng)上傳,裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。 若此文所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng)(點(diǎn)擊聯(lián)系客服),我們立即給予刪除!

溫馨提示:如果因?yàn)榫W(wǎng)速或其他原因下載失敗請(qǐng)重新下載,重復(fù)下載不扣分。




關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶(hù)上傳的文檔直接被用戶(hù)下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!