0

我尝试并在互联网上到处寻找,我似乎找不到太多。
现在,不用多说,我的问题是:我必须为大学制作一个项目,其中包括使用 C++ 中的 OOP 编程逻辑制作“电子电话簿”。我收到的材料非常模糊,所以我必须以某种方式自己做。

这是我到目前为止所做的代码(在互联网的帮助下):

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include <fstream.h>

void op1()
{
    printf("\n Add contact");
    getch();
}
void op2()
{
    printf("\n Delete contact");
    getch();
}
void op3()
{
    printf("\n Edit a contact");
    getch();
}
void op4()
{
    printf("\n Find a contact");
    getch();
}
void op5()
{
    printf("\n Sort the contacts");
    getch();
}

void op6()
{
    printf("\n 'About the program, details etc. etc.");
    getch();
}



void print_menu()
{
  system("cls");        // clearing window
    printf("\nMenu:");
    printf("\n##############################\n");
  printf("\n1. Add contact");
  printf("\n2. Delete contact");
  printf("\n3. Edit a contact");
  printf("\n4. Find a contact");
  printf("\n5. Sort the contacts");
  printf("\n6. About");
  printf("\n7. Exit");
    printf("\n\n##############################");

  printf("\n\nInput your option: ");
}

// Text centering function - begin
void centerText(char* s)
{
     int l=strlen(s);
     int pos=(int)((80-l)/2);
     for(int i=0;i<pos;i++)
         cout<<" ";
     cout<<s<<endl;
}
// Text centering functon - end

void main()
{

    {   

        printf("\n 'My University' 'My City' ");
        printf("\n Faculty of Electrical Engineer and Computer Science");
        printf("\n Study program used: C++\n\n");
        centerText("C++ Project");
        centerText("<The Phonebook>");
        printf("\n");
        centerText("<my name, Year I of study, Group>");
        printf("\n");
        printf("\n");
        centerText("<june.2013>");
        system("pause>nul"); // Screen is paused until a key is pressed (to allow this text to be viewed)

    }
    // ... Sequence for password verification ...
    {
        char password[20], my_password[20]="2013";
        system("cls");
        printf("WARNING!\n");
        printf("Authentication required!\n");
        printf("\nType input password: ");
        scanf("%19s",password);

            if (strcmp(password, my_password)!=0)
            {

                printf("\n\nIncorrect password !!!\n");
                printf("The program will now exit...\n");
                getch();
                return;
            }

        printf("\n\nPassword is correct !\n");
        printf("The program is executed !\n");
        getch();
    }
  char optiune;

  // ... Sequence for option choosing ...

  do
  {
  print_menu();
  fflush(stdin);
  cin>>optiune;
  switch(optiune)
  {
    case '1':  op1(); break;
    case '2':  op2(); break;
    case '3':  op3(); break;
    case '4':  op4(); break;
    case '5':  op5(); break;
    case '6':  op6(); break;
    case '7':  exit(0);
    default :   printf("\n\nIncorrect option !"); 
                fflush(stdin);
                getch();
  }
  }
  while(1);
}

我的想法是,我可以以某种方式使用此菜单,只需在其中一个 op() 函数中插入重定向到另一个文件,该文件是单独文件中的 1 个函数。所以我会把这个程序作为一个主程序,每个“添加、编辑、删除..etc的功能都在这个程序之外,我会单独处理它们。事情是..我不知道这样做。我查看了“标题”工作系统,但我并没有真正找到任何有价值的东西。也许我不知道看但相信我,我真的试过了。非常感谢任何反馈,但请记住我是非常新手。如果可以,请尽可能详细地解释。我感谢阅读整本书的任何人。我会感谢开头的。

4

2 回答 2

3
  1. 你说你在任何地方都找不到帮助,然后你说“我必须为大学做一个项目”。所以大概你已经有导师和/或教授来帮助你学习?除此之外,任何曾经写过的介绍性 C++ 书籍都将涵盖您所要求的内容,请点击此处。

  2. 您说“在 C++ 中使用 OOP 编程逻辑”,但除了内置的 IO 类之外,您不使用任何 OOP。

  3. 正确缩进你的代码。

  4. 不要使用这些:

    #include <conio.h>
    
    getch()
    
    system("cls")
    
  5. 您正在混合调用printf()withstd::cout和调用scanf()with std::cin- 选择其中一个,并且永远不要使用scanf().

  6. 如果您使用的是 C++,则使用std::string比这更好:

    void centerText(char* s)
    
  7. 这里的演员表是不必要的,当你被分配到一个int它会自动转换:

    int pos=(int)((80-l)/2);
    
  8. main()返回int,不要这样做:

    void main()
    
  9. 你不能fflush(stdin),输入流上没有定义刷新。

  10. 不要像这样把东西放在一行上,因为它看起来很糟糕:

    case '1':  op1(); break;
    
  11. return 0exit(0)正常情况下要好,因为exit(0)不会破坏你的对象。

于 2013-08-08T16:28:02.930 回答
0

在许多应用程序中,构建解决方案需要三个主要组件。有视图,这是您与用户交互的方式。到目前为止,您的工作看起来好像您正在为此构建一个相当明智的命令行界面。然后是存储联系人的后端。这可以是文件、数据库或某种 JSON 或 XML 结构。介于两者之间的是控制器,它使视图和数据库保持同步并执行来自用户的命令和查询。也许这对您的课程作业来说太过分了,如果您在终止程序时忘记了所有内容,您会很高兴。在这种情况下,您需要一个基于内存的结构来保存数据。我建议您创建一个类来保存联系人中的所有字段并存储在 std::vector<Contact> 中。

于 2013-08-08T16:23:46.220 回答