C++小例子

复制代码
#include <iostream>
#include <conio.h>
int main(){
    std::cout<<"Enter two numbers:"<<std::endl;
    int v1,v2;
    std::cin>>v1>>v2;
    int lower,upper;
    if(v1<=v2){
        lower = v1;
        upper = v2;
    }else{
        lower = v2;
        upper = v1;
    }
    int sum = 0;
    for(int val = lower;val<=upper;++val){
        sum +=val;
    }

    std::cout<<"Sum of "<<lower<<"to"<<upper
             <<" is "
             <<sum<<std::endl;
    getch();
    return 0;
}
复制代码

输入两个数

计算出,他们之间的和

例如输入1、3

计算1+2+3




本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/3723660.html,如需转载请自行联系原作者

c++语言实例#include <iostream.h> const int N=1000000; class set { int IntArray[N]; int last; public: int Append(); int Delete(); int Nember(int x); int Empty(); int Full(); void Display(); void find(); set(); }; int set::Nember(int x) { int i; for(i=0;i<last;i++) if(IntArray[i]==x) return 1; return 0; } set::set() { last=0; } int set::Empty() //判断集合是否空 { if(last==0) return 1; return 0; } int set::Full() { if(last==N) return 1; return 0; } int set::Append() { if(Full()) return 0; int x; cout<<"请输入你要添加的数"; cin>>x; if(Nember(x)) return 0;// 集合中已经有x IntArray[last]=x; last++; return 1; // 插入x } int set::Delete() { if(Empty()) { cout<<"数组为空"; return 0; } int i; int x; int j; cout<<"请输入你要删除的数"; cin>>x; for(i=0;i<last;i++) if(IntArray[i]==x) { for(j=i;j<last;j++) IntArray[j]=IntArray[j+1]; last--; cout<<"删除后:"<<endl;; Display(); return 1; } cout<<"集合中没有你要删除的数!"<<endl; return 0; } void set::Display() { int i; for(i=0;i<last;i++) { cout<<IntArray[i]<<'\t'; if((i+1)%5==0) cout<<endl; } cout<<endl; } void set::find() { if(Empty()) { cout<<"数组为空"; } else { int x; int i; cout<<"请输入要查找的数:"; cin>>x; for(i=0;i<last;i++) if(IntArray[i]==x) break; if(i>=last) cout<<"没有查到!"<<endl; else cout<<"找到了,在数组第"<<i+1<<"位置"<<endl; } } void main() { set b; b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Display(); b.Display(); b.Display(); b.Display(); b.Display(); b.Display(); b.Delete(); b.Delete(); b.Delete(); b.Delete(); b.Delete(); b.Delete(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值