复杂信息的表达和处理
表示一项事物的单个特征可以用变量;
表示一项事物的多个特征可以用多个变量;
表示多个事物的某个特征可以用一维数组;
表示具有多个特征的多个事物呢?
一个变量可以描述一个事物的单个特征;
一个数组可以描述多个事物的同一个特征或者描述一个事物的多个同类特征;
结构体可以描述一个事物的多个特征。
从变量到结构体,是从特征的描述到事物描述的变化。
除了特征外,事物还有状态的变化和功能。如一幅图像,可以缩放、倾斜,亮度可以变量、变暗、还可以改变颜色等。
要完整描述事物的特征及其功能,C++提供功能是“类”。
C++中用类描述一类具有相同特征和功能的事物。
复杂信息的表达
一维数组
例:将单词中小写字母转换成大写
#include<iostream>
using namespace std;
int main() {
char str[10];
int i = 0;
cout << "输入str";
cin >> str;
while (str[i]!= '\0') {
str[i] = str[i] - 32;
i = i + 1;
}
cout << str << endl;
return 0;
}
二维数组
初始化方式
全部元素
int A[3][3]={1,2,3,4,5,6,7,8,9};
int A[][3]={1,2,3,4,5,6,7,8,9,};
int A[3][3]={{1,2,3},{4,5,6},{7,8,9}};
部分元素
int B[3][3]={1,2,3};
int B[3][3]={{1,2},{3,4},{7,8}};
**注:**行标列标不能是变量
使用方法
**注:**下标不能越界
问题
如果使用一维数组a[M*N]和二维数组b[M][N]表示同一矩阵,则b[i][j]与a中哪个下标的元素对应?
#include<iostream>
using namespace std;
int main() {
const int m = 2, n = 3;
int a[m * n], b[m][n];
int i, j, x = 1;
for (i = 0; i < 6; i++, x++)
a[i] = x;
x = 1;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++, x++) {
b[i][j] = x;
cout << a[i * n + j] << "\t" << b[i][j] << endl;
}
}
return 0;
}
2.月份转换
编写程序,将用户输入的阿拉伯数字转换成对应月份的英文单词
#include<iostream>
using namespace std;
int main() {
char month[12][10] = {
"January","February","March","April","May","June",
"July","August","September","October","November",
"December"
};
int m;
cout << "输入m值:";
cin >> m;
if (m > 0 && m < 13)
cout << month[m - 1];
else
{
cout << "The month is wrong";
}
return 0;
}
结构体
结构体类型定义方法
语句格式
struct <结构体名>{
类型名 成员名表1;
类型名 成员名表2;
…
类型名 成员名表n;
}
**注:**同意结构体的成员不能同名,但可与其它变量同名
结构体变量声明方法
语句格式
struct <结构体> <变量列表>;
声明时机
定义结构体类型后声明
struct Date today;
定义结构体类型同时声明,可以省略结构体名
struct Date{int year,month,day;}today;
结构体变量初始化方法
struct Date today={2015,2,20};
结构体变量使用方法
today.year=2015
today.month=2
today.day=20
结构体数组
定义方法
struct <结构体名> <结构体数组名>[<数组大小>];
struct Date manydates[30];
初始化方法
struct Date manydates[30]={{2015,1,1},{2015,2,17},{2015,3,8}};
struct Date manydates[]={{2015,1,1},{2015,2,17},{2015,3,8}};
使用方法
manydate[i].year=2015
manydate[i].month=2
manydate[i].day=20
例题
例:简易通讯录
struct telelist {
char name[8];
char sex;
char num1[5];
char num2[5];
}list1[3];
int i;
for (i = 0; i <= 2; i++) {
cout << "输入name,sex,num1,num2值:";
cin >> list1[i].name >> list1[i].sex >> list1[i].num1
>> list1[i].num2;
}
for (i = 2; i >= 0; i--) {
cout << list1[i].name << "/" << list1[i].sex << "/"
<< list1[i].num1 << "/" << list1[i].num2 << endl;
}
枚举
定义方法
语句格式
enum <枚举类型名>{枚举常量表};
例如:
enum Week{Sun,Mon,Tes,Wed,Thu,Fri,Sat};
//Sun=0,Mon=1,Tes=2,Wed=3,Thu=4,Fri=5,Sat=6
**注:**枚举常量是以标识符形式标识的整型量,而不是字符串或字面常量
变量声明方法
语句格式
enum <枚举类型名> <枚举变量列表>;
声明时机
定义枚举类型后晟敏
enum COLOR background,foreground;
定义枚举类型同时声明
enum Week {Sun=7,Mon=1,Tes,Wed,Thu,Fri,Sat}
begin,end;
变量的使用方法
begin=Mon;//将枚举常量赋给枚举变量
begin=end;//相同类型的枚举变量赋值
begin=(Week)1;
int a=begin;
int b=Sun;
cout<<begin;
cout<<end-begin;
例题
三色球组合
口袋中有红,黄,蓝3种颜色的小球,如果每次从口袋中取出3种不同颜色的小球,编写程序,打印出每种组合.
#include<iostream>
using namespace std;
int main() {
enum color { red, yellow, blue };
int temp, i, j;
for (i = red; i <= yellow; i++) {
for (j = i + 1; j <= blue; j++) {
for (int t = 0; t < 2; t++) {
switch (t)
{
case 0:
temp = i;
break;
case 1:
temp = j;
break;
}
switch (enum color)
{
case red:
cout << "red" << "\t";
break;
case yellow:
cout << "yellow" << "\t";
break;
case blue:
cout << "blue" << "\t";
break;
default:
break;
}
}
}
}
return 0;
}
冒泡排序
用户从键盘输入N,然后输入N个实数,使用冒泡排序方法对这N个元素排序,输出排序后的数据.
#include<iostream>
#include<cmath>
using namespace std;
int main() {
double a[100];
int N;
int i = 0, j = 0;
cout << "输入元素的实际个数N:";
cin >> N;
//输入数据
for (i = 0; i < N; i++) {
cout << "输入N个元素:";
cin >> a[i];
}
//排序
for (i = 0; i < N; i++) {//控制n-1趟冒泡
for (j = 0; j < N - 1 - i; j++) {
if (a[j]>a[j+1])
{
int tmp;
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
return 0;
}
文字信息统计
用户输入一段文本(英文),统计其字符总个数,大写字母个数,小写字母个数,数字个数及其他字符个数.
#include<iostream>
#include<cmath>
using namespace std;
int main() {
const int N = 101;
char str[N];
int len = 0, capital = 0, smallletter = 0, digit = 0, others = 0;
int i;
cout<<"输入字符串:"<<endl;
cin.getline(str, N);
i = 0;
while (str[i] != '\0') {
len++;
if (str[i] <= 'Z' && str[i] >= 'A') {
capital++;
}
else if (str[i] <= 'z' && str[i] >= 'a') {
smallletter++;
}
else if (str[i] <= '9' && str[i] >= '0') {
digit++;
}
else {
others++;
}
i++;
}
//输出结果
cout << "字符串总长度:" << len << endl;
cout << "大写字母:" << capital << endl;
cout << "小写字母:" << smallletter << endl;
cout << "数字个数:" << digit << endl;
cout << "其他字符:" << others << endl;
return 0;
}
**注:**在VS2008及以上版本中调用strcpy,strcat等函数时由于安全原因会提示警告
#define _CRT_SECURE_NO_WARNINGS
[程序分析2]
C++提供了一些字符串处理的库函数,方便字符串操作.
常见的有:
int strlen(char *s);//求字符串s的长度
//将字符串source复制的destin中
char *strcpy(char *destin,char *source);
int strcmp(char *string1,char *string2);//比较string1和string2
//将source连接到destin末尾
char *strcat(char *destin,char *source);
char *strlwr(char *string);//string转换为小写
char *strupr(char *string);//string转换为大写
使用String字符串的操作
#include<iostream>
#include<cmath>
using namespace std;
int main() {
string text1("Heavy rains are pushing water levels beyond the limit.");
string text2, text3;
int k;
text2 = "Sluice gates at Three Gorges Dam opended to daischarge water.";
text3 = text1 + text2;
k = text3.find("Heavy");
text3.erase(k, sizeof("Heavy") - 1);
text3.insert(k, "Strong");
cout << text3 << endl;
return 0;
}
矩阵乘法
用户输入AMN,BNK两个矩阵的元素,计算它们的乘积并输出.其中M,N,K也由用户输入,它们均不超过20.
#include<iostream>
#include<cmath>
using namespace std;
int main() {
const int M = 20, N = 20, K = 20;
double A[M][N], B[N][K], C[M][K];
int M1, N1, N2, K1;
int i, j, k;
cout << "请输入第1个矩阵的维数M N" << endl;
cin >> M1 >> N1;
cout << "请按行输入第1个矩阵的元素" << endl;
for (i = 0; i < M1; i++) {
for (j = 0; j < N1; j++) {
cin >> A[i][j];
}
}
cout << "请输入第2个矩阵的维数N K" << endl;
cin >> N2 >> K1;
while (N2 != N1) {
cout << "第2个矩阵的行数应等于第1个矩阵的列数,请重输" << endl;
cin >> N2 >> K1;
}
cout << "请按行输入第2个矩阵的元素" << endl;
for (i = 0; i < N1; i++) {
for (j = 0; j < K1; j++) {
cin >> B[i][j];
}
}
for (i = 0; i < M1; i++) {
for (j = 0; j < K1; j++) {
C[i][j] = 0;
for (k = 0; k < N1; k++) {
C[i][j] = C[i][j] + A[i][k]*B[k][j];
}
}
}
//输出乘积矩阵的元素
for (i = 0; i < M1; i++) {
for (j = 0; j < K1; j++) {
cout << C[i][j] << "\t";
}
cout << endl;
}
return 0;
}
取子字符串
用户输入一个字符串,然后输入起始位置k和长度l
显示从第k个字符开始,长度为l的子字符串
要求字符串输入一次,字串操作可以多次,输入位置和长度均为0时停止
#include<iostream>
#include<cmath>
using namespace std;
int main() {
char str[101];//源字符串
char sub[101];//子字符串
int len;//源字符串的长度
int k, l;//子字符串起始位置,子字符串长度
int i, j = 0;//循环变量
//输入源字符串
cout << "请输入字符串(可以有空格):" << endl;
cin.getline(str, 100);//输入带空格的字符串
//求字符串的长度
len = 0;
while (str[len] != '\0') {
len++;
}
cout << "请输入子串起始位置和长度:" << endl;
cin >> k >> l;
while (k != 0 && l != 0) {
j = 0;
for (i = k - 1; i < k + l - 1 && i < len; i++) {
sub[j] = str[i];
j++;
}
sub[j] = '\0';
cout << sub << endl;
cout << "请输入子串起始位置和长度:" << endl;
cin >> k >> l;
}
return 0;
}
词频统计
输入一系列英文单词(单词之间用空格隔开),用"xyz"表示输入结束.
统计各单词出现的次数(单词不区分大小写),对单词按字典顺序进行排序后输出单词和词频.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
struct WordList
{//字典结构体
char word[20];//单词
int freq;//使用次数
};
int main() {
WordList list[1000];//结构体数组
int N = 0;//实际单词数
int i, j, k;//循环变量,临时变量
char tmp[20];//临时存放新输入的单词
//-----输入单词-------
cout << "请输入一系列英语单词,以xyz表示输入结束" << endl;
cin >> tmp;
while (strcmp(tmp, "xyz") != 0) {//不是单词的结束符时循环
for (i = 0; i < N; i++) {//在当前词典中逐个查
if (strcmp(list[i].word, tmp) == 0) {
list[i].freq++;//词频加1
break;//不再查找
}
}//结束时,N为词典中的单词数
if (i >= N) {//这时是没有找到,添加该词
strcpy(list[i].word, tmp);//添加单词
list[i].freq = 1;//词频置1
N++;//单词数加1
}
cin >> tmp;//继续输入单词
}
//-----对词典进行排序---------
for (i = 0; i < N - 1; i++) {//控制N-1次选择
k = i;//先设i是当前最小元素的下标
for (j = i + 1; j < N; j++) {
//与后面的单词比较
if (strcmp(list[j].word, list[k].word) < 0) {
k = j;//记下最小元素的下标
}
}
if (k != i) {//最小的下标不是i
WordList tmp;//交换下标是k和i的两个元素
tmp = list[i];
list[i] = list[k];
list[k] = tmp;
}
}
//-----输出结果-------
cout << "词频统计结果如下:" << endl;
for (i = 0; i < N; i++) {
cout << list[i].word << "\t" << list[i].freq << endl;
}
return 0;
}
题目描述:
定义包含学号、姓名和成绩的学生信息结构类型,完成以下功能:
(1)键盘输入n个学生信息数据(n从键盘输入);
(2)使用“冒泡法”对学生成绩由高到低排序;
(3)输出排序后学生信息。
输入输出样例
输入样例:
3
100001 wang-li 91
100002 chen-da-wei 95
100003 guo-tao 93
输出样例:
100002 chen-da-wei 95
100003 guo-tao 93
100001 wang-li 91
样例输入:
2
111111 wang 78
222222 liu 100
1
2
3
样例输出:
222222 liu 100
111111 wang 78
1
2
参考代码
#include<stdio.h>
struct student
{
int num;
char name[20];
float score;
}stu[100];
void sort(struct student stu[], int n)
{
int i, j;
struct student t;//再声明一个
for (i = 0; i < n - 1; i++)//标准冒泡排序
{
for (j = i + 1; j < n; j++)
{
if (stu[i].score<stu[j].score)
{
t = stu[i];
stu[i] = stu[j];
stu[j] = t;
}
else if (stu[i].score == stu[j].score)//当成绩一样的时候
{
if (stu[i].num < stu[j].num)
{
t = stu[i];
stu[i] = stu[j];
stu[j] = t;
}
}
}
}
}
int main()
{
int n;
int i;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &stu[i].num);
scanf("%s", &stu[i].name);
scanf("%f", &stu[i].score);
}
sort(stu, n);
for (i = 0; i < n; i++)
printf("%d %s %1.0f\n", stu[i].num, stu[i].name, stu[i].score);//为什么要加1.0?
return 0;
}