【项目2】分段函数求值
#include <iostream>
using namespace std;
int main( )
{
int x,y;
cout<<"请输入一个数:";
cin>>x;
if(x>=1)
y=x-1;
else
y=-x+1;
cout<<y<<endl;
return 0;
}
【项目3】两点距离
#include <iostream>
#include<cmath>
using namespace std;
int main( )
{
double x1,y1,x2,y2,d;
cout<<"请输入两个点的坐标:";
cin>>x1>>y1>>x2>>y2;
d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
cout<<"两点之间的距离为:"<<d<<endl;
return 0;
}【项目4】模拟ATM
#include<iostream>
using namespace std;
int main()
{
int password,x;
cout << "曾氏银行欢迎您!" << endl;
cout << "请输入密码:";
cin >> password;
if(password==123456)
{
cout<< "1.查询" << "\n";
cout<< "2.取款" << "\n";
cout<< "3.存款" << "\n";
cout<< "4.转账" << "\n";
cout<< "0.退出" << "\n";
cout<< "请输入功能选择:";
cin>> x;
if (x>=0&&x<=4)
cout <<"谢谢,您选择了"<<x<<"号功能" << endl;
else
cout<<"输入选择有误!"<<endl;
}
else
cout<<"密码错误,请重新输入!"<<endl;
return 0;
}
本文包含三个C++程序实例:分段函数求值、计算两点间距离及模拟ATM操作流程。通过这些实例,读者可以了解如何使用条件语句、数学函数及简单的用户交互。
1万+

被折叠的 条评论
为什么被折叠?



