#include<iostream>
int main()
{
int n;
enum color{red,yellow,green,blue};
for (n=red; n<=blue;n++)
std::cout<<"n="<<n<<'\t';
std::cout<<'\n';
return 0;
}
②
#include<iostream>
int main()
{
std::cout<<4<<2<<'\n';
std::cout<<(4<<2)<<'\n';
return 0;
}
③=④说明代码的编写很灵活~
③
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b;
c=(a<<2);
d=(b>>2);
cout<<"c="<<c;
cout<<"d="<<d<<'\n';
cout<<((a<b)?c:d);
}
④
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<((a<b)?(a<<2):(b>>2));
}
⑤
#include<iostream>
using namespace std;←非常重要!!!不然出现未知名运算!
int main()
{
int n=100,i=80;
cout<<"十进制"<<dec<<n<<std::endl;
cout<<i<<endl;
cout<<"八进制"<<oct<<n<<std::endl;
cout<<i<<endl;
cout<<"十六进制"<<hex<<n<<std::endl;
return 0;
}
象限题
#include<iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
if(x>0&&y>0)
cout<<"点:"<<x<<","<<y<<"在第一象限"<<endl;
else if(x<0 && y<0)
cout<<"点:"<<x<<","<<y<<"在第四象限"<<endl;
else if(x<0 && y>0)
cout<<"点:"<<x<<","<<y<<"在第二象限"<<endl;
else if(x>0 && y<0)
cout<<"点:"<<x<<","<<y<<"在第三象限"<<endl;
else if(x==0 && y==0)
cout<<"点:"<<x<<","<<y<<"在原点"<<endl;
else if(x>0 && y==0)
cout<<"点:"<<x<<","<<y<<"在y轴正半轴"<<endl;
else if(x<0 && y==0);
cout<<"点:"<<x<<","<<y<<"在y轴负半轴"<<endl;
else if(x==0 && y>0)
cout<<"点:"<<x<<","<<y<<"在x轴正半轴"<<endl;
else if(x==0 && y<0)
cout<<"点:"<<x<<","<<y<<"在x轴正半轴"<<endl;
return 0;
}
⑥
#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
if(x==1注意!→空格||空格←注意!x==3 || x==5 || x==7 || x==8 || x==10 || x==12)
cout<<"月:"<<"共有31天"<<endl;
else if(x==2)
cout<<"月:"<<"共有28天"<<endl;
else if(x==4 || x==6 || x==9 || x==11)
cout<<"月:"<<"共有30天"<<endl;
else
cout<<"请输入正确年份:";
return 0;
}
改进:
#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
if(x==1 || x==3 || x==5 || x==7 || x==8 || x==10 || x==12)
cout<<"月:"<<"共有31天"<<endl;
else if(x==4 || x==6 || x==9 || x==11)
cout<<"月:"<<"共有30天"<<endl;
else if(x==2)
{int y;
cin>>y;
if(→只有一组条件不用加第二个条件(y%4←取余,被几整出余几==0 && y>0 && 加别的条件) || 加这个也可是闰年(整组条件))
cout<<y<<"年是闰年,有29天";
else
cout<<y<<"年是平年,有28天";
}
else
cout<<"请输入正确年份:";
return 0;
}
⑦switch语句
#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
switch(x)
{
case 1:
case 5:
case 3:
case 7:
case 8:
case 10:
case 12:
cout<<"共有31天:";break;
case 2:
{
int y;
cout<<"请输入年份:";
if(y%4==0 && y>0 )
cout<<y<<"年是闰年,有29天";
else
cout<<y<<"年是平年,有28天";
}
case 4:
case 6:
case 9:
case 11:cout<<"共有30天:";break;
default:cout<<"请输入正确月份:";
}
return 0;
}
⑧
1 3 5 ---99求值
#include<iostream>
using namespace std;
int main()
{
int i,s=0;
i=1;
while(i<=99)
{
s+=i;
i+=2;
}
std ::<<"sum="<<sum<<endl;
return 0;
}
do while形式
#include<iostream>
int main()
{
int i,s=0;
i=1;
do
{
s+=i;
i+=2;
}
while(i<=99);
std::cout<<"s="<<s<<std::endl;
return 0;
}
⑨
for语序:
#include<iostream>
int main()
{
int sum=0,i=1;
for(i=1;i<=100;i++)
{
sum=sum+i;
}
std::cout<<"sum="<<sum<<std::endl;
return 0;
}
10 循环跳出
#include<iostream>
using namespace std;
int main()
{
int i;
while(1)
{
cin>>i;
if(i<0 && i%2!=0)
break;←跳出
cout<<i;
}
return 0;
}
11 月份改进
#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
if(x==1 || x==3 || x==5 || x==7 || x==8 || x==10 || x==12)
cout<<"月:"<<"共有31天"<<endl;
else if(x==4 || x==6 || x==9 || x==11)
cout<<"月:"<<"共有30天"<<endl;
else if(x==2)
{int y;
cout<<"请输入年份";
cin>>y;
if(y%400==0 || y%100!=0 && y%4==0)
cout<<y<<"年是闰年,有29天";
else
cout<<y<<"年是平年,有28天";
}
else
cout<<"请输入正确年份:";
return 0;
}
调用
#include<iostream.h>
int s(int a, int b)
{
return a+b;
}
int main()
{
int a,b;
cin>>a>>b;
cout<<s;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a;
for(a=0;a<=127;a++)
{
std::cout<<a<<'\t';
if(a%10==0);
}
cout<<'\n';
cout<<'\n';
cout<<'\n';
cout<<'\n';
cout<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<' '<<"制作人软件(2)班王江朴";
return 0;
}
#include<iostream>
using namespace std;
int abc()
{
int i, j=1;
for(i=0;i<=3;i++)
j++;
return j;
}
int abc1()
{
int i;
static int j=1;
for(i=0;i<3;i++)
j++;
return j;
}
int main()
{
cout<<abc()<<'\n';
cout<<'\n'<<"_________________"<<'\n';
cout<<abc1()<<'\n';
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int b[10];
int i;
for(i=0;i<=9;i++)
cin>>b[i];
for(i=9;i>=0;i--)
cout<<b[i];
}