当前位置:首页 » 编程语言 » 100分左右的c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

100分左右的c语言

发布时间: 2022-10-06 18:33:43

⑴ 100分求这道c语言题怎么解!!!!!!!!!!!在线等!

#include<iostream.h>
int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,31,30,31,30,31}};
// day_tab 二维数组存放各月天数,第一行对应非闰年,第二行对应闰年
class Date
{
int year,month,day;
int leap(int);
int dton(Date &);
Date ntod(int);
public:
Date(){}
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
void setday(int d){day=d;}
void setmonth(int m){month=m;}
void setyear(int y){year=y;}
int getday(){return day;}
int getmonth(){return month;}
int getyear(){return year;}
Date operator+(int days)
{
static Date date;
int number=dton(*this)+days;
date=ntod(number);
return date;
}
Date operator-(int days)
{
static Date date;
int number=dton(*this);
number-=days;
date=ntod(number);
return date;
}
int operator-(Date &b)
{
int days=dton(*this)-dton(b)-1;
return days;
}
void disp()
{
cout<<year<<"."<<month<<"."<<day<<endl;
}
};
int Date::leap(int year)
{
if(year%4==0&&year%100!=0||year%400==0) // 是闰年
return 1;
else // 不是闰年
return 0;
}
int Date::dton(Date &d)
{
int y,m,days=0;
for(y=1;y<=d.year;y++)
if(leap(y))
days+=366;
else
days+=365;
for(m=0;m<d.month-1;m++)
if(leap(d.year))
days+=day_tab[1][m];
else
days+=day_tab[0][m];
days+=d.day;
return days;
}
Date Date::ntod(int n)
{
int y=1,m=1,d,rest=n,lp;
while(1)
{
if(leap(y))
{
if(rest<=366)
break;
else
rest-=366;
}
else
{
if(rest<=365)
break;
else
rest-=365;
}
y++;
}
y--;
lp=leap(y);
while(1)
{
if(lp)
{
if(rest>day_tab[1][m-1])
rest-=day_tab[1][m-1];
else
break;
}
else
{
if(rest>day_tab[0][m-1])
rest-=day_tab[0][m-1];
else
break;
}
m++;
}
d=rest;
return Date(y,m,d);
}
void main()
{
Date now(2002,6,12),then(2003,2,10);
cout<<"now:"; now.disp();
cout<<"then:"; then.disp();
cout<<"相差天数:"<<(then-now)<<endl;
Date d1=now+100,d2=now-100;
cout<<"now+100:"; d1.disp();
cout<<"now-100:"; d2.disp();
}

本程序的执行结果如下:
now:2002.6.12
then:2003.2.10
相差天数:242
now+100:2002.9.20
now-100:2002.3.4

#include<iostream.h>
class Time
{
int hour,minute,second;
public:
Time(){}
Time(int h,int m,int s)
{
hour=h;minute=m;second=s;
}
Time(int h,int m)
{
hour=h;minute=m;second=0;
}
void sethour(int h){hour=h;}
void setminute(int m){minute=m;}
void setsecond(int s){second=s;}
int gethour(){return hour;}
int getminute(){return minute;}
int getsecond(){return second;}
Time operator+(Time);
Time operator-(Time);
void disp()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
};
Time Time::operator+(Time t)
{
int carry,hh,mm,ss;
ss=getsecond()+t.getsecond();
if(ss>60)
{
ss-=60;
carry=1; // 进位标记
}
else carry=0;
mm=getminute()+t.getminute()+carry;
if(mm>60)
{
mm-=60;
carry=1;
}
else carry=0;
hh=gethour()+t.gethour()+carry;
if(hh>24)
hh=24;
static Time result(hh,mm,ss);
return result;
}
Time Time::operator-(Time t)
{
int borrow,hh,mm,ss;
ss=getsecond()-t.getsecond();
if(ss<0)
{
ss+=60;
borrow=1; // 借位标记
}
else borrow=1;
mm=getminute()-t.getminute()-borrow;
if(mm<0)
{
mm+=60;
borrow=1;
}
else borrow=0;
hh=gethour()-t.gethour()-borrow;
if(hh<0)
hh+=24;
static Time result(hh,mm,ss);
return result;
}
void main()
{
Time now(2,24,39);
Time start(17,55);
Time t1=now-start,t2=now+start;
cout<<"输出结果:"<<endl;
cout<<" now: "; now.disp();
cout<<" start:"; start.disp();
cout<<" 相差: "; t1.disp();
cout<<" 相加: "; t2.disp();
}
本程序的执行结果如下:
输出结果:
now:2:24:39
start:17:55:0
相差:8:28:39
相加:20:19:39

⑵ C语言程序设计 100分 越快越好赶时间

/*13. teacherfun.c源程序*/
/*** teacherfun.c ***/
#include "c14_t.c" /*根据实际存放位置修改此路径*/
/*初始化双链表*/
void init()
{
First=(TEACHER *)malloc(sizeof(TEACHER)); /*为头结点申请空间*/
Last=First; /*将尾指针指向头结点*/
First->prior=Last; /*设置头结点的前驱指针*/
Last->next=First; /*设置头结点的后继指针*/
p=First; /*设置当前记录指针为头结点*/
}

/*创建教师信息循环双链表*/
void create()
{
int unit,flag=0;
float temp;
TEACHER *info; /*新增结点*/
init();
for(;;)
{
if(flag==1)break; /*标志为1,不再输入*/
clrscr(); /*清屏*/
printf("Please enter teacher infomation\n");
printf("input @ end enter\n");
info=(TEACHER *)malloc(sizeof(TEACHER));/*为新增结点申请空间*/
if(!info) /*没有空间出错处理*/
{
printf("\nout of memory");
exit(0);
}
printf("No:"); /*开始提示输入*/
scanf("%s",info->no);
if(info->no[0]=='@')/*输入@结束输入*/
{
flag=1;break;}
printf("Name:");
scanf("%s",info->name);
printf("Sex:");
scanf("%s",info->sex);
printf("Profess:");
scanf("%s",info->profess);
printf("Dept:");
scanf("%s",info->dept);
printf("Class:");
scanf("%s",info->class);
printf("Workload:");
scanf("%f",&temp);
info->workload=temp;
if(strcmp(info->profess,"prof"))unit=25; /*教授*/
if(strcmp(info->profess,"aprof"))unit=20;/*副教授*/
if(strcmp(info->profess,"lect"))unit=15;/*讲师*/
if(strcmp(info->profess,"ass"))unit=10;/*助教*/
info->lessonf=unit*info->workload;/*根据职称计算代课费*/
info->next=Last->next;/*新插入结点插在表末尾*/
info->prior=Last; /*新结点的前驱为原来的尾结点*/
Last->next=info; /*原来尾结点的后继为新结点*/
Last=info; /*新的尾结点为新结点*/
First->prior=Last;/*头结点的前驱为尾指针*/
}
return;
}
/*显示第一条记录*/
void firstr()
{
if(First==Last)return;
clear();
p=First->next;
print(p);
}
/*显示最后一条记录*/
void lastr()
{
if(First==Last)return;
clear();
p=Last;
print(p);
}
/*显示前一条记录*/
void priorr()
{
if(First==Last)
return;
if(p->prior!=First)
p=p->prior;
else
p=Last;
clear();
print(p);
}
/*显示下一条记录*/
void nextr()
{
if(First==Last)
return;
if(p==Last)
p=First->next;
else
p=p->next;
clear();
print(p);
}
/*从文件读数据*/
void load()
{
TEACHER *p1;
FILE *fp;
if((fp=fopen("data.txt","rb"))==NULL)
{
printf("can not open file\n");
return;
}
while(First->next!=First) /*如果当前表不空,删除当前表*/
{
p1=First->next;
First->next=p1->next;
free(p1);
}
free(First);
First=(TEACHER*)malloc(sizeof(TEACHER)); /*创建头结点*/
if(!First)
{
printf("out of memory!\n");
return;
}
Last=First;
First->prior=Last;
Last->next=First;
p=First;
while(!feof(fp)) /*当文件不为空时读数据*/
{
p1=(TEACHER*)malloc(sizeof(TEACHER));
if(!p1)
{
printf("out of memory!\n");
return;
}
if(1!=fread(p1,sizeof(TEACHER),1,fp))
break; /*读数据*/
p1->next=Last->next; /*将新读出的数据链在当前表尾*/
p1->prior=Last;
Last->next=p1;
Last=Last->next;
First->prior=Last;
}
fclose(fp); /*关闭文件*/
}
/*保存数据到磁盘文件*/
void save()
{
FILE *fp; /*定义指向文件的指针*/
TEACHER *p1; /* 定义移动指针*/
if((fp=fopen("data.txt","wb"))==NULL) /*为输出打开一个文本文件,如没有则建立*/
{
printf("can not open file\n"); /*如不能打开文件,显示提示信息,结束程序*/
return; /*返回*/
}
p1=First; /*移动指针从头指针开始*/
while(p1->next!=First) /*如p1不为空*/
{
fwrite(p1->next,sizeof(TEACHER),1,fp); /*写入一条记录*/
p1=p1->next; /*指针后移,处理下一条记录*/
}
fclose(fp); /*关闭文件*/
}
/*删除记录*/
void delete()
{
TEACHER *p1;
if(First==Last)
return;/*表为空*/
if(p==First) /*p为头结点*/
p=First->next;
if(p==Last)/*p为尾结点*/
Last=p->prior;
p1=p; /*一般情况*/
p=p->next;
p1->prior->next=p1->next;
p1->next->prior=p1->prior;
free(p1);
}
/*输出记录*/
void print(TEACHER *p)
{
int x1=70,y1=100;
char str[20];
outtextxy(x1+110,y1+75, p->no);
outtextxy(x1+360,y1+75,p->name);
outtextxy(x1+110,y1+105,p->sex);
outtextxy(x1+360,y1+105,p->profess);
outtextxy(x1+110,y1+135,p->dept);
outtextxy(x1+360,y1+135,p->class);
sprintf(str,"%f",p->workload);
outtextxy(x1+110,y1+165,str);
sprintf(str,"%f",p->lessonf);
outtextxy(x1+360,y1+165,str);
}
/*****清除界面显示信息******/
void clear()
{
int x1=70,y1=100,m,n;
for(m=0;m<4;m++)
for(n=0;n<2;n++)
{
setfillstyle(1,WHITE);/*白色覆盖原有信息*/
bar(x1+n*250+100,y1+50+m*30+20,x1+n*250+200,y1+50+m*30+40);
}
}
/*sort排序函数*/
void sort()
{
TEACHER *p0,*p00,*p1,*p11,*templast;
if(First->next==First||First->next->next==First)return;
p00=First; /*作排好序表的表头和第一个结点*/
p0=First->next;
p1=p0->next;
First->prior=p0;
p0->next=First;
templast=p0;
while(p1!=First) /*当p1没有转回到表头时*/
{
p11=p1; /*将p11作为待插入结点*/
p1=p1->next; /*p1指向下一个待排序结点*/
p00=First; /*从头结点开始寻找插入位置*/
p0=p00->next; /*p0是p00的后继*/
while(p0!=First&&p11->workload>p0->workload)
{
p00=p0;/*当新插入结点比当前表结点大时,指针后移*/
p0=p0->next;
}
if( p0==First)/*如果p0移到了头结点*/
{
p11->next=p00->next;
p11->prior=p00;
p00->next=p11;
p0->prior=p11;
templast=p11;
}
else /*新插入结点介于p00和p0之间*/
{
p11->next=p0;
p11->prior=p00;
p0->prior=p11;
p00->next=p11;
}
}
Last=templast; /*设置尾指针*/
p=First; /*设置当前记录指针*/
}


⑶ 100分 c语言对比两个文件

最长公共子序列方法比较两个文件的相似性。输入两个文件的名字,输出一个文件,不同的地方用红色标出。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define max(x,y) (((x)>(y))?(x):(y))

int LCS(char *str1, char *same1, int len1, char *str2, char *same2, int len2)
{
int same, i, j;
int **midLCS;

midLCS = (int **)malloc(sizeof(int *) * (len1 + 1));
for (i = 0; i <= len1; i++) {
midLCS[i] = (int *)malloc(sizeof(int) * (len2 + 1));
}
for (i = 0; i <= len1; i++) {
midLCS[i][0] = 0;
}
for (i = 0; i <= len2; i++) {
midLCS[0][i] = 0;
}

for (i = 1; i <= len1; i++)
{
for (j = 1; j <= len2; j++)
{
if (str1[i - 1] == str2[j - 1]) {
midLCS[i][j] = midLCS[i - 1][j - 1] + 1;
}
else {
midLCS[i][j] = max(midLCS[i - 1][j], midLCS[i][j - 1]);
}
}
}

for (i = len1, j = len2; i > 0 && j > 0; )
{
if (str1[i - 1] == str2[j - 1]) {
same1[i - 1] = 1;
same2[j - 1] = 1;
--i;
--j;
}
else if (midLCS[i - 1][j] > midLCS[i][j - 1]) {
--i;
}
else {
--j;
}
}

same = midLCS[len1][len2];
for (i = 0; i <= len1; i++) {
free(midLCS[i]);
}
free(midLCS);

return same;
}

void show_compare(char *str, char *same, int len, FILE *fout)
{
int i, flag = 0;

for (i = 0; i < len; i++)
{
if (str[i] == '\n') {
fprintf(fout, " <br> ");
}
else if (str[i] == '\t') {
fprintf(fout, " ");
}
else {
if (same[i] == 1) {
if (flag == 1) {
fprintf(fout, " </font> ");
flag = 0;
}
fputc(str[i], fout);
}
else {
if (flag == 0) {
fprintf(fout, " <font color=red> ");
flag = 1;
}
fputc(str[i], fout);
}
}
}
if (flag == 1)
fprintf(fout, " </font> ");
}

int main()
{
int i, len1, len2;
FILE *fin1, *fin2, *fout;
char buf[1024];
char *str1, *str2, *same1, *same2;

printf("file1: ");
scanf("%s", buf);
fin1 = fopen(buf, "rb");
if (fin1 == NULL) {
printf("%s not exist!\n", buf);
return 0;
}
printf("file2: ");
scanf("%s", buf);
fin2 = fopen(buf, "rb");
if (fin2 == NULL) {
printf("%s not exist!\n", buf);
return 0;
}
fout = fopen("compare.html", "w+");

fseek(fin1, 0, SEEK_END);
len1 = (int)ftell(fin1);
fseek(fin1, 0, SEEK_SET);
fseek(fin2, 0, SEEK_END);
len2 = (int)ftell(fin2);
fseek(fin2, 0, SEEK_SET);

str1 = (char *)malloc(sizeof(char) * len1);
str2 = (char *)malloc(sizeof(char) * len2);
same1 = (char *)malloc(sizeof(char) * len1);
same2 = (char *)malloc(sizeof(char) * len2);
fread(str1, 1, len1, fin1);
fread(str2, 1, len2, fin2);

LCS(str1, same1, len1, str2, same2, len2);

show_compare(str1, same1, len1, fout);
fprintf(fout, " ----------------------------------------------------------------<br> ");
show_compare(str2, same2, len2, fout);

free(str1);
free(str2);
free(same1);
free(same2);
fclose(fin1);
fclose(fin2);
fclose(fout);

}

⑷ 求C语言编程序代码,写的全的100分就是你的~

//输入功能:输入30名学生的学号、班级、姓名、上机起始时间
#include<time.h>
#include<stdio.h>
#include<string.h>
#include<Windows.h>
#define N 20
#define M 100
struct student
{ char id[N];
char theclass[N];
char name[N];
char ontime[N];
}
student[M];
int n;
void addition()
{ int i;
printf("\n请输入录入学生信息的总数:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ printf("\n\t请输入第%d个学生学号(20s):",i);
scanf("%s",student[i-1].id);
printf("\n\t请输入第%d个学生班级(20s):",i);
scanf("%s",student[i-1].theclass);
printf("\n\t请输入第%d个学生姓名(20s):",i);
scanf("%s",student[i-1].name);
printf("\n\t请输入第%d个学生上机时间(20s)(例:02):",i);
scanf("%s",student[i-1].ontime);
printf("\n\t提示:您已成功录入第%d条信息\n",i);
}
}

//计算功能:计算每个下机学生的上机费用,每小时1元。
//(上机费用=上机时间* 1.0/h ,不足一小时按一小时计算)
void calculate()
{ int hours;
char times[30];
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime(&rawtime );
strcpy(times,asctime(timeinfo));
printf("\t所有学生上机费用如下:\n");
for(int i=1;i<=n;i++)
{printf("学生%d费用:",i); if((student[i-1].ontime[3]-48)*10+student[i-1].ontime[4]>(times[14]-48)*10+times[15]) hours=(times[11]-48)*10+times[12]-(student[i-1].ontime[0]-48)*10-student[i-1].ontime[1];
else hours=(times[11]-48)*10+times[12]-(student[i-1].ontime[0]-48)*10-student[i-1].ontime[1]+1;
printf("%d\n",hours);
}
}
void calculate()
{ int hours;
char times[30];
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime(&rawtime );
strcpy(times,asctime(timeinfo));
printf("\t所有学生上机费用如下:\n");
for(int i=1;i<=n;i++)
{ printf("学生%d费用:",i); if((student[i-1].ontime[3]-48)*10+student[i-1].ontime[4]>(times[14]-48)*10+times[15]) hours=(times[11]-48)*10+times[12]-(student[i-1].ontime[0]-48)*10-student[i-1].ontime[1];
else hours=(times[11]-48)*10+times[12]-(student[i-1].ontime[0]-48)*10-student[i-1].ontime[1]+1;
printf("%d\n",hours);
}
}

//查询功能:按条件(班级、学号、姓名)显示学生的上机时间。
void search()
{ int i,b,c,count;
do
{ char find[20];
printf("\n请选择查询方式:1.根据学号查询;2.根据班级查询;3.根据姓名查询;4.根据上机时间:");
scanf("%d",&b);
switch(b)
{ case 1: count=PF_FLOATING_POINT_PRECISION_ERRATA; printf("\n**请输入学生的学号:");
scanf("%s",find);
for(i=0;i<n;i++)
{
if (strcmp(student[i].id,find)==0)
{
count++;
if(count==PF_FLOATING_POINT_EMULATED)
printf("学生学号\t学生班级\t学生姓名\t上机时间\n"); printf("%8s%15s%15s%17s",student[i].id,student[i].theclass, student[i].name,student[i].ontime);
}
}
if(!count)
printf("****提示:该生不存在");
goto A;
case 2: count=PF_FLOATING_POINT_PRECISION_ERRATA;
printf("\n**请输入学生的班级:");
scanf("%s",find);
for(i=0;i<n;i++)
{ if (strcmp(student[i].theclass,find)==0)
{ count++;
if(count==PF_FLOATING_POINT_EMULATED)
printf("学生学号\t学生班级\t学生姓名\t上机时间\n"); printf("%8s%15s%15s%17s",student[i].id,student[i].theclass, student[i].name,student[i].ontime);
}
}
if(!count)
printf("提示:该生不存在!.....");
goto A;
case 3: count=PF_FLOATING_POINT_PRECISION_ERRATA;
printf("\n**请输入学生的姓名:");
scanf("%s",find);
for(i=0;i<n;i++)
{
if (strcmp(student[i].name,find)==0)
{
count++;
if(count==PF_FLOATING_POINT_EMULATED)
printf("学生学号\t学生班级\t学生姓名\t上机时间\n"); printf("%8s%15s%15s%17s",student[i].id,student[i].theclass, student[i].name,student[i].ontime);
}
}
if(!count) printf("提示:该生不存在!请重新输入!");
goto A;
case 4: count=PF_FLOATING_POINT_PRECISION_ERRATA;
printf("\n**请输入学生的上机时间:");
scanf("%s",find);
for(i=0;i<n;i++)
{ if (strcmp(student[i].ontime,find)==0)
{ count++;
if(count==PF_FLOATING_POINT_EMULATED)
printf("学生学号\t学生班级\t学生姓名\t上机时间\n"); printf("%8s%15s%15s%17s",student[i].id,student[i].theclass, student[i].name,student[i].ontime);
}
}
if(!count)
printf("****提示:该生不存在!");
goto A;
default:printf("*****提示:输入错误");
}
A:printf("\n\t**1.继续\n\t**0.返回主菜单");
printf("\n\t 请输入您的选择:");
scanf("%d",&c);
}
while(c);
}

//机器使用情况的显示(显示方式不限但要一目了然)
void menu()
{
printf("\n\t*******************欢迎进入机房收费管理系统!*******************\n");
printf("\t* 1.录入功能2.计算功能*\n");
printf("\t* 3.查询功能0.-*EXIT*- *\n"); printf("\t***************************************************************\n");
printf("\n\t 请输入您的选择:");
}
void main()
{ system("color 5f");
int a;
C:menu();
scanf("%d",&a);
switch(a)
{
case 0:printf("正在退出......\n谢谢使用本系统,再见");break;
case 1:addition();
goto C;
/*录入功能*/
case 2:calculate();
goto C; /*浏览功能*/
case 3:search();
goto C; /*查询功能*/
}
}

/*说明:以上程序有错,自己调试,改一下吧。大概就是这样了,作业还是要自己慢慢做哈嘛。才会懂,程序要自己多看多写,多改,才会懂的。*/

⑸ 100分悬赏简单C语言!!!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 1024

int main(int argc, char* argv[])
{
int i = 0;
char buffer[2][MAX_LINE];
FILE* input = NULL;
FILE* output = NULL;

if (argc == 1)
{
printf("usage: %s [input file].\n", argv[0]);
exit(1);
}
input = fopen(argv[1], "r");
if (input == NULL)
{
printf("%s: cannot open file \"%s\" for reading.\n", argv[0], argv[1]);
exit(1);
}
output = fopen("output.txt", "w");
while (!feof(input))
{
if(!fgets(buffer[i%2], MAX_LINE, input)) break;
if (strcmp(buffer[i%2], buffer[(i+1)%2]) != 0)
fprintf(output, "%s", buffer[i%2]);
i = i + 1;
}

return 0;
}

⑹ C语言100分求解

#include<stdio.h>
#include<string.h>
voidmain()
{
charstr[100];//定义一个字符串数组
inti,n,flag=1;
scanf("%s",str);
n=strlen(str);//计算str实际长度(不算'')
for(i=0;i<n/2;i++)
if(str[i]!=str[n-1-i]){flag=0;break;}//如果不符合回文条件,flag赋值为0
if(flag)//如果flag=1,就说明一直符合回文条件
{
printf("Yes");
}
else//如果flag=0,就说明至少有一处不符合回文条件
{
printf("No");
}
printf(" ");
}

⑺ 跪求100行左右的c语言简单代码,大一水平就行,什么类型都可以。

//学生成绩管理系统C代码
/*头文件*/
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>/*其它说明*/
#include<string.h>/*字符串函数*/
#include<mem.h>/*内存操作函数*/
#include<ctype.h>/*字符操作函数*/
#include<alloc.h>/*动态地址分配函数*/

#defineLENsizeof(STUDENT)

typedefstructstu/*定义结构体数组用于缓存数据*/
{
charnum[6];
charname[5];
intscore[3];
intsum;
floataverage;
intorder;
structstu*next;
}STUDENT;

/*函数原型*/
STUDENT*init();/*初始化函数*/
intmenu_select();/*菜单函数*/
STUDENT*create();/*创建链表*/
voidprint(STUDENT*head);/*显示全部记录*/
voidsearch(STUDENT*head);/*查找记录*/
STUDENT*delete(STUDENT*head);/*删除记录*/
STUDENT*sort(STUDENT*head);/*排序*/
STUDENT*insert(STUDENT*head,STUDENT*newnode);/*插入记录*/
voidsave(STUDENT*head);/*保存文件*/
STUDENT*load();/*读文件*/

/*主函数界面*/
main()
{
STUDENT*head,newnode;
head=init();/*链表初始化,使head的值为NULL*/
for(;;)/*循环无限次*/
{
switch(menu_select())
{
case1:head=create();break;
case2:print(head);break;
case3:search(head);break;
case4:head=delete(head);break;
case5:head=sort(head);break;
case6:head=insert(head,&newnode);break;/*&newnode表示返回地址*/
case7:save(head);break;
case8:head=load();break;
case9:exit(0);/*如菜单返回值为9则程序结束*/
}
}
}

/*初始化函数*/
STUDENT*init()
{
returnNULL;/*返回空指针*/
}

/*菜单选择函数*/
menu_select()
{
intn;
structdated;/*定义时间结构体*/
getdate(&d);/*读取系统日期并把它放到结构体d中*/
printf("pressanykeytoenterthemenu......");/*按任一键进入主菜单*/
getch();/*从键盘读取一个字符,但不显示于屏幕*/
clrscr();/*清屏*/
printf("******************************************************************************** ");
printf(" Welcometo ");
printf(" Thestudentscoremanagesystem ");
printf("*************************************MENU*************************************** ");
printf(" 1.Entertherecord ");/*输入学生成绩记录*/
printf(" 2.Printtherecord ");/*显示*/
printf(" 3.Searchrecordonname ");/*寻找*/
printf(" 4.Deletearecord ");/*删除*/
printf(" 5.Sorttomakenewafile ");/*排序*/
printf(" 6.Insertrecordtolist ");/*插入*/
printf(" 7.Savethefile ");/*保存*/
printf(" 8.Loadthefile ");/*读取*/
printf(" 9.Quit ");/*退出*/
printf(" MadebyHuHaihong. ");
printf("******************************************************************************** ");
printf(" %d\%d\%d ",d.da_year,d.da_mon,d.da_day);/*显示当前系统日期*/
do{
printf(" Enteryourchoice(1~9):");
scanf("%d",&n);
}while(n<1||n>9);/*如果选择项不在1~9之间则重输*/
return(n);/*返回选择项,主函数根据该数调用相应的函数*/
}

/*输入函数*/
STUDENT*create()
{
inti,s;
STUDENT*head=NULL,*p;/*定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT*)malloc(LEN);/*开辟一个新的单元*/
if(!p)/*如果指针p为空*/
{printf(" Outofmemory.");/*输出内存溢出*/
return(head);/*返回头指针,下同*/
}
printf("Enterthenum(0:listend):");
scanf("%s",p->num);
if(p->num[0]=='0')break;/*如果学号首字符为0则结束输入*/
printf("Enterthename:");
scanf("%s",p->name);
printf("Pleaseenterthe%dscores ",3);/*提示开始输入成绩*/
s=0;/*计算每个学生的总分,初值为0*/
for(i=0;i<3;i++)/*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0||p->score[i]>100)/*确保成绩在0~100之间*/
printf("Dataerror,pleaseenteragain. ");
}while(p->score[i]<0||p->score[i]>100);
s=s+p->score[i];/*累加各门成绩*/
}
p->sum=s;/*将总分保存*/
p->average=(float)s/3;/*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0;/*未排序前此值为0*/
p->next=head;/*将头结点做为新输入结点的后继结点*/
head=p;/*新输入结点为新的头结点*/
}
return(head);
}

/*显示全部记录函数*/
voidprint(STUDENT*head)
{
inti=0;/*统计记录条数*/
STUDENT*p;/*移动指针*/
clrscr();
p=head;/*初值为头指针*/
printf(" ************************************STUDENT************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Rec|Num|Name|Sc1|Sc2|Sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
while(p!=NULL)
{
i++;
printf("|%3d|%4s|%-4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
i,p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("------------------------------------------------------------------------------- ");
printf("**************************************END************************************** ");
}

/*查找记录函数*/
voidsearch(STUDENT*head)
{
STUDENT*p;/*移动指针*/
chars[5];/*存放姓名用的字符数组*/
clrscr();
printf("Pleaseenternameforsearching. ");
scanf("%s",s);
p=head;/*将头指针赋给p*/
while(strcmp(p->name,s)&&p!=NULL)/*当记录的姓名不是要找的,或指针不为空时*/
p=p->next;/*移动指针,指向下一结点*/
if(p!=NULL)/*如果指针不为空*/
{printf(" *************************************FOUND************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Num|Name|sc1|sc2|sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
printf("|%4s|%4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("------------------------------------------------------------------------------- ");
printf("***************************************END************************************** ");
}
else
printf(" Thereisnonum%sstudentonthelist. ",s);/*显示没有该学生*/
}

/*删除记录函数*/
STUDENT*delete(STUDENT*head)
{intn;
STUDENT*p1,*p2;/*p1为查找到要删除的结点指针,p2为其前驱指针*/
charc,s[6];/*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Pleaseenterthedeletednum:");
scanf("%s",s);
p1=p2=head;/*给p1和p2赋初值头指针*/
while(strcmp(p1->num,s)&&p1!=NULL)/*当记录的学号不是要找的,或指针不为空时*/
{p2=p1;/*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next;/*将p1指针指向下一条记录*/
}
if(strcmp(p1->num,s)==0)/*学号找到了*/
{printf("**************************************FOUND************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Num|Name|sc1|sc2|sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
printf("|%4s|%4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("------------------------------------------------------------------------------- ");
printf("***************************************END************************************** ");
printf("AreyousuretodeletethestudentY/N?");/*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N')break;/*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head)/*若p1==head,说明被删结点是首结点*/
head=p1->next;/*把第二个结点地址赋予head*/
else
p2->next=p1->next;/*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf(" Num%sstudenthavebeendeleted. ",s);
printf("Don'tforgettosave. ");break;/*删除后就跳出循环*/
}
}
}
else
printf(" Thereisnonum%sstudentonthelist. ",s);/*找不到该结点*/
return(head);
}

/*排序函数*/
STUDENT*sort(STUDENT*head)
{inti=0;/*保存名次*/
STUDENT*p1,*p2,*t,*temp;/*定义临时指针*/
temp=head->next;/*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL;/*第一个结点为新表的头结点*/
while(temp!=NULL)/*当原表不为空时,进行排序*/
{
t=temp;/*取原表的头结点*/
temp=temp->next;/*原表头结点指针后移*/
p1=head;/*设定移动指针p1,从头指针开始*/
p2=head;/*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->average<p1->average&&p1!=NULL)/*作成绩平均分比较*/
{
p2=p1;/*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2)/*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1;/*待排序点的后继为p*/
head=t;/*新头结点为待排序点*/
}
else/*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1;/*t的后继是p1*/
p2->next=t;/*p2的后继是t*/
}
}
p1=head;/*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL)/*当p1不为空时,进行下列操作*/
{
i++;/*结点序号*/
p1->order=i;/*将结点序号赋值给名次*/
p1=p1->next;/*指针后移*/
}
printf("Sortingissucessful. ");/*排序成功*/
return(head);
}

/*插入记录函数*/
STUDENT*insert(STUDENT*head,STUDENT*newnode)
{STUDENT*p0,*p1,*p2;
intn,sum1,i;
p1=head;/*使p1指向第一个结点*/
p0=newnode;/*p0指向要插入的结点*/
printf(" Pleaseenteranewnoderecord. ");/*提示输入记录信息*/
printf("Enterthenum:");
scanf("%s",newnode->num);
printf("Enterthename:");
scanf("%s",newnode->name);
printf("Pleaseenterthe%dscores. ",3);
sum1=0;/*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("Dataerror,pleaseenteragain. ");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1+newnode->score[i];/*累加各门成绩*/
}
newnode->sum=sum1;/*将总分存入新记录中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==NULL)/*原来的链表是空表*/
{head=p0;p0->next=NULL;}/*使p0指向的结点作为头结点*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1;/*使p2指向刚才p1指向的结点*/
p1=p1->next;/*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0;/*插到原来第一个结点之前*/
elsep2->next=p0;/*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}/*插到最后的结点之后*/
}
n=n+1;/*结点数加1*/
head=sort(head);/*调用排序的函数,将学生成绩重新排序*/
printf(" Student%shavebeeninserted. ",newnode->name);
printf("Don'tforgettosavethenewnodefile. ");
return(head);
}

/*保存数据到文件函数*/
voidsave(STUDENT*head)
{FILE*fp;/*定义指向文件的指针*/
STUDENT*p;/*定义移动指针*/
charoutfile[10];
printf("Enteroutfilename,forexamplec:\score ");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)/*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannotopenthefile ");
return;/*若打不开则返回菜单*/
}
printf(" Savingthefile...... ");
p=head;/*移动指针从头指针开始*/
while(p!=NULL)/*如p不为空*/
{
fwrite(p,LEN,1,fp);/*写入一条记录*/
p=p->next;/*指针后移*/
}
fclose(fp);/*关闭文件*/
printf("Savethefilesuccessfully! ");
}

/*从文件读数据函数*/
STUDENT*load()
{STUDENT*p1,*p2,*head=NULL;/*定义记录指针变量*/
FILE*fp;/*定义指向文件的指针*/
charinfile[10];
printf("Enterinfilename,forexamplec:\score ");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)/*打开一个二进制文件,为只读方式*/
{
printf("Cannotopenthefile. ");
return(head);
}
printf(" Loadingthefile! ");
p1=(STUDENT*)malloc(LEN);/*开辟一个新单元*/
if(!p1)
{
printf("Outofmemory! ");
return(head);
}
head=p1;/*申请到空间,将其作为头指针*/
while(!feof(fp))/*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1)break;/*如果没读到数据,跳出循环*/
p1->next=(STUDENT*)malloc(LEN);/*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Outofmemory! ");
return(head);
}
p2=p1;/*使p2指向刚才p1指向的结点*/
p1=p1->next;/*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL;/*最后一个结点的后继指针为空*/
fclose(fp);
printf("! ");
return(head);
}

⑻ 100分跪求 舞台灯C语言程序

//舞台灯
//P1口输出高电平导通
//P3.2口控制开关
//P3.3口控制模式

#include<reg51.h>
#define uchar unsigned char

bit key1=0; //开关标志
bit key2=1; //模式标志

void START()interrupt 0 using 0
{
key1=~key1; //开关控制
}
void CHANGE()interrupt 2 using 2
{
key2=~key2; //改变点亮模式
}

void delay(uchar time)
{
uchar i;
while(time--)
for(i=0;i<250;i++);
}

void main()
{
uchar led_l=0x10;
uchar led_r=0x08;
uchar led;

IE=0x85;
TCON=0x05;

while(1)
{
while(key1) //检查是否按下START键
{
led=led_l+led_r;
P1=led; //输出灯亮码

if(key2) //检查是否按下CHANGE键
{
led_l=led_l<<1;led_r=led_r>>1; //默认状态下未按CHANGE时从中向两边点亮
if(led_l==0x00){led_l=0x10;led_r=0x08;}
}
else
{
led_l=led_l>>1;led_r=led_r<<1; //按下CHANGE时
if(led_l==0x08){led_l=0x80;led_r=0x01;}

}
delay(200); //延时
}
P1=0x00;
}
}