标签:编程练习 操作 算法 hang class testing .com 有用 tar
实验结论
1.函数重载编程练习:
编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main( )函数中定义不同类型 数据,调用测试。
1 #include <iostream> 2 using namespace std; 3 4 struct Complex{ 5 double real; 6 double imaginary; 7 }; 8 int add(int,int); 9 double add(double,double); 10 Complex add(Complex,Complex); 11 int main(){ 12 int a1,a2,asum; 13 double b1,b2,bsum; 14 Complex c1,c2,csum; 15 cout<<"please enter two integers:"; 16 cin>>a1>>a2; 17 asum=add(a1,a2); 18 cout<<a1<<"+"<<a2<<"="<<asum<<endl; 19 cout<<"please enter two real numbers:"; 20 cin>>b1>>b2; 21 bsum=add(b1,b2); 22 cout<<b1<<"+"<<b2<<"="<<bsum<<endl; 23 cout<<"please enter two plurals:"; 24 cin>>c1.real>>c1.imaginary>>c2.real>>c2.imaginary; 25 cout<<"c1"<<"="<<c1.real<<"+"<<c1.imaginary<<"i"<<endl; 26 cout<<"c2"<<"="<<c2.real<<"+"<<c2.imaginary<<"i"<<endl; 27 csum=add(c1,c2); 28 cout<<"c1+c2"<<"="<<csum.real<<"+"<<csum.imaginary<<"i"<<endl; 29 return 0; 30 } 31 int add(int a1,int a2){ 32 int asum; 33 asum=a1+a2; 34 return asum; 35 } 36 double add(double b1,double b2){ 37 double bsum; 38 bsum=b1+b2; 39 return bsum; 40 } 41 Complex add(Complex c1,Complex c2){ 42 Complex csum; 43 csum.real=c1.real+c2.real; 44 csum.imaginary=c1.imaginary+c2.imaginary; 45 return csum; 46 }
运行截图
2.函数模板编程练习
编写实现快速排序函数模板,并在main( )函数中,定义不同类型数据,调用测试。(算法可参考这里, 内有排序示意图及算法逻辑)
参考了网上许多快速排序法的程序,此程序参考(https://www.cnblogs.com/miracleswgm/p/9199124.html)
1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 void Quicksort(int num[],int left,int right); 5 6 int main(){ 7 int i; 8 int num[10]={9,8,7,6,5,4,3,2,1,78}; 9 Quicksort(num,0,9); 10 for(i=0;i<=9;i++) 11 cout<<num[i]<<setw(3); 12 cout<<endl; 13 return 0; 14 } 15 16 int partition(int num[], int left, int right) 17 { 18 int i = left + 1 ; 19 int j = right; 20 int temp = num[left]; 21 22 while(i <= j) 23 { 24 while (num[i] < temp) 25 { 26 i++; 27 } 28 while (num[j] > temp ) 29 { 30 j--; 31 } 32 if (i < j) 33 swap(num[i++], num[j--]); 34 else i++; 35 } 36 swap(num[j], num[left]); 37 return j; 38 39 } 40 41 void Quicksort(int num[],int left,int right) 42 { 43 int j; 44 if (left>right) 45 return; 46 j = partition(num, left, right); 47 Quicksort(num, left, j - 1); 48 Quicksort(num, j + 1, right); 49 }
运行截图
3.类的定义、实现和使用编程练习
设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下:
每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。
支持设置用户信息setInfo( )。允许设置信息时密码默认为6个1,联系邮箱默认为空串。
支持打印用户信息printInfo( )。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。
支持修改密码changePasswd( ),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。
在main( )函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信息)
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 // User类的声明 5 class User 6 { 7 public: 8 void setInfo(string name0,string passwd0="111111",string email0=""); 9 string getname(); 10 string getpasswd(); 11 string getemail(); 12 void changePasswd(); 13 void printInfo(); 14 private: 15 string name; 16 string passwd; 17 string email; 18 }; 19 void User::setInfo(string name0,string passwd0,string email0){ 20 name = name0; 21 passwd = passwd0; 22 email = email0; 23 } 24 string User::getname(){ 25 return name; 26 } 27 string User::getpasswd(){ 28 return passwd; 29 } 30 string User::getemail(){ 31 return email; 32 } 33 void User::changePasswd(){ 34 int i=3; 35 string oldpasswd,newpasswd; 36 while(i){ 37 cout<<"please enter the old passwd:"; 38 cin>>oldpasswd; 39 if(passwd== oldpasswd) 40 { cout<<"please enter a new pass word:"; 41 cin>>newpasswd; 42 passwd=newpasswd; 43 cout<<"change pass word successfully"<<endl; 44 break;} 45 cout<<"passwdb input error,please re-enter again:"; 46 cin>>oldpasswd; 47 } 48 if(i==0) 49 { 50 cout<<"you are wrong,please try after a while"; 51 } 52 } 53 54 void User::printInfo(){ 55 cout << "name: " << name << endl; 56 cout << "passwd: " << "******" << endl; 57 cout << "email: " << email<<endl; 58 } 59 60 61 int main() { 62 cout << "testing 1......" << endl; 63 User user1; 64 user1.setInfo("Leonard"); 65 user1.printInfo(); 66 user1.changePasswd(); 67 user1.printInfo(); 68 cout << endl << "testing 2......" << endl << endl; 69 User user2; 70 user2.setInfo("Jonny","92197","xyz@hotmail.com"); 71 user2.printInfo(); 72 return 0; }
运行截图
实验总结与体会
1.结构体相关的知识还需强化
2.类相关的知识还是很薄弱的
实验一评论链接:
https://www.cnblogs.com/joey-yan/p/10543990.html#4213143
https://www.cnblogs.com/lovecpp/p/10520456.html#4211900
https://www.cnblogs.com/qiuqiuwr/p/10525885.html#4211895
标签:编程练习 操作 算法 hang class testing .com 有用 tar
原文地址:https://www.cnblogs.com/dadadacy/p/10597778.html