c++实验二
1
#include<iostream>
using namespace std;
struct Complex {
double x;
double y;
};
int add(int, int);
double add(double,double);
Complex add(Complex, Complex);
int main() {
Complex x={1,2};
Complex y={1.1,2.2};
Complex c=add(x,y);
cout<<"1+1="<<add(1,1)<<endl;
cout<<"2.0+5.1="<<add(2.0,5.1)<<endl;
cout<<"Complex中xy的和为"<<c.x<<","<<c.y<<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.y=x.y+y.y;
sum.x=x.x+y.x;
return sum;
}
截图:
2
#include<iostream>
using namespace std;
void sort(int a[],int,int);
int main()
{
int a[]={34,65,12,43,67,5,78,10,3,70},k;
int len=sizeof(a)/sizeof(int);
cout<<"The orginal array:"<<endl;
for(k=0;k<len;k++)
cout<<a[k]<<",";
cout<<endl;
sort(a,0,len-1);
cout<<"after sorting:"<<endl;
for(k=0;k<len;k++)
cout<<a[k]<<",";
cout<<endl;
system("pause");
return 0;
}
void sort(int s[], int l, int r)
{
if (l< r)
{
int i = l, j = r, x = s[l];
while (i < j)
{
while(i < j && s[j]>= x)
j--;
if(i < j)
s[i++] = s[j];
while(i < j && s[i]< x)
i++;
if(i < j)
s[j--] = s[i];
}
s[i] = x;
sort(s, l, i - 1);
sort(s, i + 1, r);
}
}
截图:
3
#include<iostream>
#include <string>
using namespace std;
class user{
public:
void setinfo(string name0,string passwd0="111111",string email0=" ");
void printinfo();
void changepasswd();
private:
string name;
string passwd;
string email;
};
void user::setinfo(string name0,string passwd0,string email0){
name=name0;
passwd=passwd0;
email=email0;
}
void user::printinfo(){
cout<<"name: "<<name<<endl;
cout<<"passwd:"<<"******"<<endl;
cout<<"email: "<<email<<endl;
}
void user::changepasswd(){
cout<<"please enter the old passward:";
int n=3;
string passwd1;
while(n--){
cin>>passwd1;
if(passwd1==passwd)break;
else
cout<<"passwd input error,please re-enter again:";
}
if(!n)cout<<"please try after a while.";
}
int main(){
cout<<"testing 1..."<<endl;
user user1;
user1.setinfo("Leonard");
user1.printinfo();
user1.changepasswd();
user1.printinfo();
cout<<endl;
cout<<"testing 2..."<<endl;
user user2;
user2.setinfo("Jonny","92197","xyz@hotmail.com");
user2.printinfo();
return 0;
}
截图:
总结:快速排序一直搞不懂,上网查了好久,看了好多不一样的,最终得出自己的方法,类的作业太长太繁杂,以前从来没写过这么长的程序,但定下心来还是可以慢慢写出来的,总之还是基础不够牢固,一些基本的语句不会写,导致作业交的很慢,还是的多多练习,
老师说语句要写的好得多看别人写的好语句,今后一定多多向好同学学习!
评价:
https://www.cnblogs.com/cwj0505/p/10588248.html
https://www.cnblogs.com/wyf-blogs/p/10587306.html
https://www.cnblogs.com/jessi-wu1005/p/10588322.html