#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int readstudents(struct students stu[]); //讀取學生資訊
int readsexcode(struct sexcode sex[]); //讀取性別代碼
int readcollegecode(struct collegecode colle[]); //讀取學院代碼
void transform(struct students stu[],struct sexcode sexy[],struct collegecode colle[],int a,int b,int c); //錄入
void namesort(struct students stu[],int a); //按照姓名排序
void averagesort(struct students stu[],int a); //平均分排序
void searchcollege(struct students stu[],int a); //按學院搜索學生
void namesearch(struct students stu[],int a); //按名字搜索
void printstudent(struct students stu[],struct sexcode sex[],struct collegecode colle[],int a,int b,int c); //輸出
void change(struct students stu[],int a);
int passwordfun();
void passwordchange();
void averagefun(struct students stu[],int a);
struct students{
int num;
char name[20];
int sexnum;
char sex[4];
int collegenum;
char college[20];
int score[10];
float average;
};
struct sexcode{
int sexnum;
char sex[4];
};
struct collegecode{
int collegenum;
char college[20];
};
int main()
{
int a,b,c;
int choice;
struct students stu[100];
struct sexcode sexy[2];
struct collegecode colle[10];
int t = passwordfun();
if(t == 0){
printf("請使用正確密碼重新登陸程式!\n");
return 0;
}
while(1){
system("cls");
printf("***********學生成績系統選單************ \n* 1. 按姓名排序,輸出 *\n* 2. 按平均成績排序,輸出 *\n* 3. 輸出給定學院學生 *\n* 4. 修改給定學生成績資訊 *\n* 6. 按姓名查詢學生,輸出 *\n* 7.洗掉學生成績 *\n* 8. 修改系統密碼 *\n* 0. 回傳上一級選單 *\n***************************************\n");
}
}
int readstudents(struct students stu[]) //讀取學生資訊
{
int i = 0;
FILE *fp;
fp = fopen("D:\\abc\\Student_Info.txt","r+");
if(fp == 0){
printf("can not open this file\n");
exit (0);
}
fscanf(fp,"%d",&stu[i].num);
while(!feof(fp)){
fscanf(fp,"%s%d%d",stu[i].name,&stu[i].sexnum,&stu[i].collegenum);
for(int j = 0 ;j < 10; j++ ){
fscanf(fp,"%d",&stu[i].score[j]);
}
i++;
fscanf(fp,"%d",&stu[i].num);
}
fclose(fp);
return i;
}
int readsexcode(struct sexcode sexy[]) //讀取學生性別資訊
{
int i = 0;
FILE *fp;
fp = fopen("D:\\abc\\S_Info.txt","r");
if(fp == 0){
printf("can not open this file\n");
exit (0);
}
fscanf(fp,"%d",&sexy[i].sexnum);
while(!feof(fp)){
fscanf(fp,"%s",sexy[i].sex);
i++;
fscanf(fp,"%d",&sexy[i].sexnum);
}
fclose(fp);
return i;
}
int readcollegecode(struct collegecode colle[]) //讀取學生學院資訊
{
int i = 0;
FILE *fp;
fp = fopen("D:\\abc\\C_Info.txt","r");
if(fp == 0){
printf("can not open this file\n");
exit (0);
}
fscanf(fp,"%d",&colle[i].collegenum);
while(!feof(fp)){
fscanf(fp,"%s",colle[i].college);
i++;
fscanf(fp,"%d",&colle[i].collegenum);
}
fclose(fp);
return i;
}
void averagefun(struct students stu[],int a) //平均分
{
for(int i = 0; i < a; i++){
int sum = 0;
for(int j = 0; j < 10; j++){
sum = sum + stu[i].score[j];
}
stu[i].average = 1.0 * sum / 10;
}
return ;
}
void namesort(struct students stu[],int a) //按姓名排序
{
for(int i = 0; i < a-1; i++){
for(int j = i+1; j < a ; j++){
if(strcmp(stu[i].name,stu[j].name) < 0){
struct students temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
return ;
}
void averagesort(struct students stu[],int a) // 按平均分排序
{
for(int i = 0; i < a-1; i++){
for(int j = i+1; j < a ; j++){
if(stu[i].average < stu[j].average){
struct students temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
return ;
}
void printstudent(struct students stu[],struct sexcode sexy[],struct collegecode colle[],int a,int b,int c) //輸出資訊
{
for(int i = 0;i < a; i++){
printf("%d%8s%4s%12s",stu[i].num,stu[i].name,stu[i].sex,stu[i].college);
for(int j = 0; j < c; j++){
printf("%4d",stu[i].score[j]);
}
printf("%7.2f\n",stu[i].average);
}
return ;
}
void transform(struct students stu[],struct sexcode sexy[],struct collegecode colle[],int a,int b,int c) //修改資訊
{
for(int i = 0; i < a; i++){
for(int j = 0; j < b; j++){
if(stu[i].sexnum == sexy[j].sexnum){
strcpy(stu[i].sex,sexy[j].sex);
break;
}
}
for(int j = 0; j < c; j++){
if(stu[i].collegenum == (colle[j].collegenum - 1)){
strcpy(stu[i].college,colle[j].college);
break;
}
}
}
}
void searchcollege(struct students stu[],int a) //查找學院并輸出
{
char collegename[20];
printf("請輸入需要查找的學院的名稱:\n") ;
scanf("%s",collegename);
for(int i = 0;i < a; i++){
if(strcmp(stu[i].college,collegename) == 0){
printf("%d%8s%4s%12s",stu[i].num,stu[i].name,stu[i].sex,stu[i].college);
for(int j = 0; j < 10; j++){
printf("%4d",stu[i].score[j]);
}
printf("%7.2f\n",stu[i].average);
}
}
return ;
}
void namesearch(struct students stu[],int a) //按姓名查找
{
printf("請輸入需要查找的學生的姓名:\n");
char name[20];
scanf("%s",name);
for(int i = 0;i < a; i++){
if(strcmp(stu[i].name,name) == 0){
printf("%d%8s%4s%12s",stu[i].num,stu[i].name,stu[i].sex,stu[i].college);
for(int j = 0; j < 10; j++){
printf("%4d",stu[i].score[j]);
}
printf("%7.2f\n",stu[i].average);
}
}
return ;
}
void change(struct students stu[],int a) //按名字搜索修改分數
{
char name[20];
printf("請輸入需要修改成績的學生的姓名:\n");
scanf("%s",name);
printf("請輸入改學生各科成績,不需要修改的按原樣輸入:\n");
for(int i = 0;i < a; i++){
if(strcmp(stu[i].name,name) == 0){
for(int j = 1; j <= 10; j++){
printf("第%d門成績: ",j);
scanf("%d",&stu[i].score[j-1]);
printf("\n");
}
}
}
int i = 0;
FILE *fp;
fp = fopen("D:\\abc\\Student_Info.txt","w");
if(fp == 0){
printf("can not open this file\n");
exit (0);
}
while(i<100){
fprintf(fp,"%d %s %d %d",stu[i].num,stu[i].name,stu[i].sexnum,stu[i].collegenum);
for(int j = 0 ;j < 10; j++ ){
fprintf(fp," %d",stu[i].score[j]);
if(j == 9){
fprintf(fp,"\n");
}
}
i++;
}
fclose(fp);
return ;
}
int passwordfun() //密碼
{
char password_1[20],password_2[20],password_3[20];
int i = 0;
FILE *fp;
fp = fopen("D:\\abc\\password.txt","r");
if(fp == 0){
printf("can not open this file\n");
exit (0);
}
fscanf(fp,"%s",password_2);
fclose(fp);
if(password_2[i] == '\0'){ //當檢測到密碼文本為空時
printf("歡迎使用學生成績管理系統!\n首次使用系統,您可以直接進入主選單,進入主選單后請及時修改密碼!\n");
system("pause");
return 1;
}
else
{
printf("請輸入系統密碼:\n");
//保存輸入的密碼
char s[20];
int t,n_,x;
for(x=0;x<20;x++){
s[x]=getch();
if(s[x]=='\r') break;
printf("*");
}
s[x]='\0';
for(t=0;t<20;t++){ //將輸入的密碼存放在password_1中
password_1[t]=s[t];
}
for(i = 0; password_2[i] != '\0'; i++){
if(password_2[i] <= 127 && password_2[i] >= 4){ //password_2為真實密碼
password_3[i] = password_2[i] - 3; //密碼密文加密(+3)
}else{
password_3[i] = 127 - password_3[i];
} //password_3為按演算法處理后的真實密碼
}
password_3[i] = '\0';
if(strcmp(password_1,password_3) == 0){
return 1;
}else{
printf("密碼錯誤!\n程式結束!請使用正確密碼重新登入程式!\n");
system("pause");
return 0;
}
}
}
void passwordchange() //密碼修改
{
FILE *fp;
char newpassword_1[20];
char newpassword_2[20];
int i = 0;
printf("請輸入新的密碼:\n");
scanf("%s",newpassword_1);
for(i = 0; newpassword_1[i] != '\0'; i++){
if(newpassword_1[i] + 3 <= 127){
newpassword_2[i] = newpassword_1[i] + 3;
}else{
newpassword_2[i] = newpassword_1[i] + 3 -127;
}
}
newpassword_2[i] = '\0';
fp = fopen("D:\\abc\\password.txt","w");
if(fp == 0){
printf("can not open this file\n");
exit (0);
}
fprintf(fp,"%s",newpassword_2);
fclose(fp);
printf("密碼修改成功!\n");
exit(0);
}
編譯沒有問題 但是運行的時候顯示的是can not open this file 有大佬幫幫忙嗎
uj5u.com熱心網友回復:
fp = fopen("D:\\abc\\password.txt", "r");可能是你D 盤沒有這個檔案,所以它exit(0);退出了
uj5u.com熱心網友回復:
可能是因為沒有這個檔案,或者檔案沒有讀取權限,在每個printf("can not open this file\n");的地方加上打不開的檔案名,比如改成printf("can not open password.txt\n");,然后你就知道到底是沒有哪個檔案了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/53561.html
標籤:新手樂園
下一篇:亂數不隨機?
