管理最多100个联系人的程序。 [英] A program for the management of a maximum of 100 contacts.

查看:64
本文介绍了管理最多100个联系人的程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,这是我的大学练习之一,我有点挣扎。如果你帮我编写代码那就太棒了:



编写一个程序来管理最多100个联系人的联系方式。

程序应允许在结构中存储以下数据:

  typedef   struct  names {
char name [ 20 ]; / * 姓名* /
char surname [ 20 ]; / * 姓氏* /
char number [ 20 ]; / * 电话号码* /
char mobile [ 20 ]; / * 手机号码* /
}名称;





程序应允许用户通过菜单执行新联系人的输入(允许最终重复姓名和姓氏,但应发出警告如果是重复的用户,并要求确认输入),并显示完整列表。



===



更新:我不敢相信我没有把我的尝试放在下面,我很抱歉,我绝对不会把我的职责放在别人肩上:)



无论如何,这是我的代码,我遇到了很多错误,我不知道为什么!

感谢您的考虑:




 #include< stdio.h> 
#include< stdlib.h>
#include< string.h>
#define MAX 100


typedef struct 姓名{
char name [ 20 ]; / * 姓名* /
char surname [ 20 ]; / * 姓氏* /
char number [ 20 ]; / * 电话号码* /
int mobile [ 20 ]; / * 手机号码* /
}姓名;
int main()
{
int i;

名称n [ 20 ];
for (i = 0 ; i< MAX; i ++){
printf ( 输入名称%d:,i);
scanf( %s,n [i] .name);
printf( 输入姓氏%d:,i);
scanf( %s,n [i] .surname);
if ((strcmp(n [i] .name,n [i ++]。name)== 0 )&& strcmp(n [i] .surname,n [i ++] .surname == 0 )){
printf( 警告,还有另一个名字!!!);
退出( 1 );
}
printf( 输入手机%d:,i) ;
scanf( %d,& n [i] .mobile);
}
for (i = 1 ; i< MAX + 1; i ++ ){
printf( %d。%s%s:%d,n [I] .name和N [1] .surname,N [1]。移动);
}

return 0 ;
}

解决方案

这是一个关于如何进行的计划:



(1)让您的IDE和调试器运行。如果没有合适的工具,没有工作的调试器,你就无处可去。



(2)阅读任务说明。例如,我想念菜单。然后,您为移动电话号码选择了另一种数据类型。如果输入了重复的名称,程序应该发出警告,但不能中止!



(3)检查程序逻辑:如果检查是否给定名称已存在,您必须检查已输入的所有名称!看一下索引i你在做什么!当你检查副本时,不要弄乱它。



(4)看一下你正在使用的变量和结构名称:MAX不是数组大小的好名字。这些名称可能被库或其他程序组件使用。 MAX_PERSON_ENTRIES怎么样 - 告诉另一位读者你的意思,甚至不写评论。此外,您的数据结构名称选择不是很好:结构包含有关单个人的数据;为什么这个名字是复数呢?如果它应该被称为名称。更好的是 Person ,因为它不仅包含人的名字,还包含其他一些数据。 (如果您的老师选择了数据结构的命名,我会对课程的质量产生怀疑。)



(5)如果用户想要怎么办?仅输入五个数据集?你迫使他进入20.没有好主意。



(6)在调试器和单步模式下运行你的程序。从来没有想过使用一些你没有在调试器中运行的代码以及每个可能的分支。在这样做的同时,你会注意到你的程序出了什么问题,并在你第一次真正运行之前学会解决大多数问题。 (是的,即使是很多有多年经验的专业人士也坚持这个规则!所以为什么不从一开始就习惯它。)



干杯!

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!


我会这样说:如果我是你的导师,给你那个结构使用,我会肯定测试如何解决缓冲区溢出问题。


Hey guys, this is one of my university exercises which I have a bit struggling with. It'd be great if you help me out with its coding :

Write a program for the management of a book of contacts of maximum 100 contacts.
The program shall allow the storage of following data in a structure:

typedef struct names {
char name[20]; /*Name*/
char surname[20]; /*Surname*/
char number[20]; /*Phone Number*/
char mobile[20]; /*Mobile Number*/
} Names;



The program shall allow the user to perform, by means of a menu, the input of a new contact (allowing eventual duplicates of name and surname, but shall give a warning to the user in case of duplicates and shall ask to confirm the input), and displays the complete list.

===

Update: I can't believe I didn't put my attempts in its following, I am sorry and I am definitely not putting my duties on somebody else shoulder :)

Anyway, this is my codes which I have been faced with many errors and I don't know exactly why!
Thanks for your considering :


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100


typedef struct names {
char name[20]; /*Name*/
char surname[20]; /*Surname*/
char number[20]; /*Phone Number*/
int mobile[20]; /*Mobile Number*/
}Names;
int main()
{
    int i;

    Names n[20];
    for(i=0;i<MAX;i++){
        printf("Enter the name %d: ",i);
        scanf("%s", n[i].name);
        printf("Enter the surname %d: ",i);
        scanf("%s", n[i].surname);
        if((strcmp(n[i].name, n[i++].name)==0) && strcmp(n[i].surname, n[i++].surname==0)){
        printf("Warning, There is another name the same!!!");
        exit(1);
        }
        printf("Enter the mobile %d: ",i);
        scanf("%d", &n[i].mobile);
    }
    for(i=1;i<MAX+1;i++){
        printf("%d. %s %s: %d",n[i].name,n[i].surname,n[i].mobile);
    }

    return 0;
}

解决方案

Here is a plan for you how to proceed:

(1) Get your IDE and debugger running. Nobody can work without the proper tools and without a working debugger you simply get nowhere.

(2) Read the task description. For example, I miss the menu. Then you chose another data type for the mobile phone number. And the program should give a warning if a duplicate name is entered, but not abort!

(3) Check your program logic: If you are checking whether a given name is already present, you have to check against ALL names already entered! Take a look at what you are doing with index i! Don't mess it up, while you are checking for duplicates.

(4) Take a look at the variable and structure names you are using: MAX is not a good name for the size of your array. Such names are likely to be used by libraries or other program components. How about MAX_PERSON_ENTRIES - that tells another reader what you mean without even writing a comment. Also, the name of your data structure is not chosen very well: The structure contains data about a single person; why is the name in the plural then? If at all it should be called "Name". Better would be Person, as it not only contains the name of a person, but some other data also. (If the naming of the data structure was chosen by your teacher, I would become suspicious about the quality of the course).

(5) What if the user wants to enter five data sets only? You are forcing him to enter 20. No good idea.

(6) Run your program in the debugger and in single-step mode. Never ever even think about using a piece of code that you have not run in the debugger and through every possible branch. While doing that you will notice what is going wrong in your program and learn to fix most problems before you even do the first real run. (And yes, even many professionals with many years experience stick to this rule! So why not getting used to it from the very beginning.)

Cheers!


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


I will say this: If I were your instructor, having given you that structure to use, I would definitely test for how you address buffer overflow(s).


这篇关于管理最多100个联系人的程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆