⑴ 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实际长度(不算'