我有一个带有要删除的行的向量V.我需要使用以下函数: - 读取和写入存储公司列表的矩阵A. - 读取和写入V,其中要删除的行是。 - 删除A中的行,其中X行作为值存储在V。
中我的问题:
如何使用函数读取要删除的X行并将其存储在V中?我可以在main()中做得很好,但是我无法弄清楚如何在函数中进行读取。
我在stergere()函数中做错了什么?如果我用X替换X行中的元素然后在删除后写入矩阵时忽略所述元素,它可以正常工作,但这不是最佳的。我想实际删除所述元素,以便矩阵的整体大小也缩小。
另外,如果你可以指出我正确的方向而不必询问用户他/她想要删除多少行,那就太棒了。现在,我必须使用一个变量来存储要删除的行数(从键盘读取),然后使用从键盘读取的大小动态分配V.是否有任何方法让用户不断读取X值,直到按下某个键,并将这些元素存储在V?有点像c ++中的vector :: push_back吗?
谢谢,这里是代码:
// ConsoleApplication76.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>
// alocarea matricei B in memorie in functie de numarul de linii de sters din vectorul x
void alocare(float*** b, int* y, int m, int *n, int x){
*y = m - x;
(*b) = (float**)malloc((*y)*sizeof(float*));
for (int i = 0; i<(*y); i++)
(*b)[i] = (float*)malloc((*n)*sizeof(float));
}
//citirea matricei A
void citireA(float*** a, int *m, int *n){
printf("Nr linii m al matricei a : ");
scanf_s("%d", &(*m));
printf("Nr coloane n al matricei a : ");
scanf_s("%d", &(*n));
(*a) = (float**)malloc((*m)*sizeof(float*));
for (int i = 0; i < (*m); i++)
(*a)[i] = (float*)malloc((*n)*sizeof(float));
for (int i = 0; i < (*m); i++)
for (int j = 0; j < (*n); j++){
printf("a[%d][%d]: ", i, j);
scanf_s("%f", &(*a)[i][j]);
}
printf("\n");
}
// afisarea matricei A
void afisareA(float **a, int m, int n){
for (int i = 0; i<m; i++){
printf("%d: ", i);
for (int j = 0; j < n; j++){
printf(" %5.2f ", a[i][j]);
}
printf("\n");
}
printf("\n");
}
// citirea vectorului V
void citireV(int** v, int *x){
printf("Numar de linii de sters: ");
scanf_s("%d", &(*x));
*(v) = (int*)malloc(*(x)*sizeof(int));
for (int i = 0; i < *(x); i++){
printf("Linia ce va fi stearsa: ");
scanf_s("%d", &(*v)[i]);
}
}
// afisarea vectorului V
void afisareV(int* v, int x){
printf("\nLiniile ce vor fi sterse: ");
for (int i = 0; i < x; i++)
printf("%d ", v[i]);
printf("\n");
}
bool nullT(int m, int x){
if (m == x){
printf("Matricea rezultata va fi nula\n");
return true;
}
return false;
}
// functie pentru a sterge valorile care se afla de mai multe ori in vectorul v. in caz ca aceeasi linie
// se afla de 2 ori in vectorul V, in matricea A nu va putea fi gasita linia v[i] in a doua iteratie, cauzand un crash.
void stergereDup(int** v, int *x){
for (int i = 0; i < (*x)-1; i++)
for (int j = i + 1; j < (*x); j++)
if ( (*v)[i] == (*v)[j] ) {
for (int ii = i; ii < (*x) - 1; ii++)
(*v)[ii] = (*v)[ii + 1];
(*x)--;
}
}
// afisarea matricei B
void afisareB(float **b, int y, int n){
for (int i = 0; i<y; i++){
printf("%d: ", i);
for (int j = 0; j < n; j++){
printf(" %5.2f ", b[i][j]);
}
printf("\n");
}
printf("\n");
}
// functia principala de stergere a linillor v[i] din matricea A
void stergere(float** a, float*** b, int* v, int* y, int *m, int n, int x){
int kb = 0;
int q = 0;
bool test = false;
while (q != *m){
for (int i = 0; i < x; i++)
if (v[i] == q) test = true;
if (!test){
for (int i = 0; i < n; i++){
(*b)[kb][i] = a[q][i];
}
kb++;
printf("\n");
}
test = false;
q++;
}
}
int main(){
float** a;
float** b;
int* v;
int m, n, x, y;
citireA(&a, &m, &n);
afisareA(a, m, n);
citireV(&v, &x);
stergereDup(&v, &x);
afisareV(v, x);
if (nullT(m, x)) return 0;
else {
alocare(&b, &y, m, &n, x);
stergere(a, &b, v, &y, &m, n, x);
afisareB(b, y, n);
}
return 0;
}