C++语言

C++中的if结构:

利用if语句的嵌套求3个整数中的最大值

#include<iostream.h>
void main()
{
int a,b,c,max;
cout<<"请输入3个整数:"<<endl;
cin>>a>>b>>c;
if(a>b)
{
if(a>c)max=a;
else max=c;
}
else
{
if(b>c)max=b;
else max=c;
}
cout<<"3个整数中最大的数是:"<<max<<endl;
}

利用if实现计算器功能

#include<iostream.h>
void main()
{
int x,y;
char ch;
cout<<"请输入任意两个数:";
cin>>x>>y;
cout<<"请输入运算符:";
cin>>ch;
if(ch=='+')
cout<<x<<ch<<y<<"="<<x+y<<endl;
else if(ch=='-')
cout<<x<<ch<<y<<"="<<x-y<<endl;
else if(ch=='*')
cout<<x<<ch<<y<<"="<<x*y<<endl;
else if(ch=='/')
if(y!=0)
cout<<x<<ch<<y<<"="<<x/y<<endl;
else
cout<<"除数不能是0!"<<endl;
else cout<<"运算符输入错误"<<endl;
}

C++中的switch结构:

利用switch语句计算商品折扣问题

#include<iostream.h>
void main()
{
int kind;
float price;
cout<<"肉食类代码10打6折"<<endl;
cout<<"水果类代码11打7折"<<endl;
cout<<"蔬菜类代码12打7折"<<endl;
cout<<"文具类代码13打8折"<<endl;
cout<<"其他类商品均打9折"<<endl;
cout<<"请输入商品代码和原价"<<endl;
cout<<"商品代码:";
cin>>kind;
cout<<"商品原价:";
cin>>price;
switch(kind)
{
case 10:price*=0.6;
 break;
case 11:
case 12:price*=0.7;
 break;
case 13:price*=0.8;
 break;
default:price*=0.9;
}
cout<<"\n"<<kind<<"类商品的现价是:"<<price<<endl;
}

利用switch实现计算器功能

#include<iostream.h>
void main()
{
int x,y;
char ch;
cout<<"请输入任意两个数:";
cin>>x>>y;
cout<<"请输入运算符:";
cin>>ch;
switch(ch)
{
case '+':
cout<<x<<ch<<y<<"="<<x+y<<endl;
break;
case '-':
cout<<x<<ch<<y<<"="<<x-y<<endl;
break;
case '*':
cout<<x<<ch<<y<<"="<<x*y<<endl;
break;
case '/':
if(y!=0)
cout<<x<<ch<<y<<"="<<x/y<<endl;
else
cout<<"除数不能是0!"<<endl;
break;
}
}

C++中的while循环:

利用while循环语句计算1~100的和

#include<iostream.h>
void main()
{
int i=1,sum=0;
while(i<=100)
{
sum+=i;
i++;                        
}
cout<<"1到100的和是:"<<sum<<endl;
}

C++中的do...while循环:

利用do...while循环语句计算1~100的和

#include<iostream.h>
void main()
{
int i=1,sum=0;
do
{
sum+=i;
i++;
}while(i<=100);
cout<<"1到100的和是:"<<sum<<endl;
}

利用do...while循环语句计算任意一个整数的阶乘

#include<iostream.h>
void main()
{
int n,i=1,f=1;
cout<<"任意输入一个正整数:";
cin>>n;
do
{
f*=i;
i++;
}while(i<=n);
cout<<n<<"!="<<f<<endl;
}

自己做的

输入密码的程序:

#include<iostream.h>
void main()
{
int password=10,i=0,b=2,key;
while(i<3)
{
cout<<"请输入密码:";
cin>>key;
i++;
if((password!=key)&&(cout<<"密码错误"))
{
cout<<"你还有"<<b--<<"次机会"<<endl;
}
else if(password=key)
{
cout<<"密码正确"<<endl;
break;
}
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值