[2015.8.6]C++学习总结

唉今天顾着聊天,没怎么学编程,就只做了一个小小个计算器,(还没做到高精度。。),当作用来复习一下函数,switch,和do-while语句吧。

//小型计算机1.0
#include <iostream>
using namespace std;
int plus(int x,int y)
{
cout<<x<<"+"<<y<<"="<<x+y<<endl;
return 1;
}
int minus(int x,int y)
{
cout<<x<<"-"<<y<<"="<<x-y<<endl;
return 1;
}
int times(int x,int y)
{
cout<<x<<"*"<<y<<"="<<x*y<<endl;
return 1;
}
int division(int x,int y)
{
cout<<x<<"/"<<y<<"="<<x/y<<endl;
return 1;
}
int mod(int x,int y)
{
cout<<x<<"%"<<y<<"="<<x%y<<endl;
return 1;
}


int main()
{
int n;
n=0;
do
{
cout<<"请输入你要计算的算式:"<<endl;
int a,b;
char c;
cin>>a>>c>>b;
switch (c)
{
case '+':n=plus(a,b);break;
case '-':n=minus(a,b);break;
case '*':n=times(a,b);break;
case '/':n=division(a,b);break;
case '%':n=mod(a,b);break;
default :n=0;cout<<"你输入的格式不正确:请重新输入: "<<endl;
}
}while (n==0);//当括号里面的表达式是时就跳出循环;这里与pascal的repeat-until有一点相似
return 0;
}




冒泡排序:(与相邻的比)

//冒泡排序
#include <iostream>
using namespace std;
int main()
{
int a[10],n,i,j,b;
cin>>n;
for (i=0;i<=n-1;i++)
cin>>a[i];
for (i=0;i<=n-2;i++)
for (j=i+1;j<=n-1;j++)
{
if (a[i]>a[j])
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
for (i=0;i<=n-1;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值