本文介绍了如何将此C ++更改为C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
const int SIZE = 6;
// parse one equation
void Equation(string line, double coefRow[], double& coefB, string& coefVal){
int len = int(line.length());
bool firstItem = true;
int j=0, m=0;
coefVal = " ";
string eq = "", item="";
for (int i = 0; i < len; i++){
// the case like +x or -x in the start of the line
if (line[i] == '+' || line[i] == '-'){
if (isalpha(line[i + 1])){
coefVal += line[i + 1];
coefRow[j++] = (line[i] == '+') ? 1. : -1.;
i++;
continue;
}
}
// the last coefficient and the coefficient after the sign "="
if (line[i] == '='){
coefRow[j++] = atof(item.c_str()); // the last coefficient
item = "";
for (int l = i + 1; l < len; l++)
item += line[l];
coefB = atof(item.c_str()); // the coefficient after the sign "="
break;
}
if (isalpha(line[i])){
if (firstItem){ // the case like x in the start of the line
firstItem = false;
coefRow[j++] = 1.;
}
else coefRow[j++] = atof(item.c_str()); // the coefficient after the sign "+" or "-"
item = "";
m = 0;
coefVal += line[i];
firstItem = false;
continue;
}
// the case when the current symbol is not letter or sign (digit and decimal)
if (m>8)cerr << "The maximum length of coefficients >8\n";
item += line[i];
m++;
firstItem = false;
}
}
//Recursive function for the determinant calculation
double Determinant(double A[SIZE][SIZE], int size){
double res = 0;
if (size == 2)return A[1][1]; // real matrix size =1
if (size == 3)return A[1][1] * A[2][2] - A[2][1] * A[1][2]; // real matrix size=2
// use the first column and the new matrix with size=size-1
int newSize = size - 1;
double NewA[SIZE][SIZE];
for (int i = 1; i<size;> for (int j = 1; j < newSize; j++){
for (int k = 1; k < newSize; k++)
if (k >= i) NewA[j][k] = A[j+ 1][k + 1];
else NewA[j][k] = A[j + 1][k ];
}
int p = (i % 2) ? 1:-1;
res += p * A[1][i] * Determinant(NewA, newSize); // recursive call function with smaler size
}
return res;
}
// Cramer's rule
void Solve(double A[SIZE][SIZE], double b[SIZE], double sol[SIZE], int size){
double temp[SIZE][SIZE], det;
//call function for the determinant calculation
det = Determinant(A, size);
for (int j = 1; j < size; j++){
//temp = A
// copy A to temp
for (int k = 1; k < size; k++)
for (int l = 1; l < size; l++)
temp[k][l] = A[k][l];
// replace column number j to new values (b)
for (int i = 1; i < size; i++)
temp[i][j] = b[i];
/* print for the program testing
cout << "\ntemp: \n";
for (int k = 1; k < size; k++){
for (int l = 1; l < size; l++)
cout << temp[k][l] << " ";
cout << endl;
}
cout << "det(temp)=" << Determinant(temp, size) << endl;
*/
//the solution of the system
sol[j] = Determinant(temp, size) / det;
}
}
int main(){
// Tests for the program
// string line[] = { "1578x+2345y+8452z=467", "6543x+2348y+9873z=7654", "4230x+6740y+5433z=4546" }; // size = 3
// string line[] = {"78x-45y+52z=7", "43x+48y-73z=54", "30x+40y+33z=46"}; // size = 3
// string line[] = {"50a+37b+c=-44.39", "43a+39b+c=-45.31", "52a+41b+c=-44.96"}; //size = 3
// string line[] = { "a+0b+0c=-44.39", "0a+b+0c=-45.31", "0a+0b+c=-44.96" }; //size = 3
// string line[] = {"2.25x+2.5y+4z+3t=0.5", "0.25x+42y+3.1z+0t=1.33", "4.2x+11y+0z+3t=1.5", "10x+3.2y+0z+2t=1"}; //size = 4
string row, coefVals;
double coefRow[SIZE], coefB;
int i, j, size;
double det;
double A[SIZE][SIZE], b[SIZE], sol[SIZE];
do{
cout << "Number of Input equations (minimum 1 ~ maximum 5): ";
cin >> size;
} while (size < 1 || size > 5);
cout << "Please, enter the " << size++ << " equations \n"
<< "(Equations to be entered as strings, without spaces)\n";
for (i = 1; i < size; i++){
cin >> row; //
//row=line[i - 1]; // for tests(instead input lines)
Equation(row, coefRow, coefB, coefVals);
for (j = 1; j < size; j++)
A[i][j] = coefRow[j - 1];
b[i] = coefB;
}
cout << "\nA: \n";
for (i = 1; i < size; i++){
for (j = 1; j < size; j++)
cout << A[i][j] << " ";
cout << endl;
}
cout << "\nb: \n";
for (i = 1; i < size; i++)
cout << b[i] << endl;
cout << "\nVariables: \n";
for (i = 1; i < size; i++)
cout << coefVals[i] << endl;
cout << "\nDeterminant= ";
det = Determinant(A, size);
cout << det;
if (det != 0){
Solve(A, b, sol, size);
cout << "\nSolution: \n";
for (i = 1; i < size; i++)
cout << sol[i] << endl;
}
else
cout << "\nSystem has not solution ";
cin.get();
cin.get();
return 0;
}
我的尝试:
这是我的最后一个项目,我无法将其改为c如此困难所以如果任何人可以帮助
What I have tried:
Its my final project and i couldn't change it to c its so difficult so if any one can help
推荐答案
using namespace std;
用printf替换cout
replace "cout" with "printf"
这篇关于如何将此C ++更改为C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!