项目2:数组选择
从键盘中输入10个数放在数组A中,将该数组中不重复的数放到数组B中
#include <iostream>
#include<windows.h>//为了使程序不闪退而使用该函数,在使用VC6.0编写时不需要此函数
using namespace std;
int main()
{
const int S = 10;
int a[S], b[S], i, k=0,j=0,f=0;
cout << "请输入10个数:";
for (i = 0; i < S; i++)
cin >> a[i];
for (i = 0; i < S; i++)
{
f = 1;
for (j = 0; j < S; j++)
{
if (a[i] == a[j]&&i!=j)
f = 0;
}
if (f==1)
{
b[k] = a[i];
k++;
}
}
cout << "数组B为:";
for (i = 0; i < k; i++)
cout << b[i]<<" ";
system("pause");//见第一条注释
项目3:成绩
在数组score中将要存储某小组程序设计的成绩(设有10人),编程实现下列功能:
(1)输入小组人数及成绩,要保证成绩在0-100之间;
(2)输出该小组的最高成绩、最低成绩、平均成绩;
(3)输出考得最高成绩和最低成绩的同学的人数;
(4)输出考得最高成绩和最低成绩的同学的学号(设数组下标即学号,可能有相同的成绩)。
(5)(选做)求出所有同学成绩的标准偏差,标准偏差公式为,其中为xi样本(即某同学成绩),x(上带一横)为均值(前面已经求出),N为样本数目;
#include <iostream>
#include<windows.h>//为了使程序不闪退而使用该函数,在使用VC6.0编写时不需要此函数
using namespace std;
int main()
{
const int Z = 10;
int a[Z], b, c, d, e=0,f=0,g=0,k=0,h=0,i=0,n[Z],m[Z],x=0,y=0;//b是最高成绩,c是最低成绩,d是平均成绩,e是最高成绩人数,f是最低成绩人数,g是最高成绩学号,k是最低成绩学号
for (i = 0; i < Z; i++)
{
cout << "请输入第" << i+1 << "位同学的成绩";
cin >> a[i];
if (a[i]>100)
{
cout << "错误!";
}
else
{
g += a[i];
}
}
d = g / Z;
cout << "平均分为:" << d<<endl;
b = a[0];
g = 0;
c = a[0];
k = 0;
for (i = 0; i < Z; i++)
{
if (a[i]>b)
{
b = a[i];
g = i;
}
if (a[i] < c)
{
c = a[i];
k = i;
}
}
for (i = 0; i < Z; i++)
{
if (a[i] == a[g])
{
e++;
n[x] = i+1;
x++;
}
if (a[i] == a[k])
{
f++;
m[y] = i+1;
y++;
}
}
cout << "最高成绩为:" << a[g] << ",最高分有" << e << "位同学。";
cout << "这些同学的学号为:";
for (i = 0; i < x; i++)
cout << n[i]<<" ";
cout <<endl<< "最低成绩为:" << a[k] << ",最低分有" << f << "位同学。";
cout << "这些同学的学号为:";
for (i = 0; i < y; i++)
cout << m[i] << " ";
cout << "\n";
system("pause");//见第一条注释
}
项目4:字符串
统计输出字符串中(大/小写)字母个数,数字个数及其它字符个数。
#include<cstdio>
#include <iostream>
#include<windows.h>//为了使程序不闪退而使用该函数,在使用VC6.0编写时不需要此函数
using namespace std;
int main()
{
char str[10];
int i = 0, n = 0,m=0,x=0,y=0;
cout << "输入字符串:";
gets(str);
while (str[i] != '\0')
{
if (str[i] >= '0'&&str[i] <= '9') n++;
else if
(str[i] >= 'a'&&str[i] <= 'z') m++;
else if
(str[i] >= 'A'&&str[i] <= 'Z') x++;
else
y++;
i++;
}
cout << "其中的数字个数是: " << n << endl;
cout << "其中的大写字母个数是: " << x << endl;
cout << "其中的小写字母个数是: " <<m << endl;
cout << "其中的其他字符个数是: " << y << endl;
system("pause");//见第一条注释
}
项目5:求和
从键盘中输入10个数放入数组a,求a数组中正数和负数的和。
#include <iostream>
#include<windows.h>//为了使程序不闪退而使用该函数,在使用VC6.0编写时不需要此函数
using namespace std;
int main()
{
double a[10], j=0, k=0;
int i;
for (i = 0; i < 10; i++)
{
cin >> a[i];
if (a[i] >= 0)
j = j + a[i];
else
k = k + a[i];
}
cout << "正数的和:" << j<<endl;
cout << "负数的和:" << k << endl;
system("pause");//见第一条注释
}