2011-12-23 二维数组的操作:
/* 程序头部注释开始
* 版权多有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:二维数组的操作
* 作 者:张旭
* 完成日期: 2011 年 12 月 23 日
* 版 本号: 27.0
* 对任务及求解方法的描述部分
* 输入描述:由程序产生随机数作为初始值
* 问题描述:……
* 程序输出:……
* 程序头部的注释结束
*/
#include <iostream>
#include <time.h>
using namespace std;
void setdata(int a[8][8]); //设置随机数
void out(int a[8][8]); //输出数组
void outDiagonal(int a[8][8]); //输出对角线元素的值
void mine(int a[8][8], int x, int y); //按“扫雷”游戏的规则输出相邻格子
void change(int a[8][8]); //按要求改变数值
int main()
{
int a[8][8], x, y;
setdata(a);
out(a);
outDiagonal(a);
cout << "输入一个位置:";
cin >> x >> y;
mine(a, x, y);
change(a);
out(a);
return 0;
}
void setdata(int a[8][8])
{
int i, j;
srand(time(NULL)); //需要用当前时间作“种子”,以便每次运行取得的序列不同
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
a[i][j] = rand() % 50 + 1;
return;
}
void out(int a[8][8])
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cout << a [i][j] << '\t';
}
cout << endl;
}
return;
}
void outDiagonal(int a[8][8])
{
for (int i = 0; i < 8; i++)
{
cout << a [i][i] << " ";
}
cout << endl;
for (i = 0; i < 8; i++)
{
cout << a [i][7 - i] << " ";
}
cout << endl;
return;
}
void mine(int a[8][8], int x, int y)
{
if (!(((x < 7) && (x > 0)) || ((y > 0) && (y < 7))))
{
cout << "输入错误" << endl;
}
else
{
int s = 0;
cout << "这个数为:" << a[x][y] << endl;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if ((i >= 0) && (i <= 7) && (j >= 0) && (j <= 7) && !((i == x ) && (j == y)))
{
cout << a[i][j] << '\t';
s += a[i][j];
}
}
}
cout << endl << "sum =" << s << endl;
}
return;
}
void change(int a[8][8])
{
for(int i = 1; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
a[i][j] = a[i - 1][j] + a[i - 1][(j + 1) % 8];
}
}
return;
}
学习感悟:了解了二维数组的应用,深化了对数组的理解。
2011-12-21 传递数组值:
/* 版权多有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张旭
* 完成日期: 2011 年 12 月 21 日
* 版本号: 26.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
#include <cstring>
using namespace std;
void fun (char *, char *, int);
int main ()
{
char str1 [20] = "I am a student", str2 [20];
int m;
cin >> m;
fun (str1, str2, m);
cout << str2 << endl;
return 0;
}
void fun (char *p, char *q, int m)
{
int n = strlen (p);
if(m <= n)
{
for (int i = m - 1, j = 0;i < n; i++, j++)
{
q [j] = p [i];
}
for (;j < 20; j++)
{
q [j] = '\0';
}
}
else
{
for (int i = 0;i < 20; i++)
{
q [i] = '\0';
}
}
return;
}
学习感悟:学习了对字符数组的操作。
2011-12-17 计算时间:
/* 版权多有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张旭
* 完成日期: 2011 年 12 月 17 日
* 版本号: 25.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;
struct Birth
{
int year;
int mouth;
int day;
int hour;
int minute;
int second;
};
int main ()
{
bool nian = false;
int ping [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int run [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int tomouth, t = 0;
Birth yours;
cout << "Please write your birthday's year:" <<endl;
cin >> yours.year;
if((yours.year % 4 == 0 && yours.year % 100 != 0) || (yours.year % 100 == 0 && yours.year % 400 == 0))
{
nian = true;
}
cout << "Please write your birthday's mouth:" <<endl;
do
{
cin >> yours.mouth;
}while (yours.mouth > 12 || yours.mouth < 1);
if (nian)
{
tomouth = ping [yours.mouth];
}
else
{
tomouth = run [yours.mouth];
}
cout << "Please write your birthday's day:" <<endl;
do
{
cin >> yours.day;
}while (yours.day > tomouth || yours.day < 1);
for (int i = 1; i < yours.mouth; i++)
{
if (!nian)
{
t += ping [i];
}
else
{
t += run [i];
}
}
t = t + yours.day;
cout << "Your date is the" << t << " day in this year." << endl;
cout << "Please write your birthday's hour:" <<endl;
do
{
cin >> yours.hour;
}while (yours.hour > 24 || yours.hour < 1);
cout << "Please write your birthday's minute:" <<endl;
do
{
cin >> yours.minute;
}while (yours.minute > 60 || yours.minute < 1);
cout << "Please write your birthday's second:" <<endl;
do
{
cin >> yours.second;
}while (yours.second > 60 || yours.second < 1);
long d = 0;
d = t * 24 * 3600 + yours.hour * 3600 + yours.minute * 60 + yours.second;
cout << "Your second is the " << d << " second in this year." << endl;
return 0;
}
学习感悟:在编写这个程序的过程中出现很多问题,经过改正,使自己积累了很多有用的经验。
2011-12-17 构造链表:
/* 版权多有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张旭
* 完成日期: 2011 年 12 月 17 日
* 版本号: 24.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#define NULL 0
using namespace std;
struct Student
{
char num [12];
string name;
int grade1;
int grade2;
int grade3;
int gradesum;
struct Student *next;
};
int main ()
{
Student *head = NULL, *p, *q;
double ave = 0;
int sum = 0;
ifstream infile ("score.txt" , ios::in);
if (!infile)
{
cerr << "open error!" << endl;
exit(1);
}
for (int i = 0; i < 180; i++)
{
p = new Student;
infile >> p ->num >> p ->name >> p ->grade1 >> p ->grade2 >> p ->grade3;
p ->gradesum = p ->grade1 + p ->grade2 + p ->grade3;
sum += p ->gradesum;
p ->next = NULL;
if (i == 0)
{
head = p;
}
else
{
q ->next = p;
}
q = p;
}
infile.close();
ave = sum / 180;
p = head;
while (p != NULL)
{
if (p ->gradesum >= ave && p ->grade1 >59 && p ->grade2 >59 && p ->grade3 >59)
{
cout << p ->num << '\t' << p ->name << '\t' << p ->grade1 << '\t' << p ->grade2 << '\t' << p ->grade3 << '\t' << p ->gradesum << endl;
}
p = p ->next;
}
return 0;
}
学习感悟:通过这个程序使我学会使用链表,很高兴学习这个,很灵活,很有意思。
2011-12-17 排序选择:
/* 版权多有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张旭
* 完成日期: 2011 年 12 月 17 日
* 版本号: 23.0
* 对任务及求解方法的描述部分
* 输入描述:一个整数x
* 问题描述:输出x的所有奇数因子
* 程序输出:x的所有奇数因子及其个数
* 程序头部的注释结束
*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct Student
{
char num [12];
string name;
int grade1;
int grade2;
int grade3;
int gradesum;
};
void school_demand(Student *, string *, int);
void bubble_sort (Student *, int);
void output_array (Student *, int);
inline int sum (int, int, int);
int main ()
{
Student stu [180];
string dem [30];
ifstream infile ("score.txt" , ios::in);
if (!infile)
{
cerr << "open error!" << endl;
exit(1);
}
for (Student *i = stu; i < stu + 180; i++)
{
infile >> i ->num >> i ->name >> i ->grade1 >> i ->grade2 >> i ->grade3;
}
infile.close();
for (i = stu; i < stu + 180; i++)
{
i ->gradesum = sum (i ->grade1, i ->grade2, i ->grade3);
}
bubble_sort (stu, 180);
school_demand(stu, dem, 30);
output_array (stu, 180);
cout << "获奖名单:" <<endl;
for (string *q = dem; q < dem + 30; q++)
{
cout << *q <<" ";
}
cout << endl;
return 0;
}
void bubble_sort(Student score[], int num)
{
Student t;
for(int i = 0; i < num; i++)
{
for(int j = 0; j < num - 1 - i; j++)
{
if(score[j].gradesum < score[j+1].gradesum)
{
t=score[j + 1];
score[j + 1] = score[j];
score[j] = t;
}
}
}
}
void school_demand (Student stu[], string dem [], int num)
{
for (int i = 0, j = 0; j < num; i++)
{
if (stu[i].grade1 > 59 && stu[i].grade2 > 59 && stu[i].grade3 > 59)
{
dem [j] = stu[i].name;
j++;
}
}
return;
}
void output_array (Student stu [], int num)
{
for (int i = 0; i < num; i++)
{
cout << stu [i].num << '\t' << stu [i].name << '\t' << stu [i].grade1 << '\t' << stu [i].grade2 << '\t' << stu [i].grade3 << '\t' << stu [i].gradesum << endl;
}
return;
}
inline int sum (int x, int y, int z)
{
int sum = 0;
sum = x + y + z;
return sum;
}
学习感悟:综合的对所学的知识的应用,收获很大。
2011-12-09 求出一个数所有的奇数因子:
/* 程序头部注释开始
* 版权多有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:求奇数因子
* 作 者: 张旭
* 完成日期: 2011 年 12 月 09 日
* 版本号: 22.0
* 对任务及求解方法的描述部分
* 输入描述:一个整数x
* 问题描述:输出x的所有奇数因子
* 程序输出:x的所有奇数因子及其个数
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;
int fun (int, int *);
int main (void)
{
int a [50], x, n;
cin >> x;
n = fun(x, a);
cout << x <<"的奇因子共有" << n << "个,分别是:";
for(int i = 0; i < n; i++)
cout << a [i] << " ";
cout << endl;
return 0;
}
int fun (int x, int *pp)
{
int s = x / 2;
int t = 0;
for (int i = 1; i <= s; i++)
{
if (i % 2 == 1 && x % i == 0)
{
*(pp + t) = i;
t++;
}
}
return t;
}
学习感悟:学会利用指针解决问题。
2011-12-07 冒泡排序算法:
/* 程序头部注释开始
* 版权所有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:冒泡指针程序
* 作 者:张旭
* 完成日期: 2011 年 12 月 07 日
* 版 本号:21.0
* 对任务及求解方法的描述部分
* 输入描述:要排序的数据在程序中初始化
* 问题描述:实现冒泡排序
* 程序输出:排序后的结果
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;
void bubble_sort (int *p, int num); //不要对自定义函数的声明有任何改动
void output_array (int *, int);
int main (void) //不要对main函数有任何改动
{
int a[20] = {86,46,22,18,77,45,32,80,26,88,57,67,20,18,28,17,54,49,11,16};
int b[15] = {27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};
bubble_sort(a, 20); //用冒泡法按降序排序a中元素
output_array(a, 20); //输出排序后的数组
bubble_sort(b, 15); //用冒泡法按降序排序b中元素
output_array(b, 15); //输出排序后的数组
return 0;
}
//下面定义自定义函数
void bubble_sort (int *p, int num)
{
int t = *p, *i, *k;
int a, b;
for (a = 0, i = p; a < num; i++, a++)
{
for (b = 1, k = p + 1; b < num - a; k++, b++)
{
if (*(k - 1) > *k)
{
t = *k;
*k = *(k - 1);
*(k - 1) = t;
}
}
}
return;
}
void output_array (int *p, int num)
{
for (int i = 0;i < num; p++, i++)
{
cout << *p << " ";
}
return;
}
学习感悟:感觉还是舞蹈比较生动一些,计算机尚需努力。
2011-12-02 ASCII文件的操作:
/* 程序头部注释开始
* 版权所有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:salary
* 作 者: 张旭
* 完成日期: 2011 年 12 月 02 日
* 版 本号: 20.0
* 对任务及求解方法的描述部分
* 输入描述:文件salary.txt
* 问题描述:从文件salary.txt中读入500名工人的工资,全部增加100元后进行排序(好事,涨工资了),将排序后的结果在屏幕上输出,并保存到文件ordered_salary.txt中
* 程序输出:ordered_salary.txt
* 程序头部的注释结束
*/
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int j = 0 , i = 0;
double a[500] , t = 0;
ifstream infile ("salary.txt" , ios::in);
if (!infile)
{
cerr << "open error!" << endl;
exit(1);
}
for (i = 0;i < 500;i++)
{
infile >> a [i];
}
infile.close();
for(i = 0;i < 500;i++)
{
a [i] = a [i] + 100;
}
for(j = 0;j < 499;j++)
{
for(i = 1;i < 499 - j;i++)
{
if(a [i - 1] < a [i])
{
t = a [i];
a [i] = a[i - 1];
a [i - 1] = t;
}
}
}
for (i = 0;i < 500;i++)
{
cout << setiosflags (ios :: fixed) << setprecision(2) << setw(10) << a[i];
}
ofstream outfile("order_salary.txt" , ios::out);
for(i = 0;i < 500;i++)
{
outfile << a[i] << " ";
}
outfile.close();
return 0;
}
学习感悟:多多练习!
2011-12-02 操作字符数组:
/* 程序头部注释开始
* 版权所有,翻版必究。
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:字符数组的操作
* 作 者: 张旭
* 完成日期: 2011 年 11 月 30 日
* 版本号: 19.0
* 对任务及求解方法的描述部分
* 输入描述:要处理的字符串在程序中通过赋初值完成
* 问题描述:求出字符数组中字符的个数及计算句子中各字符出现的频数
* 程序输出:字符数组中字符的个数和句子中各字符出现的频数
* 程序头部的注释结束
*/
#include<iostream>
using namespace std;
//函数声明
int length(char str[]);
void output_frequency(char str[]);
int main(void) //不要对main函数有任何改动
{
char str[]="he threw three free throws";
cout<<"要处理的字符串为:"<<str<<endl;
cout<<"字符串长度为:"<<length(str)<<endl;
cout<<"字符串中各字符出现的频数为:";
output_frequency(str);
cout<<endl;
return 0;
}
int length(char str[])
{
for (int i = 0;str[i] != '\0';i++);
return i;
}
void output_frequency(char str[])
{
char yuan [50] = {'\0'};
int n = length(str);
char zimu [50] = {'\0'};
int shuliang [50] = {0};
strcpy (yuan , str);
for (int i = 0;i <= n;i++)
{
for (int q = 0;str[q] != '\0';q++)
{
if (yuan [q] != '\0' && zimu [i] != yuan[q])
{
if (zimu[i] == '\0')
{
zimu [i] = yuan[q];
shuliang [i]++;
yuan [q] = '\0';
}
}
if (yuan [q] != '\0' && zimu [i] == yuan[q])
{
shuliang [i]++;
yuan [q] = '\0';
}
}
}
for (int w = 0;zimu[w] != '\0';w++) //输出
{
cout << zimu[w] << '-' << shuliang[w] <<"; ";
}
return;
}
学习感悟:在编写这个程序时,我领教了很多好的算法,发现了编程的魅力所在。
2011-11-30 - 2011-09-26 学习足迹略。