码迷,mamicode.com
首页 > 其他好文 > 详细

实验2

时间:2019-03-26 13:20:06      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:length   个数   程序   assign   排序   数组元素   string   .com   代码   


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()
{
	int i = 1 , j = 2;
	double m = 3.2 , n = 4.6;
	Complex a , b;
	a.imaginary = 1000.8 , a.real = 888.8;
	b.imaginary = 2000.8 , b.real = 999.9;
	cout<<"请输入整型数值i , j的值"<<endl;
	cin>>i>>j;
	int k = add(i , j);
	cout<<"a + b =  "<<k<<endl;
	cout<<"请输入双精度数值m , n的值"<<endl;
	cin>>m>>n;
	double s = add(m , n);
	cout<<"m + n =  "<<s<<endl;
	cout<<"请输入复杂数据a的imaginary和real值:"<<endl;
	cin>>a.imaginary>>a.real;
	cout<<"请输入复杂数据b的imaginary和real值:"<<endl;
	cin>>b.imaginary>>b.real;
	//  cout<<"a + b = "<<endl;
	Complex c;
	c = add(a , b);
	cout<<"总和的imaginary为 "<<c.imaginary<<endl;
	cout<<"总和的real为 "<<c.real<<endl;
	return 0;
}

int add(int i , int j)
{
	int k = i + j;
	return k;
}

double add(double m , double n)
{
	double s;
	s = m + n;
	return s;
}

Complex add(Complex a , Complex b)
{
	Complex c;
	c.imaginary = a.imaginary + b.imaginary;
	c.real = a.real + b.real;
	return c;
}

  

技术图片

#include<iostream>
//  #include<vector>
using namespace std;

int quickSort(int iArray[],int left, int right)
{
    //快速排序之前先判断一下当前待排序数组元素个数是不是大于1 否则就没有必要排序
    if (left >= right)
    {
        //直接退出排序代码  没有必要进行排序了
        return 0; 
    }

    //开始进行快排算法
    //首先我们先保存left索引对应的数据  当前数据作为切割数组的轴
    int piovt = iArray[left];

    //定义临时变量保存数组2端的索引
    int leftIndex = left;
    int rightIndex = right;

    while (leftIndex < rightIndex)
    {
        //现在我们通过循环从右边开始搜索一个比轴值小的数据
        while (leftIndex < rightIndex)
        {
            //如果右边的数大于当前的参数轴值
            if (piovt <= iArray[rightIndex])
            {
                //右端索引指示器左移
                rightIndex--;
            }
            else
            {
                //说明我们右端出现比轴值更大的数据
                //这个时候我们就可以把这个更大的数据填充到索引轴索引对应的地方
                iArray[leftIndex] = iArray[rightIndex];
                leftIndex++;
                //我们需要跳出循环了当前工作完毕
                break;
            }
        }

        //从左边开始搜索一个比轴值更大的数填写上次留下的坑
        while (leftIndex < rightIndex)
        {
            //如果左边的数据小于轴值 我们索引指示器就往右走
            if (piovt >= iArray[leftIndex])
            {
                leftIndex++;
            }
            else
            {
                //说明我们在左端找到了比轴值更大的数据
                iArray[rightIndex] = iArray[leftIndex];
                rightIndex--;
                break;
            }
        }
    }

    iArray[leftIndex] = piovt;

    quickSort(iArray, left, leftIndex - 1);
    quickSort(iArray, rightIndex + 1, right);
	return 0;
}

void quickSort(char iArray[],int left, int right)
{
    //快速排序之前先判断一下当前待排序数组元素个数是不是大于1 否则就没有必要排序
    if (left >= right)
    {
        //直接退出排序代码  没有必要进行排序了
        return; 
    }

    //开始进行快排算法
    //首先我们先保存left索引对应的数据  当前数据作为切割数组的轴
    char piovt = iArray[left];

    //定义临时变量保存数组2端的索引
    char leftIndex = left;
    char rightIndex = right;

    while (leftIndex < rightIndex)
    {
        //现在我们通过循环从右边开始搜索一个比轴值小的数据
        while (leftIndex < rightIndex)
        {
            //如果右边的数大于当前的参数轴值
            if (piovt <= iArray[rightIndex])
            {
                //右端索引指示器左移
                rightIndex--;
            }
            else
            {
                //说明我们右端出现比轴值更大的数据
                //这个时候我们就可以把这个更大的数据填充到索引轴索引对应的地方
                iArray[leftIndex] = iArray[rightIndex];
                leftIndex++;
                //我们需要跳出循环了当前工作完毕
                break;
            }
        }

        //从左边开始搜索一个比轴值更大的数填写上次留下的坑
        while (leftIndex < rightIndex)
        {
            //如果左边的数据小于轴值 我们索引指示器就往右走
            if (piovt >= iArray[leftIndex])
            {
                leftIndex++;
            }
            else
            {
                //说明我们在左端找到了比轴值更大的数据
                iArray[rightIndex] = iArray[leftIndex];
                rightIndex--;
                break;
            }
        }
    }

    iArray[leftIndex] = piovt;

    quickSort(iArray, left, leftIndex - 1);
    quickSort(iArray, rightIndex + 1, right);
}

int main()
{
	int a[10];
	char b[10];
	cout<<"请输入10个整型数 "<<endl;
	for(int i = 0 ; i < 10 ; i++)
		cin>>a[i];
	cout<<"未排序之前这十个数为: "<<endl;
	for(i = 0 ; i < 10 ; i++)
		cout<<a[i]<<" ";
	cout<<endl;
	quickSort(a , 0 , 9);
	cout<<"快速排序之后这十个数为: "<<endl;
	for(i = 0 ; i < 10 ; i++)
		cout<<a[i]<<" ";
	cout<<endl;
	cout<<"请输入10个字符 "<<endl;
	for( i = 0 ; i < 10 ; i++)
		cin>>b[i];
	cout<<"未排序之前这十个字符为: "<<endl;
	for(i = 0 ; i < 10 ; i++)
		cout<<b[i]<<" ";
	cout<<endl;
	quickSort(b , 0 , 9);
	cout<<"快速排序之后这十个字符为: "<<endl;
	for(i = 0 ; i < 10 ; i++)
		cout<<b[i]<<" ";

	return 0;
}

  技术图片

 

程序三 基础篇

#include<iostream>
#include<cstring>
#include<string>
using namespace std;

class User
{
public:
    void setInfo(string name1 , string passwd1 , string email1);
    int setInfo(string name1);
    void changePasswd();
    void printInfo();
private:
    string name;
    string passwd;
    string email;
};

void User::setInfo(string name1 , string passwd1 , string email1)
{
    name.assign(name1);
    passwd.assign(passwd1);
    email.assign(email1);
}

int User::setInfo(string name1)    
{
    name.assign(name1);
    email.assign("");
    passwd.assign("123456");
    return 0;
}


void User::changePasswd()
{
    string old_ps;
    string passwd1;
    cout<<"请输入你原来的密码: "<<endl;
    cin>>old_ps;
    int n = 1;
    while(old_ps != passwd)
    {
        if(n < 3)
        {
            int i = 3 - n;
            cout<<"请继续输入密码,您还有"<<i<<"次验证机会"<<endl;
            cin>>old_ps;
            n++;
        }
        else
        {
            cout<<"请稍后再试,暂时退出修改密码程序"<<endl;
            return;
        }
    }
    cout<<"请输入你新的密码: "<<endl;
    cin>>passwd1;
    passwd.assign(passwd1);
    return;
}

void User::printInfo()
{
    cout<<"用户名为: "<<name<<endl;
    cout<<"密码为: "<<"******"<<endl;
    cout<<"邮箱为: "<<email<<endl;
    return;
}

int main()
{
    cout<<"testing 1......"<<endl;
    User user1;
    user1.setInfo("James");
    user1.printInfo();
    user1.changePasswd();
    user1.printInfo();

    cout<<endl<<"testing 2......"<<endl<<endl;
    User user2;
    user2.setInfo("Frank" , "457281" , "172636271.qq.com");
    user2.printInfo();

    return 0;
}

 

技术图片

 

拓展篇

#include<iostream>
#include<cstring>
#include<string>
#include<conio.h>
#include <cctype>
using namespace std;

string get()
{
char c;
int i=0;
string pas;
while ((c=getch())!=\r)
{
if (c==\b) { //处理退格键
putch(c);
putch( );
putch(c);
if (!pas.empty()) pas=pas.erase(pas.size()-1,1);
}
else if (isprint(c)){ //只用可显示字符做密码
putch(*);
pas+=c;
}
}
cout<<endl;
return pas;
}

class User
{
public:
    void setInfo(string name1 , string passwd1 , string email1);
    int setInfo(string name1);
    void changePasswd();
    void printInfo();
    void changeEmail();
private:
    string name;
    string passwd;
    string email;
};


void User::setInfo(string name1 , string passwd1 , string email1)
{
    name.assign(name1);
    passwd.assign(passwd1);
    email.assign(email1);
}

int User::setInfo(string name1)    
{
    name.assign(name1);
    passwd.assign("123456");
    email.assign("");
    return 0;
}


void User::changePasswd()
{
    string old_ps;
    string passwd1;
    cout<<"请输入你原来的密码: "<<endl;
    old_ps = get();
    int n = 1;
    while(old_ps != passwd)
    {
        if(n < 3)
        {
            old_ps.assign("");
            int i = 3 - n;
            cout<<"请继续输入密码,您还有"<<i<<"次验证机会"<<endl;
            old_ps = get();
        }
        else
        {
            cout<<"请稍后再试,暂时退出修改密码程序"<<endl;
            return;
        }
    }
    bool judge = true;
    while(judge)
    {
    cout<<"请输入你新的密码: "<<endl;
    passwd1 = get();
    cout<<endl;
    if (passwd1.length() > 5)
    {
        passwd.assign(passwd1);
        return;
    }
    else
    {
        cout<<"您所设置的新密码长度过小,请重新设置 "<<endl;
    }
    }
}

void User::printInfo()
{
    cout<<"用户名为: "<<name<<endl;
    cout<<"密码为: "<<"******"<<endl;
    cout<<"邮箱为: "<<email<<endl;
    return;
}

void User::changeEmail()
{
    string old_ps;
    string::size_type idx;
    string email1;
    string judge = "@";
    while(true)
    {
    cout<<"请输入你新的邮箱: "<<endl;
    cin>>email1;
    idx=email1.find(judge);
    if(idx == string::npos )
    {
        cout<<"您输入的新邮箱不符合规则 :"<<endl;
    }      
    else
    {
        email.assign(email1);
        break;
    }
    }
}

int main()
{
    cout<<"testing 1......"<<endl;
    User user1;
    user1.setInfo("James");
    user1.printInfo();
    user1.changePasswd();
    user1.changeEmail();
    user1.printInfo();

    cout<<endl<<"testing 2......"<<endl<<endl;
    User user2;
    user2.setInfo("Frank" , "457281" , "172636271.qq.com");
    user2.printInfo();

    return 0;
}

技术图片

 

实验2

标签:length   个数   程序   assign   排序   数组元素   string   .com   代码   

原文地址:https://www.cnblogs.com/jzgjzg/p/10599286.html

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