0
我在這裏很難找到問題,程序必須讀取數字N,然後讀取N長度爲2的向量,將第一個向量的每個數字與第二個向量的相關數字相乘,然後減去每個先前的乘法(示例A [0] * B [0] - A [1] * B [1] .... A [N-1] * B [N-1])任何答覆將不勝感激。 輸入值:3/ 1 2 -3 /4 -5 -6 輸出:-4 下面的代碼:使用向量的C++程序
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
using namespace std;
/* Splits string using whitespace as delimeter */
void split_input(vector<double> &vector_values, const string &input)
{
char delim = ' ';
stringstream mySstream(input);
string temp;
while(getline(mySstream, temp, delim))
{
vector_values.push_back(atof(temp.c_str()));
}
}
double multiply(vector<double> &first_vector, vector<double> &second_vector)
{
double result = 0;
for(int i = 0; i < first_vector.size(); i++)
{
if (i == 0)
{
result = first_vector[i]*second_vector[i];
}
else
{
result -= first_vector[i]*second_vector[i];
}
cout << result << endl;
}
return result;
}
int main()
{
vector<double> first_vector;
vector<double> second_vector;
int num;
string input_values;
cout << "Please enter a number: " << endl;
cin >> num ;
/* Ignores the endline char from the previous cin */
cin.ignore();
/* Read first string, split it and store the values in a vector */
getline(cin, input_values);
split_input(first_vector, input_values);
cin.ignore();
/* Read second string, split it and store the values in a vector */
getline(cin, input_values);
split_input(second_vector, input_values);
/* Multiply vectors */
cout << multiply(first_vector, second_vector) << endl;
return 0;
}
如果您向我們展示某些輸入和輸出值是什麼,這將有所幫助。 – vacuumhead
我的答案解決了您的問題嗎? – Schrieveslaach