您的位置:

c语言程序编号,c语言字母序号编程

c语言程序编号,c语言字母序号编程

更新:

本文目录一览:

C语言给数组中的元素按顺序编号

首先,for(i=0;i10;i++)

for(j=0;j10;j++)

for(i=0;i10;i++)

这三个for循环是打错了?

其次你的比较那儿也有问题,试想,当执行到7的时候,7和1比,count = 1,7再和第二个1比,count等于2,后面两个不变,但是你往b数组中存放的时候,count又加了1,肯定就变成3了,9同理,把算法再改改

C语言编程问题:给数组中的元素按顺序编号

最直接的办法,用一个数组来作hash,时间复杂度最低,但是有最大值的限制,如下:

#includestdio.h

#define MAX_NUM 10000

#define n 10

int main()

{

int b[MAX_NUM] = {0};

int a[n],i,j;

int max = 0;

for(i=0;in;i++)

{

scanf("%d",a[i]);

if(a[i] = MAX_NUM)

{

printf("invalid value [%d]! max than %d\n",a[i], MAX_NUM);

return -1;

}

b[a[i]] = 1;

if(a[i] max)

{

max = a[i];

}

}

int idx = 1;

for(i = 0; i = max; i++)

{

if(b[i] 0)

{

b[i] = idx;

idx++;

}

}

for(i=0;in - 1;i++)

printf("%d,",b[a[i]]);

printf("%d\n", b[a[n - 1]]);

return 0;

}

-----------

修改过的:

#includestdio.h

#define MAX_NUM 10000

#define n 10

int main() {

int b[MAX_NUM] = {0};

int a[n],i,j;

int max = 0;

for(i=0;in;i++) {

scanf("%d",a[i]);

if(a[i] = MAX_NUM)

return -1;

b[a[i]] = 1;

max = a[i] max ? a[i] : max;

}

int idx = 1;

for(i = 0; i = max; i++)

b[i] = b[i] 0 ? idx++ : b[i];

for(i=0;in - 1;i++)

printf("%d,",b[a[i]]);

printf("%d\n", b[a[n - 1]]);

return 0;

}

不过代码长度有这么重要么?代码的可读性和正确性才是第一位的! 如果太纠结于长度,不如把所有代码都写道一行算了。多爽:“看,我的代码一行就搞定了;”

如何编写C语言程序使得输入编号就可以显示其他信息

简单点的,你可以定义两个数组A和B,A存放编号,B存放编号对应的其他信息,写一个函数,返回类型为string,参数为编号,遍历A数组,当找到编号匹配时记下是第i个元素,然后提取B中第i个元素对应的值并且返回。

如何编写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;

    }

c语言 整数编号

这是你原来的方法,我改了两行,

一个是put[j]=str[i],这一行要改成put[j]=str[i]-'0',这是从char 到 int的转换,如果不变就成了把put[j]赋值成字符型str[i]的值了,

另一个在后面那个函数,return have使用之后,后面的代码就不执行了,就好象短路一样,

要放在后面,或是有条件的返回

可是改了这两处之后还是不对,排出来结果差的太多,好象是你的冒泡内部的问题,要重新换种方法试试..我对排序很外行,查不出来根本所在

#include stdio.h

struct nn

{ int no;

int num;

};

typedef struct nn DATA;

int number( char * , DATA []);

int main( )

{

DATA b[100];

char sa[500]; //

int i, n;

gets( sa );

n = number( sa, b );

for ( i=0; in; i++ )

printf("%d %d\n", b[i].num, b[i].no );

return 0;

}

//

void bubbleSort(int arr[],int n)

{

int i,j,t;

for(i=0;in-1;i++)

{

for(j=0;jn-i-1;j++)

{

if(arr[j+1]arr[j])

{

t=arr[j+1];

arr[j+1]=arr[j];

arr[j]=t;

}

}

}

}

int number( char * str, DATA b[] )

{

int put[251],anshunxu[251],bianhao[251];

int i,j=0,have;

// 得数的个数

for(i=0;str[i]!='\0';i++)

{

if(str[i]!=' ')

{

put[j]=str[i]-'0';

j++;

}

}

put[j]='\0';

have=j;

j=0;

//return have;

// 把put复制到anshunxu

for(i=0;ihave;i++)

{

anshunxu[i]=put[i];

}

// 冒泡排序

bubbleSort(anshunxu,have);

// 得到编号的数组

for(i=0;ihave;i++)

{

for(j=0;jhave;j++)

{

if(put[i]==anshunxu[j])

{

bianhao[i]=j+1;

}

}

}

bianhao[i]='\0';

for(i=0;ihave;i++)

{

b[i].num=put[i];

b[i].no=bianhao[i];

}

return have;

}

以上回答你满意么?