⑴ 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實際長度(不算'