06 数组2字符数组程序设计

文章展示了C++编程中的几个实例,包括输入学生ID进行搜索、删除指定ID、检查字符是否对称以及不同进制之间的转换。
摘要由CSDN通过智能技术生成

温馨提示:请做完作业后再核对答案

第1题

答案:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    string IDs[100],x;
    int xuehao,n=0,fenshu[100],i=0,j=0,y=0;
    bool find=false;
    cout<<"Enter the student IDs (-1 to stop)"<<endl;
    cin>>IDs[0];
    while(IDs[n]!="-1"){
        n+=1;
        cin>>IDs[n];
    }// n为学生人数
    cout<<"Now enter the test result for each student"<<endl;
    for(j=0;j<n;j++){
        cout<<"Enter the result for "<<IDs[j]<<":";
        cin>>fenshu[j];
    }
    cout<<"Searching for a student - enter an ID ";
    cin>>x;
    for(i=0;i<n;i++)
    {if (IDs[i]==x){find=true;y=fenshu[i];}
    }
    if(find==true)
    {cout<<"Student "<<x<<" has result "<<y<<endl;}
    else{cout<<"Student "<<x<<" not found"<<endl;}
    return 0;
}

第2题

答案:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    string IDs[100],x;
    int xuehao,n=0,i=0,j=0;bool find=false;
    cout<<"Enter five student IDs in the correct order"<<endl;
    for(n=0;n<5;n++){
    cout<<"Enter ID number "<<n+1<<":";
    cin>>IDs[n];
    }// n为学生人数,最终值为5
    cout<<"Which student ID do you want to delete?";
    cin>>x;
    for(i=0;i<n;i++){
        if (IDs[i]==x){find=true;break;}
        }if (find==false){
        cout<<"ID "<<x<<" is not in the array"<<endl;
        cout<<endl;
        cout<<"Here is the full list of ID numbers"<<endl;
        for(j=0;j<n;j++){cout<<IDs[j]<<"  ";}
    }
    else {cout<<endl;
        cout<<"Here is the full list of ID numbers"<<endl;
        for(j=0;j<n;j++)
        {if (j>=i){IDs[j]=IDs[j+1];}
        }
    for(j=0;j<n-1;j++){cout<<IDs[j]<<"  ";}
    }
    return 0;
}

第3题

答案
#include<iostream>
#include<cstring>
using namespace std;
int main() {
	char a[100];
	cin >> a;
	int i=0,l,k=1;
	l = strlen(a);
        for(i=0;i<l;i++){
	    if (a[i]!=a[l-i-1])
	        {k=0;break;}
	    }
	if (k==1) 
        {cout << "yes" << endl;}
	else {cout << "no" << endl;}
	return 0;
}

第4题

答案:
#include<iostream>
using namespace std;
int main(){
    char c[256],x[256];
    cin.getline(c,255);
    //如果写cin>>c;没空格可以,有空格就不行了
    int i,j=0;
    for(i=0;i<256;i++){
        if (c[i]!=' ' && c[i]!='\t')
        // if (c[i]!=32 && c[i]!=9)也可以
        // 32和9分别是空格和tab的ASCII码值
            {
            x[j]=c[i];j++;
            }
    }
    cout<<x;
    return 0;
}

第5题

答案:
#include<iostream>
using namespace std;
int main(){
    int a[11],i,x,j;
    cout<<"Enter the array:";
    for(i=0;i<10;i++){cin>>a[i];}
    cout<<"Enter a new data:";
    cin>>x;
    if(x<=a[0])
    {for(i=10;i>0;i--){a[i]=a[i-1];}
        a[0]=x;}
    else if(x>=a[9]){a[10]=x;}
    else{
    for (i=9;i>0;i--){
        if (x<=a[i] && x>a[i-1]){
            for(j=9;j>=i;j--){a[j+1]=a[j];}
            a[i]=x;}
        }
    }
    for(i=0;i<11;i++){cout<<a[i]<<" ";}
    return 0;
}

第6题

答案:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int j = 0, sum = 0, i = 0,l1;
	char str[50];
	cin>>str;
	l1=strlen(str);//strlen用到了<cstring>库
	for (i=0;i<l1;i++){
	    if (str[i]>='0' && str[i]<='9') 
		{j=str[i]-'0';}
		if (str[i] >= 'A'&& str[i] <= 'F')
		{j=str[i]-'A'+10;}
		sum=sum*16+j;
	}
	cout<<sum<<endl;
	return 0;
}

第7题

答案:
#include<iostream>
using namespace std;
int main(){
    char c[256],t;
    cin.getline(c,255);
    //cin>>c;没空格可以,有空格就不行了
    int i;
    for(i=0;i<256;i++){
        if (c[i]>=65 && c[i]<=90){
            c[i]+=32;
        }
        else if (c[i]>=97 && c[i]<=122){
            c[i]-=32;
        }else{;}
    }
    cout<<c;
}

第8题

答案:
#include<iostream>
using namespace std;
int main(){
    int x,n,i=0,j;
    char a[256];
    cout<<"Enter a decimal value:";
    cin>>x;
    cout<<"Convert to:";
    cin>>n;cout<<"After converting:";
    if (x==0) {cout<<'0';}
    else {
        if (n==2 || n==8 || n==16) {
            while(x) {
                if(x%n<10){a[i]=x%n+'0';}
                else {a[i]=x%n-10+'A';}
                x=x/n;
                i+=1;
                }
            for(j=i-1;j>=0;j--){cout<<a[j];}
            
        }
    }
    if (n==2) {cout<<'B';}
    else if (n==8) {cout<<'Q';}
    else if (n==16) {cout<<'H';}
    return 0 ;
}

代码是自己写的,所以不免有错误或不简便的地方,欢迎大佬们批评指正 ~   .゚ヽ(。◕‿◕。)ノ゚

祝大家学业进步 ~

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值