项目1:中国计算机设计大赛赛事统计
【问题描述】
参加计算机设计大赛的n个学校编号为1~n,赛事分成m个项目,项目的编号为1~m.比赛获奖按照得分降序,取前三名,写一个统计程序产生各种成绩单和得分报表。
【基本要求】
1)每个比赛项目至少有10支参赛队;每个学校最多有6支队伍参赛;
2)能统计各学校的总分;
3)可以按照学校编号或名称,学校的总分、各项目的总分排序输出;
4)可以按学校编号查询学校某个项目的获奖情况;可以按项目编号查询取得前三名的学校;
5)数据存入文件并能随时查询
【设计要求】
1)输入数据形式和范围:可以输入学校的名称,赛事项目的名称。
2)输出形式:有中文提示,各学校分数为整数
3)界面要求:交互设计要合理,每个功能可以设立菜单,根据提示,可以完成相关功能的要求。
4)存储结构:学生自己根据系统功能要求自己设计,但是赛事相关数据要存储在文件中。
【测试数据】
要求使用全部合法数据,整体非法数据,局部非法数据。进行程序测试,以保证程序的稳定。
【实现提示】
假设3<赛事项目数量<=10,学校名称长度不超过20个字符。每个赛事结束时,将其编号、名称输入,并依次输入参赛学校编号、学校名称和成绩。
【代码】
import java.sql.SQLOutput;
import java.util.*;
class test {
static int m;
static int n;
static Project[] project = new Project[10];
static School[] school = new School[10];
static class Project {
int projectCode;
String projectName;
int teamnum;
int[] jointeam = new int[10];
int[] score = new int[10];
}
static class School {
int schoolCode;
String schoolName;
int sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入项目的数目:");
m = sc.nextInt();
while(m<=3||m>10){ System.out.println("输入有误!");
System.out.println("请重新输入");
Scanner s = new Scanner(System.in);
m = s.nextInt();
}
while(m>0&&m<=10){
System.out.println("输入成功!一共有"+m+"个项目");
break;
}
System.out.println("请输入学校的数目:");
n = sc.nextInt();
if (n <= 0) {
System.out.println("输入有误!");
System.out.println("请重新输入");
Scanner c = new Scanner(System.in);
n = c.nextInt();
}
while (true) {
System.out.println("欢迎使用赛事统计查询系统");
System.out.println("1.添加信息");
System.out.println("2.按学校编号排序输出");
System.out.println("3.按项目总分排序输出");
System.out.println("4.按学校编号查询学校某个项目的获奖情况");
System.out.println("5.按项目编号查询取得前三名的学校");
System.out.println("0.退出");
System.out.println("请输入你的选择:");
String nothing = sc.nextLine();
int option = sc.nextInt();
if (option != 0 && option != 1 && option != 2 && option != 3 && option != 4 && option != 5) {
System.out.println("输入错误");
System.exit(0);
}
switch (option) {
case 1:
addInformation();
break;
case 2:
printByCodes();
break;
case 3:
printByScore();
break;
case 4:
findByCodes();
break;
case 5:
getTopThree();
break;
case 0:
System.out.println("谢谢使用");
System.exit(0);
}
}
}
public static void addInformation() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < m; i++) {
project[i] = new Project();
project[i].projectCode = i + 1;
System.out.println("请输入第" + (i + 1) + "个项目的名称:");
project[i].projectName = sc.nextLine();
System.out.println("添加成功!");
}
for (int i = 0; i < m; i++) {
System.out.println("项目编号" + project[i].projectCode +
"项目名称" + project[i].projectName);
}
//录入学校
for (int i = 0; i < n; i++) {
school[i] = new School();
school[i].schoolCode = i + 1;
System.out.println("请输入第" + (i + 1) + "个学校的名称:");
school[i].schoolName = sc.nextLine();
System.out.println("添加成功!");
}
for (int i = 0; i < n; i++) {
System.out.println("学校编号" + school[i].schoolCode +
"学校名称" + school[i].schoolName);
}
for (int i = 0; i < m; i++) {
System.out.println("请输入参加" + project[i].projectName + "的学校数量:");
project[i].teamnum = sc.nextInt();
System.out.println("请依次输入参加" + project[i].projectName + "的学校编号:");
for (int j = 0; j < project[i].teamnum; j++) {
project[i].jointeam[j] = sc.nextInt();
System.out.println("输入成功!");
}
System.out.println("参加项目" + project[i].projectName + "的学校有");
for (int j = 0; j < project[i].teamnum; j++) {
System.out.println(project[i].jointeam[j] + " ");
}
//录入分数
System.out.println("请依次输入参加" + project[i].projectName + "的学校的分数");
for (int j = 0; j < project[i].teamnum; j++) {
int score = sc.nextInt();
if (score >= 0 && score <= 100) {
project[i].score[j] = score;
System.out.println("输入成功!");
} else {
System.out.println("您输入的有误!");
System.exit(0);
}
}
System.out.println("显示如下:");
for (int j = 0; j < project[i].teamnum; j++) {
System.out.println("编号" + project[i].jointeam[j] + "的学校取得的成绩:" + project[i].score[j]);
}
}
}
public static void printByCodes() {
//按学校编号排序输出
int sum = 0;
for (int i = 0; i < n; i++) {
System.out.println("学校编号:" + school[i].schoolCode);
System.out.println("学校名称:" + school[i].schoolName);
for (int j = 0; j < m; j++) {
for (int k = 0; k < project[j].jointeam[k]; k++) {
if (project[j].jointeam[k] == school[i].schoolCode) {
System.out.println("参赛项目:" + project[j].projectName);
System.out.println("取得成绩:" + project[j].score[k]);
System.out.println("*****");
sum += project[j].score[k];
}
}
}
System.out.println("学校取得的总分是:" + sum);
school[i].sum = sum;
sum = 0;
System.out.println("**********************");
}
}
public static void printByScore() {
int temp = 0;
int[] temps = new int[n];
for (int i = 0; i < n; i++) {
temps[i] = school[i].sum;
}
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (temps[i] > temps[j]) {
temp = temps[i];
temps[i] = temps[j];
temps[j] = temp;
}
}
}
System.out.println("按照总分排序学校如下:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (temps[i] == school[j].sum) {
System.out.println("总分第" + (n - i) + "名的学校是" + school[j].schoolName);
System.out.println("其总分是:" + school[j].sum);
System.out.println("*******");
}
}
}
}
public static void findByCodes() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要查询的学校编号");
int scode = sc.nextInt();
System.out.println("请输入你想要查询的项目编号");
int pcode = sc.nextInt();
int[] temps = new int[project[pcode - 1].teamnum];
int len = temps.length;
for (int i = 0; i < len; i++) {
temps[i] = project[pcode - 1].score[i];
}
int temp = 0;
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
if (temps[i] > temps[j]) {
temp = temps[i];
temps[i] = temps[j];
temps[j] = temp;
}
}
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (project[pcode - 1].score[i] == temps[j]) {
System.out.println("该学校在该项目中排第" + j + "名");
}
}
}
}
public static void getTopThree() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您想要查询的项目的编号:");
int code = sc.nextInt();
int[] temps = new int[project[code - 1].teamnum];
int len = temps.length;
for (int i = 0; i < len; i++) {
temps[i] = project[code - 1].score[i];
}
int temp = 0;
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
if (temps[i] > temps[j]) {
temp = temps[i];
temps[i] = temps[j];
temps[j] = temp;
}
}
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < n; j++) {
if (temps[len - 1] == project[code - 1].score[i]) {
if (school[j].schoolCode == project[code - 1].jointeam[i]) {
System.out.println("第一名的学校是:" + school[j].schoolName);
}
}
if (temps[len - 2] == project[code - 1].score[i]) {
if (school[j].schoolCode == project[code - 1].jointeam[i]) {
System.out.println("第二名的学校是:" + school[j].schoolName);
}
}
if (temps[len - 3] == project[code - 1].score[i]) {
if (school[j].schoolCode == project[code - 1].jointeam[i]) {
System.out.println("第三名的学校是:" + school[j].schoolName);
}
}
}
}
}
}
【运行图片】
项目2:校园导游咨询
【问题描述】
设计一个校园导游程序,为来访的客人提供各种信息查询服务。
【基本要求】
(1) 设计你所在学校的校园平面图,所含景点不少于10个.以图中顶点表示校内各景点,存放景点名称、代号、简介 等信息;以边表示路径,存放路径长度等相关信息。
(2) 为来访客人提供图中任意景点相关信息的查询。
(3) 为来访客人提供图中任意景点的问路查询,即查询任意两个景点之间的一条最短的简单路径。
【测试数据】
以江苏科技大学长山校区为例。
【实现提示】
一般情况下,校园的道路是双向通行的,可设校园平面图是一个无向网.顶点和边均含有相关信息.
【代码】
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define OK 1
#define ERROR 0
#define MaxInt 32767 //极大值
#define MVNum 50 //最大顶点数
typedef int Status;
typedef string VerTexType; //假设顶点的数据类型为string类型
typedef string InfoType;
typedef int ArcType; //假设边的权值类型为整型
bool visited[MVNum]; //访问标志数组,值为"false"时代表该顶点没被访问,值为"true"时代表该顶点已被访问
typedef struct vex {
VerTexType name;
InfoType otherinfo;
}vex, * vexptr;
//图的邻接矩阵存储表示
typedef struct {
vex vexs[MVNum]; //顶点表
ArcType arcs[MVNum][MVNum]; //邻接矩阵
int vexnum, arcnum; //图的当前点数和边数
}AMGraph;
//获取顶点位置:若G中存在顶点u,则返回该顶点在图中的位置;否则返回其他信息
int LocateVex(AMGraph G, VerTexType u) {
int index = -1; //原始下标,没找到元素返回-1
for (int i = 0; i < G.vexnum; i++) { //遍历顶点数组
if (u == G.vexs[i].name) {
index = i; //记录元素下标
}
}
return index; //返回下标
}
//采用邻接矩阵表示法创建无向网G,表示地图
Status CreateUDN(AMGraph& G) {
G.vexnum = 9;
G.arcnum = 18;
G.vexs[0].name = "3号组团";
G.vexs[0].otherinfo = "3号组团简介";
G.vexs[1].name = "西苑食堂";
G.vexs[1].otherinfo = "西苑食堂简介";
G.vexs[2].name = "明德园";
G.vexs[2].otherinfo = "明德园简介";
G.vexs[3].name = "文体中心";
G.vexs[3].otherinfo = "文体中心简介";
G.vexs[4].name = "西操场";
G.vexs[4].otherinfo = "西操场简介";
G.vexs[5].name = "文理大楼";
G.vexs[5].otherinfo = "文理大楼简介";
G.vexs[6].name = "北苑";
G.vexs[6].otherinfo = "北苑简介";
G.vexs[7].name = "求索园";
G.vexs[7].otherinfo = "求索园简介";
G.vexs[8].name = "东苑食堂";
G.vexs[8].otherinfo = "东苑食堂简介";
G.vexs[9].name = "图书馆";
G.vexs[9].otherinfo = "图书馆简介";
for (int i = 0; i < G.vexnum; ++i) { //初始化邻接矩阵,边的权值均置为极大值MaxInt
for (int j = 0; j < G.vexnum; ++j) {
G.arcs[i][j] = MaxInt;
}
}
G.arcs[0][1] = G.arcs[1][0] = 100;
G.arcs[0][3] = G.arcs[3][0] = 200;
G.arcs[1][2] = G.arcs[2][1] = 80;
G.arcs[1][3] = G.arcs[3][1] = 150;
G.arcs[2][4] = G.arcs[4][2] = 120;
G.arcs[2][5] = G.arcs[5][2] = 110;
G.arcs[3][4] = G.arcs[4][3] = 50;
G.arcs[4][7] = G.arcs[7][4] = 150;
G.arcs[4][8] = G.arcs[8][4] = 230;
G.arcs[5][6] = G.arcs[6][5] = 80;
G.arcs[5][7] = G.arcs[7][5] = 60;
G.arcs[6][9] = G.arcs[9][6] = 100;
G.arcs[7][8] = G.arcs[8][7] = 90;
G.arcs[7][9] = G.arcs[9][7] = 70;
G.arcs[8][9] = G.arcs[9][8] = 50;
return OK;
}
//输出景点介绍
void PrintInfo(AMGraph G, VerTexType a) {
for (int i = 0; i < G.vexnum; i++) {
if (G.vexs[i].name == a) {
cout << G.vexs[i].otherinfo << endl;
return;
}
}
cout << "没有此景点。" << endl;
return;
}
//景点a1到景点a2的最短路径
void ShortestPath(AMGraph G, VerTexType a1, VerTexType a2) {
int v, w;
int n = G.vexnum; //n为G中顶点个数 //记录从源点v0到终点vi是否已被确定最短路径长度,true表示确定,false表示尚未确定
int* Path = new int[n];//记录从源点v0到终点vi的当前最短路径上vi的直接前驱顶点序号
int* D=new int[n];
bool* S=new bool[n]; //记录从源点v0到终点vi的当前最短路径长度
int v1 = LocateVex(G, a1);
int v2 = LocateVex(G, a2);
for (v = 0; v < n; ++v) { //n个顶点依次初始化
S[v] = false; //S初始化为空集
D[v] = G.arcs[v1][v]; //将v0到各个终点的最短路径长度初始化为弧上的权值
if (D[v] < MaxInt) Path[v] = v1; //如果v0和v之间有弧,则将v的前驱置为v0
else Path[v] = -1; //如果v0和v之间无弧,则将v的前驱置为-1
}
S[v1] = true; //将v0加入S
D[v1] = 0; //源点到源点的距离为0
/*----------初始化结束,开始主循环,每次求得v0到某个顶点v的最短路径,将v加到S集----------*/
for (int i = 1; i < n; ++i) { //对其余n-1个顶点,依次进行计算
int min = MaxInt;
for (w = 0; w < n; ++w) {
if (!S[w] && D[w] < min) {
v = w;
min = D[w]; //选择一条当前最短路径,终点为v
}
}
S[v] = true; //将v加入S
for (w = 0; w < n; ++w) { //更新从v_0出发到集合V-S上所有顶点的最短路径长度
if (!S[w] && (D[v] + G.arcs[v][w] < D[w])) {
D[w] = D[v] + G.arcs[v][w]; //更新D[w]
Path[w] = v; //更改w的前驱为v
}
}
}
int a = v2;
cout << G.vexs[a].name;
while (Path[a] != -1) {
cout << "<-" << G.vexs[Path[a]].name;
a = Path[a];
}
delete Path;
delete D;
delete S;
}
int main() {
AMGraph G;
int m;
VerTexType v1, v2;
VerTexType b;
CreateUDN(G);
while (1) {
cout << "**************************校园导游咨询***************************" << endl;
cout << "* 【1】. 问路查询 *" << endl;
cout << "* 【2】. 景点信息查询 *" << endl;
cout << "* 【3】. 退出程序 *" << endl;
cout << "*****************************************************************" << endl;
cin >> m;
switch (m) {
case 1: {
VerTexType v1, v2;
cout << "请输入起点和终点:";
cin >> v1 >> v2;
cout << v1 << "到" << v2 << "的最短路径为:";
ShortestPath(G, v1, v2);
cout << endl;
system("pause");
system("cls");
break;
}
case 2: {
cout << "请输入要查询的景点名称:";
cin >> b;
PrintInfo(G, b);
system("pause");
system("cls");
break;
}
case 3:
return 0;
}
}
}
【运行图片】
项目3:算术表达式求解
【问题描述】
设计一个简单的算术表达式计算器。
【基本要求】
实现标准整数类型的四则运算表达式的求值(包含括号,可多层嵌入).
【测试数据】
(30+2*70)/3-12*3
5+(9*(62-37)+15)*6
要求自行设计非法表达式,进行程序测试,以保证程序的稳定运行。
【实现提示】
可以设计以下辅助函数
status isNumber(char ReadInChar); //视ReadInchar 是否是数字而返回 TRUE 或 FALSE 。
int TurnToInteger(char IntChar); // 将字符’0’.’9’ 转换为整数
【代码】
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
#include<string.h>
using namespace std;
stack<int> q1;//数字栈
stack<char> q2;//符号栈
int flag = 0;//用来判断第一位是‘-’的情况
int flag2 = 0;//用来判断第一位是‘-’的情况
int flag1 = 0;//用来判断除数是0的情况
//取出数字栈顶两个数字进行s运算函数,需要注意!因为我们需要对数字栈进行入
//栈出栈操作,改变了数字栈的结构,所以我们需要在栈名前加“&”
void calculate(stack<int>& q1, char s)
{
int x, y, z;
switch (s)
{
case '+':
{
x = q1.top();
q1.pop();
y = q1.top();
q1.pop();
z = x + y;
q1.push(z);
break;
}
case '-':
{
x = q1.top(); q1.pop();
y = q1.top(); q1.pop();
z = y - x;
q1.push(z);
break;
}
case '*':
{
x = q1.top(); q1.pop();
y = q1.top(); q1.pop();
z = x * y;
q1.push(z);
break;
}
case '/':
{
x = q1.top(); q1.pop();
y = q1.top(); q1.pop();
if (x == 0)
{
flag1 = 1;
break;
}
z = y / x;
q1.push(z);
break;
}
}
}
//用数字来表示符号优先级
int fuhao(char s)
{
if (s == '+' || s == '-')
return 1;
if (s == '*' || s == '/')
return 2;
if (s == '(' || s == ')')
return 3;
if (s == '#')
return -1;
}
//对两个栈进行入栈出栈以及计算的函数
void function(char a[100])
{
q2.push('#');//在符号栈先入一个“#”,防止后序找栈顶为空的时候发生异常
int len = strlen(a);
if (a[0] == '-')//如果第一个数字是负号的情况,要单独考虑
{
flag2 = 1;
a[0] = '0';
}
for (int i = 0; i < len; i++)//主循环
{
if (a[i] == '(')
{
q2.push(a[i]);
}
else if (a[i] == ')')
{
while (q2.top() != '(')
{
calculate(q1, q2.top());
q2.pop();
}
q2.pop();
}
else if (a[i] >= '0' && a[i] <= '9')
{
int num = 0;
//数字不一定只有一位,如果是多位数字要进行处理
while (a[i] >= '0' && a[i] <= '9')
{
num *= 10;
num += (a[i] - '0');
i++;
}
i--;
if (flag2 == 1 && flag == 0)//对第一位是负数的单独处理
num *= -1;
q1.push(num);
flag = 1;
}
else
{
if (a[i] == '+' || a[i] == '-')
{
if ((fuhao(q2.top()) >= fuhao(a[i])) && q2.top() != '(')
{
while (fuhao(q2.top()) >= fuhao(a[i]) && q2.top() != '(')
{
calculate(q1, q2.top());
q2.pop();
}
q2.push(a[i]);
}
else
{
q2.push(a[i]);
}
}
else if (a[i] == '*' || a[i] == '/')
{
if (fuhao(q2.top()) == fuhao(a[i]))
{
calculate(q1, q2.top());
if (flag1 == 1)
{
printf("error");
return;
}
q2.pop();
q2.push(a[i]);
}
else
{
q2.push(a[i]);
}
}
}
}
//对栈中剩余数字进行处理,当字符栈空了自然运算就结束了
while (q2.top() != '#')
{
calculate(q1, q2.top());
if (flag1 == 1)
{
printf("error");
return;
}
q2.pop();
}
printf("%d", q1.top());
}
int main()//主函数
{
char a[100];
scanf_s("%s", a, 100);
function(a);
return 0;
}
【运行图片】