C 常用增删改查源码

{
InitializeComponent();
per = k;
sno = s;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if == true)
{ uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
= true;
}
else
{
= true;
= true;
= true;
= true;
}
}
到首记录
checkstate;
}
uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
();
DataSet ds = new DataSet();
{
if == true)
{ = 450; }
else
{ = 250; }
}
private void button1_Click(object sender, EventArgs e)
{
if ("你确定要删除该记录吗", "询问", , ==
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
();
string sqlupdate = " update studentinfo set ssex='" + sex + "',sage='" + age + "',sclass='" + sclass + "',sdept='" + sdept + "',saddress='" + saddress + "',sphone='" + sphone + "',sqq='" + sqq + "' where sno='" + sno + "'";
SqlCommand sc = new SqlCommand(sqlupdate, conn);
();
("数据已经修改成功", "温馨提示");
();
}
catch (Exception ex)
{
());
}
}
}
else
{
("对不起,你还没有选中'我要修改',所以您无法修改","温馨提示");
}
}
}
}
();
return;
}ห้องสมุดไป่ตู้
if == "")
{
("系别不能为空", "温馨提示", , ;
();
return;
}
}
else
{
string connstr = "server=IT32;uid=sa;pwd='sa';database=sc;";
try
{
SqlConnection conn = new SqlConnection(connstr);
= "";
= "";
= "";
= "";
= "";
= "";
= "";
= "";
= "";
("恭喜你已成功删除","温馨提示");
();
}
}
}
}
}
修改的代码:
using System;
using ;
using ;
using ;
using ;
namespace 学生成绩管理系统
{
public partial class studentxiugai : Form
这个是添加的代码
using System;
using ;
using ;
using ;
using ;
namespace 学生成绩管理系统
{
public partial class studentluru : Form
{
uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
using (SqlConnection conn = new SqlConnection(connstr))
{
();
DataSet ds = new DataSet();
string sqlset = "select * from studentinfo";
elete();
(ds, "coust");
{
();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
(ds, "coust");
DataTable dt = ["coust"];
();
return dt;
}
}
private void button1_Click(object sender, EventArgs e)
{
();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
(ds, "coust");
DataTable dt = ["coust"];
();
return dt;
}
}
private void button1_Click(object sender, EventArgs e)
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
{
}
else
{
}
}
private void checkstate(int pos)
{
if (pos == 0)
{
= false;
= false;
= true;
{
if == true)
{
string sno = ;
string sname = ;
string sex = ;
string age = ;
string sclass = ;
string sdept = ;
string saddress = ;
string sphone = ;
string sqq = ;
if == "" || == "" || == "" || == "")
{
if == "")
{
("性别不能为空", "温馨提示", , ;
();
return;
}
if == "")
{
("年龄不能为空", "温馨提示", , ;
();
return;
}
if == "")
{
("班级不能为空", "温馨提示", , ;
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
(ds, "coust");
DataTable dt = ["coust"];
();
return dt;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
string per;
string sno;
CurrencyManager cmAmend;
public studentxiugai()
{
InitializeComponent();
}
public studentxiugai(string k,string s)
{
{
string xuehao = ;
string sqlsel = "select * from studentinfo where sno='" + xuehao + "'";
}
合集下载

C#常用增删改查源码

C#常用增删改查源码
string sclass = textBox5.Text;
string sdept = textBox6.Text;
string saddress = textBox7.Text;
string sphone = textBox8.Text;
string sqq = textBox9.Text;
if (textBox1.Text == "" || textBox2.Text == "" || textBox5.Text == "")
SqlCommand sc = new SqlCommand(sqlinsert, conn);
sc.ExecuteNonQuery();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
{
if (textBox1.Text == "")
{
MessageBox.Show("学号不能为空", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox1.Focus();
return;
}
if (textBox2.Text == "")
cmAmend = (CurrencyManager)BindingContext[dt];
}
if (per == "普通用户")

C#增删查改

C#增删查改
{
try
{
conn.Open();
cmd = new OleDbCommand(sql, conn);
OleDbDataReader dr = cmd.ExecuteReader();
{
int i;
cmd = new OleDbCommand(sql, conn);
try
{
conn.Open();
i = cmd.ExecuteNonQuery();
return dr;
}
ceturn null;
}
OracleCommand cmd = new OracleCommand(); OracleConnection con = new OracleConnection("数据库连接字符串"); cmd.Connection = con; con.Open(); mandType = CommandType.Text; mandText="sql"; cmd.ExecuteNonQuery(); con.Close();如果是查询数据,有两种,一种是返回1*1的值,如select count(*) ,需要调用cmd.ExecuteOracleScalar()方法,返回一个值另一种是row*columns,即如select * from ,你需要用到 OracleDataAdapter类,用adapter.fill(datatable)的方法去填充一个数据集
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}
catch (Exception e)

c数据库的增删改查项目案例

c数据库的增删改查项目案例

c数据库的增删改查项目案例一、项目准备。

1. 得包含SQLite相关的头文件。

SQLite有个方便的C接口,我们这样写:`#include <stdio.h>`:这是标准输入输出头文件,用于在控制台打印信息啥的。

`#include <sqlite3.h>`:这个就是SQLite数据库操作的关键头文件啦。

2. 然后呢,定义一个数据库连接对象。

就像你要跟数据库交朋友,得有个联系方式一样。

`sqlite3 db;`.二、连接数据库(这就像是给数据库打电话)1. 我们使用`sqlite3_open`函数来打开数据库。

如果数据库不存在,SQLite会自动创建一个。

比如我们创建一个名为`test.db`的数据库(这名字就很普通,就像大街上随便一个名字一样)。

代码是这样的:`int rc = sqlite3_open("test.db", &db);`.如果`rc`不等于`SQLITE_OK`(这是SQLite定义的一个常量,表示操作成功),那就说明出问题了,可能是文件权限问题或者磁盘满了之类的(就像打电话占线或者号码不存在一样)。

我们可以这样处理错误:`if (rc!= SQLITE_OK) {`.`fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));`.`sqlite3_close(db);`.`return 1;`.`}`.三、创建表(这就像是给数据库盖房子,先把房子结构规划好)1. 我们要创建一个表,就像在数据库这个大社区里盖一栋楼。

假设我们要创建一个存储用户信息的表,有`id`(就像用户的身份证号,独一无二的)、`name`(用户的名字)和`age`(用户的年龄)这几个字段。

我们用SQL语句来创建这个表,SQL语句就像是给数据库下达的建筑蓝图指令。

C 常用增删改查源码

C 常用增删改查源码

这个是添加的代码using System;usingusing ;using ;using ;using ;usingusingnamespace 学生成绩管理系统{public partial class studentluru : Form{uid=sa;pwd=sa;database=sc";using (SqlConnection conn = new SqlConnection(connstr)){();DataSet ds = new DataSet();SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);(ds, "coust");DataTable dt = ["coust"];();return dt;}}private void button1_Click(object sender, EventArgs e){if == true){string xuehao = ;string sqlsel = "select * from studentinfo where sno='" + xuehao + "'";= bangding(sqlsel);}else{string xingming = ;string sqlsel = "select * from studentinfo where sname='" + xingming + "'"; = bangding(sqlsel);}}private void radioButton1_CheckedChanged(object sender, EventArgs e) {if == true){= true;= false;}else{= false;= true;}}private void checkstate(int pos){if (pos == 0){= false;= false;= true;= true;}else{= true;= true;= true;= true;}}到首记录checkstate;}uid=sa;pwd=sa;database=sc";using (SqlConnection conn = new SqlConnection(connstr)){();DataSet ds = new DataSet();SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);(ds, "coust");DataTable dt = ["coust"];();return dt;}}private void checkBox1_CheckedChanged(object sender, EventArgs e) {if == true){ = 450; }else{ = 250; }}private void button1_Click(object sender, EventArgs e){if ("你确定要删除该记录吗", "询问", , =={int pos = uid=sa;pwd=sa;database=sc";using (SqlConnection conn = new SqlConnection(connstr)){();DataSet ds = new DataSet();string sqlset = "select * from studentinfo";elete();(ds, "coust");= "";= "";= "";= "";= "";= "";= "";= "";= "";= bangding(sqlset);("恭喜你已成功删除","温馨提示");();}}}}}修改的代码:using System;usingusing ;using ;using ;using ;usingusingnamespace 学生成绩管理系统{public partial class studentxiugai : Form{string connstr = "server=.;uid=sa;pwd=sa;database=sc";string per;string sno;CurrencyManager cmAmend;public studentxiugai(){InitializeComponent();}public studentxiugai(string k,string s){InitializeComponent();per = k;sno = s;}private void checkBox1_CheckedChanged(object sender, EventArgs e) {if == true){ uid=sa;pwd=sa;database=sc";using (SqlConnection conn = new SqlConnection(connstr)){();DataSet ds = new DataSet();SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn); (ds, "coust");DataTable dt = ["coust"];();return dt;}}private void button1_Click(object sender, EventArgs e){if == true){string sno = ;string sname = ;string sex = ;string age = ;string sclass = ;string sdept = ;string saddress = ;string sphone = ;string sqq = ;if == "" || == "" || == "" || == ""){if == ""){("性别不能为空", "温馨提示", , ;();return;}if == ""){("年龄不能为空", "温馨提示", , ;();return;}if == ""){("班级不能为空", "温馨提示", , ;();return;}if == ""){("系别不能为空", "温馨提示", , ;();return;}}else{string connstr = "server=IT32;uid=sa;pwd='sa';database=sc;";try{SqlConnection conn = new SqlConnection(connstr);();string sqlupdate = " update studentinfo set ssex='" + sex + "',sage='" + age + "',sclass='" + sclass + "',sdept='" + sdept + "',saddress='" + saddress + "',sphone='" + sphone + "',sqq='" + sqq + "' where sno='" + sno + "'";SqlCommand sc = new SqlCommand(sqlupdate, conn);();("数据已经修改成功", "温馨提示");();}catch (Exception ex){());}}}else{("对不起,你还没有选中'我要修改',所以您无法修改","温馨提示");}}}}。

C++文件内容增删改查询排序案例

C++文件内容增删改查询排序案例

#include <iostream>#include <fstream>#include <windows.h>#include <iomanip>using namespace std;//定义一个学生结构体struct Student{char sn[5];//学号char name[10];//姓名};//函数原型声明void mainForm(fstream &);void mainMenu();void inputInfo(fstream &);//录入功能模块void showAll(fstream &);//显示功能模块void find(fstream &);//查找功能模块void modify(fstream &);//修改功能模块void del(fstream &);//删除功能模块void sort(fstream &);//排序功能模块void showHead();//输出表头信息void showInfo(struct Student );//显示一条记录void fopen(fstream &);//打开文件及检测bool findBySn(fstream &,char *);//按学号关键字查找bool getFromFile(fstream &,struct Student &,int);//从文件中获取指定位置的记录给结构体变量int getMinPosition(fstream &,struct Student &);//获取最小sn记录位置bool delBySn(fstream &,char *);//从文件中删除指定sn的记录bool writeOne(fstream &,struct Student );//主函数void main(){//如果以app方式新建文件,每次运行时,不会清空文件内容,而且student.txt文件不存在,也会新建文件//但是,新建文件时,如果不加上ios::app打开方式,如果文件不存在,还会出错,如果文件存在,会清空文件内容//所以,我们在课程设计中,新建文件最好以app方式打开,以免每运行一次,文件内容被清空一次,每次都要录入//数据,影响效率。

C#--SQLserver数据库基本操作(增、删、改、查)

C#--SQLserver数据库基本操作(增、删、改、查)

C#--SQLserver数据库基本操作(增、删、改、查)写在前⾯:常⽤数据库:SQLserver:Oracle:MySQL:Access:在⽇常的⼯作中,通常⼀个项⽬会⼤量⽤的数据库的各种基本操作。

SQLserver数据库是最为常见的⼀种数据库,本⽂则主要是记录了C#对SQL的连接、增、删、改、查的基本操作,如有什么问题还请各位⼤佬指教。

后续也将对其他⼏个常⽤的数据库进⾏相应的整理,链接已经附在⽂章开始。

话不多说,开始码代码。

引⽤:using System.Data; //DataSet引⽤集using System.Data.SqlClient; //sql引⽤集先声明⼀个SqlConnection便于后续使⽤。

private SqlConnection sql_con;//声明⼀个SqlConnectionsql打开:///<summary>/// SQLserver open///</summary>///<param name="link">link statement</param>///<returns>Success:success; Fail:reason</returns>public string Sqlserver_Open(string link){ try { sql_con = new SqlConnection(link); sql_con.Open(); return"success"; } catch (Exception ex) { return ex.Message; }}sql关闭:///<summary>/// SQLserver close///</summary>///<returns>Success:success Fail:reason</returns>public string Sqlserver_Close(){ try { if (sql_con == null) { return"No database connection"; } if (sql_con.State == ConnectionState.Open || sql_con.State == ConnectionState.Connecting) { sql_con.Close(); sql_con.Dispose(); } else { if (sql_con.State == ConnectionState.Closed) { return"success"; } } return"success"; } catch (Exception ex) { return ex.Message; }}sql的增删改:///<summary>/// SQLserver insert,delete,update///</summary>///<param name="sql">insert,delete,update statement</param>///<returns>Success:success + Number of affected rows; Fail:reason</returns> public string Sqlserver_Insdelupd(string sql){ try { int num = 0; if (sql_con == null) { return"Please open the database connection first"; } if (sql_con.State == ConnectionState.Open) { SqlCommand sqlCommand = new SqlCommand(sql, sql_con); num = sqlCommand.ExecuteNonQuery(); } else { if (sql_con.State == ConnectionState.Closed) { return"Database connection closed"; } if (sql_con.State == ConnectionState.Broken) { return"Database connection is destroyed"; } if (sql_con.State == ConnectionState.Connecting) { return"The database is in connection"; } } return"success" + num; } catch (Exception ex) { return ex.Message.ToString(); }}sql的查:///<summary>/// SQLserver select///</summary>///<param name="sql">select statement</param>///<param name="record">Success:success; Fail:reason</param>///<returns>select result</returns>public DataSet Sqlserver_Select(string sql, out string record){ try { DataSet dataSet = new DataSet(); if (sql_con == null) { record = "Please open the database connection first"; return dataSet; }if (sql_con.State == ConnectionState.Open) { SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sql_con); sqlDataAdapter.Fill(dataSet, "sample"); sqlDataAdapter.Dispose(); record = "success"; return dataSet; return dataSet; } if (sql_con.State == ConnectionState.Broken) { record = "Database connection is destroyed"; return dataSet; } if (sql_con.State == ConnectionState.Connecting) { record = "The database is in connection"; return dataSet; } record = "ERROR"; return dataSet; } catch (Exception ex) { DataSet dataSet = new DataSet(); record = ex.Message.ToString(); return dataSet; }}⼩编发现以上这种封装⽅式还是很⿇烦,每次对SQL进⾏增删改查的时候还得先打开数据库,最后还要关闭,实际运⽤起来⽐较⿇烦。

C#增删改查模版

//使用适配器对象来执行查询命令
SqlDataAdapter dap = new SqlDataAdapter(sql,con);
foreach(SqlParameter p in list)
{
//SqlDataAdapter有1个属性表示SqlCommand对象
SqlConnection con = new SqlConnection(constr);
try
{
con.Opew SqlCommand (sql,con);
foreach(SqlParameter p in list)
{
cmd.Parameters.Add(p);
}
//CommandBehavior.CloseConnection,当关闭DataReader对象的同时关闭关联的连接对象
public static object SelectSingle(string sql,List<SqlParameter> list)
{
using(SqlConnection con = new SqlConnection (constr))
dap.SelectCommand.Parameters.Add(p);
}
DataTable dt = new DataTable();
dap.Fill(dt);
return dt;
{
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
foreach(SqlParameter p in list)
{
}catch(Exception ex)

c语言实现顺序表的增删查改逆置简单代码

c语言实现顺序表的增删查改逆置简单代码1. 顺序表的定义顺序表是一种线性表,其元素在内存中按顺序存储,每个元素占用连续的存储单元。

顺序表的特点是存取速度快,但插入和删除元素时需要移动大量的元素。

顺序表可以用结构体来表示,其定义如下:typedef struct_SeqList {int*data; // 指向数据元素的指针int size; // 顺序表的长度int capacity; // 顺序表的容量} SeqList;2. 顺序表的初始化顺序表的初始化需要分配内存空间来存放数据元素。

可以使用以下代码来初始化顺序表:SeqList*init_seq_list(int capacity) {SeqList*list= (SeqList*)malloc(sizeof(SeqList));if (list==NULL) {return NULL;}list->data= (int*)malloc(sizeof(int) *capacity);if (list->data==NULL) {free(list);return NULL;}list->size=0;list->capacity=capacity;return list;}3. 顺序表的插入在顺序表中插入元素需要移动后面的元素,以保证元素的顺序性。

可以使用以下代码在顺序表中插入元素:int insert_seq_list(SeqList*list, int index, int value) {if (index<0||index>list->size) {return-1;}if (list->size==list->capacity) {// 扩容顺序表int*new_data= (int*)realloc(list->data, sizeof(int) *list->capacity*2);if (new_data==NULL) {return-1;}list->data=new_data;list->capacity*=2;}// 移动后面的元素for (int i=list->size; i>index; i--) {list->data[i] =list->data[i-1];}// 插入元素list->data[index] =value;list->size++;return0;}4. 顺序表的删除从顺序表中删除元素需要移动后面的元素,以保证元素的顺序性。

增删改查参考代码


conn.Close();
}
int y = pubDate.Year;
int m = pubDate.Month;
int d = pubDate.Day;
string newDate = y + "-" + m + "-" + d;
//定义一个变量
int rowCount = 0;
//TRY块显示出错信息
try
{
//连接连接数据
string connString = "Data Source ='何银财\\SQLEXPRESS'; Initial Catalog = BookDb; User Id = 'sa'; Pwd = '0401'";
}
else
{
MessageBox.Show("恭喜登陆成功!");
}
窗体事件加载查询
private void FormAddBookInfo_Load(object sender, EventArgs e)
}
if (count > 0)
{
MessageBox.Show("delete ok");
}
else
{
MessageBox.Show("delete error");
删除信息参考代码:
//取得文本框中的信息
string bookName = this.textBox1.Text.Trim();
//T-SQL查询(选择表查询出相关字段删除)

C++定义一个学生类,并实现增删改查

C++定义⼀个学⽣类,并实现增删改查//头⽂件#ifndef STUDENT_H#define STUDENT_H#include <string>#include <iostream>using namespace std;class Student{private:int id; //IDchar* name; //姓名int yuwen; //语⽂int shuxue;//数学public:Student(); //构造成员~Student(); //析构成员Student(int id, char* name, int yuwen, int shuxue);void Set_id(int id); //设置idvoid Set_name(char* name); //设置姓名void Set_yuwen(int yuwen); //设置语⽂成绩void Set_shuxue(int shuxue); //设置数学成绩int return_id(); //返回idchar* return_name(); //返回姓名int return_yuwen(); //返回语⽂成绩int return_shuxue(); //返回数学成绩Student* next; //定义⼀个类成员指向下⼀个类成员};struct StudentData{int listsize; //存放成员的个数Student* m_head; //定义⼀个student的头节点};Student* Student_Link(StudentData* list, Student *other); //成员添加Student* Student_Delete(StudentData* list); //成员删除Student* Student_Select(StudentData* list); //成员查找Student* Student_Set(StudentData* list); //成员修改Student* Student_Show(StudentData* list); //成员遍历#endif//cpp⽂件#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include <stdlib.h>#include <string.h>#include "Student.h"using namespace std;Student::Student() //构造成员{id = yuwen = shuxue = NULL;name = new char[50+1];name = NULL;}Student::~Student() //析构成员{delete []name;}Student::Student(int m_id, char* m_name, int m_yuwen, int m_shuxue){this->id = m_id;name = new char[50];strcpy(this->name,m_name);this->yuwen = m_yuwen;this->shuxue = m_shuxue;printf("学⽣信息创建成功!\n");}void Student::Set_id(int m_id) //设置id{this->id = m_id;}void Student::Set_name(char* m_name) //设置姓名{strcpy(this->name, m_name);void Student::Set_yuwen(int m_yuwen) //设置语⽂成绩{this->yuwen = m_yuwen;}void Student::Set_shuxue(int m_shuxue) //设置数学成绩{this->shuxue = m_shuxue;}int Student::return_id() //返回id{return id;}char* Student::return_name() //返回姓名{return name;}int Student::return_yuwen() //返回语⽂成绩{return yuwen;}int Student::return_shuxue() //返回数学成绩{return shuxue;}//成员添加Student* Student_Link(StudentData* list, Student *other) //成员添加从头部添加{//Student* temp = list->m_head;other->next = list->m_head->next;list->m_head->next = other;list->listsize++;return list->m_head;}//成员删除Student* Student_Delete(StudentData* list){if (list->listsize == 0){printf("空数据\n");return list->m_head;}Student* cur = list->m_head->next; //定义⼀个指针指向下⼀个成员Student* pre = list->m_head; //定义⼀个指针指向上⼀个成员int mode;//定义删除的内容 1为姓名,2为学号cout <<"请输⼊要删除的条件,1为学号,2为姓名,请输⼊1或者2,输⼊其他则直接退出:"; cin >> mode;switch (mode){case1:int id;cout << "请输⼊要删除的学⽣id:";cin >> id;while (cur != NULL){if (cur->return_id() == id){pre->next = cur->next; //将上⼀个节点和下⼀个节点连接delete cur; //将节点释放list->listsize--; //存储的数据个数减1cur = NULL; //将指针指向空,防⽌野指针cout << "删除成功\n";return list->m_head;}pre = cur;cur = cur->next;}return list->m_head;case2:char name[50];cout << "请输⼊要删除的姓名:";cin >> name;while (cur != NULL){if (strcmp(cur->return_name(),name)==0){pre->next = cur->next; //将上个节点的next指向下个节点的next;delete cur;list->listsize--;cur = NULL; //将指针指向空,防⽌野指针cout << "删除成功\n";return list->m_head;cur = cur->next;}printf("找不到该学⽣!\n");return list->m_head;default:printf("输⼊错误,已退出!");return list->m_head;break;}}//成员查找Student* Student_Select(StudentData* list){if (list->listsize == 0) //判断存放数据的个数是否为0{printf("空表\n");return list->m_head;}Student* temp = list->m_head->next; //定义⼀个临时指针指向头节点的下⼀个节点并遍历链表,默认第⼀个头节点不打印int mode;//定义删除的内容 1为姓名,2为学号cout << "请输⼊要查找的条件,1为学号,2为姓名,请输⼊1或者2,输⼊其他则直接退出:";cin >> mode;switch (mode){case1:int id;cout << "请输⼊要查找的学⽣id:";cin >> id;while (temp != NULL){if (temp->return_id() == id ){cout << "找到啦! " << "该同学的语⽂成绩:" << temp->return_yuwen() << " 数学成绩:" << temp->return_shuxue() << endl;return list->m_head;}temp = temp->next;}printf("找不到该学⽣!\n");return list->m_head;break;case2:char name[50];cout << "请输⼊要查找的学⽣的姓名:";cin >> name;while (temp != NULL){if (strcmp(temp->return_name(), name) == 0){cout << "找到啦! " << "该同学的语⽂成绩:" << temp->return_yuwen() << " 数学成绩:" << temp->return_shuxue() << endl;return list->m_head;}temp = temp->next;}printf("找不到该学⽣!\n");return list->m_head;default:printf("输⼊错误,已退出!");return list->m_head;break;}cout << "没找到您要的数据\n";return list->m_head;}//成员修改Student* Student_Set(StudentData* list){//mode 1为语⽂ 2为数学if (list->listsize == 0) //判断存放数据的个数是否为0{printf("空数据\n");return list->m_head;}int mode;//定义删除的内容 1为姓名,2为学号Student* temp = list->m_head->next; //定义⼀个临时指针指向头节点的下⼀个节点并遍历链表,默认第⼀个头节点不打印switch (mode){case1:{int m_id;cout << "请输⼊要修改的学⽣id:";cin >> m_id;while (temp != NULL){if (temp->return_id() == m_id){int subject;//定义⼀个科⽬ 1为语⽂ 2为数学cout << "请确认要修改的科⽬成绩,1为语⽂ 2为数学,输⼊错误则直接退出:";cin >> subject;if (subject == 1){int fenshu;cout << "请输⼊要修改的分数(只能输⼊整数):";cin >> fenshu;temp->Set_yuwen(fenshu);printf("成绩修改成功\n");return list->m_head;}else if (subject == 2){int fenshu;cout << "请输⼊要修改的分数(只能输⼊整数):";cin >> fenshu;temp->Set_shuxue(fenshu);printf("成绩修改成功\n");return list->m_head;}else{cout << "输⼊有误,退出";return list->m_head;break;}}temp = temp->next;}cout << "未找到要修改的数据\n";return list->m_head;}case2:{char* name = new char[50];cout << "请输⼊要修改的学⽣名称:";cin >> name;while (temp != NULL){if (strcmp(temp->return_name(), name) == 0){int subject;//定义⼀个科⽬ 1为语⽂ 2为数学cout << "请确认要修改的科⽬成绩,1为语⽂ 2为数学,输⼊错误则直接退出:";cin >> subject;if (subject == 1){int fenshu;cout << "请输⼊要修改的分数:";cin >> fenshu;temp->Set_yuwen(fenshu);printf("成绩修改成功\n");return list->m_head;}else if (subject == 2){int fenshu;cout << "请输⼊要修改分数(只能输⼊整数):";cin >> fenshu;temp->Set_shuxue(fenshu);printf("成绩修改成功\n");return list->m_head;}else{cout << "输⼊有误,退出\n";break;}temp = temp->next;}cout << "没找到您要的数据\n";return list->m_head;}default:printf("输⼊错误,已退出!");return list->m_head;break;}}//成员遍历Student* Student_Show(StudentData *list){if (list==NULL||list->listsize == 0) //判断存放数据的个数是否为0{printf("空表\n");return list->m_head;}Student* temp = list->m_head->next; //定义⼀个临时指针指向头节点的下⼀个节点并遍历链表,默认第⼀个头节点不打印printf("%-16s%-16s%-16s%-16s\n", "学⽣id", "姓名", "语⽂成绩", "数学成绩");while (temp != NULL){//打印成员,并将当前成员指针指向下⼀个成员节点printf("%-16d%-16s%-16d%-16d\n", temp->return_id(), temp->return_name(), temp->return_yuwen(), temp->return_shuxue());//cout << temp->return_id() << " " << temp->return_name() << " " << temp->return_shuxue() << " " << temp->return_yuwen() << endl; temp = temp->next;}return list->m_head;}//初始化头节点void init_Student_Data_m_head(StudentData* list){//动态申请⼀个头节点,并初始化,初始化后将头节点的next指向NULL, 数据成员的个数设置为0list->m_head = new Student;list->listsize = 0;list->m_head->next = NULL;}//成员清空StudentData* Free_Student(StudentData* list){if (list->listsize == 0) //判断存放数据的个数是否为0{printf("空表\n");return list;}Student* temp = list->m_head->next; //定义⼀个临时指针指向头节点的下⼀个节点并遍历链表,默认第⼀个头节点不打印while (temp != NULL){//记录⼀个指针, 并遍历成员逐个释放,最后将指针置为NULL 防⽌野指针保留头节点,头节点不⽤释放Student* pre = temp;*pre = *temp;temp = temp->next;delete pre;pre = NULL;list->listsize--; //空间⼤⼩减⼀}list->m_head->next = NULL;printf("数据清理完成,数据已清空\n");return list;}//初始化⼀个类Student* init_Student(){int id;char name[50];int yuwen;int shuxue;cout << "请按顺序输⼊学号、姓名、语⽂成绩、数学成绩,每输⼊⼀次按回车确认\n";cout << "学号(只能输⼊整数):";cin >> id;cout << "姓名:";cout << "语⽂成绩(只能输⼊整数):";cin >> yuwen;cout << "数学成绩(只能输⼊整数):";cin >> shuxue;Student* temp = new Student(id,name,yuwen,shuxue);return temp;}void play(){printf("-------------------学⽣信息管理系统-----------------------\n");printf("1:初始化管理系统\n");printf("2:新增⼀个学⽣信息并录⼊\n");printf("3:删除⼀个学⽣信息\n");printf("4:查找⼀个学⽣的成绩\n");printf("5:修改⼀个学⽣的成绩\n");printf("6:清空所有学⽣信息\n");printf("7:打印所有学⽣信息\n");printf("8:退出系统\n");printf("------------------ 请输⼊您的选择1-8 -----------------------\n");StudentData *data = new StudentData;init_Student_Data_m_head(data);while (1){int num;cout << "请输⼊指令(1-8),按回车确认:";cin >> num;switch (num){case1://初始化头节点init_Student_Data_m_head(data);cout << " 当前指令,即将创建⼀个学⽣管理系统并初始化\n"; printf("初始化学⽣管理系统成功!\n");cout << endl;break;case2://新增⼀个学⽣成员并初始化cout << " 当前指令,即将添加⼀个学⽣成员\n";Student_Link(data, init_Student());cout << endl;break;case3://删除⼀个学⽣成员cout << " 当前指令,即将删除⼀个学⽣成员\n";Student_Delete(data);cout << endl;break;case4://查询某个成员的成绩cout << " 当前指令,即将查询⼀个学⽣成员的成绩\n";Student_Select(data);cout << endl;break;case5://修改某个成员的成绩cout << " 当前指令,即将修改⼀个学⽣成员的成绩\n";Student_Set(data);cout << endl;break;case6://清空所有学⽣数据cout << " 当前指令,即将删除所有数据\n";Free_Student(data);cout << endl;break;case7://打印所有学⽣数据cout << " 当前指令,打印所有成员数据\n";Student_Show(data);cout << endl;break;default:cout << "输⼊指令有误,请重新输⼊!" << endl;break;}}}int main(){play();return0;。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档