大一下 c + + 上机实验总结(十四)

本文包含多个C++编程实例,包括字符串操作、学生信息输入输出、矩阵操作、数组排序及字符串处理等,通过具体代码展示如何实现特定功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大一下c + +上机实验总目录:大一下c + +上机实验总结目录


1、阅读程序,其中s::connect()函数实现字符串连接。把这个成员函数改写为重载+运算符函数,并修改main函数的对应代码,使其正确运行。

#include <iostream>
#include<cstring>
using namespace std;
class s
{  public:
      s() { *str = '\0'; len = 0; }
      s( char *pstr )
      {  strcpy( str,pstr );  len = strlen(pstr);  }
      char *gets() {  return str;  }
      int getLen() {  return len;  }
      s connect( s obj );						//字符串连接函数声明
   private:
      char str[100];
      int len;
};
s s::connect( s obj )							//字符串连接函数定义
{  strcat( str,obj.str );
   return str; 
}
int main()
{  s obj1( "Visual" ), obj2( " C++" ), obj3(" language");
   obj3 = (obj1.connect(obj2)).connect(obj3);		//调用字符串连接函数
   cout << "obj3.str = "<<obj3.gets() << endl;
   cout<<"obj3.len = "<<obj3.getLen()<<endl;
   return 0;
}

【解答】


#include <iostream>
#include<cstring>
using namespace std;
class s
{  public:
		s() 
		{ *str = '\0'; len = 0; }
		s( char *pstr )
		{  strcpy( str,pstr );
		   len = strlen(pstr);
		}
		char *gets()
		{  return str; }
		int getLen()
		{  return len; }
		s operator+( s obj );
  private:
	   char str[100];
	   int len;
};
s s::operator+( s obj )
{
	 strcat( str,obj.str );
	 return str; 
}	
int main()
{
	s obj1( "Visual" ), obj2( " C++" ), obj3(" language");
	obj3 = obj1 + obj2 + obj3;
	cout << "obj3.str = "<<obj3.gets() << endl;
	cout<<"obj3.len = "<<obj3.getLen()<<endl;
        return 0;
}

2、改写下述程序中的student类,用重载运算符>>函数代替input函数;用重载运算符<<函数代替output函数;并修改main函数,使其得到正确的运行结果。

#include <iostream>
using namespace std;
class student
{     char name[20];
      unsigned id;
      double score;
   public:
      student(char s[20]="\0", unsigned k=0, double t=0)
      {  strcpy(name,s);
         id=k;
         score=t;
      }
      void input()
      {  cout<<"name? ";
         cin>>name;
         cout<<"id? ";
         cin>>id;
         cout<<"score? ";
         cin>>score;
      }
      void output()
      {  cout<<"name: "<<name<<"\tid: "<<id<<"\tscore: "<<score<<endl;  }
};
int main()
{  student s;
   s.input();
   s.output();
   return 0;
}

【解答】

#include <iostream>
using namespace std;
class student
{ 
	char name[20];
	unsigned id;
	double score;
	public:
		student(char s[20]="\0", unsigned k=0, double t=0)
		{
			strcpy(name,s);
			id=k;
			score=t;
		}
		friend ostream & operator<<(ostream &, const student &);
		friend istream & operator>>(istream &, student &);
};
ostream & operator<<(ostream &out, const student &s)
{
	  out<<"name: "<<s.name<<"\tid: "<<s.id<<"\tscore: "<<s.score<<endl;
          return out;
}
istream & operator>>(istream &in, student &s)
{
cout<<"name? ";
in>>s.name;
cout<<"id? ";
in>>s.id;
cout<<"score? ";
in>>s.score;
return in;
}
int main()
{
	student s;
	cin>>s;
	cout<<s;
        return 0;
}

3、建立一个矩阵类Array,存储一个4×4的矩阵,能找出矩阵中最大值元素并计算矩阵中数据的平均值数。
参考答案:

#include <iostream>
using namespace std;
class Array							
{
	int a[4][4];						
public:
	void set()							
	{
		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++)
				cin>>a[i][j];
	}
	int max();
	double ave();
};
double Array::ave()					
{
	double sum=0;
	for(int i=0;i<4;i++)				
		for(int j=0;j<4;j++)			
			sum=sum+a[i][j];			
	return sum/16;					
}
int Array::max()
{
	int maxv=a[0][0];					
	for(int i=0;i<4;i++)				
		for(int j=0;j<4;j++)			
			if(a[i][j]>maxv)			
				maxv=a[i][j];
	return maxv;					
}
int main()
{
	Array a;							
	a.set();							
	cout<<a.ave()<<endl;				
	cout<<a.max()<<endl;				
	return 0;
}

4、编写程序,将数组A中的n个数据从小到大写入数组B中,数据类型可以是整型、单精度型、双精度型。用重载函数实现。

#include<iostream.h>
void sort_write(int *a,int *b,int n)
{int t; 
 for(i=0;i<n;i++)//对数组a进行选择法排序
   for(int j=i+1;j<n)
     if(a[i]>a[j])
        {t=a[i];a[i]=a[j];a[j]=t;}
 for(int i=0;i<n;i++)//将数组a的数据复制给数组b
   b[i]=a[i];
 }
void sort_write(float *a,float *b,int n)
{float t; 
 for(i=0;i<n;i++)//对数组a进行选择法排序
   for(int j=i+1;j<n)
     if(a[i]>a[j])
        {t=a[i];a[i]=a[j];a[j]=t;}
 for(int i=0;i<n;i++)//将数组a的数据复制给数组b
   b[i]=a[i];
 }
 
void sort_write(double *a,double *b,int n)
{double t; 
 for(i=0;i<n;i++)//对数组b进行选择法排序
   for(int j=i+1;j<n)
     if(a[i]>a[j])
        {t=a[i];a[i]=a[j];a[j]=t;}
 for(int i=0;i<n;i++)//将数组a的数据复制给数组b
   b[i]=a[i];
 }
 int main()
{int n=10,a[]={12,5,7,9,1,3,23,6,10,2},a1[10];
 double b[]={12.34,4.56,56.78,8.89,9.98,1.2,45.4,23.1,34.12,11.2},b1[10];
 sort_write(a,a1,10);
 sort_write(b,b1,10);
 for(int i=0;i<10;i++)
    cout<<a1[i]<<" ";
 cout<<endl;
 for(i=0;i<10;i++)
   cout<<b1[i]<<" ";
 cout<<endl;
 return 0;
}
}

5、输入n个字符串,将其中以字母A开头的字符串输出。提示:因为n的值可以根据键盘输入的不同而不同,所以,此题可以考虑用new创建string类型的动态数组。
参考答案:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string *my_string;
	int n;
	cin>>n;
	my_string=new string[n];

	for(int i=0;i<n;i++)
		cin>>my_string[i];
	for(i=0;i<n;i++)
	{
	 if(!my_string[i].find("A",0))//该find函数功能:返回字符串my_string[i]从位置0开始子串“A”第一次出现的位置;此行语句也可用if(my_string[i][0]=='A')
			cout<<my_string[i]<<endl;
	}
	return 0;
}

6、教材 同步练习6.1的程序练习的第1题:阅读程序,写出运行结果。一定要自己理解运行结果。

运行结果:
10
5
10       5
15       5

7、教材 同步练习6.2的程序练习的第1题:阅读程序,写出运行结果。一定要自己理解运行结果。
运行结果:

调用构造函数1.
4        8
调用构造函数2.
4        8
20
调用析构函数
调用析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值