我从计算中获得了一个带有小数部分为0的double值,然后想要将此值转换为由数字组成的char数组(使用ascii代码),然后返回它...我为它编写了以下代码...
char* as_string(double a,char* ptr)
{
int digits=0;
for(int i=1;;i++)
{if(static_cast<int>(a/pow(10.0,i))==0)
{digits=i;
break;}
}
char b[digits];
for(int i=digits-1;i>=0;i--)
{
b[digits-1-i]=static_cast<int>(a/pow(10.0,i))+48;
a=a-((b[digits-1-i]-48)*pow(10.0,i));
}
strncpy(ptr,b,digits);
return ptr;
}
Run Code Online (Sandbox Code Playgroud)
一切都很顺利......没有语法错误,但在运行时每次都重写了...经过一番审查后,我发现错误发生在指针ptr和strcpy语句周围(因为数组b已成功测试为充满了数字)...任何人都可以.指出错误?
这是我写的一些代码,用于检查string's文件中的状态:
bool aviasm::in1(string s)
{
ifstream in("optab1.txt",ios::in);//opening the optab
//cout<<"entered in1 func"<<endl;
char c;
string x,y;
while((c=in.get())!=EOF)
{
in.putback(c);
in>>x;
in>>y;
if(x==s)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
确保被搜索的字符串位于第一列中,optab1.txt并且optab1.txt每行中总共有两列.现在的问题是,无论传递什么字符串s,因为函数的参数总是返回false.你能告诉我为什么会这样吗?
我有以下代码:
int func(int a)
{
int b=2;
int c,d,e,f,g;
//some code which involves a,b,c,d,e,f,g
}
int main()
{
int s=3;
func(s);
}
Run Code Online (Sandbox Code Playgroud)
现在发生的事情是当main开始执行时:
1.
它将s推入堆栈
2.它调用func()
3.func()将b,c,d,e,f,g推送到堆栈
4.现在当代码涉及时执行a,b,c,d,e,fg,以便知道必须弹出func()的所有局部变量的值.然后检索一个值.现在如果再次使用bcdefg,它们的值将如何被检索(因为它们已被弹出)?
我编写了以下代码来接受许多输入,然后按特定顺序输出它们.
#DEFINE cases 100
struct job
{
int w;
};
class compjob
{
public:
bool operator()( job j1,job j2)
{
if(j2.w>j1.w)
return true;
return false;
}
};
int main()
{
priority_queue< job, vector<job>, compjob > jobs;
int weight;
for(int i=1;i<=cases;i++)
{
cin>>weight;
job job1;
job1.w=weight;
jobs.push(job1);
} //for loop ends here
for(int i=1;i<=cases;i++)
{
job job1= jobs.pop(); ////////////ERROR!!!!!/////////
cout<<job1.w<<endl;
}
return 0;;
}
Run Code Online (Sandbox Code Playgroud)
但是当我编译代码时,在上面标记的行上显示编译错误:
Invalid conversion from 'void' to non scalar 'job'.
Run Code Online (Sandbox Code Playgroud)
我认为我没有正确地声明作业priority_queue.另外,请解释声明中第二个参数的意义(即矢量,我真的不知道它的用途)
我写了下面的代码来洗牌一副牌:
int i,j;
for(int x=1;x<53;x++) {
i=rand()%4;
j=rand()%13;
if(deck[i][j]=0)
deck[i][j]=x;
else
x--;
}
Run Code Online (Sandbox Code Playgroud)
这没有产生任何结果,而以下代码产生了结果:
int i,j;
for(int x=1;x<53;x++) {
do {
i=rand()%4;
j=rand()%13;
} while(deck[i][j]!=0);
deck[i][j]=x
}
Run Code Online (Sandbox Code Playgroud)
这有什么不同?
特定
string name="avinash";
cout<<&name;
Run Code Online (Sandbox Code Playgroud)
如果name是指向a的指针那么我们如何在指针上使用地址?
int main()
{
char name[]="avinash";
const char* nameano="a";
strtok(name,"n");
cout<<"the size of name is"<< sizeof(name);
cout<< name;
}
Run Code Online (Sandbox Code Playgroud)
strtok takes in arguments (char*, const char*); name is an array, and hence a pointer to its first element. But if we make a declaration like
string name="avinash";
Run Code Online (Sandbox Code Playgroud)
and pass name as first argument to strtok, then the program doesn't work, but it should, because name, a string, is a pointer to its first character.
Also, if we write …
我遇到了以下程序:
class Counter {
protected:
unsigned int count;
public:
Counter(): count(0) {}
Counter(int c): count(c) {}
unsigned int get_count() { return count; }
Counter operator++() { return Counter(++count); }
};
Run Code Online (Sandbox Code Playgroud)
最后一个成员函数做什么(Counter(++count))?
以下是一个实现DFA(确定性有限自动机)的简单程序.但是,我的问题不涉及DFA.
#include<iostream>
using namespace std;
int main()
{
char c;
int state=1;
while((c=cin.get())!='4')
{
switch(state)
{
case 1:
if(c=='0')
state=2;
if(c=='1')
state=1;
if(c=='2')
state=2;
break;
case 2:
if(c=='0')
state=5;
if(c=='1')
state=1;
if(c=='2')
state=3;
break;
case 3:
if(c=='0')
state=1;
if(c=='1')
state=5;
if(c=='2')
state=4;
break;
case 4:
if(c=='0')
state=3;
if(c=='1')
state=4;
if(c=='2')
state=5;
break;
case 5:
if(c=='0')
state=5;
if(c=='1')
state=4;
if(c=='2')
state=1;
break;
default:
cout<<"Input will not be accepted"<<endl;
} //switch
cout<<"Current state is "<<state<<endl;
} //while
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我发现每行输出两次.例如,当我输入0 1 …
我有以下来自奥尔良的客户和谷物的代码片段。(虽然在 Orleans 中推荐的开发方式是等待任务,但以下代码在某些时候并不等待,纯粹是出于实验目的)
// client code
while(true)
{
Console.WriteLine("Client giving another request");
double temperature = random.NextDouble() * 40;
var grain = client.GetGrain<ITemperatureSensorGrain>(500);
Task t = sensor.SubmitTemperatureAsync((float)temperature);
Console.WriteLine("Client Task Status - "+t.Status);
await Task.Delay(5000);
}
// ITemperatureSensorGrain code
public async Task SubmitTemperatureAsync(float temperature)
{
long grainId = this.GetPrimaryKeyLong();
Console.WriteLine($"{grainId} outer received temperature: {temperature}");
Task x = SubmitTemp(temperature); // SubmitTemp() is another function in the same grain
x.Ignore();
Console.WriteLine($"{grainId} outer received temperature: {temperature} exiting");
}
public async Task SubmitTemp(float temp)
{ …Run Code Online (Sandbox Code Playgroud)