码迷,mamicode.com
首页 > 编程语言 > 详细

c++实验二

时间:2019-03-19 01:15:45      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:use   clu   turn   int end   实验   图片   real   new   quic   

1、函数重载编程练习

#include<iostream>
using namespace std;
struct Complex {
double real;
double imaginary;
};
int add(int, int);
double add(double,double);
Complex add(Complex, Complex);
int main() {
Complex x={2.1,5.2};
Complex y={3.1,5.1};
Complex c=add(x,y);
cout<<"2+5的和是"<<add(2,5)<<endl;
cout<<"2.4+5.1的和是"<<add(2.4,5.1)<<endl;
cout<<"Complex中xy的和是"<<c.real<<","<<c.imaginary<<endl;
return 0;
}
int add(int x,int y){
return x+y;
}
double add(double x,double y){
return x+y;
}
Complex add(Complex x,Complex y){
Complex sum;
sum.imaginary=x.imaginary+y.imaginary;
sum.real=x.real+y.real;
return sum;
}

截图:

技术图片

2、函数模板编程练习

#include<iostream>
using namespace std;
template<class T>
T partition(T arr[], int low, int high){
T key;
key = arr[low];
while(low<high){
while(low <high && arr[high]>= key )
high--;
if(low<high)
arr[low++] = arr[high];
while( low<high && arr[low]<=key )
low++;
if(low<high)
arr[high--] = arr[low];
}
arr[low] = key;
return low;
}
template<class T>
void quick_sort(T arr[], int start, int end){
int pos;
if (start<end){
pos = partition(arr, start, end);
quick_sort(arr,start,pos-1);
quick_sort(arr,pos+1,end);
}
}

int main(){
int i;
int arr[6]={32,12,7, 78, 23,45};
double arrs[9]={32.1,12.8,12.3,11.9,7.5,78.1,23.4,45.5,1.1};
printf("排序前\n");
for(i=0;i<6;i++)
cout<<arr[i]<<" ";

quick_sort(arr,0,5);

printf("\n排序后\n");
for(i=0; i<6; i++)
cout<<arr[i]<<" ";

printf("\n\n排序前\n");
for(i=0;i<9;i++)
cout<<arrs[i]<<" ";

quick_sort(arrs,0,8);

printf("\n排序后\n");
for(i=0; i<9; i++)
cout<<arrs[i]<<" ";
return 0;
}

截图:

技术图片

3、类的定义、实现和使用编程练习

#include <iostream>
#include <string>
using namespace std;
class User {
public:
User(){};
void setInfo(string names,string passwds="111111",string emails="");
void changePasswd();
void printInfo();
~User(){}
private:
string name;
string passwd;
string email;
};
void User::setInfo(string names,string passwds,string emails){
name=names;
passwd=passwds;
email=emails;
}
void User::changePasswd(){
cout<<"Enter the old passwd:";
string pass;
int count=0;
cin>>pass;
while(pass!=passwd){
cout<<"passwd input error,Please re-Enter again:";
cin>>pass;
count++;
if(count==2){
cout<<"Please try after a while"<<endl;
exit(0);
}
}
cout<<"Please sign in new passwd:";
cin>>pass;
passwd=pass;
}
void User::printInfo(){
cout<<"name:"<<name<<endl;
cout<<"passwd:******"<<endl;
cout<<"email:"<<email<<endl;
}
int main() {
cout << "testing 1......" << endl;
User user1;
user1.setInfo("Leonard");
user1.printInfo();
user1.changePasswd();
user1.printInfo();
cout << endl << "testing 2......" << endl << endl;
User user2;
user2.setInfo("Ben","12306","ben@hotmail.com");
user2.printInfo();
return 0;
}

截图:

技术图片

 

c++实验二

标签:use   clu   turn   int end   实验   图片   real   new   quic   

原文地址:https://www.cnblogs.com/msag/p/10556207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!