关于switch的应用:
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
int a,b;
char c;
cin>>a>>b>>c;
switch(c)
{
case '+':cout<<a+b<<endl;break;
case '-':cout<<a-b<<endl;break;
case '*':cout<<a*b<<endl;break;
case '/':
if(b==0)
cout<<"Divided by zero!"<<endl;
else
cout<<a/b<<endl;
break;
default:cout << "Invalid operator!" << endl;break;
}
return 0;
}
#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
float a,b,c;
cin>>a>>b>>c;
float x1,x2;
float d,e;
float d=b*b - 4 * a * c;
if(d>0)
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b -sqrt(d))/(2*a);
cout<<"x1="<<x1<<";"<<"x2="<<x2<<endl;
else if(d==0)
cout<<"x1=x2="<<(-b + sqrt(d))/(2*a)<<endl;
else
d=-b / (2*a);
e=sqrt(4*a*c-b*b) / (2*a);
cout<<"x1="<<d<<"+"<<e<<"i"<<";"<<x2="<<d<<"-"<<e<<"i"<<endl;
return 0;
}