c语言选择界面循环控制方法小结_c语言循环控制语句
c语言选择界面循环控制方法小结由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言循环控制语句”。
一、利用无限可控循环
在一般有实用性的c语言程序中都用到字幕选择交互屏幕来使得程序更友好,人机互动性更强。
而符合有效得控制字母选择屏幕的循环输出则显得尤为重要。而利用无限可控循环则是一种高效的方法之一。其基本思路是。在生成字母屏幕的函数中创建一个无限循环,在循环体内利用switch,case语句或者if,else语句来控制选择结果的动态处理。如果要要用户更加灵活的使用是否来决定程序的继续运行与否,则可在循环体内在套一层循环来实现。当内层循环break到外部循环的时候程序继续正常运行。当内部循环改变了外部循环的条件并且满足终止条件时外部的无限可控循环结束。程序正常运行结束。请看以下代码实例:
int menu_select()/*菜单函数*/ {
char c;
do{
system(“cls”);/*运行前清屏*/
printf(“tt**** 商品系统****n”);/*菜单选择*/
printf(“tt | 1.输入数据 |n”);
printf(“tt | 2.显示数据 |n”);
printf(“tt | 3.插入数据 |n”);
printf(“tt | 4.删除数据 |n”);
printf(“tt | 5.查找数据 |n”);
printf(“tt | 6.统计并显示进货时间最早且最接近保质期中止时间的货物 |n”);
printf(“tt | 7.写入文件 |n”);
printf(“tt | 8.读文件 |n”);
printf(“tt | 9.对读入的文件进行排序 |n”);
printf(“tt | 0.退出 |n”);
printf(“tt*****************************************n”);
printf(“ttt请选择(0-9):”);
c=getchar();/*读入选择*/
}while(c'9');return(c-'0');/*返回选择*/ } //到此以上是定义的产生字幕屏幕的函数。。
int main()/*主函数*/ {
int n=0;
for(;;)
{
switch(menu_select())/*选择判断*/
{
case 1:
printf(“ttt输入最初商品n”);/*输入若干条记录*/
n=Input(gs,n);
break;
case 2:
printf(“ttt显示所有商品n”);/*显示所有记录*/
Display(gs,n);
break;
case 3:
printf(“ttt插入n”);
n=Insert_a_record(gs,n);/*插入*/
printf(“ttt”);
system(“pause”);
break;
case 4:
printf(“ttt删除n”);
n=Delete_a_record(gs,n);/*按商品名称查找,删除*/
printf(“ttt”);
system(“pause”);
break;
case 5:
printf(“ttt查找并显示n”);
Query_a_record(gs,n);/*查找并显示*/
printf(“ttt”);
system(“pause”);
break;
case 6:
printf(“ttt统计并显示进货时间最早且最接近保质期中止时间的货物n”);
printf(“ttt共有 %d 件商品.n”,n);/*总共记录数*/
printf(“ttt进货时间最早且最接近保质期中止时间的货物:n”);
printf(“ttt商品名称r:%sn”,gs[0].name);
printf(“ttt进货时间:%dn”,gs[0].time);
printf(“ttt生产日期:%dn”,gs[0].year);
printf(“ttt保质期:%dnn”,gs[0].no);
printf(“ttt”);
system(“pause”);
break;
case 7:
printf(“ttt写到文件中n”);
WritetoText(gs,n);/*循环写入数据*/
printf(“ttt”);
system(“pause”);
break;
case 8:
printf(“ttt文件中读数据n”);
n=AddfromText(gs,n);
printf(“ttt”);/*文件中读数据*/
break;
case 9:
printf(“tttn”);
Sort_by_time(gs,n);
printf(“ttt”);/*对读入的文件进行排序*/
break;
case 0:
printf(“ttt结束退出!n”);/*结束程序*/
printf(“ttt”);
system(“pause”);
exit(0);
}
} } //在主函数中,主函数接受来自定义字幕屏幕函数的参数,通过对参数的动态控制来达到控制整个字幕屏幕的结果的效果。
二、函数调用
除了以上的利用可控无限循环来控制整个字幕的循环输出外,还可以利用函数调用的方法来达到相同的效果。当然这个思想的难度较上一个方法小一些,不过这种方法在我个人认为比较易理解,高效,简便。用起来比较方便和灵活。思想如下:
将程序中在不同的地方并且会多处用到的函数功能相同或相近的函数(功能相近的意思是可以利用变化的参数来控制这种差别)抽象出来用一个由多个参数控制的函数来统一表达。定义好该函数之后,只需要在需要用的地方传参调用的即可。需要注意的是,在此方法中需要对方法进行提前声明。否则可能会出现异常。请看以下代码实例: #include #include #include #include #include
//declare all the function that will be defined later void save(char *name, int n);int readout(char *name, char *type);void insert(char *name);void deletedata(char *name);void rankout(char *name);void deletefile(char *name);void fileinitialize(char *name);void update(char *name);void cleanscreen();void makechoice(char *name);int main()。。//以上是程序开头的函数声明
。。
int readout(char *name, char *type){
int i;
FILE *fp;
if((fp = fopen(name, type))== NULL){
printf(“cannot open filen”);
exit(0);
}
printf(“n=========================================n”);
printf(“nametidtchinesetmathtenglish *n”);
for(i = 0;fread(&info[i], sizeof(struct student_info), 1, fp)!= 0;i++){
printf(“%st%dt%dt%dt%dt*n”, info[i].name, info[i].num,info[i].Chinese, info[i].math, info[i].english);
}
printf(“=========================================n”);
printf(“Total %d record(s)nn”, i);
fclose(fp);
return i;} //以上便是对需要多次调用的函数进行的定义。。
void insert(char *name){
//defien the insert()function to insert the record
//to the file you've appointed from the end of the file
FILE *fp;
int i, n;
p = “r”;
strcpy(type, p);
readout(filename, type);
printf(“Enter the number of record you want to insertnnumber=”);
scanf(“%d”, &n);
for(i = 0;i
printf(“>NO_%d ”, i + 1);
scanf(“%s%d%d%d%d”, info[i].name, &info[i].num, &info[i].Chinese,&info[i].math, &info[i].english);
}
if((fp = fopen(name, “a+”))== NULL){//try open the file and ready to write
printf(“cannot open filen”);
exit(0);
}
for(i = 0;i
if(fwrite(&info[i], sizeof(struct student_info), 1, fp)!= 1)
printf(“file write error”);
}
fclose(fp);
printf(“after insert the new data,data of the file like following:n”);
p = “r”;
strcpy(type, p);
readout(filename, type);
makechoice(filename);} //在以上函数的红色部分便是对函数的传参调用
我接触c语言时间不长以上文间有什么错误的地方还请各位大虾海涵 QQ:670161519