C++学习记录NO.7

#指针

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

char *stringconnect(char *a,char *b)
{
    while(*a!=0)a++;
    for(;*b!=0;b++,a++)
    {*a=*b;}
    *a=0;
    return a;
}
int main()
{
    char a[10],b[10];
    cin>>a>>b;
    stringconnect(a,b);
    cout<<a<<endl;
    return 0;
}

#include <iostream>
using namespace std;

void swap(int *a, int *b)
{
    int temp;
        temp = *a;     //通过使用中间变量将两数组中的值交换
        *a= *b;
        *b= temp;
    }

    int main()
    {
        int a,b;
        cin>>a>>b;
        swap(a,b);
        cout<<a<<" "<<b<<endl;
        return 0;
    }

#include<stdio.h>
#define N 1024
int strlen(char *str){
    int i=0;
    //字符串结尾为‘\0’
    while (*str!='\0')
    {
        i++;
        str++;
        //指针右移
        }
    return i;
}
 
 
int main(){
    char str[N];
    gets(str);
    printf("%d",strlen(str));
    return 0;
}

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char s[300];
    cin >> s;
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        if(s[i] == 'A') cout << "T";
        else if(s[i] == 'T') cout << "A";
        else if(s[i] == 'C') cout << "G";
        else if(s[i] == 'G') cout << "C";
    }
    cout << endl;
    return 0;
}

#include <iostream>
#include <string.h>
using namespace std;
void swap(int *a,int *b)
{int t=*a;*a=*b;*b=t;}
void sort(int *a,int *b,int *c)
{
    if(*a>*b)swap(*a,*b);
    if(*a>*c)swap(*a,*c);
    if(*b>*c)swap(*b,*c);
}
int main()
{
    int x,y,z;
    cin>>x>>y>>z;
    sort(&x,&y,&z);
    cout<<x<<" "<<y<<" "<<z<<endl;
    return 0;
}

#include <stdio.h>
#include <iostream>
using namespace std;
int a[20];
int n;
int count=0;
void swap(int *a,int *b)
{int temp;temp=*a;*a=*b;*b=temp;}
void perm(int list[20],int k,int m)
{
    if(k==m)
    {int i; 
    for (i = 0 ; i<=m; i++)
    {printf("%4d", list[i]);}
    printf("\n");
    return;}
    
    for(int i=k;i<=m;i++)
    {
        swap(&list[k],&list[i]);  //交换位置
        perm(list,k+1,m);        //递归调用
        swap(&list[k],&list[i]);
    }
}
int main() {
    int n;
    cin>>n;
    int a[20];
    for(int i=0;i<n;i++){a[i] = i+1;}
    perm(a,0,n-1);
        return 0;
}

#include <iostream>
using namespace std;
int main()
{
    const char* month[13]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int num[100];
    int i= 0;
    while (cin >> num[i])
    i++;
    for (int j= 0; j< i; j++)
    {cout << month[num[j]-1] << endl;}
    return 0;
}

#include<iostream>
#include<string.h>
using namespace std;
void swap(char *a,char *b);
void sort(char *a,char *b,char *c);
int main()
{
    char x[255],y[255],z[255];
    cin>>x>>y>>z;
    sort(x,y,z);
    cout<<x<<endl;
    cout<<y<<endl;
    cout<<z<<endl;
    return 0 ;
}
void swap(char*a,char*b)
{char t[255];strcpy(t,a);strcpy(a,b);strcpy(b,t);}
void sort(char *a,char*b,char *c)
{
    if(strcmp(a,b)>0)swap(a,b);
    if(strcmp(a,c)>0)swap(a,c);
    if(strcmp(b,c)>0)swap(b,c);}

#include <iostream>
#include <string.h>
using namespace std;
void inputdata(int a[],int n)
{for(int i=0;i<n;i++)
    cin>>a[i];
    }
void outputdata(int a[],int n)
{
    for(int i=0;i<n;i++)
        cout<<a[i]<<" ";
}
void swapdata(int a[],int n)
{
    //选择最小的数与第一个数对换
    int *min=a;
    for(int *p=a;p<a+n;p++)
    {
        if(*min>*p)min=p;
    }
    int t=*min;*min=*a;*a=t;
    //选择最大的数与最后一个数对换
    int *max=a;
    for(int *p=a;p<a+n;p++)
    {
        if(*max<*p)max=p;
    }
    t=*max;*max=*(a+n-1);*(a+n-1)=t;
}
int main()
{
    int a[10];
    inputdata(a,10);
    swapdata(a,10);
    outputdata(a,10);
    return 0;
}

#include <iostream>
#include<string.h>
using namespace std;
int strCompare(string str1, string str2) {
    int i = 0;
    while (str1[i] != '\0' && str2[i] != '\0' && str1[i] == str2[i]) {
        if (str1[i] == 10)
        {break;}
        i++;
    }
    return str1[i] - str2[i];
}

int main() {
    string str1,str2;
    getline(cin,str1);
    getline(cin,str2);
    int result = strCompare(str1, str2);
    cout << result << endl;
    return 0;
}

  • 21
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值