C语言课程设计报告
题目:学生信息管理系统姓名:刘轲
学号:
专业:计算机科学与技术班级:计算机 311409010122 1401
第二篇:c语言课程设计设计报告
姓名:
学号:
年 月
目录:
● 程序功能简介
● 题目分析
● 函数的调用关系图及主要算法
● 源程序及注释
● 测试流程
● 心得感想
程序一:简单数学问题
一、程序功能简介:
实现多个简单数学问题的求解。
二、题目分析
完成与菜单项对应的功能设计
1、void FindNum( ); /*用穷举法找到被咬掉的数字*/
2、void FindRoot( ); /*求解方程ax2+bx+c=0的根*/
3、void Detective( ); /*根据线索用穷举法找出牌照号码*/
4、void Monkey( ); /*根据天数循环求出第一天所摘桃子数*/
5、void Diamond( ); /*调用函数Print_Diamond( )输出line行的钻石图案*/
6、void Calculator( ); /*实现两个整数简单的加减乘除四则运算*/
三、函数的调用关系图:
四、源程序及注释:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
int menu_select();
void main() /*主函数*/
{
for(;;)
{
switch(menu_select())
{case 1:
FindNum();
system("pause");
break;
case 2:
FindRoot();
system("pause");
break;
case 3:
Detective();
system("pause");
break;
case 4:
Monkey();
system("pause");
break;
case 5:
Diamond();
system("pause");
break;
case 6:
Calculator();
system("pause");
break;
case 0:
printf("Goodbye!\n");
system("pause");
exit(0);
}
}
}
int menu_select() /*主函数菜单*/ {
char c;
do{
system("cls");
printf("1.FindNum\n");
printf("2.FindRoot\n");
printf("3.Detective\n");
printf("4.Monkey\n");
printf("5.Diamond\n");
printf("6.Calculator\n");
printf("0.Goodbye!\n");
c=getchar();
}while(c<'0'||c>'6');
return(c-'0');
}
FindNum() /*找数字*/
{
int i;
for(i=1;i<=9;i++) /*穷举*/
if((30+i)*6237==(10*i+3)*3564)
printf("%d\n",i);
}
FindRoot() /*找方程的根*/
{
float a[10],b[10],c[10],disc,x1,x2,real,image; /*定义abc三个数组存放方程的系数*/
int i,k,j;
for(i=0;i<10;i++) /*循环输入方程的三个系数*/
{
printf("please input three real numbers:\n");
scanf("%f%f%f",&a[i],&b[i],&c[i]);
printf("press '1' to continue\npress '2' to calculate\n");
scanf("%d",&k);
if (k==2) break; /*判断是否继续输入*/
}
for(j=0;j<=i;j++)
{
printf("The equation %d",j+1);
if (fabs(a[j])<=1e-7) /*a=0的情况*/
printf(" is not quadratic, solution is %.2f\n",-c[j]/b[j]);
else
{
disc=b[j]*b[j]-4*a[j]*c[j];
if (fabs(disc)<=1e-7) /*有两个相等的实根的情况*/
printf(" is not quadratic,solution is: %.2f\n",-b[j]/(2*a[j]));
else if (disc>1e-7) /*有两个不等实根的情况*/
{
x1=(-b[j]+sqrt(disc))/(2*a[j]);
x2=(-b[j]-sqrt(disc))/(2*a[j]);
printf(" has distinct real roots: %.2f and %.2f\n",x1,x2); }
else /*有两个虚根的情况*/
{
real=-b[j]/(2*a[j]);
image=sqrt(-disc)/(2*a[j]);
printf(" has complex roots:\n");
printf("%.2f+%.2f i\n",real,image);
printf("%.2f-%.2f i\n",real,image);
}
}
}
}
Detective() /*找牌照*/
{
int i,a,b,c,d;long m;
for(i=32;i<=99;i++) /*穷举找出后四位号码*/ {
m=i*i;
a=m%10;
b=m/10%10;
c=m/100%10;
d=m/1000;
if(a==b&&c==d)
break;
}
printf("%ld\n",m+310000);
}
Monkey() /*猴子吃桃问题*/
{
int n,i;
long x;
scanf("%d",&n);
x=1;
for(i=n-1;i>=1;i--)
x=(x+1)*2;
printf("%ld\n",x);
}
void print_diamond(int line)
{
int i,j,k,m,n,p,q,a,b;
for(i=1;i<=(49-line)/2;i++) /*输出(49-line)/2行的空格*/
printf("\n");
for(j=1;j<=(line+1)/2;j++)
{ for(m=1;m<=(80-line)/2;m++) /*先输出(80-line)/2个空格*/ printf(" ");
for(a=1;a<=(line-(2*j-1))/2;a++) /*行数j从1到中间一行输出(line-(2*j-1))/2个空格*/
printf(" ");
for(n=1;n<=2*j-1;n++) /*中间行及上面每行输出2*j-1个"*"*/ printf("*");
printf("\n");
}
for(k=1;k<=(line-1)/2;k++) /*同理输出中间行下面的钻石图案*/ { for(p=1;p<=(80-line)/2;p++)
printf(" ");
for(b=1;b<=k;b++)
printf(" ");
for(q=1;q<=line-2*k;q++)
printf("*");
printf("\n");
}
}
Diamond() /*钻石*/
{
int line;
scanf("%d",&line);
print_diamond(line); /*调用print_diamond输出钻石图案*/
}
Calculator() /*简单计算器*/
{
int a[4],b[4],c[4],i,j,k; /*定义ab两个数组存放数*/
char ch[4]; /*定义c存放运算符*/
FILE *fp1,*fp2;
fp1=fopen("expres.txt","r"); /*打开expres文件*/
if(fp1==NULL) /*判断打开成功*/
{
printf("Can not open file expres\n");
exit(1);
}
for(i=0;i<=3;i++)
fscanf(fp1,"%d%c%d",&a[i],&ch[i],&b[i]); /*循环读入四道题*/ for(j=0;j<=3;j++)
{
switch(ch[j]) /*判断运算符*/
{ case '+':c[j]=a[j]+b[j]; break;
case '-':c[j]=a[j]-b[j]; break;
case '*':c[j]=a[j]*b[j]; break;
case '/':c[j]=a[j]/b[j]; break;
}
}
fp2=fopen("result.txt","w"); /*打开文件result*/
if(fp2==NULL) /*判断打开文件是否成功*/
{
printf("Can not open file expres\n");
exit(2);
}
for(k=0;k<=3;k++) /*循环向文件中输入结果*/
fprintf(fp2,"%d%c%d=%d\n",a[k],ch[k],b[k],c[k]);
printf("succeed!\n");
}
五、测试流程:
运行程序
屏幕显示:1.FindNum
2.FindRoot
3.Detective
4.Monkey
5.Dismond
6.Calculator
0.Goodbye!
输入:1回车
显示:9
Press any key to continue …
按任意键返回菜单
输入:2回车
显示:please input three real number:
输入:0回车 1回车 2回车
显示:press ‘1’ to continue
press’2’ to calculate
输入:1回车
显示:please input three real number:
(按照以上方法再输入三组数据:1,2,1;1,3,2;1,0,4后选择2,回车) 显示:The equation 1 is not quadratic, solution is -2.00
The equation 2 is not quadratic, solution is -1.00
The equation 3 has distinct real roots: -1.00and-2.00
The eauation 4 has complex roots:
-0.00+2.00 i
-0.00-2.00 i
Press and key to continue …
按任意键返回菜单
输入:3 回车
显示:317744
Press and key to continue …
按任意键返回菜单
输入:4 回车
10 回车
显示:1534
Press and key to continue …
按任意键返回菜单
输入:5 回车
5 回车
输出:
*
***
*****
***
*
Press and key to continue …
按任意键返回菜单
输入:6 回车
显示:succeed!
Press and key to continue …
按任意键返回菜单
输入:0 回车
显示:Goodbye!
Press and key to continue …
按任意键结束程序
程序二:学生成绩简单管理
一、 程序功能简介:
用结构体数组结构实现简单的学生管理成绩管理功能,要求具有数据输入、数据输出、数组排序、元素插入、元素删除、查询等功能。
二、 题目分析
完成与菜单项对应的功能设计
1、int Input(struct student stu[],int n); /*从键盘输入若干条纪录,依次存放到结构体数组stud中,n为数组原有纪录数,程序运行初始纪录数为0,函数返回纪录数.*/
2、Display(struct student stu[],int n); /*显示所有记录,每10个暂停一下,n为数组元素个数*/
3、Sort_by_Name(struct student stu[],int n); /*按姓名作升序排序,要求用选择排序法*/
4、 int Insert_a_record(struct student stu[],int n); /*输入待插入的姓名和成绩,按姓名作有序插入,输出插入成功信息,返回纪录个数*/
5、int Delete_a_record(struct student stu[],int n); /*输入待删除的姓名,经确认后删除该姓名的纪录,输出删除成功与否的信息.返回数组中的纪录数*/
6、 int Query_a_recored(struct student stu[],int n);/*输入待查找的姓名,查找该姓名的纪录,输出查找成功与否的信息和该学生的信息*/
7、int Addfrom Text(struct student stud[],int n); /*从文件filename添加一批纪录到数组中 ,返回添加纪录后的新的纪录数*/
8、Write to Text(struct student stu[],int n); /*将数组中的纪录全部写入文件records.txt*/
三、函数调用关系图:
四、源程序及注释:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct student /*定义结构体数组*/
{
char name[20];
float score;
};
int menu_select();
struct student Input_one_record() /*输入一个学生的成绩记录*/ {
struct student stud;
printf("please input the name:\n");
scanf("%s",&stud.name);
printf("please input the score:\n");
scanf("%f",&stud.score);
return stud;
}
int Input(struct student stu[],int n) /*输入学生成绩记录*/
{
for(n=0;n<=39;n++)
{
stu[n]=Input_one_record(); /*调用函数Input_ont_record输入学生成绩记录*/
printf("do you want to continue:('Y'or'N')");
getchar();
if(toupper(getchar())=='N')
break;
}
return n+1;
}
void Display(struct student stu[], int n) /*输出所有学生的成绩记录*/
{
int i,j;
for(i=0;i<3;i++) /*每屏显示十个记录然后暂停*/
{
system("cls");
for(j=10*i;j<=10*i+9;j++) /*循环输出十个记录*/
{
if(j>n-1) break; printf("%s%20f\n",stu[j].name,stu[j].score);
}
if(j>n-1) break;
printf("press any key to display the next ten records:\n");
system("pause");
}
}
void Sort_by_Name(struct student stu[],int n) /*排序*/
{
int i,j,p;
struct student t;
for(i=0;i<=n-2;i++) /*选择排序法*/
{
p=i;
for(j=i+1;j<=n-1;j++) /*找到最小数据的下标*/
{
if(strcmp(stu[j].name,stu[p].name)<0)
p=j;
}
if(p!=i) /*判断最小数据是否在第一个*/
{
t=stu[p];stu[p]=stu[i];stu[i]=t;
}
}
printf("succeed!\n");
}
int Insert_a_record(struct student stu[],int n) /*插入一个学生成绩记录*/ {
struct student new;
struct student *s;
printf("please input a name:\n");
scanf("%s",&new.name);
printf("please input the score:\n");
scanf("%f",&new.score);
s=&new;
n=Insert(stu,n,s); /*调用函数Insert作有序插入*/
return n;
}
int Insert(struct student stu[],int n,struct student *s) /*有序插入*/
{
int i,j;
for(i=0;i<=n-1;i++)
if(strcmp(stu[i].name,s->name)>0) break;
for(j=n-1;j>=i;j--)
stu[j+1]=stu[j];
stu[i]=*s;
return n+1;
}
int Delete_a_record(struct student stu[],int n) /*删除一个学生的成绩记录*/ {
int k;
struct student name_to_delete;
struct student *name;
printf("please input the name you want to delete:\n");
scanf("%s",&name_to_delete.name);
name=&name_to_delete;
printf("Delete %s's record('Y'or'N'):\n",name_to_delete.name);
getchar();
if(toupper(getchar())=='Y')
k=Delete(stu,n,name); /*调用函数Delete查找数据,若存在做删除*/ if(k==-1) /*判断是否存在*/
printf("The record does not exist!\n");
else n=k;
return n;
}
int Delete(struct student stu[],int n,struct student *name)
{
int i,j;
for(i=0;i<=n;i++)
if(strcmp(stu[i].name,name->name)==0) break; /*找到需要删除的数据下标i*/
if(i==n+1) /*判断要删除的数据是否存在*/
return -1; /*不存在,返回提示信息*/
else /*存在,做删除*/
{
for(j=i;j<=n;j++)
stu[j]=stu[j+1];
printf("succeed!\n");
return n-1;
}
}
void Query_a_record(struct student stu[],int n) /*查找一个学生的成绩记录*/
{
struct student *name;
struct student name_to_query;
printf("please input the name you want to query:\n");
scanf("%s",&name_to_query.name);
name=&name_to_query;
if(Query(stu,n,name)==-1) /*调用函数Query判断要查找的记录是否存在*/
printf("%s does not exist!\n"); /*不存在输出提示信息*/
else /*存在,输出记录*/
{
name_to_query=stu[Query(stu,n,name)];
printf("succeed!\n%s\t%f\n",name_to_query.name,name_to_query.score); }
}
int Query(struct student stu[],int n,struct student *name) /*查找记录*/ {
int i;
for(i=0;i<=n-1;i++) /*找到需要查找的记录的下标*/
{
if(strcmp(stu[i].name,name->name)==0)
break;
}
if(i<=n-1) /*判断要查找的记录时候存在*/
return i; /*存在,返回下标i*/
else return -1; /*不存在,返回提示信息*/
}
int AddfromText(struct student stu[],int n) /*从文件中整批输入*/ {
int i,num;
FILE *fp; /*定义文件指针*/
char filename[20];
printf("Input the filename:\n");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("cann't open the file!\n");
system("pause");
return n;
}
fscanf(fp,"%d",&num);
for(i=0;i<num;i++) /*循环读入文件内容*/
fscanf(fp,"%s%f",&stu[n+i].name,&stu[n+i].score);
n+=num;
fclose(fp);
Sort_by_Name(stu,n); /*调用函数Sort_by_Name排序*/ return n;
}
void WritetoText(struct student stu[],int n) /*将记录写到文件*/ {
int i;
FILE *fp;
char filename[20];
printf("Write the records to a Text File\n");
printf("Input the filename:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cann't open the file\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n+1);
for(i=0;i<=n-1;i++) /*循环将记录写到文件中*/ fprintf(fp,"%s\t%f\n",stu[i].name,stu[i].score);
fclose(fp);
printf("succeed!\n");
}
void Quit()
{}
void main() /*主函数*/
{
struct student stu[40];
int n=0;
for(;;)
{
switch(menu_select())
{
case 1: n=Input(stu,n); system("pause"); break; Display(stu, n); system("pause"); break; Sort_by_Name(stu,n); system("pause"); case 2: case 3:
break; n=Insert_a_record(stu,n); system("pause"); break; n=Delete_a_record(stu,n); system("pause"); break; Query_a_record(stu,n); system("pause"); break; n=AddfromText(stu,n); system("pause"); break; WritetoText(stu,n); system("pause"); break; Quit(); system("pause"); case 4: case 5: case 6: case 7: case 8: case 0:
exit(0);
}
}
}
int menu_select() /*显示主菜单*/ {
char c;
do
{
system("cls");
printf("1.Input Records\n");
printf("2.Display()\n");
printf("3.Sort by Name\n");
printf("4.Insert a record\n");
printf("5.Delete a record\n");
printf("6.Query\n");
printf("7.AddfromText\n");
printf("8.WritetoText\n");
printf("0.Goodbye!\n");
printf("Give your choice (1-8,0):");
c=getchar();
} while(c<'0'||c>'8');
return(c-'0');
}
五、测试流程:
运行程序
屏幕显示:1.Input Records
2.Display()
3.Sort by Name
4.Insert a record
5.Delete a record
6.Query
7.AddfromText
8.WritetoText
0 .Goodbye!
Give your choice (1-8,0):
输入:1 回车
显示:please input the name:
输入:hhm 回车
显示:please input the score:
输入:75 回车
显示:Do you want to continue: (‘Y’or’N’)
输入:y 回车
(按照上述方法输入三组学生记录:gm,95;zy,95;zbn,95 再选择‘N’) 屏幕显示:press any key to continue …
按任意键返回主菜单
输入:2 回车
显示:hhm 75.000000
gm 95.000000
zy 95.000000
zbn 95.000000
Press any key to continue…
按任意键返回主菜单
输入:3回车
显示:succeed!
Press any key to continue…
按任意键返回主菜单
输入:2回车
显示:gm 95.000000
hhm 75.000000
zbn 95.000000
zy 95.000000
Press any key to continue…
按任意键返回主菜单
输入:4 回车
显示:please input a name:
输入:xjj回车
显示:please input the score:
输入:88回车
显示:Press any key to continue…
按任意键返回主菜单
输入:2 回车
显示:gm 95.000000
hhm 75.000000
xjj 88.000000
zbn 95.000000
zy 95.000000
Press any key to continue…
按任意键返回主菜单
输入:5 回车
显示:please input the name you want to delete: 输入:hhm
显示:Delete hhm’s record(‘Y’or’N’):
输入:y回车
显示:succeed!
Press any key to continue…
按任意键返回主菜单
输入:5 回车
显示:please input the name you want to delete: 输入:jj回车
显示:Delete jj’s record(‘Y’or’N’):
输入:y回车
显示: The record does not exist!
Press any key to continue…
按任意键返回主菜单
输入:6回车
显示:please input the you want to query: 输入:zbn回车
显示:succeed!
zbn 95.000000
Press any key to continue…
按任意键返回主菜单
输入:6回车
显示:please input the you want to query: 输入:hhm
显示:does not exist!
Press any key to continue…
按任意键返回主菜单
输入:7回车
显示:please input the filename:
输入:data.txt回车
显示:succeed!
Press any key to continue…
按任意键返回主菜单
显示:
dmy
jms
ly 70.000000 88.000000 60.000000 gm 95.000000
lyx 100.000000
mfs 98.000000
wq 99.000000
xdw 90.000000
xjj 88.000000
xl 88.000000
press any key to display the next ten records:
press any key to continue…
xx 90.000000
yjr 88.000000
yq 99.000000
zbn 95.000000
zx 90.000000
zy 95.000000
press any key to continue…
按任意键返回主菜单
输入:8回车
显示:write the records to a Text File
Please input the filename:
输入:records.txt
显示:succeed!
press any key to continue…
按任意键返回主菜单
输入:0回车
显示:press any key to continue…
按任意键结束程序
心得感想:
通过这次的课程设计,我收获了很多。在设计的过程中,我对课本的内容有了进一步的了解,编程及调试程序的能力都得到了较大的提高。在写第一题是还是比较轻松的,但我觉得第二题对我的能力的提高更有帮助,每个函数都调试了n多次,经常报错,当时真的很想砸电脑,不过当我写完了之后又非常地高兴很有成就感,从而促使我继续下去。而且在写第二题的时候开始没什么头绪,好不容易写出来一点东西又二三十个错,或者就是根本没有实现我想要它实现的功能,但是到后来慢慢的就越写越顺畅,真的很有意思啊!