用C++编写一个小型复数计算器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C++编写一个小型复数计算器相关的知识,希望对你有一定的参考价值。

请用面向对象方法设计并实现一个小型复数计算器,包括如下功能:
实现复数的设置和显示
实现复数加减乘除功能,并显示运算结果
实现用运算符 = = 进行复数的相等比较,并显示比较结果

谁能帮我写一个

参考技术A 需要自己仔细看看哦。

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

参考资料:http://hi.baidu.com/raoguojun/blog/item/257edc11e81bc110b8127b14.html

本回答被提问者采纳
参考技术B #include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <stdio.h>
#include <process.h>
#define EXCAPE 27
#define ENTER 13
main()
int press,i,x,y,x1,y1,ch_z=0;
int dian=0;
char ch='0'; /*input + - * / */
char emp[80],sum[80],*e,*s;
double yuan=0.000000000000;
void init(void);
void clear_z(char *u);
double strtoflt(char *p);
int getkey();
int gd=DETECT, gm;
initgraph(&gd, &gm, "");
e=emp;
s=sum;
init();
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
while(1)
press = getkey();
switch(press)
case EXCAPE:
exit(0);
case 47:
bar (x + 10, y + 80 + 10, x + 60 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0')
switch(ch)
case '/':
if (strtoflt(emp)==0.0)
ch='0';
ch_z=0;
dian=0;
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
outtextxy(x+30,y+40,"error!!!!!");
break;

yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp))
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);

else
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);

clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';


else
if (ch_z==0)
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);


else
outtextxy(x+30,y+40,sum);



ch='/';
ch_z=0;
emp[0]='\0';
e=emp;
dian=0;
break;
case 42:
bar (x + 60 + 10, y + 80 + 10, x + 60 * 2 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0')
switch(ch)
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp))
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);

else
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);

clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;


else
if (ch_z==0)
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;

else
outtextxy(x+30,y+40,sum);

ch='*';
ch_z=0;
dian=0;
break;
case 45:
bar (x + 60 * 2 + 10, y + 80 + 10, x + 60 * 3 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0')
switch(ch)
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp))
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);

else
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);

clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;


else
if (ch_z==0)
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;

else
outtextxy(x+30,y+40,sum);

ch='-';
ch_z=0;
dian=0;
break;
case 43:
bar (x + 60 * 3 + 10, y + 80 + 10, x + 60 * 4 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0')
switch(ch)
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp))
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);

else
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);

clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;


else
if (ch_z==0)
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;

else
outtextxy(x+30,y+40,sum);

ch='+';
ch_z=0;
dian=0;
break;
case 49:
bar (x + 10, y + 80 + 53 + 10, x + 60 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++)
if (emp[i]=='\0')
break;

if (ch_z==0)
*e='1';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 50:
bar (x + 60 + 10, y + 80 + 53 + 10, x + 60 * 2 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++)
if (emp[i]=='\0')
break;

if (ch_z==0)
*e='2';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 51:
bar (x + 60 * 2 + 10, y + 80 + 53 + 10, x + 60 * 3 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++)
if (emp[i]=='\0')
break;

if (ch_z==0)
*e='3';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case ENTER:
bar (x + 60 * 3 + 10, y + 80 + 53 + 10, x + 60 * 4 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
if (ch!='0')
switch(ch)
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp))
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);

else
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);

clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;


else
if (ch_z==0)
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;

else
outtextxy(x+30,y+40,sum);


ch='0';
ch_z=1;
dian=0;
break;
case 52:
bar (x + 10, y + 80 + 53 * 2 + 10, x + 60 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0)
*e='4';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 53:
bar (x + 60 + 10, y + 80 + 53 * 2 + 10, x + 60 * 2 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0)
*e='5';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 54:
bar (x + 60 * 2 +10, y + 80 + 53 * 2 + 10, x + 60 * 3 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0)
*e='6';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 46:
bar (x + 60 * 3 + 10, y + 80 + 53 * 2 + 10, x + 60 * 4 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (dian==0)
if (ch_z==0)
*e='.';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);


else
if (ch_z==0)
outtextxy(x+30,y+40,emp);
else
outtextxy(x+30,y+40,sum);

dian=1;
break;
case 55:
bar (x + 10, y + 80 + 53 * 3 + 10, x + 60 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0)
*e='7';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 56:
bar (x + 60 + 10, y + 80 + 53 * 3 + 10, x + 60 * 2 -10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0)
*e='8';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 57:
bar (x + 60 * 2 + 10, y + 80 + 53 * 3 + 10, x + 60 * 3 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0)
*e='9';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 48:
bar (x + 60 * 3 + 10, y + 80 + 53 * 3 + 10, x + 60 * 4 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0)
*e='0';e++;*e='\0';
outtextxy(x+30,y+40,emp);

else
outtextxy(x+30,y+40,sum);

break;
case 32:
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
ch='0';
ch_z=0;
dian=0;
init();
break;
case 8:
delay(8000);
for(i=0;i<=79;i++)
if (emp[i]=='\0')
break;

if (i==0)
break;
if (i!=79&&i!=0)
i--;
emp[i]='\0';
e=&emp[i];

init();
outtextxy(x+30,y+40,emp);
break;



/*---------------------------------------------------------------------*/
void init(void)
int x, y, x1, y1, i, j;
char emp;
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
cleardevice();
setbkcolor(3);
setfillstyle(1, 15);
setcolor(15);
settextstyle(1,0,1);
rectangle (x, y, x1, y1);
rectangle (x - 7, y - 7, x1 + 7, y1 + 7);
rectangle (x + 10, y + 10, x1 - 10, y + 80 - 10);
line (x, y + 80, x1, y + 80);
y = y + 80;
for (j = 1; j <= 4; j++)
x = (getmaxx() / 2) - 120;
for (i = 1; i <= 4; i++)
/* bar (x + 10, y + 10, x + 60 - 10, y + 60 - 10);*/
rectangle(x + 10, y + 10, x + 60 - 10, y + 60 - 10);
if (j == 1)
if (i == 1)
outtextxy(x + 20, y + 20, "/");
if (i == 2)
outtextxy(x + 25, y + 20, "*");
if (i == 3)
outtextxy(x + 27, y + 20, "-");
if (i == 4)
outtextxy(x + 25, y + 20, "+");

if (j == 2)
if (i == 1)
outtextxy(x + 25, y + 20, "1");
if (i == 2)
outtextxy(x + 25, y + 20, "2");
if (i == 3)
outtextxy(x + 25, y + 20, "3");
if (i == 4)
outtextxy(x + 25, y + 20, "=");

if (j == 3)
if (i == 1)
outtextxy(x + 25, y + 20, "4");
if (i == 2)
outtextxy(x + 25, y + 20, "5");
if (i == 3)
outtextxy(x + 25, y + 20, "6");
if (i == 4)
outtextxy(x + 25, y + 20, ".");

if (j == 4)
if (i == 1)
outtextxy(x + 25, y + 20, "7");
if (i == 2)
outtextxy(x + 25, y + 20, "8");
if (i == 3)
outtextxy(x + 25, y + 20, "9");
if (i == 4)
outtextxy(x + 25, y + 20, "0");

x = x + 60;

y = y + 53;


/*---------------------------------------------------------------------*/
int getkey()
char lowbyte;
int press;
while(bioskey(1)==0);
press = bioskey(0);
press = press&0xff? press&0xff: press>>8;

return(press);


double strtoflt(char *p)

double rtl=0.000000000000;
double pnt=0.000000000000;
double t = 10;
int ispoint = 0;
while (*p!='\0'||*p!='.')
if(*p<'0'||*p>'9')
break;
rtl*=10;
rtl+=*p-'0';
p++;

if (*p=='.')
ispoint=1;
p++;

while(ispoint&&*p!='\0')
pnt+=(double)(*p-'0')/t;
t*=10;
p++;

rtl+=pnt;
return (rtl);

/*-----------------------------------------------------------------------*/
void clear_z(char u[])
int i;
for(i=strlen(u)-1;i>=0;i--)
if (u[i]!='0')
break;

if (u[i]=='.')
u[i]='\0';

else
i++;
u[i]='\0';

.

复数计算器:c 中结构变量的算术运算

【中文标题】复数计算器:c 中结构变量的算术运算【英文标题】:complex number calculator: arithmetic operations with struct variables in c 【发布时间】:2021-12-13 23:20:23 【问题描述】:

尝试编写一个可以进行复数计算的 c 程序。程序必须使用这种结构:

typedef struct Complex_ 
    double RealPart;
    double ImagPart;
 Complex;

它必须使用一个函数来读取用户输入的复数,另一个函数来添加它们,另一个函数来相乘,等等。我现在正试图让这个函数来添加数字,我'正在试图弄清楚如何做到这一点。这是用于读取用户输入的函数:

Complex read_complex(void) 
    Complex user1, user2;
    printf("Enter first complex number: ");
    scanf("%lf %lf", &user1.RealPart, &user1.ImagPart);
    printf("Enter the second complex number: ");
    scanf("%lf %lf", &user2.RealPart, &user2.ImagPart);

return;

这就是我到目前为止添加复数的方法:

Complex add_complex(Complex z1, Complex z2) 
    Complex z3;

    z3 = z1 + z2;//error on this line

    return(z3);

函数必须返回 z3,z3 必须等于 z1 + z2,z1 和 z2 必须是 Complex 类型的变量。我不知道如何使它符合这些规范,因为你不能对结构变量进行算术运算。

【问题讨论】:

您将不得不在某处编写自己的代码,以显式地将实部添加到实部,将复杂部分添加到复杂部分。如您所见,您不能将+ 运算符应用于两个结构并期望它们被神奇地添加; C 没有办法解决这个问题。 (您可以让它在具有运算符重载的C++中工作。) 请注意,从 C99 开始,该语言本身就支持复数。只需包含complex.h 我希望您的read_complex 功能比显示的更多。 user1user2 是该函数的本地函数,您只能 return 其中一个(并且您目前没有返回任何内容)。如果您希望用户输入的内容保留在该函数之外,则需要将它们作为指针传递。 【参考方案1】:

不是您要问的,但您的 read_complex 函数如图所示将不起作用。建议改成如下内容

#include <stdbool.h>

bool read_complex(Complex* user1, Complex* user2)

  bool inputValid = false;
  
  // weak check for validity, a non-NULL pointer isn't necessarily
  // valid. In fact, probably better to skip this check and instead
  // document/accept UB if user1 and/or user2 are not valid pointers.
  if (user1 != NULL && user2 != NULL)
  
    printf("Enter first complex number: ");
    if (scanf("%lf %lf", &(user1->RealPart), &(user1->ImagPart)) == 2)
    
      printf("Enter the second complex number: ");
      if (scanf("%lf %lf", &(user2->RealPart), &(user2->ImagPart)) == 2)
      
        inputValid = true;
       // else, print error message?
     // else, print error message?
  

  return inputValid;

scanf 返回一个int,指示与提供的格式说明符匹配的输入数量。每个输入应该是 2,如果不是,你知道有问题。 read_complex 的调用者可以决定如果它返回false 下一步要做什么。

【讨论】:

【参考方案2】:

您不能添加或减去数据结构。

Complex add_complex(Complex z1, Complex z2) 
    Complex z3;

    z3.RealPart = z1.RealPart + z2.RealPart;
    z3.ImagPart = z1.ImagPart + z2.ImagPart;

    return(z3);

【讨论】:

以上是关于用C++编写一个小型复数计算器的主要内容,如果未能解决你的问题,请参考以下文章

c++简单复数计算器

用结构体函数计算两个复数的四则运算的程序分析、重难点和结论?

复数计算器:c 中结构变量的算术运算

C++,,,课堂作业求助,,,编写一个程序,,计算自己的生日距离今天多少天,,是星期几

c语言中使用函数和结构的复数计算器

用c++语言编写四则运算计算器