- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试编译代码时出现以下错误:
LNK1169:找到一个或多个多重定义的符号LNK2005: [函数名称] 已在 main.obj 中定义^^^ 每个函数都出现错误 LNK2005
这是我当前的代码:main.c
#define _CRT_SECURE_NO_WARNINGS –
#pragma once
#include "pa-1.h"
#include "pa-1.c"
int main()
{
//Declaring an array
int list[3] = {0,0,0};
//Declaring variables
int n = 0;
int x = 0;
int r = 0;
int y = 0;
int z = 0;
int rotateNum = 0;
int searchRecur = 0;
int collatzNum = 0;
int average = 0;
bool results;
char brackets = "";
char lookFor = "";
char paragraph = "";
char fString = "";
char sString = "";
char *e = "";
char *pattern = "";
char *text = "";
char *s1 = "";
char *s2 = "";
char *s3 = "";
//Function 1
//Will see if input is an armstrong number
printf("Enter a number and the program will check to see if it's an armstrong number: ");
scanf("%d", n);
results = f_armstrong(n);
if (results == true)
{
printf(" is an armstrong number");
}
else
{
printf("%d is not an armstrong number", n);
}
/*
//Function 2
//Will see if brackets are balanced
printf("Enter brackets and the program will check to see if they are in order: ");
scanf("%s", brackets);
*e = brackets;
results = f_brackets(*e);
if (results == true)
{
printf("Your brackets are in order");
}
else
{
printf("Your brackets are not in order");
}
//Function 3
//Will see if input is a perfect number
printf("Enter a number and the program will check to see if it's a perfect number: ");
scanf("%d", x);
results = f_perfect(x);
if (results == true)
{
printf("%d is a perfect number", x);
}
else
{
printf("%d is not a perfect number", x);
}
//Funtction 4
//Will rotate input by a number that is also given
printf("Enter a number that you want to rotate its digits: ");
scanf("%d", n);
printf("Enter a number that you want to rotate by: ");
scanf("%d", r);
rotateNum = f_rotate(n, r);
printf("The new number is: %d", rotateNum);
//Funtction 5
//Will search for a pattern in a text
printf("Enter a text you want to search through: ");
scanf("%s", paragraph);
printf("Enter a pattern you want to search for: ");
scanf("%s", lookFor);
*text = paragraph;
*pattern = lookFor;
searchRecur = f_str_search(*pattern, *text);
printf("%s can be found in your text %D times", *pattern, searchRecur);
//Funtction 6
//Will perform the Collatz conjucture on input
printf("Enter a number for the program to perform the Collatz conjucture to: ");
scanf("%d", n);
collatzNum = f_collatz_count(n);
printf("It took %d iterations", collatzNum);
//Funtction 7
//This function will take a number and create an array with the amount and perform other operation
printf("Enter a number to determine how many random numbers are generated: ");
scanf("%d", n);
average = f_array(n);
printf("The numbers were randomly generated between 1-99 and multiplied by 2. The following number is the average of the (up to 5) numbers divisible by 3: %d", average);
//Funtction 8
//This function will take two strings and put them into one, in shorter/larger/shorter order
printf("Enter the first text you want to forge with: ");
scanf("%s", fString);
printf("Enter the second text you want to forge with: ");
scanf("%s", sString);
*s1 = fString;
*s2 = sString;
f_strings(*s1, *s2, *s3);
printf("Your new string is: %s", *s3);
//Function 9
//This will take three numbers and sort them in order
printf("The program will now take three numbers and sort them in assending order. Enter the first number: ");
scanf("%d", x);
printf("Enter the second number: ");
scanf("%d", y);
printf("Enter the third number: ");
scanf("%d", z);
f_sort(x, y, z, *list);
printf("The numbers in order are: %d %d %d",list[0], list[1], list[2]);
//Funtction 10
//This will look for a set of numbers that equate to the input, when cubed
printf("Enter a number and the program will see if a set of numbers cubed will equate to it: ");
scanf("%d", n);
int * twoValues = f_cubed_sum(n);
if (twoValues[0] != NULL)
{
printf("The two values are: %d and %d", twoValues[0], twoValues[1]);
}
else
{
printf("There is no pair of integers that satasfy the formula");
}
free(twoValues);
*/
return 0;
}
pa-1.c:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "pa-1.h"
/*
This function takes an integer n as a parameter.
The purpose of this function is to determine if n is an
"Armstrong Number".
*/
bool f_armstrong(int n)
{
int numLength; //Holds how many digits are in n
int finalSum = 0; //Holds the final test value to compare with n
int digits = 0; //Holds the value of parameter n
int remainder; //Holds the value of the remainder
int userNum = n; //Holds the value of n
// Count number of digits in n
while (userNum != 0)
{
digits++;
//Takes away a digit from n
userNum /= 10;
}
//Re-assign the variables
numLength = digits;
userNum = n;
//Test loop to determine if it's an Armstrong number
while (userNum != 0)
{
//Find digit in the number
remainder = userNum % 10;
//Exponential increase the digit and add it to the sum
finalSum += pow(remainder, digits);
userNum /= 10;
}
//If the test was true
if (finalSum == n)
{
return true;
}
//If the test fails
else
{
return false;
}
}
/*
THe user enters a string phrase into the function.
The function will then go through the string and count the
number of open and closed brackets. If the sizes match it
returns true, if not it returns false.
*/
bool f_brackets(char *e)
{
//Declaring variables
int brackLen;
int counter = 0;
int openCount = 0;
int closeCount = 0;
char curChar, brackets;
bool results = true;
brackets = *e;
//Getting the length of the string
brackLen = strlen(e);
while (counter < brackLen && results == true)
{
curChar = getchar(brackets);
if (curChar == '[')
{
openCount = openCount + 1;
}
else
{
closeCount = closeCount + 1;
}
if (closeCount > openCount)
{
results = false;
}
counter = counter + 1;
}
return results;
}
/*
The user will enter a number theu want to test.
The function will then add up the sum of the divisors.
If the sum and number match it returns true, if not it
returns true.
*/
bool f_perfect(int x)
{
int userNum = x; //Variable to hold the parameter value
int sum = 1; //Holds the final sum of perfect numbers
//Divided by the user's number
//Starts at 2 since all numbers are divisable by 1
int divisor = 2;
//While the user's number is greater then the divisor
while (divisor < userNum)
{
//Int to hold the value
int test = userNum % divisor;
//If test is 0, then userNum is divisable
if (test == 0)
{
//Add the divisable to the overall sum
sum += divisor;
}
//Incrememnt
divisor++;
}
//If the sum of divisors is equal to the user's number
if (userNum == sum)
{
return true;
}
//If the sum of divisors is not equal to the user's number
else
{
return false;
}
}
/*
The purpose of this function is for the user to enter a number,
and then they enter another number which the function uses to rotate
the values of their first number.
*/
int f_rotate(int n, int r)
{
int number = n; //Holds the value of users number
const int mod = 10; //Constant value used for modulus command
int rotater = r; //Value to hold how many rotations user wants
int multiplier = 10; //Variable that will be used for multiplications
int digits = 0; //Variable to hold how many values are in number
int finalNum = 0; //Variable that holds the final rotated number
//For loop to determine how many numbers are in users entered value
for (int i = 0; number > 0; i++)
{
//Remove a digit from the value and increment
number /= 10;
digits++;
}
//Loop that will increment multiplier by 10 until it has the same 0's as user number
for (int i = 1; i < digits - rotater; i++)
{
multiplier *= 10;
}
//Reset number to original value
number = n;
//Loop until it's been rotated the correct amount of times
for (int i = 0; i < rotater; i++)
{
//Temp value that will hold the value of a spot in the user's number
int temp;
//Mod 10 to get the digit by itself
temp = number % 10;
//Subtract the value from the number and get rid of a 0
number -= temp;
number /= 10;
//If the digit did not equal 0
if (temp != 0)
{
//Multiply the the value by the multiplier and add it to final num
finalNum += temp * multiplier;
multiplier *= 10;
}
}
//Add the numbers not being rotated to the final number
finalNum += number;
//Return the rotated number
return finalNum;
}
/*
The user enters a pattern that they would like to search for.
The user also enters a text that they want to check for patterns.
The function will return the amount of times it finds the pattern
in the test.
*/
int f_str_search(char *pattern, char *text)
{
int patSize = strlen(pattern); //Varaible to hold size of pattern
int textSize = strlen(text); //Varaible to hold size of text
int occurence = 0; //Variable to hold amount of times the pattern is matched
int patElement = 0; //Array element for pattern
int textElem = 0; //Array element for text
int match = 0; //Variable to hold how many matched elements are found
//While there's still chars left to test in the text string
while (textElem < textSize)
{
//If the values in the element match
if (*(pattern + patElement) == *(text + textElem))
{
//Increment match and the pattern element
match++;
patElement++;
}
//If the values in the element do not match
else
{
//Reset match and go back to the first char in pattern
match = 0;
patElement = 0;;
}
//If matches equal size of pattern, pattern is found
if (match == patSize)
{
//Reset match and pattern element to continue checking
match = 0;
patElement = 0;
//Increment the occurence
occurence++;
//Decrement text element to start checking again in last element a match was found
textElem--;
}
//
textElem++;
}
return occurence;
}
/*
The user enters a natural number.
The program will use the "The Collatz Conjecture" and return
the amount of iterations it took to get the user's number to
equal 1.
*/
int f_collatz_count(int n)
{
int userNum = n; //Variable to hold the user's number
int iteration = 0; //Variable to hold amount of iterations
//While userNum is not 1
while (userNum != 1)
{
//testNum will hold the remainder of the number divided by 2
int testNum = userNum % 2;
//If the number was divisable by 2 (even)
if (testNum == 0)
{
//Divide number by 2 and increment iteration
userNum /= 2;
iteration++;
}
//If the number was not divisable by 2 (odd)
else
{
//Multiply the number by 3 and add 1.
userNum = (userNum * 3) + 1;
//Increment iterations
iteration++;
}
}
//Return total iterations
return iteration;
}
/*
User enters a number.
The function will add however many random numbers the user entered.
It will then multiply all the random numbers by 2, and will return the
average of the first 5 numbers divisable by 3.
*/
int f_array(int n)
{
//Creating variables
int remainder, average;
int counter = 0;
int avgCount = 0;
int total = 0;
bool results = false;
//Creating arrayAn
int* A;
A = malloc(n * sizeof(int));
//Initializing random seed
srand(time(NULL));
//Populating array with random numbers
while (counter < n)
{
A[counter] = rand() % 99 + 1;
counter = counter + 1;
}
//Resetting counter
counter = 0;
//Multiplying array by 2
while (counter < n)
{
A[counter] = A[counter] * 2;
counter = counter + 1;
}
//Restting counter
counter = 0;
//Getting the average of first 5 numbers divisible by 3
while (counter < n && results == false)
{
//Checking if divisible by 3
remainder = A[counter] % 3;
if (remainder == 0)
{
total = total + A[counter];
avgCount = avgCount + 1;
}
if (avgCount = 5)
{
results = true;
}
counter = counter + 1;
}
average = total / avgCount;
free(A);
return average;
}
/*
The user enters two different strings.
The function will test to see which one is larger and then return a
combined string with the shorter string, the longer string, and the
shorter string again.
*/
void f_strings(char *s1, char *s2, char *s3)
{
//Declaring variables
int firstCount, secCount;
//Comparing the string lengths
firstCount = strlen(s1);
secCount = strlen(s2);
if (firstCount > secCount)
{
*s3 = *s1 << *s2 << *s1;
}
else
{
*s3 = *s2 << *s1 << *s2;
}
return;
}
/*
The user enters 3 numbers.
The function then sorts those numbers and lists them in ascending
order.
*/
void f_sort(int x, int y, int z, int *list)
{
const int arraySize = 3; //Constant for the size of array
int element = 0; //Holds numerical value for array element
int num1 = x; //Holds value of first entered number
int num2 = y; //Holds value of second entered number
int num3 = z; //Holds value of third entered number
int temp; //Holds value of number being swapped
//If the first number is larger then the second
if (num1 > num2)
{
//Swap their values
temp = num2;
num2 = num1;
num1 = temp;
}
//If the first number is larger then the third
if (num1 > num3)
{
//Swap their values
temp = num3;
num3 = num1;
num1 = temp;
}
//If the second number is larger then the third
if (num2 > num3)
{
//Swap their values
temp = num3;
num3 = num2;
num2 = temp;
}
//Allocate memory
list = (int *)malloc(arraySize);
//Add the values into the array in ascending order
*(list + element) = num1;
element++;
*(list + element) = num2;
element++;
*(list + element) = num3;
printf("%s \n", "Here is your numbers sorted in ascending order:");
//Loops until all 3 values are printed
for (int i = 0; i < arraySize; i++)
{
printf("%d \n", *(list + i));
}
}
/*
The user enters a number.
The function will test if the cubed sum of 2 numbers is equal
to the number they entered.
*/
int *f_cubed_sum(int n)
{
//Variable to hold the max value the program can go to
const int maxValue = cbrt(n);
const int exponant = 3; //Constant exponant value
int element = 0; //Holds value of array
int test1 = 0; //Value of first number cubed
int test2 = 0; //Value of second number cubed
int *pair1;
int *pair2;
int *pairs;
int totalPairs = 0; //Holds if pairs have been found
pair1 = (int *)malloc(5 * maxValue);
pair2 = (int *)malloc(5 * maxValue);
//While the first test number is less then or equal to max value
while (test1 <= maxValue)
{
//Reset second test number to 0
test2 = 0;
//While the second test number is less then or equal to max value
while (test2 <= maxValue)
{
//Sum of the two test numbers
int sum = 0;
//Cube the test numbers and add them to sum
sum += pow(test1, exponant);
sum += pow(test2, exponant);
//If sum is equal to users number
if (sum == n)
{
//If one of the test numbers is 0
if (test1 == 0)
{
*(pair1 + element) = 0;
*(pair2 + element) = test2;
totalPairs += 2;
}
else if (test2 == 0)
{
*(pair1 + element) = test1;
*(pair2 + element) = 0;
totalPairs += 2;
}
else
{
*(pair1 + element) = test1;
*(pair2 + element) = test2;
element++;
totalPairs += 2;
}
}
//Increment test number 2
test2++;
}
//Increment test number 1
test1++;
}
//If no pairs were found
if (totalPairs == 0)
{
//Free memory and return NULL
free(pair1);
free(pair2);
return NULL;
}
//If there were pairs found
else
{
int pairElement = 0;
int pairsElement = 0;
pairs = (int *)malloc(100 * totalPairs);
for (int i = 0; pairElement < sizeof(pair1); i++)
{
if (*(pair2 + pairElement) != 0 && *(pair1 + pairElement) != 0)
{
*(pairs + pairsElement) = *(pair1 + pairElement);
pairsElement++;
*(pairs + pairsElement) = *(pair2 + pairElement);
pairsElement++;
pairElement++;
}
else
{
pairElement++;
}
}
//Return array of pairs
return pairs;
}
}
pa-1.h
#pragma once
// define a boolean type to use in the functions
#ifndef _BOOL_TYPE_
#define _BOOL_TYPE_
typedef enum { false, true } bool;
#endif
bool f_armstrong(int n);
bool f_brackets(char *e);
bool f_perfect(int x);
int f_rotate(int n, int r);
int f_str_search(char *pattern, char *text);
int f_collatz_count(int n);
int f_array(int n);
void f_strings(char *s1, char *s2, char *s3);
void f_sort(int x, int y, int z, int *list);
int *f_cubed_sum(int n);
我正在使用 Visual Studio。
谢谢!
最佳答案
您不应包含 .c
文件。删除此行
#include "pa-1.c"
并编译所有.c
文件,例如:
gcc main.c pa-1.c -o prog.exe
编辑:没有注意到您正在使用 Visual Studio,在这种情况下,只需将所有文件添加到同一项目中并构建它就足够了。所以忽略上面的gcc
命令。
关于c - C 中的 LNK1169 和 LNK2005 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33661732/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!