输入学生成绩,要求输出成绩等级,90分及以上为’A’,80-90分为‘B’,70-79分为‘C’,60-69分为‘D’,60分以下为‘E’
#include<iostream>
using namespace std;
void main()
{
int s;
cout<<"input s:"<<endl;
cin>>s;
if(s<0||s>100)
cout<<"error!"<<endl;
else
if(s>=90)
cout<<"A"<<endl;
else if(s>=80)
cout<<"B"<<endl;
else if(s>=70)
cout<<"C"<<endl;
else if(s>=60)
cout<<"D"<<endl;
else
cout<<"E"<<endl;
}
#include<iostream>
using namespace std;
void main()
{
int s;
cout<<"input s:"<<endl;
cin>>s;
if(s<0||s>100)
cout<<"error!"<<endl;
else
if(s<90)
if(s<80)
if(s<70)
if(s<60)
cout<<"E"<<endl;
else
cout<<"D"<<endl;
else
cout<<"C"<<endl;
else
cout<<"B"<<endl;
else
cout<<"A"<<endl;
}
习题2.
判断某一年是闰年
#include<iostream>
using namespace std;
void main()
{
int y,flag;
cout<<"input y."<<endl;
cin>>y;
if(y%4==0)
if(y%100==0)
if(y%400==0)
flag=1;
else
flag=0;
else
flag=1;
else
flag=0;
if(flag==1)
cout<<y<<"年是闰年。"<<endl;
else
cout<<y<<"年是平年。"<<endl;
}
#include<iostream>
using namespace std;
void main()
{
int y,flag;
cout<<"input y:"<<endl;
cin>>y;
if(y%4!=0)
flag=0;
else if(y%100!=0)
flag=1;
else if(y%400!=0)
flag=0;
else
flag=1;
if(flag==1)
cout<<y<<"是闰年。"<<endl;
else
cout<<y<<"是平年。"<<endl;
}
习题3.
求方程ax^2+bx=c=0的根
#include<iostream>
#include<math.h>
using namespace std;
void main()
{
float a,b,c,dt,x1,x2,p,q;
cout<<"input a,b,c"<<endl;
cin>>a>>b>>c;
if(a==0)
{
x1=-c/b;
cout<<"x="<<x1<<endl;
}
else
{
dt=b*b-4*a*c;
p=-b/(2*a);
q=sqrt(fabs(dt))/(2*a);
if(dt>0)
{
x1=p+q;
x2=p-q;
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
else if(dt==0)
{
x1=p;
cout<<"x1=x2="<<x1<<endl;
}
else
{
cout<<"x1="<<p<<"+"<<q<<"*i"<<endl; //cout<<"x1=p+q*i"<<endl;提前写好要输出的语句
cout<<"x2="<<p<<"-"<<q<<"*i"<<endl; //cout<<"x2=p-q*i"<<endl;
}
}
}
习题4.
判断整数的正负性跟奇偶性
先判断奇偶后正负性
#include<iostream>
using namespace std;
void main()
{
int x;
cout<<"input x:"<<endl;
cin>>x;
if(x%2==0)
{
if(x>0)
cout<<x<<"是正偶数。"<<endl;
else if(x<0)
cout<<x<<"是负偶数。"<<endl;
else
cout<<"x=0"<<endl;
}
else
{
if(x>0)
cout<<x<<"是正奇数。"<<endl;
else
cout<<x<<"是负奇数。"<<endl;
}
}
先判断正负性后奇偶性
#include<iostream>
using namespace std;
void main()
{
int x;
cout<<"input x:"<<endl;
cin>>x;
if("x%2==0")
{
if(x>0)
cout<<x<<"是正偶数。"<<endl;
else if(x<0)
cout<<x<<"是负偶数。"<<endl;
else
cout<<"x=0"<<endl;
}
else
{
if(x>0)
cout<<x<<"是正奇数。"<<endl;
else
cout<<x<<"是负奇数。"<<endl;
}
}
例2:商店打折售货,购货金额数量越大,折扣越大,具体标准为(m:购货金额,d:折扣率)
m<250(元) d=0%
250<=m<500 d=5%
500<=m<1000 d=7.5%
1000<=m<2000 d=10%
m>=2000 d=15%
从键盘输入购货金额,计算实付的金额。
#include<iostream>
using namespace std;
void main()
{
float m,f,d; //m:购货金额 d:折扣率 f:实付金额
int c;
cout<<"input m:"<<endl; //f算出来d已给值,只需输入m值
cin>>m;
if(m>=0) //if else 情况你也可能不给人家钱
{
c=m/250;
switch(c) //switch(必须是整数)写法1: switch(m/250) m实形,导致最终c不是整型写法2: switch(int (m/250) )强制转换,可以
{
case 0:d=0;break;
case 1:d=0.05;break; //输入d需注意转换%
case 2:
case 3:d=0.075;break;
case 4:
case 5:
case 6:
case 7:d=0.1;break;
default:d=0.15;
}
f=m*(1-d);
cout<<"f="<<f<<endl;
}
else
cout<<"error!"<<endl;
}
习题1:
从键盘输入一个年份和月份,判断该月有多少天。
月份多少天:
31天: 1,3,5,7,8,10,12
30天: 4,6,9,11
2月 :闰年 29天 ; 平年28天
判断是否为闰年
y%4 == 0 不成立,平年
y%100 == 0 不成立,闰年
y%400==0 成立,闰年;不成立平年
#include<iostream.h>
void main()
{
int y,m,d;
cout<<"input y,m:"<<endl;
cin>>y>>m;
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d=31;break;
case 4:
case 6:
case 9:
case 11:d=30;break;
case 2:
{
if(y%4==0)
if(y%100==0)
if(y%400==0)
d=29;
else
d=28;
else
d=29;
else
d=28;
}break;
default:cout<<"input error!"<<endl;
}
cout<<y<<"年"<<m<<"月有"<<d<<"天。"