77范文网 - 专业文章范例文档资料分享平台

为C++初学者准备的一些简单例子

来源:网络收集 时间:2018-11-22 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

//you must konw the differences between BASIC and C++ //in c++ ,= do not mean equal!while == has the meaning ! #include \#include using namespace std; int n;

bool flag=true;

int main(int argc, char* argv[]) { cout<<\请输入一个整数: \ cin>>n; for(int i=2;i!=n;++i){ if(n%i==0){cout<<\这是一个非质数\ } if(flag==true) cout<<\你输入了一个质数\ return 0; }

88***************************************************************************** /*这个习题考察的是对初始值的理解;在定义func这个函数的时候 对C,D规定 了初始值,但是如果在调用的时候对于CD有了重新的定义,。那么输出的值 就不熟3和4了;这里的理解有误*/ #include \#include using namespace std;

void func(int a,int b,int c=4,int d=5) { cout<

int main(int argc, char* argv[]) { func(10,25,35,40); func(10,11,12); func(113,13); return 0; }

//简单的四舍五入程序,自己的想法,很不错~ #include \#include using namespace std; int f(double n){ int n1=n; if(n-n1>0&&0.5>n-n1) return n1;

if(n-n1>=0.5) return n1+1; }

int main(int argc, char* argv[]) { double x; cin>>x; cout<

********************************************************************** #include \# include #include using namespace std; int re;

int main(int argc, char* argv[]) {

srand((unsigned)time(NULL)); int ran1,ran2;

ran1=rand()0+1; ran2=rand()0+1;

cout<>re;

if(re==ran1+ran2) cout<<\真棒,答对了\

else{ cout<<\再算一次吧~ \ cin>>re;

if(re==ran1+ran2)cout<<\真棒,答对了\ else cout<<\真遗憾,答错了\ } return 0; }

// 习题3点11.cpp : Defines the entry point for the console application. // 判断这个函数的的结果

// when you do such kind of exercise ,pay attention to the progress //make sure that you konw the progress well,if not ,calm down~

#include \#include using namespace std;

int main(int argc, char* argv[]) {

}

int a[10]={77,83,72,54,92,65,75,81,43,96}; int b[5]={60,70,80,90,100}; int c[5]={0};

for(int i=0;i<10;i++) { int j=0; while(a[i]>=b[j])j++; c[j]++; } for(i=0;i<5;i++)cout<

// 测试复制构造函数.cpp : Defines the entry point for the console application. //

// 对象与类的测试.cpp : Defines the entry point for the console application.

//this code is an example of the destructor and the copy consturctor;pay attention to the result and understand the execute progress;

// take notice of of mark 1 and mark2;mark1 is about the definition of the destructor;

#include \#include using namespace std; class rect{ float x,y; public: rect(float a=1, float b=1){x=a,y=b; cout<<\ void set(float a=0,float b=0){x=a,y=b;} rect(rect & obj4){x=obj4.x; y=obj4.y;cout<<\ float peri(); float area(); ~rect(){cout<<\ };

float rect::peri() { float z; z=2*x+2*y; return z;

}

float rect::area() { float z; z=x*y; return z; }

void main() { rect obj(78.5,52),obj2,obj3(obj); cout<<\ cout<<\ cout<<\ cout<<\ obj.set(12,34); obj2.set(34,78); cout<<\ cout<<\ cout<<\}

***************************************************************************8 // 对象与类的测试.cpp : Defines the entry point for the console application. //

#include \#include using namespace std; class rect{ float x,y; public: rect(float a=1, float b=1){x=a,y=b;} void set(float a=0,float b=0){x=a,y=b;} float peri(); float area(); };

float rect::peri() { float z; z=2*x+2*y; return z; }

float rect::area() { float z;

z=x*y; return z; }

void main() { rect obj(78.5,52),obj2; cout<<\ cout<<\ cout<<\ obj.set(12,34); obj2.set(34,78); cout<<\ cout<<\ cout<<\}

#include \#include #include using namespace std;

int white,black,sum,ran1,ran2;

int main(int argc, char* argv[]) { srand((unsigned)time(NULL)); cout<<\请输入白球的个数:\ cin>>white; cout<<\请输入黑球的个数: \ cin>>black; sum=black+white; while(black+white!=1){ ran1=rand()%sum+1; ran2=rand()%sum+1; if(ran1,ran2<=white ){ white--; sum--; } if(ran1,ran2>white ){ white--; sum--; }

if(ran1white){ black--; sum--; }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库为C++初学者准备的一些简单例子在线全文阅读。

为C++初学者准备的一些简单例子.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/297766.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: