fix:some strcmp caused bug

main
高世波 1 year ago
parent 7681a396b0
commit 4dfff61c02

Binary file not shown.

@ -6,7 +6,7 @@ void cancel(ss*stu){
char*ID = (char*)malloc(sizeof(char)*5); char*ID = (char*)malloc(sizeof(char)*5);
scanf("%s",ID); scanf("%s",ID);
for(int i = 0; i < stu->nums; ++i){ for(int i = 0; i < stu->nums; ++i){
if(strcmp(stu->student[i].id,ID)){ if(strcmp(stu->student[i].id,ID) == 0){
for(int j = i; j < stu->nums-1; ++j){ for(int j = i; j < stu->nums-1; ++j){
stu->student[j] = stu->student[j+1]; stu->student[j] = stu->student[j+1];
} }
@ -15,5 +15,5 @@ void cancel(ss*stu){
allPrint(stu); allPrint(stu);
printf("continue?\n"); printf("continue?\n");
scanf("%s",tmp); scanf("%s",tmp);
}while(strcmp(tmp,"yes")); }while(!strcmp(tmp,"yes"));
} }

@ -1,29 +1,19 @@
#include "mysyslib.h" #include "mysyslib.h"
void Print(Student*student){
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n",&student->id,&student->class,&student->name,student->score1,student->score2,student->score3,student->score);
}
void allPrint(ss*stu){
for(int i = 0; i < stu->nums; ++i){
Print(&stu->student[i]);
}
}
void input(ss*stu){ void input(ss*stu){
char tmp[3]; char tmp[5];
do{ do{
int pst = stu->nums; int pst = stu->nums;
printf("Id "); printf("Id ");
scanf("%s",&stu->student[pst].id); scanf(" %s",stu->student[pst].id);
printf("\n"); printf("\n");
printf("class "); printf("class ");
scanf("%s",&stu->student[pst].class); scanf(" %s",stu->student[pst].class);
printf("\n"); printf("\n");
printf("name "); printf("name ");
scanf("%s",&stu->student[pst].name); scanf(" %s",stu->student[pst].name);
printf("\n"); printf("\n");
printf("score1 "); printf("score1 ");
@ -43,7 +33,7 @@ void input(ss*stu){
printf("continue?\n"); printf("continue?\n");
scanf("%s",tmp); scanf("%s",tmp);
}while(!strcmp(&tmp,"yes")); }while(!strcmp(tmp,"yes"));
return; return;
} }

@ -1,20 +1,11 @@
#include "mysyslib.h" #include "mysyslib.h"
int main(){ int main(){
char ipt; int ipt;
ss* stu = init(6); ss* stu = init(6);
while(1){ while(1){
ipt = mainMenu(); if(mainMenu(stu)){
if(ipt=='i'){
input(stu);
}else if(ipt=='o'){
cancel(stu);
}else if(ipt=='m'){
}else if(ipt=='q'){
break; break;
}else{
continue;
} }
} }
return 0; return 0;

@ -1,30 +1,39 @@
#include "mysyslib.h" #include "mysyslib.h"
char mainMenu() { int mainMenu(ss *stu) {
char out[4][8] = {"Input","Output","Order","Quit"}; char out[6][8] = {"input","delete","select","order","output","quit"};
for(int i=1;i<5;i++){ for(int i=1;i<7;i++){
for(int j=0;j<30;j++){ for(int j=0;j<30;j++){
printf(" "); printf(" ");
} }
printf("%d.%s\n",i,out[i-1]); printf("%d.%s\n",i,out[i-1]);
} }
char ipt; int ipt;
scanf("%c",&ipt); scanf("%d",&ipt);
switch(ipt){ switch(ipt){
case 'i': case 1:
printf("You are trying to Input info\n"); printf("You are trying to Input info\n");
input(stu); //录入学生信息
break; break;
case 'o': case 2:
printf("You are trying to Output info\n"); printf("You are trying to Delete info\n");
cancel(stu); //删除学生信息
break;
case 3:
printf("You are trying to Selete info\n");
break; break;
case 'm': case 4:
printf("You are trying to Make things ordered\n"); printf("You are trying to Make things ordered\n");
break; break;
case 'q': case 5:
printf("You are trying to Output info\n");
allPrint(stu); //输出所有学生信息
break;
case 6:
printf("You are about to Quit\n"); printf("You are about to Quit\n");
break; break;
default: default:
printf("Wrong input\n"); printf("Wrong input\n");
} }
return ipt; return (ipt==6)?1:0;
} }

@ -18,7 +18,7 @@ typedef struct{
}ss; }ss;
ss* init(int); ss* init(int);
char mainMenu(); int mainMenu(ss *stu);
void getCom(ss*); void getCom(ss*);
void Print(Student*); void Print(Student*);
void allPrint(ss*); void allPrint(ss*);

@ -1,11 +1,15 @@
#include "mysyslib.h" #include "mysyslib.h"
void Print(Student*student){ void Print(Student*student){
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n",student->id,student->class,student->name,student-> score1,student->score2,student->score3,student->score); printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n",student->id,student->class,student->name,student->score1,student->score2,student->score3,student->score);
} }
void allPrint(ss*stu){ void allPrint(ss*stu){
if(stu->nums){
for(int i = 0; i < stu->nums; ++i){ for(int i = 0; i < stu->nums; ++i){
Print(&stu->student[i]); Print(&stu->student[i]);
} }
}else{
printf("No data exist\n");
}
} }

@ -5,147 +5,147 @@
// Created by theBao on 2023/11/4. // Created by theBao on 2023/11/4.
// //
#include <stdio.h> // #include <stdio.h>
#include <stdlib.h> // #include <stdlib.h>
#include <string.h> // #include <string.h>
//
typedef struct{ // typedef struct{
char *id; // char *id;
char *class; // char *class;
char *name; // char *name;
double score1; // double score1;
double score2; // double score2;
double score3; // double score3;
double score; // double score;
}Student; // }Student;
//
typedef struct{ // typedef struct{
Student* student; // Student* student;
int nums; // int nums;
}ss; // }ss;
//
ss* init(int); // ss* init(int);
void toMenu(ss*); // void toMenu(ss*);
void getCom(ss*); // void getCom(ss*);
void Print(Student*); // void Print(Student*);
void allPrint(ss*); // void allPrint(ss*);
void input(ss*); // void input(ss*);
void cancel(ss*); // void cancel(ss*);
void Select(ss*); // void Select(ss*);
//
int main(int argc, const char * argv[]){ // int main(int argc, const char * argv[]){
ss* stu = init(6); // ss* stu = init(6);
toMenu(stu); // toMenu(stu);
return 0; // return 0;
} // }
//
ss* init(int Nums){ // ss* init(int Nums){
ss* stu = (ss*)malloc(sizeof(ss)); // ss* stu = (ss*)malloc(sizeof(ss));
if(!stu) return NULL; // if(!stu) return NULL;
//
stu->nums = 0; // stu->nums = 0;
stu->student = (Student*)malloc(sizeof(Student)*Nums); // stu->student = (Student*)malloc(sizeof(Student)*Nums);
if(!stu->student) return NULL; // if(!stu->student) return NULL;
//
for(int i = 0; i < Nums; ++i){ // for(int i = 0; i < Nums; ++i){
stu->student[i].id = (char*)malloc(sizeof(char)*5); // stu->student[i].id = (char*)malloc(sizeof(char)*5);
stu->student[i].class = (char*)malloc(sizeof(char)*2); // stu->student[i].class = (char*)malloc(sizeof(char)*2);
stu->student[i].name = (char*)malloc(sizeof(char)*10); // stu->student[i].name = (char*)malloc(sizeof(char)*10);
//
} // }
return stu; // return stu;
} // }
//
void toMenu(ss*stu){ // void toMenu(ss*stu){
printf(" 1.input\n"); // printf(" 1.input\n");
printf(" 2.delete\n"); // printf(" 2.delete\n");
printf(" 3.select\n"); // printf(" 3.select\n");
printf(" 4.order\n"); // printf(" 4.order\n");
printf(" 5.output\n"); // printf(" 5.output\n");
printf(" 6.quit\n"); // printf(" 6.quit\n");
printf("please input your option\n"); // printf("please input your option\n");
getCom(stu); // getCom(stu);
} // }
//
void getCom(ss* stu){ // void getCom(ss* stu){
int option; // int option;
scanf("%d",&option); // scanf("%d",&option);
if(option == 1) input(stu); // if(option == 1) input(stu);
else if(option == 2) cancel(stu); // else if(option == 2) cancel(stu);
else if(option == 3) Select(stu); // else if(option == 3) Select(stu);
//else if(option == 4) sort(stu); // //else if(option == 4) sort(stu);
else if(option == 5) ; // else if(option == 5) ;
else return; // else return;
} // }
//
void Print(Student*student){ // void Print(Student*student){
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n",student->id,student->class,student->name,student->score1,student->score2,student->score3,student->score); // printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n",student->id,student->class,student->name,student->score1,student->score2,student->score3,student->score);
} // }
//
void allPrint(ss*stu){ // void allPrint(ss*stu){
for(int i = 0; i < stu->nums; ++i){ // for(int i = 0; i < stu->nums; ++i){
Print(&stu->student[i]); // Print(&stu->student[i]);
} // }
} // }
//
void input(ss*stu){ // void input(ss*stu){
char *tmp = (char*)malloc(sizeof(char)*3); // char *tmp = (char*)malloc(sizeof(char)*3);
do{ // do{
printf("Id "); // printf("Id ");
// get // // get
printf("\n"); // printf("\n");
//
printf("class "); // printf("class ");
// get // // get
printf("\n"); // printf("\n");
//
printf("name "); // printf("name ");
// get // // get
printf("\n"); // printf("\n");
//
printf("score1 "); // printf("score1 ");
// get // // get
printf("\n"); // printf("\n");
//
printf("score2 "); // printf("score2 ");
// get // // get
printf("\n"); // printf("\n");
//
printf("score3 "); // printf("score3 ");
// get // // get
printf("\n"); // printf("\n");
//
printf("continue?\n"); // printf("continue?\n");
scanf("%s",tmp); // scanf("%s",tmp);
}while(strcmp(tmp,"yes")); // }while(strcmp(tmp,"yes"));
//
toMenu(stu); // toMenu(stu);
} // }
//
void cancel(ss*stu){ // void cancel(ss*stu){
char* tmp = (char*)malloc(sizeof(char)*3); // char* tmp = (char*)malloc(sizeof(char)*3);
do{ // do{
char*ID = (char*)malloc(sizeof(char)*5); // char*ID = (char*)malloc(sizeof(char)*5);
scanf("%s",ID); // scanf("%s",ID);
for(int i = 0; i < stu->nums; ++i){ // for(int i = 0; i < stu->nums; ++i){
if(strcmp(stu->student[i].id,ID)){ // if(strcmp(stu->student[i].id,ID)){
for(int j = i; j < stu->nums-1; ++j){ // for(int j = i; j < stu->nums-1; ++j){
stu->student[j] = stu->student[j+1]; // stu->student[j] = stu->student[j+1];
} // }
} // }
} // }
allPrint(stu); // allPrint(stu);
printf("continue?\n"); // printf("continue?\n");
scanf("%s",tmp); // scanf("%s",tmp);
}while(strcmp(tmp,"yes")); // }while(strcmp(tmp,"yes"));
toMenu(stu); // toMenu(stu);
} // }
//
void Select(ss*stu){ // void Select(ss*stu){
char* tmp = (char*)malloc(sizeof(char)*3); // char* tmp = (char*)malloc(sizeof(char)*3);
do{ // do{
; // ;
}while(0); // }while(0);
} // }
//
//void insert(ss*stu) // //void insert(ss*stu)

Loading…
Cancel
Save