1、编写程序求m和n之间(包括m和n,m<=n)所有素数。要求使用函数调用实现,该函数功能是:判断某一个数是否为素数。m和n由键盘输入,输入输出均在主函数中完成。
#include <iostream>
using namespace std;
int pd(int n)
{
int i,m=0,flag=0;
for(i=1;i<=n;i++)
if(n%i==0)
m++;
if(m==2)
flag=1;
return flag;
}
int main()
{
int m,n,b,k;
cin>>m>>n;
for(k=m;k<=n;k++)
{
b=pd(k);
if(b)
cout<<k<<" ";
}
return 0;
}
2,编写程序,输入奇数m,显示如下形式的菱形。
样例输入:5 样例输入:7
样例输出: 样例输出:
#include <iostream>
using namespace std;
int main()
{
int m,n,blank,i,j;
cin>>m;
if(m%2==0)
m++;
n=m/2+1; //上半部分三角形
blank=n-1;
for(i=1;i<=n;i++)
{
for(j=0;j<blank;j++)
{
cout<<" ";
}
for(j=1;j<=2*i-1;j++)
{
cout<<"*";
}
cout<<endl;
blank--;
}
n=m/2; //下半部分三角形
blank=1;
for(i=n;i>=1;i--)
{
for(j=0;j<blank;j++)
{
cout<<" ";
}
for(j=1;j<=2*i-1;j++)
{
cout<<"*";
}
cout<<endl;
blank++;
}
return 0;
}
3、定义一个表示人的类People。每个人包含两个信息:姓名和年龄。在People类的基础上,派生出一个表示学生的类Student。每位学生有一个学号和班级号。
编写主函数,使用people类测试数据 {"ZhangSan", 18},Student类的测试数据 {"LiSi", 19, 202301, "F1003001"},{"WangWu", 18, 202302, "F1102008"} 来测试类。
#include <iostream>
#include <cstring>
using namespace std;
class People {
public:
People( const char *s , int a) { // 构造函数
name = new char[strlen(s) + 1];
strcpy(name, s);
age = a;
cout << "People constructor:" << '[' << name << "] age : " << age << endl;
};
~People(){ // 析构函数
cout << "People destructor: " << '[' << name << "] age : " << age << endl;
delete name;
}
protected:
char *name;
int age;
};
class Student:public People {
public:
Student(const char *s, int a, int n, char *cls): People(s, a)
{ s_no = n;
class_no = new char[strlen(cls) + 1];
strcpy(class_no, cls);
cout << "Student constructor: student number is " << s_no
<< ", class number is " << class_no << endl;
}
~Student()
{ cout << "Student destructor: student numberis " << s_no
<< ", class number is " << class_no << endl;
delete class_no;
}
private:
int s_no;
char *class_no;
};
int main()
{ People p( "ZhangSan", 18);
cout << endl;
Student s1( "LiSi", 19, 202301, "F1003001");
cout << endl;
Student s2( "WangWu", 18, 202302, "F1102008" );
cout << endl;
return 0;}
4、编写汽车类的程序,要求如下:(1)car 类有车牌,车主,类型,有构造函数和输出函数;
(2)出租车taxi继承了car类,新增了座位数和价格,
(3)在main()函数,并测试。car 类的测试数据("沪A01100", "张三","法拉利");taxi 类的测试数据("沪B01100", "李四", "桑塔纳", 5, 2.5);
#include <iostream>
#include <cstring>
using namespace std;
class car {
char no[10];
char owner[10];
char type[20];
public:
car(char *s1, char *s2, char *s3) {
strcpy(no, s1);
strcpy(owner, s2);
strcpy(type, s3);
}
void modifyNo(char *s) {strcpy(no, s); }
void modifyOwner(char *s) {strcpy(owner, s); }
const char *getNo() const { return no; }
const char *getOwner() const {return owner; }
const char *getType() const { return type; }
};
class taxi : public car {
int seat;
double price;
public:
taxi(char *s1, char *s2, char *s3, int s, double p):car(s1,s2,s3)
{ seat = s; price = p; }
int getSeat() const {return seat; }
double getPrice() const { return price; }
};
int main()
{car car1("沪A01100", "张三","法拉利");
taxi taxi1("沪B01100", "李四", "桑塔纳", 5, 2.5);
cout << car1.getNo() << " " << car1.getOwner() << " " << car1.getType() << endl;
cout << taxi1.getNo() << " " << taxi1.getOwner() << " " << taxi1.getType() << endl;
return 0;}