
正文
c语言如何自动编号函数 c语言编写一个自动选号程序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
C语言如何一次声明a1 到 a10?
在计算机或者现实中,我们每做一件事情都有做这件事情的目的,不是盲目的去干。
这里一次声明a1到a10的目的何在,在什么地方使用,还是就是简单的看语法,如果简单的看语法就完全没有必要了,如果有作用最好定义一个结构体,这样这些变量的地址空间在一起便于程序运行。或者定义一个数组存储这些变量的值。
相关问答
Q1: 如何编写C语言程序使得输入编号就可以显示其他的个人信息
1、写一个结构体数组用来记录信息
这里我写了一个可以存储一个人的姓名、电话、邮箱的结构体。
struct note{
char name[100];
char phone[100];
char mail[100];
}people[1000];
2、用文件储存更加方便
p=fopen("list.txt","r");
if(p==NULL)
{
fclose(p);
p=fopen("list.txt","w");
fclose(p);
}
3、写一个简单的界面(可以用死循环)
while(1)
{
n=0;
p=fopen("list.txt","r");
while(fscanf(p,"%s%s%s",people[n].name,people[n].phone,people[n].mail)!=EOF)
n++;
fclose(p);
///--------一次循环更新一次数据
4、写一个简单的查找程序
int k;
cout"输入1读取,输入2输入"endl;
cink;
if(k==1)
{
cout"输入信息"endl;
char s[100];
cins;
bool ok=0;
for(i=0;istrlen(s);i++)
if(s[i]='0's[i]='9')
ok=1;
//自动识别输入的是姓名还是电话号码
if(ok==0)
{
//cout"通过姓名找到联系人"endl;
//system("pause");
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].name)==0)
{
you=1;
cout"姓名"people[i].nameendl;
cout"电话号码"people[i].phoneendl;
cout"邮箱"people[i].mailendl;
}
if(you==0)
cout"没有通过姓名找到联系人"endl;
}
if(ok==1)
{
//cout"通过电话找联系人"endl;
//system("pause");
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].phone)==0)
{
you=1;
cout"姓名"people[i].nameendl;
cout"电话号码"people[i].phoneendl;
cout"邮箱"people[i].mailendl;
}
if(you==0)
cout"没有通过电话找到联系人"endl;
}
5、添加信息的代码
if(k==2)
{
p1=fopen("list.txt","a+");
char ss[1000];
cout"请输入姓名"endl;
cinss;
fprintf(p1,"%s\n",ss);
cout"请输入电话"endl;
cinss;
fprintf(p1,"%s\n",ss);
cout"请输入邮箱"endl;
cinss;
fprintf(p1,"%s\n",ss);
fclose(p1);
}
}
最终的程序
#include cstdio
#include cmath
#include cstring
#include algorithm
#include iostream
#include cstring
#include cmath
using namespace std;
FILE *p,*p1;
struct note{
char name[100];
char phone[100];
char mail[100];
}people[1000];
int main()
{
int n=0,i,j;
p=fopen("list.txt","r");
if(p==NULL)
{
fclose(p);
p=fopen("list.txt","w");
fclose(p);
}
while(1)
{
n=0;
p=fopen("list.txt","r");
while(fscanf(p,"%s%s%s",people[n].name,people[n].phone,people[n].mail)!=EOF)
n++;
fclose(p);
///---------------
int k;
cout"输入1读取,输入2输入"endl;
cink;
if(k==1)
{
cout"输入信息"endl;
char s[100];
cins;
bool ok=0;
for(i=0;istrlen(s);i++)
if(s[i]='0's[i]='9')
ok=1;
if(ok==0)
{
//cout"通过姓名找到联系人"endl;
//system("pause");
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].name)==0)
{
you=1;
cout"姓名"people[i].nameendl;
cout"电话号码"people[i].phoneendl;
cout"邮箱"people[i].mailendl;
}
if(you==0)
cout"没有通过姓名找到联系人"endl;
}
if(ok==1)
{
//cout"通过电话找联系人"endl;
//system("pause");
bool you=0;
for(i=0;in;i++)
if(strcmp(s,people[i].phone)==0)
{
you=1;
cout"姓名"people[i].nameendl;
cout"电话号码"people[i].phoneendl;
cout"邮箱"people[i].mailendl;
}
if(you==0)
cout"没有通过电话找到联系人"endl;
}
}
if(k==2)
{
p1=fopen("list.txt","a+");
char ss[1000];
cout"请输入姓名"endl;
cinss;
fprintf(p1,"%s\n",ss);
cout"请输入电话"endl;
cinss;
fprintf(p1,"%s\n",ss);
cout"请输入邮箱"endl;
cinss;
fprintf(p1,"%s\n",ss);
fclose(p1);
}
}
return 0;
}
Q2: C语言中字符串怎样转换为asc编号
字符串要转为ascii码值c语言如何自动编号函数,需要逐个字符转换为ASCII码。
在C语言中c语言如何自动编号函数,要输出字符的ASCII码值,只需要用%d的格式,用printf函数输出即可。因为%d的格式下,会将字符型变量转为整型,值就是ASCII码值。
编写函数如下:
void print_asc(const char *s)
{
while(*s) printf("%d ",*s++);
}
Q3: 在C语言中如何指定字符串编码方式
计算机用两个字节来表示一个汉字c语言如何自动编号函数,“我”在内存里就是这样存放c语言如何自动编号函数的:CE D2。CE是str[0]的内容c语言如何自动编号函数,D2是str[1]的内容。第一次循环输出str[0],但是这个字符在ASCII字符集里代表这样一个东西Î(不知道在这儿能不能正常显示……),但是Windows的命令提示符程序读取到这里就会自动使用宽字符集,也就是说,它已经准备好读取下一个字符,然后把他们当成一个字符显示出来。于是就出现c语言如何自动编号函数了那个汉字。 #include stdio.h int main() { printf("%c%c",(char)0xce,(char)0xd2); }
Q4: c语言编写一个通讯录系统 用简单一点的c😊
#define N 100
#define SIZE 30
#include stdio.h
#include string.h
#include conio.h
struct student{
char num[SIZE];
char name[SIZE];
char tel[SIZE];
};
/*函数声明*/
void myprint();/*显示菜单*/
int mycreat(struct student*p,int n);/*输入通讯录*/void mydisplay(struct student*p,int n);/*显示通讯录*/void mysearch(struct student*p,int n);/*查找*/
void mymodify(struct student*p,int n);/*修改通讯录*/int myadd(struct student*p,int n);/*增加通讯录*/int mydelete(struct student*p,int n);/*删除*/void mysort(struct student*p,int n);/*排序*/
void sch_num(struct student*p,int n);/*按学号查找*/void sch_name(struct student*p,int n);/*按姓名查找*/
int loadinfo(struct student*p,int n);/*载入通讯录信息*/int saveinfo(struct student*p,int n);/*保存通讯录信息*/void main(){
/*文件定义*/char choose,yes_no;
struct studentrecord[N];/*存放通讯录信息,共100条*/int total=0,flag;/*通讯录总数*/
total=loadinfo(record,N);/*从文件读取记录信息,不超过100条*/do{
myprint();/*显示菜单*/
printf("请选择:");do{
choose=getchar();}
while(choose'0'||choose'9');switch(choose){
case'1':
total=mycreat(record,total);break;case'2':
mydisplay(record,total);break;case'3':
mysearch(record,total);break;case'4':
mymodify(record,total);break;case'5':
total=myadd(record,total);break;case'6':
total=mydelete(record,total);break;case'7':
mysort(record,total);break;case'8':
flag=saveinfo(record,total);if(flag==1)
printf("\n保存成功!\n");else
printf("\n保存失败!\n");break;case'0':
break;default:
printf("\n%c为非法选项!\n",choose);}
if(choose=='0')break;printf("\n要继续选择吗(Y/N)?");do{
yes_no=getchar();}
while(yes_no!='Y'yes_no!='y'yes_no!='N'yes_no!='n');}
while(yes_no!='Y'||yes_no!='y');}
void myprint()/*显示菜单信息*/{
printf("\n\n\n\n\n");printf("\n");printf("请输入选项编号(0--7)\n");printf("1---批量输入通讯录\n");printf("2---显示通讯录\n");printf("3---查询通讯录\n");printf("4---修改通讯录\n");printf("5---添加通讯录\n");printf("6---删除通讯录\n");printf("7---排序通讯录\n");printf("8---保存通讯录\n");printf("0---退出\n");printf("\n");
}
int mycreat(struct student*p,int n)/*批量输入学生信息*/{
int i=0;int m;
printf("添加前的记录c语言如何自动编号函数,共有%d个:\n",n);printf("\n记录号学号姓名电话号码\n");
i=0;
while((in)(strcmp(p-num,"")!=0)){
printf("%d:%15s%15s%15s\n",i+1,p-num,p-name,p-tel);i++;p++;}
printf("\n请输入要输入的学生人数(%d):",N-n);
scanf("%d",m);i=0;
while(im){
printf("\n");
printf("请输入第%d个记录:\n",i+1);printf("学号:");do{
gets(p-num);}
while(strcmp(p-num,"")==0);printf("姓名:");gets(p-name);
printf("电话号码:");gets(p-tel);p++;i++;}
return m+n;}
void mydisplay(struct student*p,int n)/*显示学生信息*/{
/*clrscr();*/int i=0;
printf("\n记录号学号姓名电话号码\n");while(in){
printf("%d:%15s%15s%15s\n",i+1,p-num,p-name,p-tel);p++;i++;}}
void mymodify(struct student*p,int n)/*修改学生信息*/{
int i;
char nam[10];mydisplay(p,n);
printf("\n请输入要修改记录的姓名:");gets(nam);
for(i=0;in;i++,p++)
if(strcmp(nam,p-name)==0)break;if(i==n)
printf("无此人!\n");else{
printf("请输入正确的学号:");do{
gets(p-num);}
while(strcmp(p-num,"")==0);printf("请输入正确的姓名:");gets(p-name);
printf("请输入正确的电话号码:");gets(p-tel);}
}
void mysearch(struct student*p,int n)/*查找指定学生*/{
char c;
mydisplay(p,n);
printf("\n按学号查询(h),还是按姓名查询(m)?");c=getchar();
if(c=='h'||c=='H')
sch_num(p,n);else if(c=='m'||c=='M')
sch_name(p,n);else
printf("\n非法字符!\n");}
void sch_num(struct student*p,int n)/*按学号查询*/{
int i,flag=0;char nm[10];
printf("\n请输入要查询记录的学号:");gets(nm);
for(i=0;in;i++)
if(strcmp(nm,p[i].num)==0)
{
printf("\n学号姓名电话号码\n");printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);flag=1;
}
if(flag==0)
printf("无此人!\n");}
void sch_name(struct student*p,int n)/*按姓名查询*/{
int i,flag=0;char nam[10];
printf("\n请输入要查询记录的姓名:");gets(nam);
for(i=0;in;i++)
if(strcmp(nam,p[i].name)==0){
printf("\n学号姓名电话号码\n");printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);flag=1;}
if(flag==0)
printf("无此人!\n");
}
int myadd(struct student*p,int n)/*增加学生信息*/{
int i=0;char c;
struct studentt,*q;
printf("添加前的记录:\n");printf("\n学号姓名电话号码\n");i=0;q=p;
/*显示原有记录信息*/while(in){
printf("%15s%15s%15s\n",q-num,q-name,q-tel);i++;q++;}do{
printf("请输入新记录的学号:\n");do{
gets(t.num);
}
while(strcmp(t.num,"")==0);/*学号不能为空*/printf("请输入新记录的姓名:");gets(t.name);
printf("请输入新记录的电话号码:");gets(t.tel);
for(i=0;in;i++)
if(strcmp(t.num,(p+i)-num)==0||strcmp(t.tel,(p+i)-tel)==0){
printf("学号或电话号码重,要重新输入吗(Y/N)?");c=getchar();putchar ('\n');break;}
/*找到最后,没有重复*/if(i==n){
*(p+i)=t;
printf("添加后的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in+1;i++)
printf("%15s%15s%15s\n",(p+i)-num,(p+i)-name,p[i].tel);break;}
}
while(c=='y'||c=='Y');return n+1;}
int mydelete(struct student*p,int n)/*删除学生信息*/{
int i,x;
char c,nm[10],nam[10];
printf("删除前的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in;i++)
printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);printf("按学号删除(h),还是按姓名删除(m)?");c=getchar();
if(c=='h'||c=='H'){
printf("\n请输入要删除记录的学号:");gets(nm);
for(x=0;xn;x++)
{
gets(t.num);
}
while(strcmp(t.num,"")==0);/*学号不能为空*/printf("请输入新记录的姓名:");gets(t.name);
printf("请输入新记录的电话号码:");gets(t.tel);
for(i=0;in;i++)
if(strcmp(t.num,(p+i)-num)==0||strcmp(t.tel,(p+i)-tel)==0){
printf("学号或电话号码重,要重新输入吗(Y/N)?");c=getchar();putchar ('\n');break;}
/*找到最后,没有重复*/if(i==n){
*(p+i)=t;
printf("添加后的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in+1;i++)
printf("%15s%15s%15s\n",(p+i)-num,(p+i)-name,p[i].tel);break;}
}
while(c=='y'||c=='Y');return n+1;}
int mydelete(struct student*p,int n)/*删除学生信息*/{
int i,x;
char c,nm[10],nam[10];
printf("删除前的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in;i++)
printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);printf("按学号删除(h),还是按姓名删除(m)?");c=getchar();
if(c=='h'||c=='H'){
printf("\n请输入要删除记录的学号:");gets(nm);
for(x=0;xn;x++)
int j,i,k;char c1,c2;
struct studenttemp;
printf("\n排序前的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in;i++)
printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);printf("\n按学号排序(h),还是按姓名排序(m)?");c1=getchar();
if(c1=='h'||c1=='H'){
printf("\n按升序排序(s),还是按降序排序(j)?");c2=getchar();/*升序*/
if(c2=='s'||c2=='S'){
for(i=0;in-1;i++){
k=i;
for(j=i;jn;j++)
if(strcmp(p[k].num,p[j].num)0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;
}
printf("\n排序后的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in;i++)
printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);}
else if(c2=='j'||c2=='J')/*降序*/{
for(i=0;in-1;i++){
k=i;
for(j=0;jn;j++)
if(strcmp(p[k].num,p[j].num)0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;
}
printf("排序后的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in;i++)
printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);}
else printf("\n非法字符\n");}
else if(c1=='m'||c1=='M')/*按姓名排序*/{
printf("\n按升序排序(s),还是按降序排序(j)?");c2=getchar();
if(c2=='s'||c2=='S'){
for(i=0;in-1;i++){
k=i;
for(j=0;jn;j++)
if(strcmp(p[k].name,p[j].name)0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;}
printf("排序后的记录:\n");printf("\n学号姓名电话号码\n");for(i=0;in;i++)
printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);
}
else if(c2=='j'||c2=='J')/*降序*/{
for(i=0;in-1;i++){
k=i;
for(j=i;jn;j++)
if(strcmp(p[k].name,p[j].name)0)
k=j;
temp=p[k];p[k]=p[i];p[i]=temp;
}
printf("排序后的记录:\n");printf("\n学号
姓名电话号码\n");
for(i=0;in;i++)
printf("%15s%15s%15s\n",p[i].num,p[i].name,p[i].tel);}
else printf("\n非法字符c语言如何自动编号函数!\n");}
else printf("\n非法字符!\n");}
int loadinfo(struct student*p,int n)/*载入通讯录信息*/{
FILE*fp;int i,all=0;
char filename[30]="tongxunlu.txt";fp=fopen(filename,"r");if(fp==NULL){
printf("\n***不能打开文件%s,或%s文件不存在,创建一个新文件!
",filename,filename);*/
fp=fopen(filename,"w");}
fscanf(fp,"%d\n",all);
if(allN)/*读取记录总数*/{
printf("文件记录数太多,不能读取");return 0;}
for(i=0;iall;i++)/*读取记录信息*/{
fscanf(fp,"%s%s%s\n",p[i].num,p[i].name,p[i].tel);}
fclose(fp);/*关闭文件*/return all;}
int saveinfo(struct student*p,int n){
FILE*fp;int i,all=0;
char filename[30]="txl.dat";/*文件可以用记事本打开*/fp=fopen(filename,"w");if(fp==NULL){
printf("\n***不能建立或打开文件%s!***",filename);return 0;}
fprintf(fp,"%d\n",n);for(i=0;in;i++)
fprintf(fp,"%s%s%s\n",p[i].num,p[i].name,p[i].tel);fclose(fp);return 1;}
抄来的
关于c语言如何自动编号函数和c语言编写一个自动选号程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。





