老师叫我们做了一个JAVA程序演示这一学期的JAVA程序。

说实话,非要硬要用JAVA来演示,效果实在是不好,我搞到凌晨一点过,终于把JNI调用学会了,才学会一个简简单单的清屏,如果是用C语言来写,那该多方便呀,所以呢,我自己又用C写了一个JAVA程序演示。

主要运用的就是读取文件下下的所有.java文件啦。有点相当于DIR的功能了,呵呵……

好了,不说废话了,上代码。

//
//    Made By SUNGJIRA   2009
//
#include <stdio.h>
#include <dirent.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
int TestIsJava(char *src);
int Show(char num);
void PAUSE(int i);
char *GetFileList(int index);
struct FileList *GetJavaFile(char *dir);
struct FileList
{
	char filename[512];
	struct FileList *next;
}
*head, *node;
int filenum = 0;
HANDLE consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
int main(int argc, char *argv[])
{
 
	head = GetJavaFile("java");
	if (filenum == 0)
	{
		SetConsoleTextAttribute(consolehwnd, 15);
		puts("*******************************************************************************");
		puts("                                舒俊杰JAVA程序演示                             ");
		puts("*******************************************************************************\n\n\n\n\n\n\n\n\n");
		SetConsoleTextAttribute(consolehwnd, 14);
		puts("                          请将JAVA源程序放到JAVA文件夹中。\n");
		printf("                          按任意键退出...");
		getch();
		return 0;
	}
	while (1)
	{
		int key;
		int i;
		system("CLS");
		SetConsoleTextAttribute(consolehwnd, 15);
		puts("*******************************************************************************");
		puts("                                舒俊杰JAVA程序演示                             ");
		puts("*******************************************************************************");
		printf("共有%d个需要演示的JAVA程序。\n\n", filenum);
		SetConsoleTextAttribute(consolehwnd, 14);
		for (i = 1;i <= filenum;i++)
		{
			printf("   %d) %s\n\n", i, GetFileList(i) );
		}
		printf("   0) exit\n\n");
		SetConsoleTextAttribute(consolehwnd, 15);
		printf("请选择一个功能:");
		key = getche() - '0';
		Sleep(200);
		if (key == 0) break;
		if (key <= 0 || key > filenum)
		{
			printf("\n\n您的输入错误!请重新选择。");
			PAUSE(2000);
			continue;
		}
		Show(key);
	}
	puts("\n\nGood bye!");
	PAUSE(2000);
	return 0;
}
int Show(char num)
{
	while (1)
	{
		int key;
		system("CLS");
		SetConsoleTextAttribute(consolehwnd, 15);
		puts("*******************************************************************************");
		puts("                                舒俊杰JAVA程序演示                             ");
		puts("*******************************************************************************");
		printf("您现在选择的是:\"%s\"\n\n", GetFileList(num));
		SetConsoleTextAttribute(consolehwnd, 14);
		printf("   1) 查看代码\n\n");
		printf("   2) 运行程序\n\n");
		printf("   0) 返回上级\n\n");
		SetConsoleTextAttribute(consolehwnd, 15);
		printf("请选择一个功能:");
		key = getche() - '0';
		Sleep(200);
		if (key == 0) break;
		if (key == 1)
		{
			system("CLS");
			char comm[512] = "cd java&TYPE ";
			strcat(comm, GetFileList(num));
			strcat(comm, ".java");
			system(comm);
			SetConsoleTextAttribute(consolehwnd, 14);
			printf("\n\n按任意键返回");
			getch();
		}
		if (key == 2)
		{
 
			system("CLS");
			SetConsoleTextAttribute(consolehwnd, 14);
			char comm[512] = "cd java&javac \"";
			strcat(comm, GetFileList(num));
			strcat(comm, ".java\"");
			printf("javac %s.java\n", GetFileList(num));
			system(comm);
			strcpy(comm, "cd java&java \"");
			strcat(comm, GetFileList(num));
			printf("java %s ", GetFileList(num));
			char args[512];
			gets(args);
			strcat(comm, "\" ");
			strcat(comm, args);
			SetConsoleTextAttribute(consolehwnd, 15);
			system(comm);
			SetConsoleTextAttribute(consolehwnd, 14);
			printf("\n\n按任意键返回");
			getch();
		}
	}
}
int TestIsJava(char *src)
{
	int i = strlen(src);
	if (i < 4) return 0;
	if (src[i -1] != 'a' && src[i - 1] != 'A') return 0;
	if (src[i -2] != 'v' && src[i - 2] != 'V') return 0;
	if (src[i -3] != 'a' && src[i - 3] != 'A') return 0;
	if (src[i -4] != 'j' && src[i - 4] != 'J') return 0;
	if (src[i -5] != '.') return 0;
	src[i -5] = 0;
	return 1;
}
void PAUSE(int j)
{
	for (int i = 0;i < j / 100;i++)
	{
		printf(" \b");
		Sleep(100);
	}
}
struct FileList *GetJavaFile(char *directory)
{
	DIR *directory_pointer;
	struct dirent *entry;
	directory_pointer = opendir(directory);
	node = head = (struct FileList *)malloc(sizeof(struct FileList));
	while ((entry = readdir(directory_pointer)) != NULL)
	{
		if (strcmp(entry->d_name, ".") == 0) continue;
		if (strcmp(entry->d_name, "..") == 0) continue;
		if (TestIsJava(entry->d_name) == 0) continue;
		strcpy(node->filename, entry->d_name);
		node->next = (struct FileList *)malloc(sizeof(struct FileList));
		node = node->next;
		filenum++;
	}
	closedir(directory_pointer);
	node->next = NULL;
	return head;
}
char *GetFileList(int i)
{
	int j = 0;
	struct FileList *temp = head;
	while (temp->next)
	{
		j++;
		if (j == i) break;
		temp = temp->next;
	}
	return temp->filename;
}

下载压缩包吧,包含了所有的东西,还有用来演示的程序。

下载地址:
JAVA程序演示

原创文章,转载请注明: 转载自耍下
本文链接地址: C语言 – JAVA程序演示