Do title records --day32

"Algorithm Notes" Section 3.2 - Getting Started Analog -> Search element

C title repeated pit, which can not directly pay attention to the struct string, (but returns to normal output Process returned -1073741819 (0xC0000005)

The use of an array of characters, but not directly compare the size of the array, write their own same function, (equal sign will always direct return 0 .. Curiously! This formula will return 0 .. confused)

There is sure to pat attention to the inside, the title did not say a set of data, it is processed in more than read

id To use string instead of int, not that he fixed up a few positions, carefully read the title, pay attention to routine

#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
bool same(char s1[],char s2[])
{
   // printf("hah%d %d\n",strlen(s1),strlen(s2));
    if(strlen(s1)!=strlen(s2))
        return false;
    for(int i=0;i<strlen(s1);i++)
    {
        if(s1[i]!=s2[i])
            return false;
        //printf("this i%d\n",i);
    }
    return true;
}
struct student
{
    char id[256];
    char name[256];
    char gender[256];
    int secnum;
}stu[1005];
int main()
{
    int n,m;
    char searchnum[256];
    int q;
    while(scanf("%d",&n)!=EOF)
    {
    for(int i=0;i<n;i++)
    {
        scanf("%s",stu[i].id);
        getchar();
        scanf("%s",stu[i].name);
        getchar();
        scanf("%s",stu[i].gender);
        scanf("%d",&stu[i].secnum);
    }
    scanf("%d",&m);
    getchar();
    while(m--)
    {
        scanf("%s",&searchnum);
        //printf("%s\n",searchnum);
        getchar();
        int temp=0;
        //printf("%d\n",same(stu[temp].id,searchnum));
        while(temp<n && !same(stu[temp].id,searchnum))
       //printf("%d\n",!stu[temp].id==searchnum);
       // while(temp<n && !stu[temp].id==searchnum)
            temp++;
        if(temp==n)
            printf("No Answer!\n");
        else
        {
            printf("%s ",stu[temp].id);
            printf("%s ",stu[temp].name);
            printf("%s ",stu[temp].gender);
            printf("%d\n",stu[temp].secnum);
        }
        }
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/tingxilin/p/11285989.html