目录
1 C++概述
1.1 计算两个整数相加之和
#include <iostream>
using namespace std;
/*函数功能:计算两个整数相加之和 */
int Add(int a, int b)
{
return (a + b);
}
//主函数
int main()
{
int x, y, sum = 0;
cout<<"Input two integers:";
cin>>x>>y;
sum = Add(x, y);
cout<<"sum = "<<sum<<endl;
return 0;
}
1.2 计算三个整数相加之和
#include <iostream>
using namespace std;
/*函数功能:计算三个整数相加之和 */
int Add(int a, int b, int c)
{
return (a + b + c);
}
//主函数
int main()
{
int x, y, z, sum = 0;
cout<<"Input three integers:";
cin>>x>>y>>z;
sum = Add(x, y, z);
cout<<"sum = "<<sum<<endl;
return 0;
}
2 选择结构
2.1 小老鼠走迷宫1(if语句)
#include <iostream>
using namespace std;
int main()
{
char cKey;
cin >> cKey;
if(cKey == 'L')
cout<<"Move Left"<<endl;
else if(cKey == 'R')
cout<<"Move right"<<endl;
else if(cKey == 'U')
cout<<"Move up"<<endl;
else if(cKey == 'D')
cout<<"Move down"<<endl;
else if(cKey == 'O')
cout<<"I did it!"<<endl;
else
cout<<"Input Wrong!"<<endl;
return 0;
}
2.2 小老鼠走迷宫1(if语句)(多个单分支结构)
#include <iostream>
using namespace std;
int main()
{
char cKey;
bool flag = true;
cin >> cKey;
if(cKey == 'L'){
cout<<"Move Left"<<endl;
flag = false;
}
if(cKey == 'R'){
cout<<"Move right"<<endl;
flag = false;
}
if(cKey == 'U'){
cout<<"Move up"<<endl;
flag = false;
}
if(cKey == 'D'){
cout<<"Move down"<<endl;
flag = false;
}
if(cKey == 'O'){
cout<<"I did it!"<<endl;
flag = false;
}
if(flag)
cout<<"Input Wrong!"<<endl;
return 0;
}
2.3 小老鼠走迷宫2(switch语句)
#include <iostream>
using namespace std;
int main()
{
char cKey;
cin>>cKey;
switch(cKey)
{
case 'L':
cout<<"Move left"<<endl; break;
case 'R':
cout<<"Move right"<<endl; break;
case 'D':
cout<<"Move Down"<<endl; break;
case 'U':
cout<<"Move Up"<<endl; break;
case 'B':
cout<<"Move back"<<endl; break;
default:
cout<<"Error"<<endl;
}
return 0;
}
2.4 小老鼠走迷宫2(switch语句)(延伸1)
#include <iostream>
using namespace std;
int main()
{
char cKey;
cin>>cKey;
switch(cKey)
{
case 'L':
cout<<"Move left"<<endl; break;
case 'R':
cout<<"Move right"<<endl; break;
case 'D':
cout<<"Move Down"<<endl; break;
case 'U':
cout<<"Move Up"<<endl; break;
case 'B':
cout<<"Move back"<<endl; break;
case '1':
cout<<"Move upper left"<<endl; break;
case '2':
cout<<"Move upper right"<<endl; break;
case '3':
cout<<"Move lower left"<<endl; break;
case '4':
cout<<"Move lower right"<<endl; break;
default:
cout<<"Error"<<endl;
}
return 0;
}
2.5 小老鼠走迷宫2(switch语句)(延伸2)
#include <iostream>
using namespace std;
int main()
{
char cKey;
cin>>cKey;
if(cKey == 'O'){
cout<<"I did it!"<<endl;
}else
switch(cKey)
{
case 'L':
cout<<"Move left"<<endl; break;
case 'R':
cout<<"Move right"<<endl; break;
case 'D':
cout<<"Move Down"<<endl; break;
case 'U':
cout<<"Move Up"<<endl; break;
case 'B':
cout<<"Move back"<<endl; break;
default:
cout<<"Error"<<endl;
}
return 0;
}
3 循环结构
3.1 超市自助收银系统(while语句)
#include <iostream>
using namespace std;
int main()
{
float x, sum = 0.0;
cin>>x;
while(x!=0)
{
sum = sum + x;
cin>>x;
}
cout<<"The sum is "<<sum;
return 0;
}
3.2 我能成功登陆系统吗?(do-while语句)
#include <iostream>
using namespace std;
int main()
{
long pw;
int iCount = 0;
bool flag = false;
do{
cout<<"Please input password!"<<endl;
cin>>pw;
iCount++;
if(pw == 888888){
flag = true; break;
}
}while(iCount<3);
if(flag == true)
cout<<"Success!";
else
cout<<"Wrong Password!";
return 0;
}
3.3 超市自助收银系统(do-while语句)
#include <iostream>
using namespace std;
int main()
{
float x, sum = 0.0;
cin>>x;
do{
if(x==0) break;
sum = sum + x;
cin>>x;
}while(x!=0);
cout<<"The sum is "<<sum;
return 0;
}
3.4 我能成功登陆系统吗?(while语句)
#include <iostream>
using namespace std;
int main()
{
long pw;
int iCount = 0;
bool flag = false;
while(iCount<3){
cout<<"Please input password!"<<endl;
cin>>pw;
iCount++;
if(pw == 888888){
flag = true; break;
}
}
if(flag == true)
cout<<"Success!";
else
cout<<"Wrong Password!";
return 0;
}
3.5 古代神奇妙算(for语句)
#include <iostream>
using namespace std;
int main()
{
int num;
for(num = 1; num<1000; num++)
{
if(num%3==2 && num%5==3 && num%7== 4)
{
cout<<"num = "<<num<<endl;
break;
}
}
cout << "Hello world!" << endl;
return 0;
}
3.6 警察抓小偷(for语句)(多层循环)
#include <iostream>
using namespace std;
int main(){
int a,b,c,d;
for(a=1;a>=0;a--) //1:Yes 0:No
for(b=1;b>=0;b--) //1:Yes 0:No
for(c=1;c>=0;c--) //1:Yes 0:No
for(d=1;d>=0;d--) //1:Yes 0:No
if((a==0)+(c==1)+(d==1)+(d==0)==3&&a+b+c+d==1)
{
cout<<"A "<<(a?"Yes":"No")<<endl;
cout<<"B "<<(b?"Yes":"No")<<endl;
cout<<"C "<<(c?"Yes":"No")<<endl;
cout<<"D "<<(d?"Yes":"No")<<endl;
}
return 0;
}
3.7 古代神奇妙算(while语句)
#include <iostream>
using namespace std;
int main()
{
int num = 1;
while(num<1000)
{
if(num%3==2 && num%5==3 && num%7== 4)
{
cout<<"num = "<<num<<endl;
break;
}
num++;
}
return 0;
}
3.8 古代神奇妙算(do-while语句)
#include <iostream>
using namespace std;
int main()
{
int num = 1;
do{
if(num%3==2 && num%5==3 && num%7== 4)
{
cout<<"num = "<<num<<endl;
break;
}
num++;
}while(num<1000);
return 0;
}
3.9 迷宫游戏项目分解1
#include <iostream>
using namespace std;
int main()
{
int x=1,y=2,mx=10,my=9;
char ch;
cout<<"ready!\n";
while(true)
{
cout<<"draw!\n";
cin>>ch;
switch(ch)
{
case 'L':
case 'l':
y--;
cout<<"turn left:"<<x<<" "<<y<<endl;
break;
case 'R':
case 'r':
y++;
cout<<"turn right:"<<x<<" "<<y<<endl;
break;
case 'U':
case 'u':
x--;
cout<<"turn up:"<<x<<" "<<y<<endl;
break;
case 'D':
case 'd':
x++;
cout<<"turn down:"<<x<<" "<<y<<endl;
break;
default:
cout<<"press error:"<<x<<" "<<y<<endl;
}
if(mx==x && my==y) break;
}
cout<<"success!";
return 0;
}
3.10 贪吃蛇游戏1.0版本
#include <iostream>
using namespace std;
int main()
{
int x=1,y=2,mx=10,my=9;
char ch;
char flag;
cout<<"ready!\n";
while(true)
{
cout<<"draw!\n";
cin>>ch;
switch(ch)
{
case 'L':
case 'l':
if(flag=='R') continue;
y--;
cout<<"turn left:"<<x<<" "<<y<<endl;
flag = 'L';
break;
case 'R':
case 'r':
if(flag=='L') continue;
y++;
cout<<"turn right:"<<x<<" "<<y<<endl;
flag = 'R';
break;
case 'U':
case 'u':
if(flag=='D') continue;
x--;
cout<<"turn up:"<<x<<" "<<y<<endl;
flag = 'U';
break;
case 'D':
case 'd':
if(flag=='U') continue;
x++;
cout<<"turn down:"<<x<<" "<<y<<endl;
flag = 'D';
break;
default:
cout<<"press error:"<<x<<" "<<y<<endl;
}
if(mx==x && my==y) break;
}
cout<<"success!";
return 0;
}
标签:语句,std,cout,int,C++,概述,using,main,结构
From: https://www.cnblogs.com/yangyimingcode/p/17063766.html