博文

C图形学的弱题(2007-11-19 22:33:00)

摘要:以下三个都是很简单的C语言图形学的小程序,可以输出几个不错的图形,作为子函数还可以的,拿出来展览一下,不过运行的时候注意一下initgraph的路径,自己改一下吧。 #include<graphics.h>  /*A simplest C graph product,run it directly*/ main()
{int x,driver=DETECT,mode;
 registerbgidriver(EGAVGA_driver);
 initgraph(&driver,&mode,"");
 cleardevice();
 setbkcolor(9);
 for(x=170;x<=470;x=x+30)
 {line(170,200,x,100);
  line(170,200,x,300);
  line(470,200,x,100);
  line(470,200,x,300);
 }
 getch();
 closegraph();
} ---------------------------------------------------------------------- #include<graphics.h>  /*Input a number less than 50 and than Enter*/
#include<conio.h>
#include<math.h> #define pi 3.1415926
#define max 50 main()
{int i,j,n,driver=DETECT,mode;
 double r=100.0,theta,x[max],y[max];
 printf("n(<=50)=");
 scanf("%d",&n);
 registerbgidriver(EGAVGA_driver);
&nb......

阅读全文(3223) | 评论:2

中点画线法(2007-11-19 22:13:00)

摘要:运行之前也须自己设置好initgraph里面的图形驱动路径,如果是绝对路径的话,可以把前面的registerbgidriver(EGAVGA_driver);一句去掉,程序很简单,没什么好说的了。 #include<graphics.h> main()
{int a,b,d,x0,y0,x1,y1,x,y;
 int driver=DETECT,mode;
 printf("Please input two point to draw the line between:\n");
 printf("x0=");scanf("%d",&x0);
 printf("y0=");scanf("%d",&y0);
 printf("x1=");scanf("%d",&x1);
 printf("y1=");scanf("%d",&y1);
 a=y0-y1;b=x1-x0;d=2*a+b;
 registerbgidriver(EGAVGA_driver);
 initgraph(&driver,&mode,"");
 for(x=x0,y=y0;x<=x1;)
 {putpixel(x,y,14);
  if(d>=0)
  {x=x+1;
   d=d+2*a;
  }
  else
  {x=x+1;
   y=y+1;
   d=d+2*(a+b);
  }
 }
 getch();
 closegraph();
}......

阅读全文(4258) | 评论:0

打印杨辉三角的前八行(2007-09-26 10:11:00)

摘要:打印出杨辉三角的前八行,如要打印不同的行数请自行修改数组下标和控制变量的范围,仅供参考,保存在文本文件并更名为yanghui.java并在命令提示符下运行即可。 public class yanghui
{public static void main(String arg[])
 {int arr[][]=new int[8][8];
  int i,j,k;
  for(i=0;i<8;i++)
  {for(j=0;j<8;j++)
   {if(j==0||i==j)arr[i][j]=1;
   }
  }
  for(i=2;i<8;i++)
  {for(j=1;j<7;j++)
   {arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
   }
  }
  for(i=0;i<8;i++)
  {for(k=0;k<14-2*i;k+=1)System.out.print(" ");
   for(j=0;j<8;j++)
   {if(arr[i][j]!=0)System.out.print(arr[i][j]);
    if((arr[i][j]>0)&&(arr[i][j]<10))System.out.print("   ");
    if(arr[i][j]>=10)System.out.print("  ");
   }
   System.out.println();
  }
 }
}......

阅读全文(4420) | 评论:0

判断回文数(2007-09-26 10:07:00)

摘要:Java中实现的对输入的字符串判断是否回文数的程序,保存在文本文档里,并把文档更名为huiwen.java,命令提示符下运行即可。 public class huiwen {public static void main(String arg[])
 throws java.io.IOException
 {char s[]=new char[20];
  int i=-1,count;
  System.out.println("请输入要判断的串:");
  do
  {i++;
   s[i]=(char)System.in.read();
  }while(s[i]!='\n');
  count=i;
  for(i=0;i<count-2-i;i++)
  {if(s[i]!=s[count-2-i])break;
  }
  if(i<(count-2-i))System.out.println("这不是回文数。");
  else System.out.println("这是回文数。");
 }
}......

阅读全文(3913) | 评论:1

语法分析器(2007-06-23 11:59:00)

摘要:这是一个能识别加法和乘法的语法分析器,例如在提示输入后输入一个算式:3+5*2*(2+1)这类的,那么就会给出匹配过程,并判断是不是成功,请赏析!由于构造了first集,follow集,select集之类,所以比较占篇幅,惭愧! #include<string.h> int isterminal(char a)
{if((a=='i')||(a=='+')||(a=='*')||(a=='(')||(a==')')||(a=='$'))
 return(1);
 else return(0);
} int notin(char a,char *b)
{char *c;
 for(c=b;(*c)!='\0';c++)
 {if((*c)==a) break;
 }
 if((*c)=='\0') return(1);
 else return(0);
} void add(char *g,char *q,char *a)
{char *tem;
 tem=a;
 for(;(*tem)!='\0';tem++)
 {if(notin((*tem),g))
  {(*q)=(*tem);
   q++;
   (*q)='\0';
  }
 }
} int maybenull(char E)
{char product[8][5]={"SBA","A+BA","A","BDC","C*DC","C","D(S)","Di"};
 int i;
 for(i=0;i<8;i++)
 {if((product[i][0]==E)&&(product[i][1]=='\0'))
  break;
 }
 if(i<8) return(1);
 else if(i>=8) return(0);
} char......

阅读全文(5067) | 评论:1

词法分析器(C语言版)(2007-06-06 22:59:00)

摘要:这是我用C语言编写的能识别C语言的关键字之类的一个词法分析器,需要有一个程序文件,然后在程序的第一行把#define Filaname “文件名”的那个文件名换成要识别的文件的准确路径,之后就运行就可以了,这个程序是在TurboC 2.0和本网站提供的TC for windows 3.1中调试成功的,如有问题请及时联系我,谢谢!(自认为不足之处就是没有调用函数,一个main弄到底,还有就是没有注释,看不懂的话千万不要硬撑啊!) #define Filename "1.txt"
#include<string.h>
#include<stdio.h> main()
{char keyword[32][9]={"auto","break","case","char","const","continue","default","double","do","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
 char sing[40][5]={"->","++","--","<<",">>","<=",">=","==","!=","&&","||","?:","+=","-=","*=","/=","%=",">>=","<<=","&=","|=","^=","=",".","!","~","-","*","&'","/","%","+","<",">","^","|'","?",":",",",";"};
 char *s;
 char temp[10][9],ci[9];
 char *go,*now;
 int i,j,n,k,out,tag1=0,tag2=0,tag3=0;
 FILE ......

阅读全文(5877) | 评论:1

把一个100D个字的DATA区里的最小偶数找出(2006-11-05 13:52:00)

摘要:把一个100D个字的DATA区里的最小偶数找出来,区里的数是我编程序时自己设好的,大家可以改一下啊! data_seg segment
    DATA dw 99,100,98 dup(7)
    TEMP dw 100(?)
data_seg ends
code_seg segment
  assume cs:code_seg,ds:data_seg
start:    mov ax,data_seg
          mov ds,ax
          mov cx,100
          mov bx,0
          mov ax,0
          mov si,ax
again:    mov dx,DATA[si]
          and dx,1h
          jnz continue
          mov TEMP[si],dx
continue: inc bx
          add si,2
      &nb......

阅读全文(3875) | 评论:1

最简单的一个汇编程序(2006-11-05 13:47:00)

摘要:这是一个在屏幕上输一个小写字母,然后被转化为一个大写字母显示出来,看看吧,变得不怎么样,不过实现了。 code segment
     assume cs:code
start:  mov ah,1
        int 21h
        cmp al,'a'
        jb exit_0
        cmp al,'z'
        ja exit_0
        sub al,20h
        mov dl,al
        mov ah,2
        int 21h
exit_0: mov ah,4ch
        int 21h
code ends
end start......

阅读全文(4766) | 评论:2

用C语言编写的模拟进程控制的程序(2006-10-05 11:47:00)

摘要:本程序是在Turbo C 2.0环境下编写的,预计在其他C程序编译系统中也没问题。 #define null 0
#define M "         Menu\n\n1.Creat a process\n2.Destroy a process\n3.Blockaprocess\n4.Wakeup a process\n5.Suspend a process\n6.Active a process\n7.Observe the processes\n\nPlease select the item you want to realize:" struct PCB
{char name;
 int ko;
 int cha;
} struct PCB pcb[10];
int i=0; void creat()
{char a;
 int b;
 printf("Please input the name of the process:");
 scanf("%c",&a);
 printf("\nPlease input the ko of the process:");
 scanf("%d",&b);
 i++;
 pcb[i].name=a;
 pcb[i].ko=b;
 pcb[i].cha=1;
 printf(M);
} void destroy()
{char a;
 int j;
 printf("Select a process you want to destroy:");
 scanf("%c",&a);
 printf("\n");
 for(j=1;j<=10;j++)
 {if(pcb[j].name==a)
  {pcb[j].name=0;
 &nbs......

阅读全文(6005) | 评论:10

FIFO算法模拟(2006-07-04 12:18:00)

摘要:#define M 4 #define N 10 #include<stdio.h>   typedef struct Queue {char *head;  char *tail:  int=length; }Que;   Que create() {Que Q;  Q.head=(Queue)malloc(sizeof(Que));  Q.tail=Q.head;  Q.length=0;  return(Q); }   Que push(Que Q,char c) {*++Q.tail=c;  Q.length++;  if(Q.length>M)printf("Overflow!");  else return(Q); }   char pop(Que Q) {char c;  if(Q.length==0)exit(NULL);  Q.length++; else return(c); }   void visit(Que Q) {int count;  char e;  for(count=0;count<M;count++)  {e=pop(Q);   printf("%c",e);   push(Q,pop(Q));  } }   main() {Que Q,p;  char c,e;  c=getchar();  while(c!=NULL)  {for(p=Q.head;p<=Q.tail;p++)   {if(*p==c)visit(Q);    else    {e=pop(Q);     push(Q,c);    }   }  } }......

阅读全文(5076) | 评论:2