c++实例200
肯能有的例子没有题目但是都比较简单。适合初学者y=5+X++cout<0&&a<=5="<2*b+2||a2*b+2|lamaincout<"c="<b)<<(a>=b)<<(a==b)<3="<<(5>>3)<>3="<(-5>3)main#include < iostream h>ta=1,b=1=3;//显示a,b,c的值float a=35b=2.1,c=0;Cout<"a="<=0&&a<=5;/变量a在[0,5]间内Cout"(3)a="<cout<< sizeof(xy)=<ain(#include main()∥/声明变量语句中使用顺序运算Int x, y//用 sizeof计算各类种常量的字节长度cout<char c='amain()float x=3, 1416double p=0.1/测试表达式类型的转换cout<>a>>b>>Ccout<<<if(b<=c)/内层条件语句main()smallest=b:sfloat a.b.ssmallest=couts>score//用带 else if的条件语句判断处理#include if (score100)main()The score is out of rangeint x,y;else if (score>=90cIn>>xcout<=80=70cout<< Your grade is a C "<=60Your grade is a D. <#include iostream . h>aindoublecout<<"n==2*3.1416*r;/计算圆的周长,赋值给CIn>>n;变量if(n>=0&&n<=100&&n%2==0)Cout<"="<<|<cIn>>r:∥/键盘输入main|=2*31416*r;∥/计算圆的周长,赋值给变量1int a b. maxcout<"l=">a:cout<cin>>bmain()∥/出较大值/xy为操作数,c为运算符Max=a>b?a: b:nt x,y,Zcout<< Max="<>x>>01>#include ∥/多路选择语句选择不同表达式计算语句nain(switch(c1) tcase+: cout<>acase: cout<<<>bcase /: cout<include float x=365.5;声明全局变量main()main()int x=1,y=2;double r=1.0:double w=X+ycout<int s=omain()旰f(%3=-0)/能被3整除的整数,返回进行/计算和打印打印乘法九九表下次循环for(inti=1;<=9;++)continuecout<main#includesiostream. h>//声明数组和变量main()int a[5l, i, sum;doubleint x, sum=∥/定义标号11/八从键盘上循环为数组赋值L1: cout<>xcout<<"a["<>agoto L2∥/无条件转移话句,转到L2语句处else/直接显示数组元素sum+=Cout<sum=a[0]+a[1]+a[2]+a[3]+a[4];main()cout<< sum="<>X,∥/显示累加和及平均值if ( x<=0) breakcout<< sum=<//{含 iostream h头文件void main(#includesiostreamhsmain()∥输出字符常量、变量和字符串char c1=Aintcout<>adouble pi=3. 1415926, r=10.0, s=pi*Fr*r:cout<alil)∥/个cout可以输出多项数据cout</显示排序结果main(for (i=0; i>afor(=1;i<=3;i++)cout<< valuecin>>value∥/利用循环遍历数组,找出最大值的元素及其下标/二分法查找数组for(i=0;i<=4i++)found=ohigh=size-1;hile (low<=highnax=aid=(high+low )/2if (a[mid==valuecout<<\nMax="<#define size 5if (a[mid]>aif (found)cout<nal∥/找出该数组的最大元素及其下标/声明变量int h, I, Max=a[OJ[O]int i,j;for(i=0i<2;i++){float t, alfor(j=0小j<3;j++)∥/从键盘上为数组赋值if Masa[il[]ifor(i=0;i<=4|++)Max=ajg;cout<"a["<>aj:∥/对数组按从大到小顺序排序for(i=0;i<=3;i+)cout<<"Max:"<"a["<in(a[i=a[j]/声明字符数组和变量a[l=tchar str[6int i:∥/显示排序结果/从键盘上输入字符串for(i=0<=4+cout<< str=cout<>st rcoutmain(按数组和下标变量两种方式显示字符数组cout<=0;i-)cout<
用户评论