使用do-while循环实现录入学生姓名和成绩功能。要求循环录入学生姓名和

使用do-while循环实现录入学生姓名和成绩功能。要求循环录入学生姓名和,第1张

#include "stdio.h"

#include "conio.h"

#include "stdlib.h"

#include "string.h"

int studentNum = 0

#define MAXSTUDENT (1024)

#define CheckData()\

{\

if(!studentNum)\

{\

printf("没有学生数据,按任意键返回……\n")\

getch()\

return\

}\

}\

typedef struct Student

{

int id

int cScore

int mathScore

}Student

void ClearStudent(Student* student)

{

memset((void*)student,0,sizeof(*student))

}

void ShowMainMenu()

{

system("cls")

printf("1.增加记录\n2.计算每个学生的总成绩\n3.计算每门课程的平均成绩\n4.按座号查询学生的成绩\n5.按总分排名\n6.输出前n名的学生的成绩\n7.修改成绩\n0.退出\n")

}

void ShowCountScore(const Student* student)

{

system("cls")

CheckData()

for(int i = 0i <studentNumi++)

printf("学生%04d的总成绩:%d\n",(student+i)->id,(student+i)->cScore+(student+i)->mathScore)

printf("\n按任意键继续……")

getch()

}

void ShowClassAvg(const Student* student)

{

system("cls")

CheckData()

double sumc = 0

double summath = 0

for(int i = 0i <studentNumi++)

{

sumc += (student+i)->cScore

summath += (student+i)->mathScore

}

printf("C语言平均成绩为:%lf\n",sumc/studentNum)

printf("数学平均成绩为:%lf\n",summath/studentNum)

printf("\n按任意键继续……")

getch()

}

void AddStudent(Student* student)

{

system("cls")

if(studentNum == MAXSTUDENT)

{

printf("人数已满,按任意键返回……\n")

getch()

}

printf("分别录入座号、C语言成绩、数学成绩:\n")

int id = 0

int cScore = 0

int mathScore = 0

scanf("%d%d%d",&id,&cScore,&mathScore)

for(int i = 0 i <studentNumi++)

if((student+i)->id == id)

{

printf("座号重复,按任意键返回……\n")

getch()

return

}

(student+studentNum)->id = id

(student+studentNum)->cScore = cScore

(student+studentNum)->mathScore = mathScore

studentNum++

}

void SelectFromID(const Student* student)

{

system("cls")

CheckData()

printf("输入查询ID:\n")

int id = 0

scanf("%d",&id)

printf("查询结果:\n")

for(int i = 0i<studentNumi++)

if(id == (student+i)->id)

{

printf("C语言成绩为:%d\n数学成绩为:%d\n",(student+i)->cScore,(student+i)->mathScore)

break

}

printf("\n按任意键继续……")

getch()

}

void SortForCount(Student* student)

{

system("cls")

CheckData()

for(int i = 0i <studentNumi++)

{

for(int j = studentNum -1j >ij--)

if(((student+j)->cScore+(student+j)->mathScore) >((student+j-1)->cScore+(student+j-1)->mathScore))

{

Student stu

stu = *(student+j)

*(student+j) = *(student+j-1)

*(student+j-1) = stu

}

}

}

void SortForN(Student* student,int n)

{

SortForCount(student)

if(!studentNum)

return

n = n>studentNum?studentNum:n

for(int i = 0i<ni++)

printf("第%d名:\nID:%d\nC语言成绩为:%d\n数学成绩为:%d\n总分为:%d\n\n",i+1,(student+i)->id,(student+i)->cScore,(student+i)->mathScore,(student+i)->cScore+(student+i)->mathScore)

printf("\n按任意键继续……")

getch()

}

void UpdateScore(Student* student)

{

system("cls")

CheckData()

printf("输入修改ID:\n")

int id = 0

scanf("%d",&id)

int i = 0

for(i<studentNumi++)

if(id == (student+i)->id)

break

if(i == studentNum)

printf("\n查无此人,按任意键继续……")

else

{

printf("输入欲修改的C语言成绩、数学成绩:\n")

int cScore = 0

int mathScore = 0

scanf("%d%d",&cScore,&mathScore)

(student+i)->cScore=cScore

(student+i)->mathScore = mathScore

printf("\n按任意键继续……")

}

getch()

}

void RunStudent(Student* student)

{

while(1)

{

ShowMainMenu()

char chGet = getch()

switch(chGet)

{

case '1':

AddStudent(student)

break

case '2':

ShowCountScore(student)

break

case '3':

ShowClassAvg(student)

break

case '4':

SelectFromID(student)

break

case '5':

SortForCount(student)

for(int i =0i<studentNumi++)

printf("第%d名:\nID:%d\nC语言成绩为:%d\n数学成绩为:%d\n总分为:%d\n\n",i+1,(student+i)->id,(student+i)->cScore,(student+i)->mathScore,(student+i)->cScore+(student+i)->mathScore)

printf("\n按任意键继续……")

getch()

break

case '6':

{

int n = 0

printf("输入N:\n")

scanf("%d",&n)

SortForN(student,n)

}

break

case '7':

UpdateScore(student)

break

case '0':

return

default:

break

}

}

}

int main()

{

Student* allStudent = (Student*)malloc(MAXSTUDENT*sizeof(Student))

memset((void*)allStudent,0,MAXSTUDENT*sizeof(Student))

RunStudent(allStudent)

return 0

}

//修改的 #include"iostream.h" struct student { int IDNumberchar Nameint Agechar Departmentfloat As}student s[5]//创建结构体对象时没有确定数组的大小 void main() { char tempint i=1do { cout<<"请输入学号:"cin>>s[i].IDNumbercout<<"请输入姓名:"cin>>s[i].Namecout<<"请输入年龄:"cin>>s[i].Agecout<<"请输入学院信息:"cin>>s[i].Departmentcout<<"请输入平均成绩:"cin>>s[i].Asi++cout<<"是否需要输入下一位学生的信息: (Y/N)? "cin>>temp}while(temp=='Y'||temp=='y')//这里的&&修改为||否则按了Y也会退出 } 按上面修改就可以运行了,不过还是有问题:你是想循环输入,直到输入N为止,我这里修改的代码已经确定了数组的大小,所以当你连续输入超过5次的话就会出错。如果要解决这问题可以用链表或动态创建数组也可以。

楼主要实现循环,请注意看我这下的注释。最下面的那个。

运行过了。OK的

#include<stdio.h>

#include<stdlib.h>

#include <conio.h>

void input(void)//要声明

struct student

{

char name[20]

int id

int chinese

int math

int english

}stu[30]

int count=0

void main(void)

{

int c

char a

printf("\n\n\t\t\t\twelcome\n")

printf("\n")

printf("\n")

printf("\n")

printf("\********************************************************************************\n")

printf("\n")

printf("\n\n\t\t\t\t1.input" )

printf("\n\n\t\t\t\t2.")

printf("\n\n\t\t\t\t3. ")

printf("\n\n\t\t\t\t4.")

printf("\n\n\t\t\t\t5.break")

printf("\n")

printf("\n")

printf("\n")

printf("please choose the option\n")

scanf("%d",&c)

switch(c)

{

case 1:

{

input()

break

}

case 5:

return 0

break

}

}

void input(void)//这要用void

{

char c

do

{

printf("input information:\n")

printf("name:\n")

scanf("%s",&stu[count].name)

printf("id:\n")

scanf("%d",&stu[count].id)

printf("chinese:\n")

scanf("%d",&stu[count].chinese)

printf("math:\n")

scanf("%d",&stu[count].math)

printf("english:\n")

scanf("%s",&stu[count].english)

fflush(stdin)//主要是这句。清空前面的换存。要不存的字母c就成了回车键。当然无从判断了。也可以用getchar()把回车键存起来

printf("continue?(Y/N):")

c=getchar()

count++//这个要加1

}while(c=='Y'||c=='y')

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11795759.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存