在输入所有用户输入后,在此代码中,当被要求代码必须提供相同的输出时.
这是下面给出的代码的一部分,它打印输出但是在打印输出之后city_1[i].city,另一个输出打印在换行符上.
在printf发言中,我没用过"\n"之后"%s".
for (i = 0; i <= k - 1; i++) {
printf(" %s %d %f%", city_1[i].city, city_1[i].p, city_1[i].l);
//printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2015来调试我的代码.
任何人都可以在这个问题上帮助解释我吗?
以下是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
//GLOBAL-VARIABLE DECLARTION
#define MAX 1000
//GLOBAL-STRUCTURES DECLARATION
struct census {
char city[MAX];
long int p;
float l;
};
//GLOBAL-STRUCTURE-VARIABLE DECLARATION
struct census cen[MAX] = { 0 };
//USER-DEFINED FUNCTION
struct census sortm_city(struct census city_1[]);
struct census …Run Code Online (Sandbox Code Playgroud) 对于赋值,我正在尝试在C中创建一些仅使用位操作来测试整数是否为ASCII大写字母的代码.该字母将由整数形式的ASCII代码给出,意思是0x41 <= ourint <= 0x5A.在做了一些研究后,我了解到小写字母和大写字母之间的区别是单词二进制版本的第六位数.大写有一个1和更低的有一个0.
我几乎拥有它,但我的代码不起作用.
到目前为止我所拥有的是什么
!(((x>>6)<<31) & 0)
Run Code Online (Sandbox Code Playgroud)
它的作用是将代码移动到右边6以获得第六个数字作为第一个数字,然后向右移动31以使其全部为0或1然后是31 0.我必须这样做,如果它是大写的,它返回1,所以我使用感叹号.
编辑:我的新代码是
!((~(((x & 32)>>5))<<31))>>31)
Run Code Online (Sandbox Code Playgroud)
但现在我仍然坚持测试的问题 0x7fffffff
我从Integer Overflow Wiki读取以下行:
无符号整数溢出导致数量减去模2的幂,这意味着无符号整数在溢出时"环绕".
我有下面的代码,我试图创建一个哈希函数,并得到int溢出的情况.我试图通过使用缓解它,unsigned int但它没有工作,我能够看到负值.
我知道我可以通过其他方式处理它并且它可以工作,如我的代码注释所示 - Comment 2:.但这是正确的方式,为什么unsigned int没有环绕和溢出?
int hash(char *word) {
char *temp = word;
unsigned int hash = 0; // Comment 1: I tried to handle int overflow using "unsigned" int.
while (*word != '\0') {
// Comment 2: This works but I do not want to go this way.
//while ((hash * PRIME_MULTIPLIER) < 0) {
// hash = (hash * PRIME_MULTIPLIER) + 2147483647;
//}
hash = …Run Code Online (Sandbox Code Playgroud) 有谁知道我如何解决警告:当我返回new_cost时,返回从整数中生成指针而没有强制转换[默认启用]?
int *cost(int num_nodes, edge new_solution[][10])
{
int new_cost = 0;
int num_edges = 1; //set number of edges back to default
int x, y, weight;
for (x = 1; x <= num_nodes; x++) //print out new_solution
{
weight =0;//find the largest tx on each node
for (y = 1; y <= num_nodes; y++)
if (new_solution[x][y].label == 1)
{
printf("\n Edge %d:(%d %d) energy:%d",
num_edges++, x, y, new_solution[x][y].weight);
if (weight < new_solution[x][y].weight) //find highest energy used per node
{
weight …Run Code Online (Sandbox Code Playgroud) 如果我得到char* "12345"并且我想转换为int具有递归功能,我该怎么做?
这是一种如何转换char为int循环的简单方法.
while (str[i]) {
new_num *= 10;
new_num += str[i++] - '0';
}
Run Code Online (Sandbox Code Playgroud) 我想避免memset()在这样的结构上使用:
typedef struct {
int size;
float param1;
} StructA;
typedef struct {
StructA a;
unsigned int state;
float param2;
} Object;
Run Code Online (Sandbox Code Playgroud)
我可以做这样的事情(伪代码,我现在无法检查)?
Object obj;
int *adr = (int*)&obj;
for (int i; i < sizeof(obj); i++) {
*adr++ = 0;
}
Run Code Online (Sandbox Code Playgroud)
它会将obj的每个成员设置为零吗?
编辑:回答评论问题.我一直在研究一些情况(使用uni-type结构),其memset速度比手动初始化慢两倍.所以我会考虑尝试初始化多类型结构.
避免memcpy()也会很好(避免使用<string.h>lib).
程序通过命令行获取输入文件,并输出文本文件中每个字母的出现位置.不知道我哪里出错了.
int main(int argc, char *argv[]) {
char word[1000];
int a = 0;
int b = 0;
int d = 0;
int c = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作自定义代码,以便在将元素转换为二进制形式后计算char类型数组的每个元素中的零和1的数量.这是我的代码.如果有更好的方法来写它,请提及它.这适用于嵌入式系统.提前致谢.
void counter(unsigned short bits_num, short sizeof_array, char array[]) {
unsigned char zero = 0, one = 0;
while (bits_num && sizeof_array) {
if (array[sizeof_array - 1] & 1)
one++;
else
zero++;
array[sizeof_array - 1] = array[sizeof_array - 1] >> 1;
bits_num--;
if (!bits_num) {
bits_num = 8;
printf("zeros number is %i, ones number is %i in element: %x\n",
zero, one, sizeof_array - 1);
sizeof_array--;
zero = 0;
one = 0;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试解析输入,例如,101-John-3.4但按最后顺序未读取float数据3.4。我怎么解决这个问题?
for (i = 0; i < n; ++i) {
printf("Enter ID-Name-CGPA respectively: ");
scanf("%lld-%[^-]s-%f", id[i], name[i], cgpa[i]);
}
Run Code Online (Sandbox Code Playgroud) 所以我试图实现Cormen 的冒泡排序伪代码,但我似乎无法让它工作。
这是我对 Cormen 伪代码的处理方法:
void BUBBLE_SORT(int a[200], int n) {
int i, j, aux;
for (i = 1; i <= n - 1; i++) {
for (j = n; j < i + 1; j++) {
if (a[j] < a[j - 1]) {
aux = a[j];
a[j] = a[j + 1];
a[j + 1] = aux;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了在互联网上找到的另一段代码,但结果并没有不同:
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n …Run Code Online (Sandbox Code Playgroud)