[c++] 上机作业 二元一次方程求解 与闰年判断

23
2019/09

# [c++] 上机作业 二元一次方程求解 与闰年判断

#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
//4-1 4-2
//P82

// 1 两个物资的交换
void swap()
{
cout<<"swap:"<<endl;
int a,b;
cin>>a>>b;
int c=a;
a=b;
b=c;

cout<<a<<","<<b<<endl;

}
//2 求一个三位整形数字的各位
void solve1()
{
cout<<"各位数字："<<endl;
int num;
cin>>num;
while(num)
{
cout<<num%10<<" ";
num/=10;
}
cout<<endl;

}
//3 闰年

void solve2()
{
cout<<"闰年"<<endl;
int year;
cin>>year;
cout<<((year%4==0&&year%100!=0||year%400==0)?"Yes":"No")<<endl;

}
//11 读入10 个数字
//求他们的和 与 积  及其平均值
void solve3()
{
cout<<"10 个数字"<<endl;
int n=10;
double t=n;

int mut=1;

int num;

while(n--)
{
cin>>num;
mut*=num;
}

}

typedef struct
{
float x;
float y;

}Point;

void solve4(){
cout<<"输入两个点求距离："<<endl;
Point a1,a2;
cin>>a1.x>>a1.y>>a2.x>>a2.y;

float ans=sqrt(pow(abs(a1.x-a2.x),2)+pow(abs(a1.y-a2.y),2));
cout<<ans<<endl;

}
void solve5()
{
cout<<"输入三个数字 求最大数值"<<endl;
int cnt=3;
int n;
int max=0Xefffffff;

while(cnt--){
cin>>n;
max=n>max?n:max;
}
cout<<max<<endl;

}

void endSolve()
{

//ax^2+bx+c=0
cout<<"ax^2+bx+c=0求解："<<endl;
int a,b,c;
cin>>a>>b>>c;

if(a==0)
{
if(b==0){
if(c==0)
{
cout<<"x为任意数"<<endl;
}else
{
cout<<"等式不成立"<<endl;
}
}else{
if(c==0)//bx=0
{
cout<<"x为任意数"<<endl;
}else //bx+c
{
cout<<"x:"<<-(c/(double)b)<<endl;
}
}
}else{
if(b==0){
if(c==0) //ax=0
{
cout<<"x为任意数"<<endl;
}else //ax+c
{
cout<<"x:"<<-(c/(double)a)<<endl;
}
}else{
if(c==0)//ax+bx=x(a+b)=0
{
cout<<"x:0"<<endl;
}else //ax+bx+c
{
int dit=b*b-4*a*c;
if(dit==0)//有两个相同的解 》x=- b/(2a)
{
cout<<"x:"<<-((double)b/2/a)<<endl;
}else if(dit>0)//x=(-b(+-)根号下dit)/2a
{
cout<<"x1:"<<(-1*b+sqrt(dit))/2/a <<endl;
cout<<"x2:"<<(-1*b-sqrt(dit))/2/a <<endl;
}else if(dit<0)//无实数根 担忧两个共轭复数根
{
cout<<"无实数根 但两个共轭复数根"<<endl;
}
}
}
}
}

int main()
{

swap();
solve1();
solve2();
solve3();//1 2 3 4 5 6 7 8 9 10
solve4();
solve5();
endSolve();
return 0;

}


百度已收录

Last modification：September 24th, 2019 at 03:07 pm