knocker 版主:
你既然帮我调通了,就好人做到底,给上面的问题一个个"引用"回答一下好吗?
非常感谢!
帮你调通了,程序我没有看,逻辑问题自己改。
1。自定义函数不能用与库函数重名的
2。大小写对C来说是两个不同的名
3。库函数名写错
4。是不是还有4我也忘记了
#include<stdio.h> #include<stdlib.h> #include<string.h> #define M 20 #define L 100 struct aa { char name[M]; char word[M]; } user[L]; int n=0 ;
int jia() { int a,b ; randomize(); a=random(100); randomize(); b=random(100); printf("%d+%d=",a,b); return a+b ; }
int jian() { int a,b ; while(2) { randomize(); a=random(100); randomize(); b=random(100); if(a>=b)break ; } printf("%d-%d=",a,b); return a-b ; }
int chen() { int a,b ; randomize(); a=random(9)+1 ; randomize(); b=random(9)+1 ; printf("%d*%d=",a,b); return a*b ; }
int chu() { int a,b,c ; while(3) { randomize(); a=random(82); randomize(); b=random(9)+1 ; if(a>=b)break ; } c=a%b ; a=a-c ; printf("%d/%d=",a,b); return a/b ; }
void math() { int a,b,n,i ; float s=0,k ; printf("How many practise do you want to do?\n"); scanf("%d",&n); k=100.0/n ; printf("OK,every practise %.2f'\n\n",k); for(i=1;i<=n;i++) { printf("practise %2d: ",i); randomize(); a=random(4); switch(a) { case 0 :b=jia();break ; case 1 :b=jian();break ; case 2 :b=chen();break ; default :b=chu(); } scanf("%d",&a); if(a==b) { printf("OK,very good!\t +%.2f'\n\n",k); s=s+k ; } else printf("Sorry,it's wrong!\n\n"); } printf("\n\nThe totall=%.2f'",s); getch(); }
void getpassword(char p[]) { unsigned i=0,j ; while((j=getch())!=13&&i<M-1) { p[i++]=j ; printf("*"); } p[i]='\0' ; printf("\n"); return ; }
void list() { int i,j ; clrscr(); printf("\n\n"); for(i=1;i<10;i++) { for(j=1;j<=i;j++) printf("%d*%d=%d\t",j,i,j*i); printf("\n"); } printf("\n\nPress any key to return.\n"); getch(); return ; }
int save() { FILE*fp ; int i ; if((fp=fopen("n_file","wb"))==NULL) { printf("Can not open file.\n"); return 0 ; } if(fwrite(&n,sizeof(int),1,fp)!=1) { printf("Write error!\n"); return 0 ; } fclose(fp); if((fp=fopen("u_file","wb"))==NULL) { printf("Can not open file.\n"); return 0 ; } for(i=0;i<n;i++) if(fwrite(&user[i],sizeof(struct aa),1,fp)!=1) { printf("User write error!\n"); return 0 ; } fclose(fp); printf("Save successfull!\n"); getch(); return 1 ; }
int load() { FILE*fp ; int i ; if((fp=fopen("n_file","rb"))==NULL) { printf("Can not open 'n_file'.\n"); return 0 ; } if(fread(&n,sizeof(int),1,fp)!=1) { printf("'n_file read error!\n"); return 0 ; } fclose(fp); if((fp=fopen("u_file","rb"))==NULL) { printf("Can not open 'u_file'.\n"); return 0 ; } for(i=0;i<n;i++) if(fread(&user[i],sizeof(struct aa),1,fp)!=1) { printf("'u_file' read error!\n"); return 0 ; } fclose(fp); return 1 ; }
int quit() { char c ; if(save()!=1) { printf("The data not save.\n"); printf("Do you want to quite(y/n)?"); c=getchar(); if(c=='y'||c=='Y')return 0 ; else return 1 ; } else return 0 ; }
int textn(char*p) { int i ; for(i=0;i<n;i++) if(strcmp(p,user[i].name)==0)break ; if(i>=n)i=-1 ; return i ; }
int textw(char*p,int i) { return strcmp(p,user[i].word)==0?1:0 ; }
int text(char*p,char*q) { int i,j,k=0 ; i=textn(p); if(i>=0) { j=textw(q,i); if(j==1)k=1 ; else printf("The password error!\n"); } else printf("The user name is not zhuce.\n"); return k ; }
void zhuce() { char a[M],b[2][M]; int i ; clrscr(); printf("\tCreate A New User\n\n"); if(n>=M) { printf("The user is full!\n"); printf("Press any key to return.\n"); getch(); return ; } printf("Input username:"); gets(a);为什么这里要改成:scanf("%s",a)才能输入字符串 printf("Input password:"); getpassword(b[0]); printf("Input password:"); getpassword(b[1]); i=textn(a); if(i>=0)printf("The user name had be used!\n"); else if(strcmp(b[0],b[1])==0) { strcpy(user[n].name,a); strcpy(user[n].word,b[0]); n++; printf("zhuce sucessfull!\n"); } else printf("The two password is not same.\n"); printf("\n\n\nPress any key to return.\n"); getch(); return ; }
void other1() { int i,flag=1 ; clrscr(); while(2) { while(3) { printf("\t\tThe Other\n\n"); printf("1.A usefull list.\n"); printf("2.Some math practice.\n"); printf("3.Exit.\n"); printf("\n\n\nPlease choose(1~3):"); scanf("%d",&i); if(i<1||i>3) { printf("choose error!\n"); printf("Press any key to continue.\n"); getch(); } else break ; } switch(i) { case 1 : list(); break ; case 2 : math(); break ; default : flag=0 ; } if(flag==0)break ; } printf("\n\nPress any to return.\n"); getch(); return ; }
void sum(int*p,int k) { int i,s=0 ; for(i=0;i<k;i++) s+=p[i]; printf("The sum=%d .\n",s); return ; }
void sort(int*p,int k) { int t,i,j ; for(i=0;i<k-1;i++) for(j=i+1;i<k;j++) if(p[i]>p[j]) { t=p[i]; p[i]=p[j]; p[j]=t ; } printf("After sort:\n"); for(i=0;i<k;i++) printf("%6d",p[i]); printf("\n"); return ; }
void min1(int*p,int k) { int i,t=p[0];
for(i=1;i<k;i++) if(t>p[i])t=p[i]; printf("The min=%d .\n",t); return ; }
void max1(int*p,int k) { int i,t=p[0]; for(i=1;i<k;i++) if(t<p[i])t=p[i]; printf("The max=%d .\n",t); return ; }
void everage(int*p,int k1) { int i ; float k=0 ; for(i=0;i<k1;i++) k+=p[i]; k=k/n ; printf("The everage=%.2f .\n",k); return ; }
void process() { int a[100],b,i,k,flag=1 ; while(2) { printf("\n\nHow many numbers do you want to process?\n"); scanf("%d",&b); if(b<=0||b>100)printf("Input error!\n"); else break ; } printf("Input the numbers:\n"); for(i=0;i<b;i++) { printf("%2d :",i+1); scanf("%d",&a[i]); } printf("\n\nOK,press any key to continue.\n"); getch(); clrscr(); while(3) { clrscr(); while(3) { printf("\tThe Data Process System\n\n"); printf("0.Exit.\n"); printf("1.Sum.\n"); printf("2.Sort.\n"); printf("3.min.\n"); printf("4.max.\n"); printf("5.everage.\n"); printf("\n\n\nPlease choose(0~5):"); scanf("%d",&k); if(k<0||k>5) { printf("Choose error!\n"); printf("Press any key to continue.\n"); getch(); } else break ; } switch(k) { case 0 : flag=0 ; break ; case 1 : sum(a,b); break ; case 2 : sort(a,b); break ; case 3 : min1(a,b); break ; case 4 : max1(a,b); break ; default : everage(a,b); } if(flag==0)break ; printf("\n\nPress any key to continue.\n"); getch(); } printf("\nPress any key to return.\n"); getch(); return ; }
void password() { int i,j ; char a[M],b[M],c[2][M]; clrscr(); printf("\t\tModify Password\n\n"); printf("Input username:"); gets(a);为什么这里要改成:scanf("%s",a)才能输入字符串 printf("Input password:"); getpassword(b); printf("New password:"); getpassword(c[0]); printf("New password:"); getpassword(c[1]); i=text(a,b); if(i) { if(strcmp(c[0],c[1])==0) { j=textn(a); strcpy(user[j].word,c[0]); printf("\nOK,modify password success!\n"); } else printf("The two password is not same.\n"); } printf("\n\nPress any key to return.\n"); getch(); return ; }
void denlu() { char a[M],b[M]; int i,k,flag=1 ; clrscr(); printf("\t\tDen Lu\n\n"); printf("Input username:"); gets(a);为什么这里要改成:scanf("%s",a)才能输入字符串 printf("Input password:"); getpassword(b); i=text(a,b); if(i) { printf("Load successfull!\n"); printf("\n\nPress any key to continue.\n"); getch(); clrscr(); } while(i) { while(2) { printf("%s : Welcome to use this program.\n\n\n"); printf("1.The data process system.\n"); printf("2.Others.\n"); printf("3.Exit.\n"); printf("\n\nPlease choose(1~3):"); scanf("%d",&k); if(k<1||k>3) { printf("Choose error!\n"); printf("Press any key to continue.\n"); getch(); } else break ; } switch(k) { case 1 : process(); break ; case 2 : other1(); break ; default : flag=0 ; } if(flag==0)break ; } printf("Press any key to return.\n"); getch(); return ; }
void main() { int i,flag=1 ; load(); while(1) { clrscr(); while(2) { printf("\t\tA Small Program\n\n"); printf("1.Create a new user.\n"); printf("2.Denlu.\n"); printf("3.Modify password.\n"); printf("4.Close this program.\n"); printf("\n\nPlease choose(1~4):"); scanf("%d",&i); if(i>4||i<1) { printf("Choose error!\n"); printf("Press any key to continue.\n"); getch(); } else break ; } switch(i) { case 1 : zhuce(); break ; case 2 : denlu(); break ; case 3 : password(); break ; default : flag=quit(); } if(flag==0)break ; } printf("Thanks for your using!\n"); printf("\nPress any key to close.\n"); getch(); printf("You can close this window.\n"); }
望高手解答!
还没搞懂?论坛上以前曾讨论过相似的问题。缓冲区残余信息造成的。
给你一例子:
int main() {
char c[80];
printf("1. gets():"); gets(c); printf(" c = %s\n",c);
printf("1. gets():"); gets(c); printf(" c = %s\n",c);
printf("2. scanf):"); scanf("%s",c); printf(" c = %s\n",c);
printf("2. gets():"); gets(c); printf(" c = %s\n",c);
}
考虑一下最后一个gets为什么不正常?
大哥,你终于出现了,非常感谢你的帮忙,再帮我看看这个函数:
void sort(int*p,int k) { int t,i,j ; for(i=0;i<k-1;i++) for(j=i+1;i<k;j++) if(p[i]>p[j]) { t=p[i]; p[i]=p[j]; p[j]=t ; } printf("After sort:\n"); for(i=0;i<k;i++) printf("%6d",p[i]); printf("\n"); return ; }
有问题吗,怎么无法排序,里面好象没有死循环啊,
怎么执行到这不不动了?
[此贴子已经被作者于2004-09-26 22:38:26编辑过]