从键盘输入n个字符串(每串至多80个字符,且输入时以回车结束每一行),先存放在一个二维数组中,再统计每一串中大写字母的出现次数,并找出这些串中的最长串.
#include <iostream>
using namespace std;
#define MAXLENTH 80
void main ()
{
cout<<"要输入字符串个数\n";
int n=3;
cin>>n;
cout;
char strs[50][MAXLENTH];
for (int i=0; i<n; i++)
{
cin.getline(strs[i],MAXLENTH-1);
}
int j=0,
s=0, //字符串大写字母统计数目
m=0; //最长字符串下标
for (int i=0; i<n; i++)
{
if(strlen(strs[i])>strlen(strs[m])) m=i;
for (j=0,s=0; j < (int)strlen(strs[i]); j++)
if (strs[i][j]>='A' && strs[i][j]<='Z')
s++;
cout<<"字符串"<<i<<"中大写字母数目为:"<<s<<endl;
}
cout<<"最长字符串下标为:"<<m<<endl;
}
编程输入若干名(不超过100)新生数据,输出高出均分10分以上所有新生数据。其中,新生人数在程序运行时由用户输入。编程要求:
(1)定义描述新生的全局结构体类型student,成员包括学好、姓名和入学成绩。
(2)编写Input函数,用于输入n名新生数据。
(3)编写Output函数,用于输出n名新生中高于10分以上的所有新生。
(4)编写Average()函数,用于计算n名新生的平均入学成绩。
(5)在main()函数中,首先定义student类型数组用于保存不超过100名新生数据;其次定义整型变量n用于保存输入的学生人数;最后分别调用Input和Output函数完成指定任务。
#include <iostream>
using namespace std;
#define MAXPEOPLE 100
struct student {
unsigned No; //学号
char name[20]; //姓名
unsigned grade; //成绩
};
void Input(student* s,unsigned n = 1) //输入n名学生数据
{
for (unsigned i=0; i<n; i++)
cin>>s[i].No>>s[i].name>>s[i].grade;
}
double Average(student* s,unsigned n = 1) //计算n名学生成绩
{
double sum=0;
for (unsigned i=0; i<n; i++)
sum += s[i].grade;
return double(sum)/n;
}
void Output(student* s,unsigned n = 1) //输出n名学生成绩
{
double ave = Average(s,n);
for (unsigned i=0; i<n; i++)
if (s[i].grade -10 > ave)
cout<<s[i].No<<ends<<s[i].name<<ends<<s[i].grade<<endl;
}
void main ()
{
unsigned n =0;
cin>>n;
student stus[MAXPEOPLE];
Input(stus,n);
Output(stus,n);
}
编写一个程序,通过调用自定义函数f找出字符串中的最长字符串。函数f的原型为:
char*f(char s[][20],int n);
功能是找出数组s中包含的n个字符串中的最长字符串,并返回该字符串的指针值。
#include <iostream>
using namespace std;
char* f(char s[][20],int n)
{
int max=0;
for (int i=1;i<n;i++)
{
if (strlen(s[i]) > strlen(s[max])) max = i;
}
return s[max];
}
void main()
{
char* s[20];
}
定义一个平面坐标坐标中的类point ,包含表示横坐标和纵坐标的数据数据成员x和y,以及计算两点距离的成员函数。再设计一个三角形态triangle,以三个point类对象为数据成员描述三个顶点,并含有计算面积的的成员函数。要求在这两类中包括构造函数。构成完整的程序进行测试.
#include <iostream>
#include <cmath>
using namespace std;
class Point { //点类
float x,y;
public:
Point(float a,float b) {x=a; y=b;}
float dis(Point p){return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2));}
};
class Triangle { //三角类
Point a,b,c;
public:
Triangle(float aX,float aY,float bX,float bY,float cX,float cY):a(aX,aY),b(bX,bY),c(cX,cY){}
float Area() //求面积
{
float C = a.dis(b),
A = b.dis(c),
B = c.dis(a),
HalfS = (A + B + C)/2;
return sqrt(HalfS * (HalfS - A) * (HalfS - B) * (HalfS - C));
}
};
void main()
{
Triangle T1(0,0,2,0,0,2);
cout<<T1.Area()<<endl;
}
定义一个描述圆的circle类,含有表示半径的数据成员和分别计算周长,面积的函数成员。在circle 类的基础上,派生出圆柱类cylider,增添表示高度的数据成员。构成完整的程序,可用于测试定义的类。
#include <iostream>
#include <cmath>
using namespace std;
#define Pi double(3.14159265)
class Circle { //圆类
float r;
public:
Circle (float a) {r = a;}
float Perimeter() {return 2 * Pi * r;} //求周长
float Area () {return Pi * pow(r,2);} //求面积
};
class Cylider:private Circle { //圆柱体类,私有继承
float height;
public:
Cylider (float r,float h):Circle(r){height = h;}
float surface_area (){return Perimeter() * height + 2 * Area();} //求表面积
float volume () {return Area() * height;} //求体积
};
void main ()
{
Circle ci1(1);
cout<<ci1.Perimeter()<<endl
<<ci1.Area()<<endl;
Cylider cy1(1,2);
cout<<cy1.surface_area()<<endl
<<cy1.volume()<<endl;
}