复数的反三角函数计算程序(复数以通用书写格式输入、输出)
C
①
complex double z;
void Input()
{
double*p=(double*)&z;
scanf("%lg",p);
switch(getchar())
{
case'-':ungetc('-',stdin);
case'+':scanf("%lgi",p+1);
break;
case'i':1[p]=0[p];
0[p]=0;
}
}
void Output(complex double z)
{
double*p=(double*)&z;
if(0[p]!=0&&1[p]!=0)
printf("%.16G%+.16Gi\n",z);
if(0[p]!=0&&1[p]==0)
printf("%.16G\n",0[p]);
if(0[p]==0&&1[p]!=0)
printf("%.16Gi\n",1[p]);
if(0[p]==0&&1[p]==0)
printf("0\n");
}
int main()
{
for(;;)
{
system("Cls");
printf("① arcsin(z)\n② arcsec(z)\n③ arccos(z)\n④ arccsc(z)\n⑤ arctan(z)\n⑥ arccot(z)\n请选择:");
switch(getchar())
{
case'1':system("Cls");
printf("z = ");
Input();
printf("arcsin(z) = ");
Output(casin(z));
return EXIT_SUCCESS;
case'2':system("Cls");
printf("z = ");
Input();
printf("arcsec(z) = ");
Output(cacos(1/z));
return EXIT_SUCCESS;
//arcsec(0)结果不定。
case'3':system("Cls");
printf("z = ");
Input();
printf("arccos(z) = ");
Output(cacos(z));
return EXIT_SUCCESS;
case'4':system("Cls");
printf("z = ");
Input();
printf("arccsc(z) = ");
Output(casin(1/z));
return EXIT_SUCCESS;
//arccsc(0)结果不定。
case'5':system("Cls");
printf("z = ");
Input();
printf("arctan(z) = ");
Output(catan(z));
return EXIT_SUCCESS;
case'6':system("Cls");
printf("z = ");
Input();
printf("arccot(z) = ");
Output(3.1415926535897932384626433832795/2-catan(z));
return EXIT_SUCCESS;
}
}
}
②
complex double z;
void Input()
{
double*p=(double*)&z;
scanf("%lg",p);
switch(getchar())
{
case'-':ungetc('-',stdin);
case'+':scanf("%lgi",p+1);
break;
case'i':1[p]=0[p];
0[p]=0;
}
}
void Output(complex double z)
{
double*p=(double*)&z;
if(0[p]!=0&&1[p]!=0)
printf("%.16G%+.16Gi\n",z);
if(0[p]!=0&&1[p]==0)
printf("%.16G\n",0[p]);
if(0[p]==0&&1[p]!=0)
printf("%.16Gi\n",1[p]);
if(0[p]==0&&1[p]==0)
printf("0\n");
}
int main()
{
for(;;)
{
system("Cls");
printf("① arcsin(z)\n② arcsec(z)\n③ arccos(z)\n④ arccsc(z)\n⑤ arctan(z)\n⑥ arccot(z)\n请选择:");
switch(getchar())
{
case'1':system("Cls");
printf("z = ");
Input();
printf("arcsin(z) = ");
Output(asin(z));
return EXIT_SUCCESS;
case'2':system("Cls");
printf("z = ");
Input();
printf("arcsec(z) = ");
Output(acos(1/z));
return EXIT_SUCCESS;
//arcsec(0)结果不定。
case'3':system("Cls");
printf("z = ");
Input();
printf("arccos(z) = ");
Output(acos(z));
return EXIT_SUCCESS;
case'4':system("Cls");
printf("z = ");
Input();
printf("arccsc(z) = ");
Output(asin(1/z));
return EXIT_SUCCESS;
//arccsc(0)结果不定。
case'5':system("Cls");
printf("z = ");
Input();
printf("arctan(z) = ");
Output(atan(z));
return EXIT_SUCCESS;
case'6':system("Cls");
printf("z = ");
Input();
printf("arccot(z) = ");
Output(3.1415926535897932384626433832795/2-atan(z));
return EXIT_SUCCESS;
}
}
}
C++
using namespace std;
complex<double>z;
void Input()
{
double*p=reinterpret_cast<double*>(&z);
cin>>0[p];
switch(cin.peek())
{
case'+':
case'-':cin>>1[p];
break;
case'i':1[p]=0[p];
0[p]=0;
}
}
void Output(complex<double>z)
{
cout<<setprecision(16)<<uppercase;
if(z.real()!=0&&z.imag()!=0)
cout<<z.real()<<showpos<<z.imag()<<"i\n";
if(z.real()!=0&&z.imag()==0)
cout<<z.real()<<'\n';
if(z.real()==0&&z.imag()!=0)
cout<<z.imag()<<"i\n";
if(z.real()==0&&z.imag()==0)
cout<<"0\n";
}
int main()
{
for(;;)
{
system("Cls");
cout<<"① arcsin(z)\n② arcsec(z)\n③ arccos(z)\n④ arccsc(z)\n⑤ arctan(z)\n⑥ arccot(z)\n请选择:";
switch(cin.get())
{
case'1':system("Cls");
cout<<"z = ";
Input();
cout<<"arcsin(z) = ";
Output(asin(z));
return EXIT_SUCCESS;
case'2':system("Cls");
cout<<"z = ";
Input();
cout<<"arcsec(z) = ";
Output(acos(1./z));
return EXIT_SUCCESS;
//arcsec(0)结果不定。
case'3':system("Cls");
cout<<"z = ";
Input();
cout<<"arccos(z) = ";
Output(acos(z));
return EXIT_SUCCESS;
case'4':system("Cls");
cout<<"z = ";
Input();
cout<<"arccsc(z) = ";
Output(asin(1./z));
return EXIT_SUCCESS;
//arccsc(0)结果不定。
case'5':system("Cls");
cout<<"z = ";
Input();
cout<<"arctan(z) = ";
Output(atan(z));
return EXIT_SUCCESS;
case'6':system("Cls");
cout<<"z = ";
Input();
cout<<"arccot(z) = ";
Output(3.1415926535897932384626433832795/2-atan(z));
return EXIT_SUCCESS;
}
}
}
Perl
use Math::Trig;
for($l=1;$l;)
{
system "Cls";
print "① arcsin(z)\n",
"② arcsec(z)\n",
"③ arccos(z)\n",
"④ arccsc(z)\n",
"⑤ arctan(z)\n",
"⑥ arccot(z)\n",
"请选择:";
chomp($o=<STDIN>);
if($o==1)
{
system "Cls";
print "z = ";
chomp($_=<STDIN>);
$z=Math::Complex->make($_);
print "arcsin(z) = ",asin($z),"\n";
$l=0;
}
if($o==2)
{
system "Cls";
print "z = ";
chomp($_=<STDIN>);
$z=Math::Complex->make($_);
if($z!=0)
{
print "arcsec(z) = ",asec($z),"\n";
}
else
{
print "arcsec(z) = NAN+NANi\n";
}
$l=0;
}
if($o==3)
{
system "Cls";
print "z = ";
chomp($_=<STDIN>);
$z=Math::Complex->make($_);
print "arccos(z) = ",acos($z),"\n";
$l=0;
}
if($o==4)
{
system "Cls";
print "z = ";
chomp($_=<STDIN>);
$z=Math::Complex->make($_);
if($z!=0)
{
print "arccsc(z) = ",acsc($z),"\n";
}
else
{
print "arccsc(z) = NAN+NANi\n";
}
$l=0;
}
if($o==5)
{
system "Cls";
print "z = ";
chomp($_=<STDIN>);
$z=Math::Complex->make($_);
print "arctan(z) = ",atan($z),"\n";
$l=0;
}
if($o==6)
{
system "Cls";
print "z = ";
chomp($_=<STDIN>);
$z=Math::Complex->make($_);
print "arccot(z) = ",3.1415926535897932384626433832795/2-atan($z),"\n";
$l=0;
}
#此处不直接用库函数acot($z)是因为它被定义为了atan(1/$z)。
}
Forse altri canterà con miglior plettro.