因仑“3+1”工程特种兵精英论坛
标题:
用c和c#写了两个同一功能的程序
[打印本页]
作者:
天道出勤
时间:
2016-6-17 14:32
标题:
用c和c#写了两个同一功能的程序
目的:1、老账号被封、试试新账号
2、比较一下c与c#
功能:输入任意个数字,输出总和,上限int
使用方法:编译后(比如编译成tst.exe)cmd中输入tst.exe 123 456 546
注:没找到上位机论坛,只好发到这里了
这里下载源代码
*************************华丽的分割线**************************************************
#include <stdio.h>
#include <string.h>
int str_to_int(char *s)
{int sum,i;
int weight;
weight=1;
sum=0;
for(i=(strlen(s)-1);i>=0;i--)
{
sum=sum+(*(s+i)-'0')*weight;
weight*=10;
}
return sum;
}
void main(int argc,char *argv[])
{
int s,i;
s=0;
i=0;
printf("hello world\r\n");
for(i=0;i<(argc-1);i++)
{
s=s+str_to_int(*(argv+i+1));
}
printf("sum=%d\r\n",s);
}
[color=rgb(51, 102, 153) !important]复制代码
*************************华丽的分割线**************************************************
using System;
class myclass
{
public myclass()
{
}
public int str_to_int(string s)
{
int sum = 0;
int weight = 1;
char[] c = new char[s.Length];
c = s.ToCharArray();
int[] i = new int[s.Length];
for (int j = s.Length - 1; j >= 0; j--)
{
i[j] = c[j];
i[j] -= '0';
sum = sum + i[j] * weight;
weight = weight * 10;
}
return sum;
}
}
public class MainClass
{
static public void Main(string[] args)
{
int sum=0;
myclass m=new myclass();
for (int i = 0; i < args.Length ; i++)
{
sum +=m.str_to_int(args
);
}
Console.WriteLine("sum={0}", sum);
}
}
[color=rgb(51, 102, 153) !important]复制代码
c#中没有指针,所以用了一些c程序员感觉怪怪的方法(我是这么感觉),
例i[j] = c[j];i[j] -= '0';尼玛,就不能写成一句啊?对不起亲,亲写成一句会报错的。
c中用了指针还有数组的指针,虽然很效率,但即使像我这样认为自己c语言很无敌的有时也会搞错。
欢迎光临 因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/)
Powered by Discuz! X3.4