|
发表于 2014-4-2 10:16:18
|
显示全部楼层
昨天看了你的代码确实不想看下去了,不过还是坚持着看下去了,最后对你的程序进行了修改,也加了点我写的代码,就是if进行判断哪里,你可以看一下,因为你在没提醒说输入的数字在0-1之间,那如果用户输入大于1或者小于0呢?所以我帮你改了一下,你可以参看我的代码,希望对你有帮助:
#include <iostream>
#include <iomanip> //如果你这个的头文件是 # include <iomanip.h>的话,那整个程序都会报错cout
#include <string>
#include <tchar.h>
using namespace std;
#define MAXSIZE 50
//定义员工结构体
typedef struct worker_a
{
long num;
char name[10];
char sex;
int age;
char title[10];
char position[10];
float wage;
}worker;
class worker_b
{
public:
worker worker_struct[MAXSIZE];
int total;
public:
worker_b();
int insert_seq(int i, worker_a x);
int delete_seq(int i);
void print_seq();
};
//显示菜单
void menu();
void change(worker x,worker y)
{
x.age=y.age;
x.num=y.num;
x.sex=y.sex;
x.wage=y.wage;
strcpy(x.name,y.name); //这里不是strcpy_s,没有这个函数,何况你的程序中也没有这个函数
strcpy(x.position,y.position);
strcpy(x.title,y.title);
}
int _tmain(int argc, _TCHAR* argv[]) //_tmain必须包含头文件 # include <tchar.h>,不然编译不了
{
worker_b worker_object;
int n;
cin >> n;
switch (n)
{
case 1:
{
int i;
worker x;
cout << "请输入插入的位置" << endl;
cin >> i;
cout << "请输入员工的编号,姓名,性别,年龄,职务,职称,岗位薪酬:" << endl;
cin >> x.num >> x.name >> x.sex >> x.age>> x.title >> x.position >> x.wage;
cout<<"$$$$$$$$$$$$$$$$$$$$$"<<endl;
worker_object.insert_seq(i, x);
cout << "插入后的情况:" << endl;
worker_object.print_seq();
break;
}
case 0:
cout<<"煞笔!!!"<<endl;
break;
}
if (n<0 || n>1)
{
cout << "不好意思,你输入的数字不存在......." << endl;
}
return 0;
}
void menu()
{
cout << endl;
cout << "1.插入" << endl;
cout << "2.删除" << endl;
cout << "0.退出" << endl;
cout << endl;
cout << "请选择:" << endl;
}
worker_b::worker_b()
{
total = 0;
}
int worker_b::insert_seq(int i, worker x)
{
int j;
if (total == MAXSIZE)
{
cout << "table is full" << endl;
return -1;
}
if (i < 0 || i >(total))
{
cout << "place is wrong!" << endl;
return 0;
}
for (j = total; j >= i; j--)
{
change(worker_struct[j+1],worker_struct[j]);
}
change(worker_struct[i],x);
++ total;
return 1;
}
int worker_b::delete_seq(int i)
{
int j;
if (i <0 || i > total)
{
cout << "this element don't exist" << endl;
return -1;
}
for (j = i; j <= total - 1; j++)
{
change(worker_struct[j - 1],worker_struct[j]);
}
--total;
return 0;
}
void worker_b::print_seq()
{
int i;
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
for (i = 0; i <= total - 1; i++)
{
cout << worker_struct[i].num << setw(10) <<
worker_struct[i].name << setw(10) <<
worker_struct[i].sex << setw(10) <<
worker_struct[i].age << setw(10) <<
worker_struct[i].title << setw(10) <<
worker_struct[i].position << setw(10) <<
worker_struct[i].wage << setw(10) << endl;
}
cout << endl << endl;
} |
|