C语言课程设计

200行左右代码编写一个小程序, 要求每行都有说明 因为我们老师需要我们解释
在TC2.0下能运行的 。。。。。下面那位大哥的 没办法运行啊

以下程序已在win-tc和tc2.0下运行通过,已加详细注释(本人所写)。
/* 数据安全实用程序,加密解密简单程序 */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int flag;

char encrypt(char ch,int key)/*加密函数,把字符循环移位*/
{
if(ch>='a' && ch<='z') /* 如果是小写字母 */
{
ch=(ch-'a'+key%26)%26+'a'; /* 字母向后移key%26个位置,超过字母z则再从a开始向后移动 */
}
else if(ch>='A' && ch<='Z') /* 如果是大写字母 */
{
ch=(ch-'A'+key%26)%26+'A'; /* 字母向后移key%26个位置,超过字母Z则再从A开始向后移动 */
}
return ch;
}

char decrypt(char ch,int key)/*解密函数,把字符循环移位*/
{
if(ch>='a' && ch<='z') /* 如果是小写字母 */
{
ch=(ch-'a'+26-key%26)%26+'a'; /* 字母向后移26-key%26个位置,超过字母z则再从a开始向后移动 */
}
else if(ch>='A' && ch<='Z') /* 如果是大写字母 */
{
ch=(ch-'A'+26-key%26)%26+'A'; /* 字母向后移26-key%26个位置,超过字母Z则再从A开始向后移动 */
}
return ch;
}

void menu()/*菜单,1.加密,2.解密,3.显示文本文件内容*/
{
clrscr();
printf("\n=======================================================");
printf("\n1.Encrypt the text file"); /* 加密文件 */
printf("\n2.Decrypt the text file"); /* 解密文件 */
printf("\n3.Display text file contents");/* 显示加密或解密或未加密或解密的文件 */
printf("\n4.Quit\n");
printf("=========================================================\n");
printf("Please select a item:"); /* 选择一个菜单 */
}

void logo()/*显示程序信息*/
{
printf("\nwelcome to encrypt program \n ");
return;
}

void encrypt_decrypt_File(char* infile,int key, char* outfile) /* 加密或解密函数 */
{
FILE *in,*out;
char ch;
clrscr(); /* 清屏 */
if((in=fopen(infile,"r"))==NULL) /* 打开欲加密或解密的文件*/
{
printf("Can not open the infile!\n"); /* 如果打开文件失败或文件不存在打印打开失败信息 */
printf("Press any key to exit!\n");
getch(); /* 并等待任一按键然后退出程序 */
exit(0);
}
if((out=fopen(outfile,"w"))==NULL) /* 打开文件保存加密或解密后的内容*/
{
printf("Can not open the outfile!\n"); /* 如果打开文件失败或文件不存在打印打开失败信息 */
printf("Press any key to exit!\n"); /* 并等待任一按键然后退出程序 */
fclose(in); /* 关闭输入文件 */
getch(); /* 等待按键,按任一键退出程序 */
exit(0);
}
ch=fgetc(in); /*从文本文件中读入字符*/
while(ch!=EOF)/*加密或解密*/
{
/*如果是英文字符,则进行加密或解密,否则,不进行加密或解密处理*/
if((ch>='a' && ch<='z' ) || (ch>='A' && ch<='Z'))
{ if(flag==1)
fputc(encrypt(ch,key),out);
if(flag==2)
fputc(decrypt(ch,key),out);
}
else
fputc(ch,out);
ch=fgetc(in);
}
/*关闭输入及输出文件*/
fclose(in);
fclose(out);

}

void displayFile(char *infile) /*将文本文件的内容显示在屏幕上*/
{
FILE *fp;
char string[81];
if((fp=fopen(infile,"r"))==NULL) /* 以只读方式打开文本文件 */
{
printf("cann't open file");exit(0); /* 如果文件不存在或打开失败打印无法打开信息并退出程序 */
}
while(fgets(string,81,fp)!=NULL)
fputs(string,stdout); /*把所取字符串送到屏幕显示*/
fclose(fp); /* 关闭文件 */
}

int main()
{
int i,n;
char ch0,ch1;
char infile[40],outfile[40];
textbackground(LIGHTGRAY); /*设置背景颜色为浅灰色*/
textcolor(BLACK); /*设置文字颜色为黑色*/
clrscr();/*清除屏幕显示*/
logo(); /*显示程序信息*/
sleep(2); /* 延时2秒 */
menu(); /*显示屏幕菜单*/
ch0=getche();/*等待用户从键盘输入,并把输入显示在屏幕上*/
while(ch0!='4')
{
clrscr();
if(ch0=='1') /*选择加密功能*/
{
flag=1;
printf("\nPlease input the infile to be encrypted:"); /* 输入要加密的文件名 */
scanf("%s",infile); /* 该文件要和本程序放在同一个目录下 */
printf("Please input the encrypt key:");
scanf("%d",&n);/*输入加密密码*/
printf("Please input the outfile:"); /*输入存放加密内容的文件名*/
scanf("%s",outfile); /* 该文件可以自动创建 */
encrypt_decrypt_File(infile,n,outfile);
printf("\nEncrypt is over!\n");/* 加密成功 */
sleep(1); /* 延时1秒 */
}
else if(ch0=='2') /*选择解密功能*/
{
flag=2;
printf("\nPlease input the infile to be decrypted:"); /* 输入要解密的文件名 */
scanf("%s",infile); /* 该文件要和本程序放在同一个目录下 */
printf("Please input the decrypt key:");
scanf("%d",&n);/*输入解密密码,加密和解密密码应相同*/
printf("Please input the outfile:"); /*输入存放解密内容的文件名*/
scanf("%s",outfile); /* 该文件可以自动创建 */
encrypt_decrypt_File(infile,n,outfile);
printf("\nDecrypt is over!\n");
sleep(1); /* 延时1秒 */
}
else if(ch0=='3') /*选择显示文本文件功能*/
{
printf("\nPlease input the infile to be displayed:"); /* 输入要显示的文件名 */
scanf("%s",infile);
displayFile(infile);/* 显示文件 */
getch();
}
else
{ /*不合法输入*/
printf("\nplease input a valid number(1-4)\n");
sleep(1); /* 延时1秒 */
}
menu();/*显示程序菜单*/
ch0=getche(); /*等待用户下一次的功能选择*/
}
system("cls");/*清除屏幕*/
logo(); /*显示程序信息*/
printf("\nGood Bye!\n");
sleep(2);/* 延时2秒 */

system("pause"); /* 暂停,按任一键退出程序 */
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-07-04
//修改完毕
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>

const int W = 3;//横
const int H = 3;//竖
int world[W][H];//棋盘0为没放的,1是人类放的棋子,2是计算机放的棋子
int hard;

class player
{
public:
int done;//判断这步谁走……
}a,b;//姑且让a为人类,b为计算机

void Change()//走完一步后,人与计算机的先后循序颠倒一下
{
int temp;
temp = a.done;
a.done = b.done;
b.done = temp;
}

void New()//初始化界面
{
int i,j;
srand((unsigned) time(NULL));//产生随机数用
for(i=0;i<W;++i)
for(j=0;j<H;++j)
world[i][j] = 0;//初始化棋盘
a.done = rand()%2 + 1; //这里是初始化让谁先走,1先走,2后走
b.done = a.done % 2 +1;//这里是初始化让谁先走,1先走,2后走
}

void Print()//打印界面
{
int i,j;
system("cls");
printf("请按小键盘1~9键\n");
if(a.done == 1)
printf("now it's human's turn\n");
else
printf("now it's computer's turn\n");
for(i=0;i<W;++i)
{
for(j=0;j<H;++j)
printf(" %d ",world[i][j]);
putchar('\n');
}
}

int AI_easy()//计算机走棋,这个是简单版的,有点傻
{
int i,j;
srand((unsigned) time(NULL));//产生随机数用
i = rand()%W;
j = rand()%H;
if(world[W/2][H/2]==0)
{
world[W/2][H/2] = 2;
Change();
Print();
return 0;
}
while(1)
{
if(world[i][j] == 0)
{
world[i][j] = 2;
Change();
Print();
return 0;
}
else
i = rand()%W;
j = rand()%H;
}

}

int Win(int step)//判断获胜
{
int i,j,win;int n=0;
win = step;
for(i=0;i<H;++i)
if(world[i][0]==win&&world[i][1]==win&&world[i][2] == win)
return win;
for(i=0;i<W;++i)
if(world[0][i]==win&&world[1][i]==win&&world[2][i] == win)
return win;
if(world[0][0]==win&&world[1][1]==win&&world[2][2]==win)
return win;
if(world[0][2]==win&&world[1][1]==win&&world[2][0]==win)
return win;
for(i=0;i<H;++i)
for(j=0;j<W;++j)
if(world[i][j]==0)n++;
if(n==0) {printf("Draw!\n");exit(0);}//平局……因为……没地方放棋子了
return 0;
}

int Human()//人走
{
char in;
in = getch();
while (in < '1' || in > '9')
{
in = getch();
}
if(in == '7')
{
if(world[0][0] != 0)
Human();
else
{
world[0][0] = 1;
Change();
Print();
return 0;
}
}
if(in == '8')
{
if(world[0][1] != 0)
Human();
else
{
world[0][1] = 1;
Change();
Print();
return 0;
}
}
if(in == '9')
{
if(world[0][2] != 0)
Human();
else
{
world[0][2] = 1;
Change();
Print();
return 0;
}
}
if(in == '4')
{
if(world[1][0] != 0)
Human();
else
{
world[1][0] = 1;
Change();
Print();
return 0;
}
}
if(in == '5')
{
if(world[1][1] != 0)
Human();
else
{
world[1][1] = 1;
Change();
Print();
return 0;
}
}
if(in == '6')
{
if(world[1][2] != 0)
Human();
else
{
world[1][2] = 1;
Change();
Print();
return 0;
}
}
if(in == '1')
{
if(world[2][0] != 0)
Human();
else
{
world[2][0] = 1;
Change();
Print();
return 0;
}
}
if(in == '2')
{
if(world[2][1] != 0)
Human();
else
{
world[2][1] = 1;
Change();
Print();
return 0;
}
}
if(in == '3')
{
if(world[2][2] != 0)
Human();
else
{
world[2][2] = 1;
Change();
Print();
return 0;
}
}
return 0;
}

void Start()//游戏开始
{
while(1)
{
Print();
if(a.done == 1)
{
Human();
if(Win(1)) {printf("Winner is human!\n");exit(0);}
}
else
{
AI_easy();
if(Win(2)){printf("Winner is computer!\n");exit(0);};
}
}

}

int main()
{
New();
Start();
return 0 ;
}

附流程说明:
用数组定义棋盘world[H][W]=0;1为人落得子;2为计算机落得子

初始化()

哪方先行(定义一个变量done记录是否已经行动过了)

1:计算机->AI()->判断是否获胜()->人()->判断是否获胜()->循环

2:人()->判断是否获胜()->计算机AI()->判断是否获胜()->循环

获胜函数()

不能重复在某个位子落子()

AI
放中间
先判断能否直接获胜
放边角
第2个回答  2009-07-04
# include<stdio.h>
# include<string.h>
# include<malloc.h>
# include<math.h>
# define LEN sizeof(Node)
# define MAXO 50
# define MAXT 300

int Pop=0;
int Gop=0;
int Dop=0;
int Fop=0;

//节点
typedef struct TreeNode
{int flag;
char Name[MAXO];
struct TreeNode *Lchild;
struct TreeNode *Rchild;} Node;
//二叉树节点队列
typedef struct queue{
Node *data[MAXT];
int front;
int rear;
}queue;

//创建二叉树
Node *Creat()
{Node *root;
Node *CurrentNode;
CurrentNode=(Node *)malloc(LEN);

scanf("%s",&CurrentNode->Name);
if(strcmp(CurrentNode->Name,"#")==0)
{return NULL;}
else
{
root=CurrentNode;
root->Lchild=Creat();
root->Rchild=Creat();
return root;
}
}

//查找该节点的孩子
void Print(Node *root,char a[])
{ Dop++;
if(root!=NULL)
{
if(strcmp(root->Name,a)==0)
{
if(root->Lchild==NULL&&root->Rchild==NULL)
{printf("%s的孩子:\n",root->Name);
printf("# ");
printf("#\n");}
else if(root->Rchild==NULL)
{printf("%s的孩子:\n",root->Name);
printf("%s ",root->Lchild->Name);
printf("#\n"); }
else if(root->Lchild==NULL)
{printf("%s的孩子:\n",root->Name);
printf("# ");
printf("%s\n",root->Rchild->Name);}
else
{printf("%s的孩子:\n",root->Name);
printf("%s ",root->Lchild->Name);
printf("%s\n",root->Rchild->Name);
}
Pop++;
}
else
{
Print(root->Lchild,a);//递归调用
Print(root->Rchild,a);
Dop--;
}
}
}

//家谱的层次输出
void Level(Node *root,int num)
{
int comp=2;
int i=1;
queue q;//队列
q.data[0]=NULL;
q.data[1]=root;
q.front=1;q.rear=2;
printf("家谱的层次结构:\n");
while(i<=num)
{
if(q.data[q.front])
{
printf("%s ",q.data[q.front]->Name);//数据出队
if((q.front+1)==pow(2,(i)))
{printf("\n");
i++;}
q.data[q.rear++]=q.data[q.front]->Lchild;//数据入队
q.data[q.rear++]=q.data[q.front]->Rchild;
comp=comp+2;
q.front++;
Fop=Fop+1;
}
else
{
printf("# ");
if((q.front+1)==pow(2,(i)))
{printf("\n");
i++;}
q.data[q.rear++]=NULL;
q.data[q.rear++]=NULL;
comp+=2;
q.front++;
}
}
printf("\n");
}

//查找该节点的双亲
void SearchP(Node *root,char a[])
{int i=0;
if(root!=NULL)
{
if(!(root->Lchild==NULL&&root->Rchild==NULL))
{
if(root->Lchild!=NULL){
if(strcmp(root->Lchild->Name,a)==0)
{
printf("%s的双亲是:\n%s\n",a,root->Name);
Gop++;
i++;
}}
if(root->Rchild!=NULL){
if(strcmp(root->Rchild->Name,a)==0)
{
printf("%s的双亲是:\n%s\n",a,root->Name);
Gop++;
i++;
}}
if(i==0)
{
SearchP(root->Lchild,a);
SearchP(root->Rchild,a);
}
}

}
}
//家族代数的确定
int Height(Node *root)
{
int u,v;
if(root==NULL) return 0;
u=Height(root->Lchild); //递归调用
v=Height(root->Rchild);
if(u>v) return (u+1) ;
return (v+1) ;
}

//主函数
int main()
{Node *P;
int i,k;
char Cod;
char a[MAXO];
P=(Node *)malloc(LEN);
printf("输入成员名字(先序输入,输入‘#’表示无孩子)\n");
P=Creat();
k=Height(P);
printf("共有%d代\n",k);
Level(P,k);
printf("共有%d个人\n",Fop);
printf("输入要查找的名字:\n");
scanf("%s",a);
//if(strcmp(P->Name,a)==0)
//printf("无父节点\n");
//else
SearchP(P,a);
if(Gop==0)
printf("父节点No fined\n");
Print(P,a);
if(Pop==0)
printf("孩子 No Fined\n");
if(Dop<=k)
printf("是第%d代\n",Dop);
printf("输入任意字符结束:\n");
scanf("%d",&i);
}

二叉树创建家谱
1. 问题描述:
<1>.用二叉树表示家谱;
<2>.能输入家谱;
<3>.以层次结构输出家谱;
<4>.查找任一节点的双亲和孩子;
2. 设计:
程序将包含以下模块:
<1>.以递归方式创建家谱(二叉树链表存储);
<2>.以递归方式遍历二叉树实现节点双亲的查找;
<3>.以队列的方式实现家谱的层次输出;
<4>.以递归方式遍历二叉树实现节点孩子的查找;
<5>.代数确定模块;
<6>.住程序模块;

3. 调试分析:
用递归遍历建立家谱时须有结束条件,即输入‘#’号,如果不加条件程序将无休止循环;应用字符串处理函数时须加stdio.h头文件;节点数组要足够大,不然会产生溢出错误;节点的查找采用递归遍历形式,在判断是否找到时应用了两个全局变量Pop、Gop不便于程序的结构化;家谱的层次输出采用队列形式,需人为的输入家谱代数num,在每层输出的个数上采用一个math.h中的函数pow,其值为double类型,须注意;
4. 用户使用说明:
首先以先序遍历输入数据,#代表此处无节点,再输入需查找的名字.(注意:所有名字均不得加空格可以下划线代替)

这是我以前的作业,挺简单的,适合你,呵呵

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网