第三章
使用QtGui的已编译程序
https://mega.nz/#!NPJB0aDT!8DYcKhVOxhtNV0uXZ0qHJmjD69YSSXfhhZVnh9h8ylo
// QtGuiApplication1.cpp
#include "QtGuiApplication1.h"
#include "C3.h"
#include "LB_D.h"
#include <QRegExp>
#include <qvalidator.h>
#include <qlineedit.h>
#include <QLineEdit>
#include <qlineedit.h>
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QDoubleValidator *l32 = new QDoubleValidator(6, 85, 4, this);
l32->setNotation(QDoubleValidator::StandardNotation);
QRegExp l31("0|[1-6]\\d{0,0}");
QRegExp l33("^?(180|([1,9]{1}[0-9]|1[0-7][0-9])(\\.\\d{1,4})?)$");
ui.ilineEdit3_2->setValidator(l32);
ui.iLineEdit->setValidator(new QRegExpValidator(l33, this));
ui.ilineEdit3_1->setValidator(new QRegExpValidator(l31, this));
ui.ilineEdit3_2->setPlaceholderText("请输入轴径D");
ui.ilineEdit3_1->setPlaceholderText("请输入型号");
ui.iLineEdit->setPlaceholderText("请输入包角");
}
QString QtGuiApplication1::getInputValue()
{
return ui.iLineEdit->text();
}
QString QtGuiApplication1::getD()
{
return ui.ilineEdit3_2->text();
}
QString QtGuiApplication1::getXh()
{
return ui.ilineEdit3_1->text();
}
QString QtGuiApplication1::creatsize()
{
return ui.ilineEdit_LB_1->text();
}
void QtGuiApplication1::setinput3_1(QString s3_1)
{
ui.iLineEdit->setText(s3_1);
}
void QtGuiApplication1::setinput3_2(QString s3_2)
{
ui.ilineEdit3_2->setText(s3_2);
}
void QtGuiApplication1::setOutputValue(QString ov)
{
ui.oLineEdit->setText(ov);
}
void QtGuiApplication1::tip3_1(QString t31)
{
ui.label3_1->setText(t31);
}
void QtGuiApplication1::tip3_3(QString t33)
{
ui.label3_3->setText(t33);
}
void QtGuiApplication1::setD(QString oD)
{
ui.ilineEdit3_2->setText(oD);
}
void QtGuiApplication1::set3_2(QString ob, QString oh, QString ot, QString ot1)
{
ui.olineEdit3_2_b->setText(ob);
ui.olineEdit3_2_h->setText(oh);
ui.olineEdit3_2_t->setText(ot);
ui.olineEdit3_2_t1->setText(ot1);
}
void QtGuiApplication1::setXh(QString xh1)
{
ui.ilineEdit3_1->setText(xh1);
}
void QtGuiApplication1::set3_1(QString ob_1, QString oh_1, QString obp_1)
{
ui.olineEdit3_1_b->setText(ob_1);
ui.olineEdit3_1_h->setText(oh_1);
ui.olineEdit3_1_bp->setText(obp_1);
}
void QtGuiApplication1::tip3_2(QString t32)
{
ui.label3_2->setText(t32);
}
void QtGuiApplication1::on_Vxxcz_PushButton_clicked()
{
double x = getInputValue().toDouble();
if (x < 90)
{
tip3_3("输入值小于90!");
}
else
{
float y;
if (ui.radioButton_l->isChecked())
{
y = linearInterpolation(x);
}
else if (ui.radioButton_p->isChecked())
{
y = parabola(x);
}
QString ostr;
ostr.setNum(y);
setOutputValue(ostr);
tip3_3("查询完成");
show();
}
}
void QtGuiApplication1::on_V3_2PushButton_clicked()
{
float dgiven = getD().toFloat();
if (dgiven < 6)
{
tip3_2("输入值小于6!");
set3_2("", "", "", "");
}
else if (dgiven > 85)
{
tip3_2("输入值大于85!");
set3_2("", "", "", "");
}
else
{
float *r32o;
float b, h, t, t1;
r32o = jian(dgiven);
b = *r32o;
h = *(r32o + 1);
t = *(r32o + 2);
t1 = *(r32o + 3);
QString bs, hs, ts, t1s;
bs.setNum(b);
hs.setNum(h);
ts.setNum(t);
t1s.setNum(t1);
set3_2(bs, hs, ts, t1s);
tip3_2("查询完成");
show();
}
}
void QtGuiApplication1::on_V3_2PushButton_2_clicked()
{
tip3_2("请输入D值");
setinput3_2("");
set3_2("", "", "", "");
}
void QtGuiApplication1::on_V3_1PushButton_clicked()
{
int i = getXh().toInt();
if (i >= 0 && i <= 6)
{
float *r31o;
r31o = vdai(i);
float r0, r1, r2;
r0 = *r31o;
r1 = *r31o + 1;
r2 = *r31o + 2;
QString bs, hs, bps;
bs.setNum(r0);
hs.setNum(r1);
bps.setNum(r2);
set3_1(bs, hs, bps);
tip3_1("查询完成");
show();
}
else
{
tip3_1("请键入正确值!");
set3_1("", "", "");
}
}
void QtGuiApplication1::on_V3_1PushButton_2_clicked()
{
setXh("");
set3_1("", "", "");
}
void QtGuiApplication1::on_Vxxcz_PushButton_2_clicked()
{
setinput3_1("");
setOutputValue("");
tip3_3("请键入包角");
}
void QtGuiApplication1::on_pushButton_LB_1_clicked()
{
int i = creatsize().toInt();
create(i);
}
以下为控制台类程序
主函数
// Zycad.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "C3.H"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
using namespace std;
int ic, j = 0;
while (j == 0)
{
printf("请输入查询类型\nV带查询请键入1\n键和键槽的刨面尺寸查询请键入2\n包角系数K2的查询,线性插值请键入3,抛物线插值请键入4\n退出请键入5\n");
scanf("%d", &ic);
switch (ic)
{
case 1:
int i;
printf("1-7分别对应YZABCDE型\n");
printf("input=");
scanf("%d", &i);
if (i == 0)
printf("非正确输入!");
else
{
i--;
float * r31o;
r31o = vdai(i);
printf("b=%.2f,h=%.2f,bp=%.2f \n", *r31o, *(r31o + 1), *(r31o + 2));
printf("查询完毕\n");
}
break;
case 2:
float Dg;
printf("请键入直径\n");
printf("input=");
scanf("%f", &Dg);
if (Dg < 6)
{
printf("输入值小于6或非数字!");
}
else if (Dg > 85)
{
printf("输入值大于85!");
}
else
{
float *r32o;
r32o = jian(Dg);
printf("b=%.2f,h=%.2f,t=%.2f,t1=%.2f \n", *r32o, *(r32o + 1), *(r32o + 2), *(r32o + 3));
printf("查询完毕\n");
}
break;
case 3:
float x;
printf("input=");
scanf("%f", &x);
if (x < 90)
{
printf("输入值小于90或非数字!");
}
else
{
float y;
y = linearInterpolation(x);
printf("K2=%.4f\n", y);
printf("查询完毕\n");
}
break;
case 4:
printf("input=");
scanf("%f", &x);
if (x < 90)
{
printf("输入值小于90或非数字!");
}
else
{
float y;
y = parabola(x);
printf("K2=%.4f\n", y);
printf("查询完毕\n");
}
break;
case 5:
j = 1;
}
}
return 0;
}
调用的子函数
#include "stdafx.h"
#include "C3.h"
float linearInterpolation(float alpha)
{
int deg[10] = {180,170,160,150,140,130,120,110,100,90};
float k[10] = {1,0.98,0.95,0.92,0.89,0.86,0.83,0.79,0.74,0.68};
float k2;
for(int i=0; i<10; i++)
{
if(alpha >= deg[i])
{
k2 = k[i] + (k[i-1] - k[i]) * (alpha - deg[i]) / (deg[i-1] - deg[i]);
break;
}
}
return k2;
}
float parabola(float delta)
{
int x[10] = {180,170,160,150,140,130,120,110,100,90};
float y[10] = {1,0.98,0.95,0.92,0.89,0.86,0.83,0.79,0.74,0.68};
float k2;
for(int i=0; i<10; i++)
{
if(delta >= x[i])
{
k2 = (delta-x[i])*(delta-x[i+1])/(x[i-1]-x[i])/(x[i-1]-x[i+1])*y[i-1]+
(delta-x[i-1])*(delta-x[i+1])/(x[i]-x[i-1])/(x[i]-x[i+1])*y[i]+
(delta-x[i-1])*(delta-x[i])/(x[i+1]-x[i-1])/(x[i+1]-x[i])*y[i+1];
break;
}
}
return k2;
}
float * jian(float beta)
{
float b,h,t,t1;
float D[13] = {75,65,58,50,44,38,30,22,17,12,10,8,6};
float kb[13] = {22,20,18,16,14,12,10,8,6,5,4,3,2};
float kh[13] = {14,12,11,10,9,8,8,7,6,5,4,3,2};
float kt[13] = {9,7.5,7.0,6.0,5.5,5.0,5.0,4.0,3.5,3.0,2.5,1.8,1.2};
float kt1[13] = {5.4,4.9,4.4,4.3,3.8,3.3,3.3,3.3,2.8,2.3,1.8,1.4,1};
for(int i=0; i<13; i++)
{
if(beta >= D[i])
{
b=kb[i];
h=kh[i];
t=kt[i];
t1=kt1[i];
break;
}
}
float *re32;
float r32[4] = {b,h,t,t1};
re32 = r32;
return re32;
}
float * vdai(int gamma)
{
float b,h,bp;
float kb[7] = {6,10,13,17,22,32,38};
float kh[7] = {4,6,8,11,14,19,23};
float kbp[7] = {5.3,8.5,11,14,19,27,32};
b=kb[gamma];
h=kh[gamma];
bp=kbp[gamma];
float *vdaio;
float vd[3] = {b,h,bp};
vdaio = vd;
return vdaio;
}
双向链表
因双向链表已经涵盖了书中前面的功能,故在此只有双向链表的。
主函数
// Doubly linked list.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include "DLLF.h"
using namespace std;
int main()
{
int i, L, ct = 1, C;
char c;
DLLF D;
cout << "输入初始链表长度,之后输入一个字符,链表将以之为第一个元素" << endl;
cin >> L;
cin >> c;
D.lth(L,c);
D.create();
cout << "\n1访问\n2反向访问\n3搜索\n4反向搜索\n5删除\n6反向删除\n7插入\n8反向插入\n9修改\n10反向修改\n11遍历\n12反向遍历\n13退出" << endl;
while (ct = 1)
{
cout << endl;
cin >> C;
switch (C)
{
case 1:
cout << "输入访问的位置" << endl;
cin >> i;
c = D.vist(i);
cout << c << endl;
break;
case 2:
cout << "输入访问的位置" << endl;
cin >> i;
c = D.vist_1(i);
cout << c << endl;
break;
case 3:
cout << "输入需搜索的字符" << endl;
cin >> c;
i = D.search(c);
cout << i << endl;
break;
case 4:
cout << "输入需搜索的字符" << endl;
cin >> c;
i = D.search_1(c);
cout << i << endl;
break;
case 5:
cout << "输入删除的位置" << endl;
cin >> i;
D.del(i);
break;
case 6:
cout << "输入删除的位置" << endl;
cin >> i;
D.del_1(i);
break;
case 7:
cout << "键入字符,再键入位置" << endl;
cin >> c;
cin >> i;
D.insert(c, i);
break;
case 8:
cout << "键入字符,再键入位置" << endl;
cin >> c;
cin >> i;
D.insert_1(c, i);
break;
case 9:
cout << "键入字符,再键入位置" << endl;
cin >> c;
cin >> i;
D.modify(c, i);
break;
case 10:
cout << "键入字符,再键入位置" << endl;
cin >> c;
cin >> i;
D.modify_1(c, i);
break;
case 11:
cout << "正遍历" << endl;
D.bl();
break;
case 12:
cout << "逆遍历" << endl;
D.bl_1();
break;
case 13:
system("pause");
return 0;
default:
cout << "请输入正确值" << endl;
}
}
return 0;
}
调用的子函数
#include "stdafx.h"
#include "DLLF.h"
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
using namespace std;
int LEN = 0;
char SC = 'A';
struct link
{
struct link * last;
char data;
struct link * next;
} *head, *tail;
void DLLF::lth(int i, char c)
{
LEN = i;
SC = c;
return;
}
void DLLF::create(void)
{
int i;
struct link *node, *temp;
temp = node = NULL;
for (i = 0; i < LEN; i++)
{
node = (struct link *)malloc(sizeof(struct link));
node->last = NULL;
node->data = SC + i;
node->next = NULL;
if (i == 0)
{
head = tail = temp = node;
}
else
{
temp->next = node;
node->last = temp;
temp = node;
}
}
tail = temp;
}
char DLLF::vist(int i)
{
int j = 1;
struct link *temp;
temp = head;
while (temp)
{
if (j++ == i)
return(temp->data);
temp = temp->next;
}
cout << "Out!" << endl;
return('\0');
}
char DLLF::vist_1(int t)
{
int j = 1;
struct link *node;
node = tail;
while (node)
{
if (j++ == t)
return(node->data);
node = node->last;
}
cout << "Out!" << endl;
return('\0');
}
int DLLF::search(char c)
{
int i = 1;
struct link* temp;
temp = head;
while (temp)
{
if (temp->data == c)
return(i);
i++;
temp = temp->next;
}
return(0);
}
int DLLF::search_1(char c)
{
int i = 1;
struct link* node;
node = tail;
while (node)
{
if (node->data == c)
return(i);
i++;
node = node->last;
}
return(0);
}
void DLLF::del(int i)
{
int j = 1;
struct link * node, *temp, *temp_1;
node = temp = head;
temp_1 = tail;
if (i == 1)
{
temp->next->last = NULL;
head = temp->next;
LEN -= 1;
free(temp);
}
else if (i == LEN)
{
temp_1->last->next = NULL;
tail = temp_1->last;
LEN -= 1;
free(temp_1);
}
else if (i > LEN && i == 0)
return;
else
{
while (node->next)
{
if (j++ == i)
break;
node = node->next;
}
node->last->next = node->next;
node->next->last = node->last;
LEN -= 1;
free(node);
}
}
void DLLF::del_1(int i)
{
int j = 1;
struct link * node, *temp, *temp_1;
node = temp = tail;
temp_1 = head;
if (i == 1)
{
temp->last->next = NULL;
tail = temp->last;
LEN -= 1;
}
else if (i == LEN)
{
temp_1->next->last = NULL;
head = temp_1->next;
LEN -= 1;
}
else if (i > LEN)
return;
else
{
while (node->last)
{
if (j++ == i)
break;
node = node->last;
}
node->next->last = node->last;
node->last->next = node->next;
LEN -= 1;
}
}
void DLLF::insert(char c, int i)
{
int j = 1;
struct link *node, *temp;
temp = (struct link *)malloc(sizeof(struct link));
temp->data = c;
if (i < 1)
{
temp->next = head;
head = temp;
}
else
{
node = head;
while (node->next)
{
if (j++ == i)
break;
node = node->next;
}
if (node != NULL)
{
temp->next = node->next;
node->next = temp;
}
else
{
node = temp;
temp->next = NULL;
}
}
node = tail;
temp->data = c;
j = LEN - 1;
if (i>j)
{
temp->last = tail;
tail = temp;
}
else
{
while (node->last)
{
if (j-- == i)
break;
node = node->last;
}
if (node != NULL)
{
temp->last = node->last;
node->last = temp;
}
else
{
node = temp;
temp->last = NULL;
}
LEN += 1;
}
}
void DLLF::insert_1(char c, int i)
{
int j = 1;
struct link *node, *temp;
temp = (struct link *)malloc(sizeof(struct link));
temp->data = c;
if (i < 1)
{
temp->last = tail;
tail = temp;
}
else
{
node = tail;
while (node->last)
{
if (j++ == i)
break;
node = node->last;
}
if (node != NULL)
{
temp->last = node->last;
node->last = temp;
}
else
{
node = temp;
temp->last = NULL;
}
}
node = head;
temp->data = c;
j = LEN - 1;
if (i>j)
{
temp->next = head;
head = temp;
}
else
{
while (node->next)
{
if (j-- == i)
break;
node = node->next;
}
if (node != NULL)
{
temp->next = node->next;
node->next = temp;
}
else
{
node = temp;
temp->next = NULL;
}
LEN += 1;
}
}
void DLLF::bl()
{
struct link *temp;
temp = head;
char cc;
while (temp)
{
cc = temp->data;
cout << cc << " ";
temp = temp->next;
}
cout << "\n" << endl;
}
void DLLF::bl_1()
{
struct link *node;
node = tail;
char cc;
while (node)
{
cc = node->data;
cout << cc << " ";
node = node->last;
}
cout << "\n" << endl;
}
void DLLF::modify(char c, int i)
{
int j = 1;
struct link *temp;
temp = head;
while (temp)
{
if (j++ == i)
{
temp->data = c;
return;
}
temp = temp->next;
}
cout << "Out!" << endl;
return;
}
void DLLF::modify_1(char c, int i)
{
int j = 1;
struct link *temp;
temp = tail;
while (temp)
{
if (j++ == i)
{
temp->data = c;
return;
}
temp = temp->last;
}
cout << "Out!" << endl;
return;
}
堆栈
主函数
// Dzhai.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define M 10
using namespace std;
static int a[M][M] = { { 0,1,1 },{ 0,0,0,1,1 },{ 0,0,0,0,0,1 },{ 0,0,0,0,0,0,1,1 },{ 0,0,0,0,0,0,0,0,1 },{ 0,0,0,0,0,0,0,0,0,1 } };
int main()
{
int i, j, top, n, s[M], c = 0;
cout << "输入轴号 1-" << M << endl;
cin >> n;
i = j = top = 0;
if (n < 1 || n >= M)
{
cout << "无此轴号" << endl;
system("pause");
exit(0);
}
s[top] = 0;
while (top >= 0 || j < 10)
{
if (a[i][j] == 1)
{
top++;
s[top] = j;
if (j == n)
{
cout << "轴号传动路线为" << n << endl;
for (i = 0; i <= top; i++)
printf("——%d——", s[i]);
cout << endl;
system("pause");
exit(1);
}
else
{
i = j;
j = 0;
}
}
else if (j == 9)
{
j = s[top];
top--;
i = s[top];
}
j++;
}
cout << "没有找到" << n << "号轴的传动路线" << endl;
system("pause");
return 0;
}
二叉树
主函数
// Tree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "TF.h"
using namespace std;
struct btree
{
struct btree *lchd;
int data;
struct btree *rchd;
};
btree * Tree;
int main()
{
int i, j, n;
//struct btree * Tree;
Tree = new btree;
Tree = NULL;
TF T;
cout << "输入待排序数据的数量" << endl;
cin >> n;
for (i = 0; i < n; i++)
{
cout << "输入数据" << endl;
cin >> j;
if (!Tree)
Tree = T.ctree(Tree, Tree, j);
else T.ctree(Tree, Tree, j);
}
cout << "中序遍历" << endl;
T.inorder(Tree);
cout << "先序遍历" << endl;
T.preorder(Tree);
cout << "后序遍历" << endl;
T.postorder(Tree);
cout << endl;
cout << "树的高度为" << T.GetH(Tree) << endl;
system("pause");
return 0;
}
调用的子函数
#include "stdafx.h"
#include "TF.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int numb = 0;
struct btree
{
struct btree *lchd;
int data;
struct btree *rchd;
};
struct btree * TF::ctree(struct btree * Tree, struct btree *node, int num)
{
if (node == NULL)
{
if ((node = (struct btree *)malloc(sizeof(struct btree))) == NULL)
{
cout << "无足够内存空间" << endl;
system("pause");
exit(0);
}
node->lchd = NULL;
node->rchd = NULL;
node->data = num;
if (numb == 0) /*书中的函数在第一个数插值时,会出现内存读写的问题,调试时发现跳过以下的语句直接到现在的44行便无此问题*/
numb++;
else
{
if (num < Tree->data)
Tree->lchd = node;
else
Tree->rchd = node;
return node;
if (num < node->data)
ctree(node, node->lchd, num);
else Tree->rchd = node;
return node;
}
}
if (num < node->data)
ctree(node, node->lchd, num);
else ctree(node, node->rchd, num);
}
void TF::preorder(btree * node)
{
if (!node)
return;
cout << node->data << endl;
preorder(node->lchd);
preorder(node->rchd);
}
void TF::inorder(btree * node)
{
if (!node)
return;
inorder(node->lchd);
cout << node->data << endl;
inorder(node->rchd);
}
void TF::postorder(btree * node)
{
if (!node)
return;
postorder(node->lchd);
postorder(node->rchd);
cout << node->data << endl;
}
int TF::GetH(btree * node)
{
if (node)
{
return MOT(GetH(node->lchd), GetH(node->rchd)) + 1;
}
else
{
return 0;
}
}
int TF::MOT(int a, int b)
{
if (a >= b)
{
return a;
}
else
{
return b;
}
}