#include<iostream>
#include<fstream>
#include<stdio.h>
#include<time.h>
using namespace std;
typedef struct student
{
char name[20]; //学生姓名
int score; //学生成绩
}student;
///将学生的成绩写入到student文件中
void Set_student(struct student s)
{
FILE *fp=NULL;
// cout<<s.name<<" "<<s.score<<endl;
if((fp=fopen("D:\\stu.txt","a+"))==NULL)
{
cout<<"学生信息文件打开失败"<<endl;
exit(0);
}
//fwrite(&s,sizeof(struct student_type),1,fp);
fprintf(fp,"%s %d",s.name,s.score);
fclose(fp);
}
//将所有学生的成绩进行排序
void Std_sort()
{
FILE *fp=NULL;
struct student s[100],std_temp;
int i,j,num,k;
if((fp=fopen("D:\\stu.txt","r"))==NULL)
{
cout<<"用户信息文件打开失败"<<endl;
exit(0);
}
i=0;
while(!feof(fp)) //读文件
{
fscanf(fp,"%s %d",&s[i].name,&s[i].score);
i++;
}
num=i;
for(i=0;i<num;i++) //这里利用冒泡排序进行排序
{
k=i;
for(j=i;j<num;j++)
{
if(s[k].score<s[j].score)
{
k=j;
}
}
if(k!=i) //交换std[i]和std[k]的值
{
strcpy(std_temp.name,s[i].name);
std_temp.score=s[i].score;
strcpy(s[i].name,s[k].name);
s[i].score=s[k].score;
strcpy(s[k].name,std_temp.name);
s[k].score=std_temp.score;
}
}
for(i=0;i<num;i++)
{
cout<<s[i].name<<" "<<s[i].score<<endl;
}
fclose(fp);
system("pause");
}
void My_find()
{
FILE *fp=NULL;
struct student s[100];
int i,num,k;
char str[20];
cout<<"请输入你要查找学生的姓名"<<endl;
cin>>str;
if((fp=fopen("D:\\stu.txt","r"))==NULL)
{
cout<<"用户信息文件打开失败"<<endl;
exit(0);
}
i=0;
while(!feof(fp)) //读文件
{
fscanf(fp,"%s %d",&s[i].name,&s[i].score);
i++;
}
num=i;
k=0;
for(i=0;i<num;i++)
{
if(strcmp(s[i].name,str)==0)
{
cout<<s[i].name<<" "<<s[i].score<<endl;
k=1;
}
}
if(k==0)
{
cout<<"未找到相关学生的信息"<<endl;
}
fclose(fp);
system("pause");
}
int My_menu()
{
int ch;
cout<<"*******WELCOME MY CALCULATOR********"<<endl;
cout<<"1.显示所有测试者的个人信息"<<endl;
cout<<"2.进入测试"<<endl;
cout<<"3.查找某个学生的成绩"<<endl;
cout<<"4.离开"<<endl;
cin>>ch;
while(ch<1||ch>4)
{
cout<<"1.显示所有测试者的个人信息"<<endl;
cout<<"2.进入测试"<<endl;
cout<<"3.查找某个学生的成绩"<<endl;
cout<<"4.离开"<<endl;
cin>>ch;
}
return ch;
}
void My_calc()
{
int i,j,plus,k;
int x,y,z;
int ca;
struct student test_std;
char score[20];
cout<<"Input you name"<<endl;
cin>>test_std.name;
test_std.score=0;
srand((unsigned)time(NULL));
for(i=0;i<10;i++)
{
plus=rand()%10+1;
if(plus%2==1)
{
x=rand()%24+1; //生成随机数
y=rand()%24+1;
for(j=0;j<3;j++) //具有三次回答问题的机会
{
cout<<"第"<<j+1<<"次解答,请输入你的答案"<<" ";
cout<<x<<"+"<<y<<"=?"<<endl;
cin>>score;
k=0;
z=0;
while(score[k]!='\0')
{
z=z*10+score[k]-'0';
k++;
}
if(z==x+y)
{
cout<<"Congratulation"<<endl;
if(j==0)
{
test_std.score=test_std.score+10;
}
else if(j==1)
{
test_std.score=test_std.score+7;
}
else
{
test_std.score=test_std.score+5;
}
break;
}
else
{
if(j==2)
{
cout<<"Sorry, this problem is not right."<<endl;
}
else
{
cout<<"What a pity,you have"<<2-j<<"chance"<<endl;
}
}
}///end for j
}//end if
else
{
x=rand()%49+1;
y=rand()%x+1;
for(j=0;j<3;j++)
{
cout<<"第"<<j+1<<"次解答,请输入你的答案"<<" ";
cout<<x<<"-"<<y<<"=?"<<endl;
cin>>score;
k=0;
z=0;
while(score[k]!='\0')
{
z=z*10+score[k]-'0';
k++;
}
if(z==x-y)
{
cout<<"Congratulation"<<endl;
if(j==0)
{
test_std.score=test_std.score+10;
}
else if(j==1)
{
test_std.score=test_std.score+7;
}
else
{
test_std.score=test_std.score+5;
}
break;
}
else
{
if(j==2)
{
cout<<"Sorry, this problem is not right"<<endl;
}
else
{
cout<<"What a pity,you have"<<2-j<<"chance"<<endl;
}
//cout<<x<<"-"<<y<<"=?"<<endl;
}
}//end for j
}//end else
}//end for i
ca=test_std.score/10;
switch(ca)
{
case 10:
cout<<test_std.name<<" score: "<<test_std.score<<" "<<"SMART"<<endl;
break;
case 9:
cout<<test_std.name<<" score: "<<test_std.score<<" "<<"SMART"<<endl;
break;
case 8:
cout<<test_std.name<<" score: "<<test_std.score<<" "<<"GOOD"<<endl;
break;
case 7:
cout<<test_std.name<<" score: "<<test_std.score<<" "<<"OK"<<endl;
break;
case 6:
cout<<test_std.name<<" score: "<<test_std.score<< " "<<"PASS"<<endl;
break;
default:
cout<<test_std.name<<" score: "<<test_std.score<<" "<<"TRY AGAIN"<<endl;
break;
}
Set_student(test_std);
system("pause");
}
int main()
{
int choose=0;
choose=My_menu();
while(choose!=4)
{
switch(choose)
{
case 1:
Std_sort();
break;
case 2:
My_calc();
break;
case 3:
My_find();
break;
case 4:
return 0;
break;
}
system("CLS");
choose=My_menu();
}
return 0;
}
C 语言实现的一个随机加减法运算计算器
需积分: 50 122 浏览量
2015-11-25
11:59:09
上传
评论 1
收藏 1.11MB ZIP 举报
nuaazhangchao
- 粉丝: 0
- 资源: 11
最新资源
- 测试aaaaaaabbbbb
- VID20240521070643.mp4
- Android系统原理与开发学习要点详解-培训课件.zip
- 部署yolov8的tensorrt模型支持检测分割姿态估计的C++源码+部署步骤.zip
- 以简单、易用、高性能为目标、开源的时序数据库,支持Linux及Windows, Time Series Database.zip
- python-leetcode面试题解之第198题打家劫舍-题解.zip
- python-leetcode面试题解之第191题位1的个数-题解.zip
- python-leetcode面试题解之第186题反转字符串中的单词II-题解.zip
- 一个基于python的web后端高性能开发框架,下载可用
- python-leetcode面试题解之第179题最大数-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈