⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c.txt

📁 1微型打印机的C语言源程序 2连接两个链表 3输入n为偶数时
💻 TXT
字号:
1微型打印机的C语言源程序:

#define uchar unsigned char
#define uint unsigned int
#include <reg52.h>
#include <stdio.h>
#include <absacc.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define PIN XBYTE[0x8000]
#define POUT XBYTE[0x9000]
sbit PRINTSTB =P1^6;
sbit DOG=P1^7;
bdata char pin&#118alue;
sbit PRINTBUSY=pin&#118alue^7;
sbit PRINTSEL =pin&#118alue^6;
sbit PRINTERR =pin&#118alue^5;
sbit PRINTACK =pin&#118alue^4;
  
void PrintString(uchar *String1,uchar *String2);
void initprint(void);
void print(uchar a);
  
void initprint(void) //打印机初始化子程序 
{
  pin&#118alue=PIN;
  if((PRINTSEL==1)&&(PRINTERR==1))
  {
    print(0x1b); print(0x40); print(0x1b); print(0x38); print(0x4);
  }
}
void print(uchar a) //打印字符a
{
  pin&#118alue=PIN;
  if((PRINTSEL==0)||(PRINTERR==0)) return;
  for(;;) {
    DOG=~DOG;
    pin&#118alue=PIN;
    if(PRINTBUSY==0) break;
  }
  DOG=~DOG;
  POUT=a;
  PRINTSTB=1;  PRINTSTB=1;  PRINTSTB=1;  PRINTSTB=1;
  PRINTSTB=0;  PRINTSTB=0;  PRINTSTB=0;  PRINTSTB=0;
  PRINTSTB=1;
}
void PrintString(uchar *String) //打印字符串后回车
{
  uchar CH;
  for (;;) {
   DOG=~DOG;
   CH=*String;
   if (CH==0) { print(0x0d); break; }
   print(CH);
   String++;
  }
  initprint();
} 




2连接两个链表。  
#include "stdlib.h"  
#include "stdio.h"  
struct list  
{ int data;  
struct list *next;  
};  
typedef struct list node;  
typedef node *link;  
link delete_node(link pointer,link tmp)  
{if (tmp==NULL) /*delete first node*/  
 return pointer->next;  
else  
{ if(tmp->next->next==NULL)/*delete last node*/  
  tmp->next=NULL;  
 else /*delete the other node*/  
  tmp->next=tmp->next->next;  
 return pointer;  
}  
}  
void selection_sort(link pointer,int num)  
{ link tmp,btmp;  
 int i,min;  
 for(i=0;i<num;i++)  
 {  
 tmp=pointer;  
 min=tmp->data;  
 btmp=NULL;  
 while(tmp->next)  
 { if(min>tmp->next->data)  
 {min=tmp->next->data;  
  btmp=tmp;  
 }  
 tmp=tmp->next;  
 }  
printf("\40: %d\n",min);  
pointer=delete_node(pointer,btmp); 
 }  
}  
link create_list(int array[],int num)  
{ link tmp1,tmp2,pointer;  
int i;  
pointer=(link)malloc(sizeof(node));  
pointer->data=array[0];  
tmp1=pointer;  
for(i=1;i<num;i++)  
{ tmp2=(link)malloc(sizeof(node));  
 tmp2->next=NULL;  
 tmp2->data=array[i];  
 tmp1->next=tmp2;  
 tmp1=tmp1->next;  
}  
return pointer;  
}  
link concatenate(link pointer1,link pointer2)  
{ link tmp;  
tmp=pointer1;  
while(tmp->next)  
 tmp=tmp->next;  
tmp->next=pointer2;  
return pointer1;  
}  
void main(void)  
{ int arr1[]={3,12,8,9,11};  
 link ptr;  
 ptr=create_list(arr1,5);  
 selection_sort(ptr,5);  
3输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数  
   1/1+1/3+...+1/n(利用指针函数)  
 
main()  
#include "stdio.h"  
main()  
{  
float peven(),podd(),dcall();  
float sum;  
int n;  
while (1)  
{  
 scanf("%d",&n);  
 if(n>1)  
  break;  
}  
if(n%2==0)  
{  
 printf("Even=");  
 sum=dcall(peven,n);  
}  
else  
{  
 printf("Odd=");  
 sum=dcall(podd,n);  
}  
printf("%f",sum);  
}  
float peven(int n)  
{  
float s;  
int i;  
s=1;  
for(i=2;i<=n;i+=2)  
 s+=1/(float)i;  
return(s);  
}  
float podd(n)  
int n;  
{  
float s;  
int i;  
s=0;  
for(i=1;i<=n;i+=2)  
 s+=1/(float)i;  
return(s);  
}  
float dcall(fp,n)  
float (*fp)();  
int n;  
{  
float s;  
s=(*fp)(n);  
return(s);  
}  
4时间函数举例4,一个猜数游戏,判断一个人反应快慢。  
  
#include "time.h"  
#include "stdlib.h"  
#include "stdio.h"  
main()  
{char c;  
clock_t start,end;  
time_t a,b;  
double var;  
int i,guess;  
srand(time(NULL));  
printf("do you want to play it.('y' or 'n') \n");  
loop:  
while((c=getchar())=='y')  
{  
i=rand()%100;  
printf("\nplease input number you guess:\n");  
start=clock();  
a=time(NULL);  
scanf("%d",&guess);  
while(guess!=i)  
{if(guess>i)  
{printf("please input a little smaller.\n");  
scanf("%d",&guess);}  
else  
{printf("please input a little bigger.\n");  
scanf("%d",&guess);}  
}  
end=clock();  
b=time(NULL);  
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);  
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));  
if(var<15)  
printf("\1\1 You are very clever! \1\1\n\n");  
else if(var<25)  
printf("\1\1 you are normal! \1\1\n\n");  
else  
printf("\1\1 you are stupid! \1\1\n\n");  
printf("\1\1 Congradulations \1\1\n\n");  
printf("The number you guess is %d",i);  
}  
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");  
if((c=getch())=='y')  
goto loop;  
}  

5家庭财务管理小程序  
  

#include "stdio.h"  
#include "dos.h"  
main()  
{  
FILE *fp;  
struct date d;  
float sum,chm=0.0;  
int len,i,j=0;  
int c;  
char ch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8];  
pp: clrscr();  
sum=0.0;  
gotoxy(1,1);printf("|---------------------------------------------------------------------------|");  
gotoxy(1,2);printf("| money management system(C1.0) 2000.03 |");  
gotoxy(1,3);printf("|---------------------------------------------------------------------------|");  
gotoxy(1,4);printf("| -- money records -- | -- today cost list -- |");  
gotoxy(1,5);printf("| ------------------------ |-------------------------------------|");  
gotoxy(1,6);printf("| date: -------------- | |");  
gotoxy(1,7);printf("| | | | |");  
gotoxy(1,8);printf("| -------------- | |");  
gotoxy(1,9);printf("| thgs: ------------------ | |");  
gotoxy(1,10);printf("| | | | |");  
gotoxy(1,11);printf("| ------------------ | |");  
gotoxy(1,12);printf("| cost: ---------- | |");  
gotoxy(1,13);printf("| | | | |");  
gotoxy(1,14);printf("| ---------- | |");  
gotoxy(1,15);printf("| | |");  
gotoxy(1,16);printf("| | |");  
gotoxy(1,17);printf("| | |");  
gotoxy(1,18);printf("| | |");  
gotoxy(1,19);printf("| | |");  
gotoxy(1,20);printf("| | |");  
gotoxy(1,21);printf("| | |");  
gotoxy(1,22);printf("| | |");  
gotoxy(1,23);printf("|---------------------------------------------------------------------------|");  
i=0;  
getdate(&d);  
sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day);  
for(;;)  
{  
gotoxy(3,24);printf(" Tab __browse cost list Esc __quit");  
gotoxy(13,10);printf(" ");  
gotoxy(13,13);printf(" ");  
gotoxy(13,7);printf("%s",chtime);  
j=18;  
ch[0]=getch();  
if(ch[0]==27)  
break;  
strcpy(chshop,"");  
strcpy(chmoney,"");  
if(ch[0]==9)  
{  
mm:i=0;  
fp=fopen("home.dat","r+");  
gotoxy(3,24);printf(" ");  
gotoxy(6,4);printf(" list records ");  
gotoxy(1,5);printf("|-------------------------------------|");  
gotoxy(41,4);printf(" ");  
gotoxy(41,5);printf(" |");  
while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&chm)!=EOF)  
{ if(i==36)  
{ getch();  
i=0;}  
if ((i%36)<17)  
{ gotoxy(4,6+i);  
printf(" ");  
gotoxy(4,6+i);}  
else  
if((i%36)>16)  
{ gotoxy(41,4+i-17);  
printf(" ");  
gotoxy(42,4+i-17);}  
i++;  
sum=sum+chm;  
printf("%10s %-14s %6.1f\n",chtime,chshop,chm);}  
gotoxy(1,23);printf("|---------------------------------------------------------------------------|");  
gotoxy(1,24);printf("| |");  
gotoxy(1,25);printf("|---------------------------------------------------------------------------|");  
gotoxy(10,24);printf("total is %8.1f$",sum);  
fclose(fp);  
gotoxy(49,24);printf("press any key to.....");getch();goto pp;  
}  
else  
{  
while(ch[0]!='\r')  
{ if(j<10)  
{ strncat(chtime,ch,1);  
j++;}  
if(ch[0]==8)  
{  
len=strlen(chtime)-1;  
if(j>15)  
{ len=len+1; j=11;}  
strcpy(ch1,"");  
j=j-2;  
strncat(ch1,chtime,len);  
strcpy(chtime,"");  
strncat(chtime,ch1,len-1);  
gotoxy(13,7);printf(" ");}  
gotoxy(13,7);printf("%s",chtime);ch[0]=getch();  
if(ch[0]==9)  
goto mm;  
if(ch[0]==27)  
exit(1);  
}  
gotoxy(3,24);printf(" ");  
gotoxy(13,10);  
j=0;  
ch[0]=getch();  
while(ch[0]!='\r')  
{ if (j<14)  
{ strncat(chshop,ch,1);  
j++;}  
if(ch[0]==8)  
{ len=strlen(chshop)-1;  
strcpy(ch1,"");  
j=j-2;  
strncat(ch1,chshop,len);  
strcpy(chshop,"");  
strncat(chshop,ch1,len-1);  
gotoxy(13,10);printf(" ");}  
gotoxy(13,10);printf("%s",chshop);ch[0]=getch();}  
gotoxy(13,13);  
j=0;  
ch[0]=getch();  
while(ch[0]!='\r')  
{ if (j<6)  
{ strncat(chmoney,ch,1);  
j++;}  
if(ch[0]==8)  
{ len=strlen(chmoney)-1;  
strcpy(ch1,"");  
j=j-2;  
strncat(ch1,chmoney,len);  
strcpy(chmoney,"");  
strncat(chmoney,ch1,len-1);  
gotoxy(13,13);printf(" ");}  
gotoxy(13,13);printf("%s",chmoney);ch[0]=getch();}  
if((strlen(chshop)==0)||(strlen(chmoney)==0))  
continue;  
if((fp=fopen("home.dat","a+"))!=NULL);  
fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney);  
fputc('\n',fp);  
fclose(fp);  
i++;  
gotoxy(41,5+i);  
printf("%10s %-14s %-6s",chtime,chshop,chmoney);  
}}} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -