當前位置:首頁 » 編程語言 » 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;
}
}