求大神帮我改改这个程序,小弟实在弄不了了.
本帖最后由 maverick 于 2014-4-1 14:40 编辑#include "stdafx.h"
#include "iomanip"
#include "iostream"
#include<string>
using namespace std;
#define MAXSIZE 50
struct worker_a
{
long num;
char name;
char sex;
int age;
char title;
char position;
float wage;
};
class worker_b
{
private:
worker_a worker_struct;
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_a x,worker_a y)
{
x.age=y.age;
x.num=y.num;
x.sex=y.sex;
x.wage=y.wage;
strcpy_s(x.name,y.name);
strcpy_s(x.position,y.position);
strcpy_s(x.title,y.title);
}
int _tmain(int argc, _TCHAR* argv[])
{
worker_b worker_object;
int n;
//bool m = true;
/*
while (m)
{
menu();
*/
cin >> n;
switch (n)
{
case 1:
{
int i;
worker_a 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 2:
{
int i;
cout << "请输入删除位置" << endl;
cin >> i;
worker_object.delete_seq(i);
cout << "删除后的情况:" << endl;
worker_object.print_seq();
break;
}
*/
case 0:
//m = false;
cout<<"煞笔!!!"<<endl;
break;
}
/*
}
*/
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_a 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,worker_struct);
}
change(worker_struct,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,worker_struct);
}
--total;
return 0;
}
void worker_b::print_seq()
{
int i;
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
for (i = 0; i <= total - 1; i++)
{
cout << worker_struct.num << setw(10) <<
worker_struct.name << setw(10) <<
worker_struct.sex << setw(10) <<
worker_struct.age << setw(10) <<
worker_struct.title << setw(10) <<
worker_struct.position << setw(10) <<
worker_struct.wage << setw(10) << endl;
}
cout << endl << endl;
}
看不出问题在哪,但是帮你顶高点,让高手帮忙看看 怪不得你的代码没人看,没人回复,一放到编译器中,全都是错,而且你的代码是不是相比较结构体和类是吧??这代码真是一塌糊涂,一塌糊涂,呵呵…… 新手表示看不懂……路过,瞻仰,顶一下…… 说帮顶了,C++我实在无力。。 本帖最后由 woliubin 于 2014-4-2 09:51 编辑
我也没有细看,大概的浏览了一下,你的程序在我这里跑不起来,你看着自己改改吧,虽然程序很小,但是用起来挺别扭,你的结构体最好这样用
//定义员工结构体
typedef struct worker_a
{
long num;
char name;
char sex;
intage;
char title;
char position;
float wage;
}worker;
取用worker作为类型;
最下面部分换成这个
for (i = 0; i <= total - 1; i++)
{
cout << worker_struct.num << setw(10) <<
worker_struct.name << setw(10) <<
worker_struct.sex << setw(10) <<
worker_struct.age << setw(10) <<
worker_struct.title << setw(10) <<
worker_struct.position << setw(10) <<
worker_struct.wage << setw(10) << endl;
}
int worker_b::insert_seq(int i, worker_a 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,worker_struct);
}
change(worker_struct,x); //这里也出现了问题
++ total;
return 1;
}
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(x.position,y.position);
strcpy(x.title,y.title);
}
你的崩溃是因为strcpy_s用的有点问题
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#define MAXSIZE 50
//定义员工结构体
typedef struct worker_a
{
long num;
char name;
char sex;
intage;
char title;
char position;
float wage;
}worker;
class worker_b
{
public:
worker worker_struct;
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(x.position,y.position);
strcpy(x.title,y.title);
}
int _tmain(int argc, _TCHAR* argv[])
{
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;
}
/*
}
*/
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,worker_struct);
}
change(worker_struct,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,worker_struct);
}
--total;
return 0;
}
void worker_b::print_seq()
{
int i;
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
for (i = 0; i <= total - 1; i++)
{
cout << worker_struct.num << setw(10) <<
worker_struct.name << setw(10) <<
worker_struct.sex << setw(10) <<
worker_struct.age << setw(10) <<
worker_struct.title << setw(10) <<
worker_struct.position << setw(10) <<
worker_struct.wage << setw(10) << endl;
}
cout << endl << endl;
}
楼上好厉害 woliubin 发表于 2014-4-2 09:24 static/image/common/back.gif
我也没有细看,大概的浏览了一下,你的程序在我这里跑不起来,你看着自己改改吧,虽然程序很小,但是用起来 ...
strcpy与strcpy_s有什么区别? 我原来用的是strcpy,后来编译器提醒我用strcpy会出错,我就根据提示换成strcpy_s. 天使之约 发表于 2014-4-1 15:35 static/image/common/back.gif
看不出问题在哪,但是帮你顶高点,让高手帮忙看看
谢谢啦。。。。。 昨天看了你的代码确实不想看下去了,不过还是坚持着看下去了,最后对你的程序进行了修改,也加了点我写的代码,就是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;
char sex;
intage;
char title;
char position;
float wage;
}worker;
class worker_b
{
public:
worker worker_struct;
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,worker_struct);
}
change(worker_struct,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,worker_struct);
}
--total;
return 0;
}
void worker_b::print_seq()
{
int i;
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
for (i = 0; i <= total - 1; i++)
{
cout << worker_struct.num << setw(10) <<
worker_struct.name << setw(10) <<
worker_struct.sex << setw(10) <<
worker_struct.age << setw(10) <<
worker_struct.title << setw(10) <<
worker_struct.position << setw(10) <<
worker_struct.wage << setw(10) << endl;
}
cout << endl << endl;
} woliubin 发表于 2014-4-2 09:52 static/image/common/back.gif
#include "stdafx.h"
#include
#include
用strcpy好像也会出现出错,会出现Access Violation.
好像是调用strcpy时只有指针,未分配内存的缘故。 maverick 发表于 2014-4-2 10:12 static/image/common/back.gif
谢谢啦。。。。。
不用谢,互相学习互相帮助 新手,完全看不懂,太长了 大神写的为什么我跑不了啊、??!
页:
[1]