當前位置:編程學習大全網 - 源碼下載 - 高分急求,visual c++ 課程設計案例精選與編程指導 答案

高分急求,visual c++ 課程設計案例精選與編程指導 答案

C++編寫的壹個小型復數計算器,自己看看吧:

#include<iostream>

#include<cmath>

#include<cstdlib>

#include<cstring>

#include<fstream>

#include<ctime>

#define EPS 1e-5 //定義精度常數

using namespace std; //使用標準空間命名std

namespace NameCComplex //定義命名空間NameCComplex

{

class CComplex ////定義壹個CComplex類

{

private:

double Real,Image;

public:

CComplex(double real=0,double image=0) //構造函數

{

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);

};

struct User //用戶結構體類型定義

{

char szName[20]; //用戶名

int nTime; //使用次數

int nTest; //測試次數

double alAve; //平均成績

int nAdd; //加法次數

int nSub; //減法次數

int nMul; //乘法次數

double dlScore[3]; //3次測試得分

}user; //定義全局變量

CComplex CComplex::operator++() //重載運算符"++",實部與虛部均加1

{

Real++;

Image++;

return *this;

}

CComplex CComplex::operator--() //重載運算符"--",實部與虛部均減1

{

Real--;

Image--;

return *this;

}

double CComplex::mod() //求復數的模的平方,返回實部^2+虛部^2

{

return Real*Real+Image*Image;

}

int CComplex::operator>(CComplex &com) //重載運算符">",比較模的大小

{

if(mod()>com.mod())

return 1;

else

return 0;

}

int CComplex::operator<(CComplex &com)

{

if(mod()<com.mod())

return 1; //若大,則返回1

else

return 0;

}

int CComplex::operator!=(CComplex &com)

//重載運算符"!=",分別判斷復數的實部和虛部

{

if(*this==com)

return 0;

else

return 1;

}

istream & operator>>(istream &is,CComplex &com) //重載輸入,可以輸入a+bi的形式

{

cout<<"請輸入復數:";

char s[80];

is>>s; //用字符串的形式接受復數

int len=strlen(s); //求出字符串的長度

int n=0,sign=1;

//n為當前從字符串中提取出來的數字,初始化為0;sign是難道符號,初始化為正

com.Image=com.Real=0;

for(int k=0;k<len;k++) //判斷接受的字符串是否合法

{

if((s[k]<'0' || s[k]> '9') && (s[k]!='+' && s[k]!='-' && s[k]!='i'))

{

cout<<"error"<<endl;

return is; //錯誤,輸出出錯信息並返回

}

}

for(k=0;k<len;) //順序識別字符串中各字符

{

if(n!=0 &&(s[k]=='-'||s[k]=='+')) //當前字符是否是符號位

{

com.Real=sign*n;

//是符號位,且n!=0,即n已被賦值,表明當前讀取的是虛部的符號

n=0; //將原n*sign值賦給實部,將n清零,準備接受虛部的值

}

if(s[k]=='-') //當前字符為負號

{

sign=-1;k++; //給符號標誌賦值

}

if(s[k]=='+') //當前字符為正號

{

sign=1;k++; //給符號標誌賦值

}

if(s[k]=='i') //當前字符為'I'

{

if(k!=len-1) //判斷字符'I'是否為字符串中作後壹個字符

cout<<"error\n"; //如果不是,說明復數數據格式錯誤

else

com.Image=sign*n;

//是最後壹個字符,復數對象已接受完,用sign*n為虛部賦值

break;

}

while(s[k]>='0' && s[k]<='9')

//當前字符在0~9之間,將數字字符轉換成數字數值

{

n=n*10+s[k]-'0';

k++;

}

}

if(s[len-1]!='i' && n!=0)

//如果最後壹個字符不是'I',表示復數對象內只有實部,沒有虛部

{

com.Real=n*sign;

}

return is;

}

ostream & operator<<(ostream &os,CComplex &com) //重載輸入

{

if(fabs(com.Image)<EPS) // 如果虛部為0

os<<com.Real; //只輸出實部

else if((fabs(com.Real)<EPS)) //如果實部為0

os<<com.Image<<"i"; //只輸出虛部

else if(com.Image>0)

os<<com.Real<<"+"<<com.Image<<"i";

else

os<<com.Real<<com.Image<<"i"; //虛部為正

return 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;

}

int CComplex::operator==(CComplex &com) //重載等於

{

if(Real==com.Real && Image==com.Image)

return 1;

else

return 0;

}

void Test(void) //測試函數

{

user.nTest++;

cout<<"***10道題,做100以內的加減運算,滿分100分:\n";

double real1,real2,image1,image2,real3,real4,image3,image4;

CComplex answer,temp;

int score=0;

char op;

for(int i=0;i<=9;i++)

{

/////為復數產生隨機值

real1=rand()%200-100;

image1=rand()%200-100;

real2=rand()%200-100;

image2=rand()%200-100;

CComplex a(real1,image1),b(real2,image2);

real3=rand()%20-10;

image3=rand()%20-10;

real4=rand()%20-10;

image4=rand()%20-10;

CComplex c(real3,image3),d(real4,image4);

op=rand()%3; //產生隨機加減乘法運算的三個值

switch(op)

{

case 0:

answer=a+b;

cout<<a<<"加上"<<b<<"等於";

break;

case 1:

answer=a-b;

cout<<a<<"減去"<<b<<"等於";

break;

case 2:

answer=c*d;

cout<<c<<"乘以"<<d<<"等於";

break;

}

cin>>temp; //輸入用戶計算值

if(answer==temp) //比較用戶計算值

{

score+=10;

}

else

{

cout<<"此題做錯了\n";

cout<<"正確答案為:"<<answer<<endl;

}

}

cout<<"妳的最後得分是:"<<score<<endl;

if(user.nTest<=3)

{

user.alAve=0;

user.dlScore[user.nTest-1]=score;

for(int i=0;i<user.nTest;i++)

user.alAve+=user.dlScore[i];

user.alAve=user.alAve/user.nTest;

}

else

{

user.dlScore[0]=user.dlScore[1];

user.dlScore[1]=user.dlScore[2];

user.dlScore[2]=score;

for(i=0,user.alAve=0;i<3;i++)

user.alAve+=user.dlScore[i];

user.alAve=user.alAve/3;

}

cout<<"請按任意鍵繼續\n";

cout.flush();

cin.get();

cin.get();

}

void Add() //復數加法運算函數

{

user.nAdd++;

CComplex num1,num2,sum,Zero(0,0);

cout<<"加法計算\n"<<"最少輸入兩個復數,並且以0結束\n";

cout<<"第壹個復數:";

cin>>num1;

cout<<"第二個復數:";

cin>>num2;

sum=num1+num2;

cout<<"第三個復數:";

cin>>num1;

int i=4;

while(!(num1==Zero))

{

sum=sum+num1;

cout<<"第"<<i<<"個復數:";

cin>>num1;

i++;

}

cout<<"加法結果是:"<<sum<<endl;

cout<<"請按任意鍵繼續\n";

cout.flush();

cin.get();

cin.get();

}

void Sub() //復數減法預算函數

{

user.nSub++;

CComplex num1,num2,sub,Zero(0,0);

cout<<"最少輸入兩個復數,並且以0結束\n";

cout<<"第1個復數:";

cin>>num1;

cout<<"第2個復數:";

cin>>num2;

sub=num1-num2;

cout<<"第3個復數:";

cin>>num1;

int i=4;

while(!(num1==Zero))

{

sub=sub-num1;

cout<<"第"<<i<<"個復數:";

cin>>num1;

i++;

}

cout<<"減法結果是:"<<sub<<endl;

cout<<"請按任意鍵繼續\n";

cout.flush();

cin.get();

cin.get();

}

void Mul() //復數乘積函數

{

user.nMul++;

CComplex num1,num2,mul,Zero(0,0);

cout<<"乘法計算\n"<<"最少輸入兩個復數,並且以零結束\n";

cout<<"第壹個復數:";

cin>>num1;

cout<<"第二個復數:";

cin>>num2;

mul=num1*num2;

cout<<"第三個復數:";

cin>>num1;

int i=4;

while(!(num1==Zero))

{

mul*=num1;

cout<<"第"<<i<<"個復數:";

cin>>num1;

i++;

}

cout<<"乘法結果是:"<<mul<<endl;

cout<<"請按任意鍵繼續\n";

cout.flush();

cin.get();

cin.get();

}

void Add1()

{

user.nAdd ++;

CComplex num1;

cin>>num1;

++num1;

cout<<"自加的結果為"<<num1<<endl;

cout<<"按任意鍵結束\n";

cout.flush();

cin.get();

cin.get();

}

void Sub1() //復數自加運算函數

{

user.nSub++;

CComplex num1;

cin>>num1;

--num1;

cout<<"自減的結果為"<<num1<<endl;

cout<<"按任意鍵結束\n";

cout.flush();

cin.get();

cin.get();

}

void compare() //兩復數比較函數

{

CComplex num1,num2;

cout<<"輸入兩個復數\n";

cout<<"第壹個復數:";

cin>>num1;

cout<<"輸入第二個復數\n";

cin>>num2;

if(num1==num2)

cout<<"這兩個復數相等\n";

else if(num1>num2)

cout<<num1<<"的模大於"<<num2<<"的模\n";

else if(num1<num2)

cout<<num2<<"的模大於"<<num1<<"的模\n";

else

cout<<"這兩個復數的模相等\n";

cout<<"按任意鍵繼續\n";

cin.get();

cin.get();

}

void userprint() //輸出用戶信息函數

{

cout<<user.szName<<"使用的次數為:"<<user.nTime<<endl;

cout<<"其中:\t加法的次數:"<<user.nAdd<<"\t減法的次數:"<<user.nSub<<"\t乘法的次數:"<<user.nMul<<endl;

cout<<"\t測試次數:"<<user.nTest<<"\t平均成績:"<<user.alAve<<endl;

}

void Login() //當前用戶信息函數

{

char szName[20];

cout<<"請輸入您的姓名:";

cin.getline(szName,20);

ifstream infile;

User user1;

infile.open("user.dat",ios::binary|ios::in);

if(!infile)

{

cout<<"沒有原始記錄文件,您是第壹個用戶!\n";

strcpy(user.szName,szName);

user.nTest++;

return;

}

infile.read((char *)&user1,sizeof(User));

while(!infile.eof())

{

if(strcmp(user1.szName,szName)==0)

{

user=user1;

user.nTime++;

cout<<"歡迎您再次使用復數計算器!";

userprint();

cin.get();

infile.close();

return;

}

infile.read((char *) &user1,sizeof(User));

}

cout<<"歡迎您再次使用復數計算器!";

strcpy(user.szName,szName);

user.nTime++;

infile.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.seekp(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));

file.close();

return;

}

}

file.close();

fstream outfile;

outfile.open("user.dat",ios::binary|ios::app);

outfile.write((char *)&user,sizeof(User));

outfile.close();

return;

}

}

using namespace NameCComplex;

int main(void) //主函數開始

{

srand(time(NULL)); //初始化隨機數“種子”語句

Login(); //當前用戶信息函數

char strChoise[20]; //定義字符串名

do

{

system("cls");

cout<<"\t這是壹個簡單的復數計算器程序,可以實現以下功能,請按對應的按鍵(1-7)\n\n\n";

cout<<"\t=========================MENU===========================\n";

cout<<"\t1:多復數加法,以0結束\n";

cout<<"\t2:多復數減法,以0結束\n";

cout<<"\t3:測試100以內的復數加減乘法運算,1次測試10道題\n";

cout<<"\t4:多復數乘法,以0結束\n";

cout<<"\t5:復數自加\n:";

cout<<"\t6:復數自減\n:";

cout<<"\t7:復數比較\n:";

cout<<"\t0:退出程序\n\n:";

cout<<"\t請您選擇:";

cin>>strChoise;

if(strcmp(strChoise,"1")==0) //用戶選1則調用Add()函數

Add();

else if(strcmp(strChoise,"2")==0) //用戶選2則調用Sub()函數

Sub();

else if(strcmp(strChoise,"3")==0) //用戶選3則調用Test()函數

Test();

else if(strcmp(strChoise,"4")==0) //用戶選4則調用Add()函數

Mul();

else if(strcmp(strChoise,"5")==0) //用戶選5調用Add1()函數

Add1();

else if(strcmp(strChoise,"6")==0) //用戶選6則調用Sub1()函數

Sub1();

else if(strcmp(strChoise,"7")==0) //用戶選7則調用Compare()函數

compare();

else if(strcmp(strChoise,"0")==0) //用戶選0則結束調用函數

{

cout<<"\n\n\t歡迎下次繼續使用復數計算器!\n\n";

break;

}

else

{

cout<<"\n\t輸入錯誤,請按任意鍵繼續!\n";

cin.get();

cin.get();

}

}

while((strcmp(strChoise,"0")));

SaveFile(); //調用用戶資料保存函數

return 0;

}

  • 上一篇:Ranameto源代碼
  • 下一篇:dnf scassa的冰息刃怎麽打?
  • copyright 2024編程學習大全網