?? cyj.cpp
字號:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#define LEN sizeof(struct student)
struct student
{
long num;
char name[20];
float Chinese;
float Math;
float English;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("請輸入學號:");
scanf("%ld",&p1->num);
while(p1->num<=0)
{
printf("輸入的學號應大于0,請重輸:\n");
getchar();
scanf("%ld",&p1->num);
}
printf("請輸入姓名:");
scanf("%s",p1->name);
printf("請輸入語文成績:");
scanf("%f",&p1->Chinese);
while(p1->Chinese>100||p1->Chinese<0)
{
printf("請輸入0~100的數字\n");
printf("重輸語文成績:");
scanf("%f",&p1->Chinese);
}
printf("請輸入數學成績:");
scanf("%f",&p1->Math);
while(p1->Math>100||p1->Math<0)
{
printf("請輸入0~100的數字\n");
printf("重輸數學成績:");
scanf("%f\n",&p1->Math);
}
printf("請輸入英語成績:");
scanf("%f",&p1->English);
while(p1->English>100||p1->English<0)
{
printf("請輸入0~100的數字\n");
printf("重輸英語成績:");
scanf("%f",&p1->English);
}
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("請輸入學生學號:");
printf("請輸入-1退出成績輸入");
scanf("%ld",&p1->num);
if(p1->num==-1)break;
while(p1->num<=0)
{
printf("輸入的學號應大于0,請重輸:\n");
getchar();
scanf("%ld",&p1->num);
}
printf("請輸入姓名:");
scanf("%s",p1->name);
scanf("%f",&p1->Chinese);
while(p1->Chinese>100||p1->Chinese<0)
{
printf("請輸入0~100的數字\n");
getchar();
printf("重輸語文成績:");
scanf("%f",&p1->Chinese);
}
printf("請輸入數學成績:");
scanf("%f",&p1->Math);
while(p1->Math>100||p1->Math<0)
{
printf("請輸入0~100的數字\n");
getchar();
printf("重輸數學成績:");
scanf("%f",&p1->Math);
}
printf("請輸入英語成績:");
scanf("%f",&p1->English);
while(p1->English>100||p1->English<0)
{
printf("請輸入0~100的數字\n");
getchar();
printf("重輸英語成績:");
scanf("%f",&p1->English);
}
}
p2->next=NULL;
return(head);
}
/*查詢成績*/
void xin(struct student *head,char name[20])
{struct student *p1;
p1=head;
if(head!=NULL)
while(p1!=NULL)
{if(strcmp(p1->name,name)!=0)
{p1=p1->next;
}
else
{printf("\t_______________________________________\n");
printf("\t| 學號 |姓名 |語文 |數學 |英語 |\n");
printf("\t_______________________________________\n");
printf("\t| %-ld |%-6.2s|%-7.1f|%-7.1f|%-7.1f|\n",p1->num,p1->name,p1->Chinese,p1->Math,p1->English);break;}}
printf("\t_______________________________________\n");
if(p1==NULL) printf("\t%s 不存在",name);
}
/*輸出*/
void print(struct student *head)
{
struct student *p;
printf("\n\tNOW These %d records are:\n",n);
printf("\t_______________________________________\n");
printf("\t| 學號 |姓名 |語文 |數學 |英語 |\n");
printf("\t_______________________________________\n");
p=head;
if(head!=NULL)
do
{
printf("\t| %-ld |%-6.2s|%-7.1f|%-7.1f|%-7.1f|\n",p->num,p->name,p->Chinese,p->Math,p->English);
printf("\t_______________________________________\n");
p=p->next;
}while(p!=NULL);
return;
}
/*按學號排序*/
struct student *xuehao(struct student *head)
{
struct student *p2,*p1;
char n[20];
long k;
float m;
float yingyu;
float y;
p1=head;
while(p1!=NULL)
{
p2=p1->next;
while(p2!=NULL)
{
if(p1->num>p2->num)
{
k=p1->num;p1->num=p2->num;p2->num=k;m=p1->Math;p1->Math=p2->Math;p2->Math=m;
strcpy(n,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,n);
y=p1->Chinese;p1->Chinese=p2->Chinese;p2->Chinese=y;
yingyu=p1->English;p1->English=p2->English;p2->English=yingyu;
}
p2=p2->next;
}
p1=p1->next;
}
return(head);
}
/*按英語排序*/
struct student *yingyu(struct student *head)
{
struct student *p2,*p1;
char n[20];
float k;
float y;
long s;
float m;
p1=head;
while(p1!=NULL)
{
p2=p1->next;
while(p2!=NULL)
{
if(p1->English>p2->English)
{
k=p1->English;p1->English=p2->English;p2->English=k;
m=p1->Math;p1->Math=p2->Math;p2->Math=m;
strcpy(n,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,n);
y=p1->Chinese;p1->Chinese=p2->Chinese;p2->Chinese=y;
s=p1->num;p1->num=p2->num;p2->num=s;
}
p2=p2->next;
}
p1=p1->next;
}
return(head);
}
/*按數學排序*/
struct student *Math(struct student *head)
{
struct student *p2,*p1;
char n[20];
float y;
long s;
float m;
float k;
p1=head;
while(p1!=NULL)
{
p2=p1->next;
while(p2!=NULL)
{
if(p1->Math>p2->Math)
{k=p1->Math;p1->Math=p2->Math;p2->Math=k;
m=p1->English;p1->English=p2->English;p2->English=m;
strcpy(n,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,n);
y=p1->Chinese;p1->Chinese=p2->Chinese;p2->Chinese=y;
s=p1->num;p1->num=p2->num;p2->num=s;}
p2=p2->next;
}
p1=p1->next;
}
return(head);
}
/*按語文排序*/
struct student *Chinese(struct student *head)
{
struct student *p2,*p1;
char n[20];
float y;
long s;
float m;
float k;
p1=head;
while(p1!=NULL)
{
p2=p1->next;
while(p2!=NULL)
{
if(p1->Chinese>p2->Chinese)
{k=p1->Chinese;p1->Chinese=p2->Chinese;p2->Chinese=k;
m=p1->English;p1->English=p2->English;p2->English=m;
strcpy(n,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,n);
y=p1->Math;p1->Math=p2->Math;p2->Math=y;
s=p1->num;p1->num=p2->num;p2->num=s;}
p2=p2->next;
}
p1=p1->next;
}
return(head);
}
/*彩票*/
void caipiao()
{int m,i,j,n,a[1000][7];
int rand(void);
printf("請輸入你想購買的彩票組數:");
scanf("%d",&n);
printf("您的%d組彩票號碼如下:\n",n);
{for(i=0;i<n;i++)
for(j=0;j<7;j++)
{m=rand()%50;
if(m>0&&m<=49)
a[i][j]=m;
else j--;}
}
for (i=0;i<n;i++)
{for(j=0;j<7;j++)
if(j!=6)
printf("%5d",a[i][j]);
else printf("\t+%d",a[i][6]);
printf("\n");
}
}
/*計算器*/
void jisuanqi()
{int result,s;
int chengfa();
int jiafa();
int kaifang();
printf("歡迎使用計算器 1.加法\t2.乘法\t3.開方\n");
printf("\t請選擇你要的操作:");
scanf("%d",&s);
while(s>3||s<0)
{printf("\t請輸入1~~3的數字:");
scanf("%d",&s);}
switch(s)
{case 1:
result=jiafa();printf("%d",result);break;
case 2:
result=chengfa();printf("%d",result);break;
case 3:
result=kaifang();printf("%d",result);break;
}}
/*作加法*/
int jiafa()
{int a[20];
int i,n;
int s=0;
printf("請輸入n個要運用加法的數字\n");
scanf("%d",&n);
printf("輸入數字:每個數字間用空格!\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
s=s+a[i];
return(s);
}
/*作乘法*/
int chengfa()
{int a[20];
int i,n;
int s=1;
printf("請輸入n個要運用乘法的數字個數:");
scanf("%d",&n);
printf("輸入數字:每個數字間用空格!\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
s=s*a[i];
return(s);
}
/*作開方*/
int kaifang()
{int s;
int n;
printf("輸入一個數字:\n");
scanf("%d",&n);
s=sqrt(n);
return(s);
}
/*主函數*/
void main()
{
struct student *head;
int i,j,m,p;
char name[20];
j=1;
while(j)
{printf("\n");
for(i=0;i<40;i++)
printf("★");
printf("\t\t\t 歡迎使用學生成績管理系統系統\n");
printf("\t\t\t 開發者:項和平 學號2\n");
printf("\n\n");
printf("\t\t 1.成績輸入 2.計算器 3.六合彩 \n");
printf("\t\t 4.成績列表 5.成績排序 6.成績查詢 \n");
printf("\t\t 7.退出系統 \n");
printf("\n");
for(i=0;i<40;i++)
printf("★");
printf("\n");
printf("\t\t請選擇你要的操作(1~7):");
scanf("%d",&m);
while(m>7||m<0)
{printf("\t請輸入1~7的數字:");
scanf("%d",&m);}
switch(m)
{case 1:
head=creat();break;
case 2:jisuanqi();break;
case 3:caipiao();break;
case 4:
printf("\t成績列表:");
print(head);break;
case 5:
printf("\t1.學號 \t2.數學 \t3.英語 \t4.語文 \n");
printf("\t請輸入排序方式(輸入1~4數字):");
scanf("%d",&p);
if(p<1||p>4)
{printf("\t請輸入1~4的數字:");
scanf("%d",&p);}
switch(p)
{case 1:xuehao(head);
print(head);break;
case 2:Math(head);
print(head);break;
case 3:yingyu(head);
print(head);break;
case 4:Chinese(head);
print(head);break;
}break;
case 6:
printf("\t請輸入要查詢的學生的名字:");
scanf("%s",name);
xin(head,name);break;
case 7:
printf("\t謝謝使用本人的管理系統!");
printf("本系統由計算機023-項和平編寫.");
getchar();
exit(0);
break;}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -