C编程 - 如何逐步显示平方和? [英] C programming - how to display the step by step sum of square?

查看:78
本文介绍了C编程 - 如何逐步显示平方和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用visual studio来编写平方和。但我有一些问题在char * formatSeries(int n)上显示1 ^ 2 + 2 ^ 2 + 3 ^ 3.



这是我的代码。请给出一些建议。谢谢。

Im using visual studio to write sum of squares. but i having some problem on display the 1^2 + 2^2 + 3^3 in char* formatSeries(int n).

Here is my code. Please give some suggestion. Thanks.

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>

 long sumofsq(int n);

char* formatSeries(int n)
{
   int c = 2;
   printf("%d ^%d+", sumofsq(n), c);        <--Need help in this part for  
                                            display. example 3=1^2+2^2+3^2
}

void main() 
{
   int n = 5;
   printf("Given n = %d\n", n);
   printf("The sum of square for the series %s is %d \n", formatSeries(n),
   sumofsq(n));    
}

long sumofsq(int n)
{
   int sum = 0, i;
   for (i = 1; i <= n; i++)
     sum = sum + (i*i);
   return sum;
}



void main中的代码。


The code in void main.

printf(" The sum of square for the series %s is %d\n", formatSeries(n), sumofsq(n));



此输出有问题。

1 ^ 2 + 2 ^ 2 + 3 ^ 2 + 4 ^ 2 + 5 ^ 2 + 6 ^ 2< -----这个问题。我不能得到这个显示器



输出应该是这样的。



平方的总和系列1 ^ 2 + 2 ^ 2 + 3 ^ 2 + 4 ^ 2 + 5 ^ 2 + 6 ^ 2是91.



我试过的:




Having problem in this output.
1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 <----- problem on this. I cant get this display

The output should look like this.

The sum of square for the series 1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 is 91.

What I have tried:

char* formatSeries(int n)
{
   int c = 2;
   printf("%d ^%d+", sumofsq(n), c);        <--Need help in this part for  
                                            display. example 3=1^2+2^2+3^2
}

推荐答案

问题是你似乎在尝试打印一个系列,但是你只需要调用 printf 一次,这不是循环或递归方法。这不起作用:它只打印一个由 sumofsq 函数返回的值(因为 formatSeries 不会返回一个字符串,你的应用程序崩溃了。

从另一个角度看它开始:

The problem is that you seem to be trying to print a series, but you only call printf once, and that isn't in a loop or a recursive method. That won't work: it just prints a value returned by your sumofsq function (and because formatSeries doesn;t return a string, your app crashes).
Start by looking at it the other way round:
1^2+2^2+3^2 = 3

而不是

Instead of

3 = 1^2+2^2+3^2



这样,您可以通过两次调用来执行此操作 printf

1)在for循环中一个 sumofsq 打印每个字词 x ^ y (带有分隔符'+')

2)打印当 sumofsq 结束并返回一个值时的最终结果。



这是你的作业,所以我不会给你代码!

但是首先分组 formatSeries 函数并获取 sumofsq 进行打印处理它们时的每个术语。


That way, you can do this with two calls to printf:
1) One in the for loop inside sumofsq to print each term x ^ y (with a separating '+')
2) To print the final result when sumofsq finishes and returns a value.

This is your homework, so I won't give you code!
But start by binning the formatSeries function and getting sumofsq to print each term as it processes them.


如果掌握如何构建字符串并将其作为结果返回,则此代码是可以的。

This code is ok if you master how to build a string and return it as result.
void main()
{
int n = 5;
printf("Given n = %d\n", n);
printf("The sum of square for the series %s is %d \n", formatSeries(n), sumofsq(n));
}



但显然事实并非如此。

您可以更改它以简化其他部分


But obviously it is not the case.
You can change it to simplify other parts

void main()
{
int n = 5;
printf("Given n = %d\n", n);
printf("The sum of square for the series ");
formatSeries(n); // so that this part can print the formula directly
printf(" is %d \n", sumofsq(n));
}




Quote:

printf("%d ^%d+", sumofsq(n), c);



在这里你要打印公式,而不是它的结果。



当你不明白什么你的代码正在做或为什么它做它做的事情,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Here you want to print the formula, not its result.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于C编程 - 如何逐步显示平方和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆